private static final AsciiBytes LIB = new AsciiBytes("lib/");
public static ClassLoader createModuleClassLoader(Resource moduleLocation, ClassLoader parent) {
try {
File moduleFile = moduleLocation.getFile();
Archive moduleArchive = moduleFile.isDirectory() ? new ExplodedArchive(moduleFile) : new JarFileArchive(moduleFile);
List<Archive> nestedArchives = moduleArchive.getNestedArchives(new Archive.EntryFilter() {
@Override
public boolean matches(Archive.Entry entry) {
return !entry.isDirectory() && entry.getName().startsWith(LIB);
}
});
URL[] urls = new URL[nestedArchives.size() + 1];
int i = 0;
for (Archive nested : nestedArchives) {
urls[i++] = nested.getUrl();
}
urls[i] = moduleArchive.getUrl();
return new ParentLastURLClassLoader(urls, parent);
}
catch (IOException e) {
throw new RuntimeException(e);
}