use of org.apache.ziplock.ClassLoaders in project component-runtime by Talend.
the class BaseSpark method submitClasspath.
/**
* Same as {@link BaseSpark#submit(Class, String...)} but automatically
* set {@code --jars} arguments and bundle on the fly folders into jars.
*
* @param main
* the main to execute.
* @param args
* potential arguments to pass to spark submit.
*/
public void submitClasspath(final Class<?> main, final Predicate<File> classpathFilter, final String... args) {
final Set<File> files;
try {
final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
files = new UrlSet(ClassLoaders.findUrls(contextClassLoader)).excludeJvm().getUrls().stream().map(ClassLoaders::toFile).collect(toSet());
} catch (final IOException e) {
throw new IllegalArgumentException(e);
}
final String classpath = files.stream().filter(classpathFilter).map(file -> {
if (file.isDirectory()) {
// bundle it to let spark submit it
return config.get().jarCache.computeIfAbsent(file, dir -> {
final File cache = new File(getRoot(), file.getName() + "_generated_" + System.nanoTime() + ".jar");
try (final JarOutputStream out = new JarOutputStream(new FileOutputStream(cache))) {
zip(file, out, "");
} catch (final IOException e) {
fail(e.getMessage());
}
return cache;
}).getAbsolutePath();
}
return file.getAbsolutePath();
}).collect(joining(File.pathSeparator));
submit(main, Stream.concat(args == null ? Stream.empty() : Stream.of(args), Stream.of("--jars", classpath)).toArray(String[]::new));
}
Aggregations