Search in sources :

Example 1 with ClassLoaders

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));
}
Also used : AcceptScopesStrategy(org.jboss.shrinkwrap.resolver.api.maven.strategy.AcceptScopesStrategy) URL(java.net.URL) MavenResolutionStrategy(org.jboss.shrinkwrap.resolver.api.maven.strategy.MavenResolutionStrategy) Optional.of(java.util.Optional.of) LoggerFactory(org.slf4j.LoggerFactory) SparkClusterRule(org.talend.sdk.component.runtime.testing.spark.SparkClusterRule) BooleanSupplier(java.util.function.BooleanSupplier) InetAddress(java.net.InetAddress) ServerSocket(java.net.ServerSocket) Maven(org.jboss.shrinkwrap.resolver.api.maven.Maven) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) Thread.sleep(java.lang.Thread.sleep) ENGLISH(java.util.Locale.ENGLISH) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) Collectors.toSet(java.util.stream.Collectors.toSet) ScopeType(org.jboss.shrinkwrap.resolver.api.maven.ScopeType) Predicate(java.util.function.Predicate) Collection(java.util.Collection) StandardOpenOption(java.nio.file.StandardOpenOption) Set(java.util.Set) Collectors.joining(java.util.stream.Collectors.joining) StandardCharsets(java.nio.charset.StandardCharsets) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) ClassLoaders(org.apache.ziplock.ClassLoaders) Stream(java.util.stream.Stream) ConfigurableMavenResolverSystem(org.jboss.shrinkwrap.resolver.api.maven.ConfigurableMavenResolverSystem) Optional(java.util.Optional) IntStream(java.util.stream.IntStream) Socket(java.net.Socket) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Getter(lombok.Getter) HashMap(java.util.HashMap) StandardCopyOption(java.nio.file.StandardCopyOption) ArrayList(java.util.ArrayList) UrlSet(org.apache.xbean.finder.UrlSet) JarEntry(java.util.jar.JarEntry) JarOutputStream(java.util.jar.JarOutputStream) OutputStream(java.io.OutputStream) Logger(org.slf4j.Logger) MalformedURLException(java.net.MalformedURLException) Files(java.nio.file.Files) Optional.ofNullable(java.util.Optional.ofNullable) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Collectors.toList(java.util.stream.Collectors.toList) JarLocation.jarLocation(org.apache.ziplock.JarLocation.jarLocation) AllArgsConstructor(lombok.AllArgsConstructor) InputStream(java.io.InputStream) ClassLoaders(org.apache.ziplock.ClassLoaders) UrlSet(org.apache.xbean.finder.UrlSet) FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) IOException(java.io.IOException) File(java.io.File)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Thread.sleep (java.lang.Thread.sleep)1 InetAddress (java.net.InetAddress)1 MalformedURLException (java.net.MalformedURLException)1 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 URL (java.net.URL)1 UnknownHostException (java.net.UnknownHostException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 StandardCopyOption (java.nio.file.StandardCopyOption)1 StandardOpenOption (java.nio.file.StandardOpenOption)1 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1