use of loghub.Helpers.ThrowingConsumer in project LogHub by fbacchella.
the class Configuration method doClassLoader.
ClassLoader doClassLoader(Object[] pathElements) throws IOException {
final Collection<URL> urls = new ArrayList<URL>();
// Needed for all the lambda that throws exception
ThrowingPredicate<Path> filterReadable = i -> !Files.isHidden(i);
ThrowingConsumer<Path> toUrl = i -> urls.add(i.toUri().toURL());
Arrays.stream(pathElements).map(i -> Paths.get(i.toString())).filter(i -> Files.isReadable(i)).filter(filterReadable).filter(i -> (Files.isRegularFile(i) && i.toString().endsWith(".jar")) || Files.isDirectory(i)).forEach(i -> {
toUrl.accept(i);
if (Files.isDirectory(i)) {
try {
Files.list(i).filter(p -> Files.isRegularFile(p) && p.toString().endsWith(".jar")).forEach(toUrl);
} catch (Exception e) {
new RuntimeException(e);
}
}
});
// Add myself to class loader, so the custom class loader is used in priority
Path myself = locateResourcefile("loghub");
if (myself.endsWith("loghub")) {
myself = myself.getParent();
}
urls.add(myself.toUri().toURL());
return new LogHubClassloader(urls.toArray(new URL[] {}));
}
Aggregations