Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add resource implement for DinkyClassLoader #2854

Merged
merged 2 commits into from
Dec 29, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: add resource implement
Signed-off-by: licho <lecho.sun@gmail.com>
  • Loading branch information
leechor committed Dec 29, 2023
commit e7fdb4a6da4a7f59a5a27b7a3a7575b50b45d4ef
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@
import org.dinky.context.FlinkUdfPathContextHolder;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLStreamHandlerFactory;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

Expand Down Expand Up @@ -111,20 +115,62 @@ protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundE
if (loadedClass == null) {
try {
// try to use this classloader to load
loadedClass = this.findClass(name);
return findClass(name);
} catch (ClassNotFoundException e) {
// maybe is system class, try parents delegate
return super.loadClass(name, false);
}
}

if (resolve) {
}else if (resolve) {
resolveClass(loadedClass);
}
return loadedClass;
}
}

@Override
public URL getResource(String name) {
// first, try and find it via the URLClassloader
URL urlClassLoaderResource = findResource(name);

if (urlClassLoaderResource != null) {
return urlClassLoaderResource;
}

// delegate to super
return super.getResource(name);
}

@Override
public Enumeration<URL> getResources(String name) throws IOException {
// first get resources from URLClassloader
Enumeration<URL> urlClassLoaderResources = findResources(name);

final List<URL> result = new ArrayList<>();

while (urlClassLoaderResources.hasMoreElements()) {
result.add(urlClassLoaderResources.nextElement());
}

// get parent urls
Enumeration<URL> parentResources = getParent().getResources(name);

while (parentResources.hasMoreElements()) {
result.add(parentResources.nextElement());
}

return new Enumeration<URL>() {
Iterator<URL> iter = result.iterator();

public boolean hasMoreElements() {
return iter.hasNext();
}

public URL nextElement() {
return iter.next();
}
};
}

public static List<File> getJarFiles(String[] paths, List<String> notExistsFiles) {
List<File> result = new LinkedList<>();
for (String path : paths) {
Expand All @@ -147,4 +193,8 @@ public static List<File> getJarFiles(String[] paths, List<String> notExistsFiles
}
return result;
}

static {
ClassLoader.registerAsParallelCapable();
}
}
Loading