Search in sources :

Example 1 with WeaveLauncher

use of com.continuuity.weave.launcher.WeaveLauncher in project weave by continuuity.

the class YarnWeavePreparer method saveLauncher.

/**
   * Creates the launcher.jar for launch the main application.
   */
private void saveLauncher(Map<String, LocalFile> localFiles) throws URISyntaxException, IOException {
    LOG.debug("Create and copy {}", Constants.Files.LAUNCHER_JAR);
    Location location = createTempLocation(Constants.Files.LAUNCHER_JAR);
    final String launcherName = WeaveLauncher.class.getName();
    // Create a jar file with the WeaveLauncher optionally a json serialized classpath.json in it.
    final JarOutputStream jarOut = new JarOutputStream(location.getOutputStream());
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    if (classLoader == null) {
        classLoader = getClass().getClassLoader();
    }
    Dependencies.findClassDependencies(classLoader, new Dependencies.ClassAcceptor() {

        @Override
        public boolean accept(String className, URL classUrl, URL classPathUrl) {
            Preconditions.checkArgument(className.startsWith(launcherName), "Launcher jar should not have dependencies: %s", className);
            try {
                jarOut.putNextEntry(new JarEntry(className.replace('.', '/') + ".class"));
                InputStream is = classUrl.openStream();
                try {
                    ByteStreams.copy(is, jarOut);
                } finally {
                    is.close();
                }
            } catch (IOException e) {
                throw Throwables.propagate(e);
            }
            return true;
        }
    }, WeaveLauncher.class.getName());
    try {
        if (!classPaths.isEmpty()) {
            jarOut.putNextEntry(new JarEntry("classpath"));
            jarOut.write(Joiner.on(':').join(classPaths).getBytes(Charsets.UTF_8));
        }
    } finally {
        jarOut.close();
    }
    LOG.debug("Done {}", Constants.Files.LAUNCHER_JAR);
    localFiles.put(Constants.Files.LAUNCHER_JAR, createLocalFile(Constants.Files.LAUNCHER_JAR, location));
}
Also used : InputStream(java.io.InputStream) JarOutputStream(java.util.jar.JarOutputStream) WeaveLauncher(com.continuuity.weave.launcher.WeaveLauncher) Dependencies(com.continuuity.weave.internal.utils.Dependencies) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) URL(java.net.URL) Location(com.continuuity.weave.filesystem.Location)

Aggregations

Location (com.continuuity.weave.filesystem.Location)1 Dependencies (com.continuuity.weave.internal.utils.Dependencies)1 WeaveLauncher (com.continuuity.weave.launcher.WeaveLauncher)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 JarEntry (java.util.jar.JarEntry)1 JarOutputStream (java.util.jar.JarOutputStream)1