Search in sources :

Example 16 with JarEntry

use of java.util.jar.JarEntry 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)

Example 17 with JarEntry

use of java.util.jar.JarEntry in project weave by continuuity.

the class WeaveLauncher method unJar.

private static void unJar(File jarFile, File targetDir) throws IOException {
    JarInputStream jarInput = new JarInputStream(new FileInputStream(jarFile));
    try {
        JarEntry jarEntry = jarInput.getNextJarEntry();
        while (jarEntry != null) {
            File target = new File(targetDir, jarEntry.getName());
            if (jarEntry.isDirectory()) {
                target.mkdirs();
            } else {
                target.getParentFile().mkdirs();
                copy(jarInput, target);
            }
            jarEntry = jarInput.getNextJarEntry();
        }
    } finally {
        jarInput.close();
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) JarEntry(java.util.jar.JarEntry) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 18 with JarEntry

use of java.util.jar.JarEntry in project weave by continuuity.

the class ApplicationBundlerTest method unjar.

private void unjar(File jarFile, File targetDir) throws IOException {
    JarInputStream jarInput = new JarInputStream(new FileInputStream(jarFile));
    try {
        JarEntry jarEntry = jarInput.getNextJarEntry();
        while (jarEntry != null) {
            File target = new File(targetDir, jarEntry.getName());
            if (jarEntry.isDirectory()) {
                target.mkdirs();
            } else {
                target.getParentFile().mkdirs();
                ByteStreams.copy(jarInput, Files.newOutputStreamSupplier(target));
            }
            jarEntry = jarInput.getNextJarEntry();
        }
    } finally {
        jarInput.close();
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) JarEntry(java.util.jar.JarEntry) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 19 with JarEntry

use of java.util.jar.JarEntry in project weave by continuuity.

the class ApplicationBundler method saveEntry.

/**
   * Saves a class entry to the jar output.
   */
private void saveEntry(String entry, URL url, Set<String> entries, JarOutputStream jarOut, boolean compress) {
    if (!entries.add(entry)) {
        return;
    }
    try {
        JarEntry jarEntry = new JarEntry(entry);
        InputStream is = url.openStream();
        try {
            if (compress) {
                jarOut.putNextEntry(jarEntry);
                ByteStreams.copy(is, jarOut);
            } else {
                crc32.reset();
                TransferByteOutputStream os = new TransferByteOutputStream();
                CheckedOutputStream checkedOut = new CheckedOutputStream(os, crc32);
                ByteStreams.copy(is, checkedOut);
                checkedOut.close();
                long size = os.size();
                jarEntry.setMethod(JarEntry.STORED);
                jarEntry.setSize(size);
                jarEntry.setCrc(checkedOut.getChecksum().getValue());
                jarOut.putNextEntry(jarEntry);
                os.transfer(jarOut);
            }
        } finally {
            is.close();
        }
        jarOut.closeEntry();
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : InputStream(java.io.InputStream) CheckedOutputStream(java.util.zip.CheckedOutputStream) JarEntry(java.util.jar.JarEntry) IOException(java.io.IOException)

Example 20 with JarEntry

use of java.util.jar.JarEntry in project crunch by cloudera.

the class WordCountHBaseTest method jarUp.

private void jarUp(JarOutputStream jos, File baseDir, String classDir) throws IOException {
    File file = new File(baseDir, classDir);
    JarEntry e = new JarEntry(classDir);
    e.setTime(file.lastModified());
    jos.putNextEntry(e);
    ByteStreams.copy(new FileInputStream(file), jos);
    jos.closeEntry();
}
Also used : JarEntry(java.util.jar.JarEntry) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

JarEntry (java.util.jar.JarEntry)506 JarFile (java.util.jar.JarFile)238 File (java.io.File)188 IOException (java.io.IOException)167 InputStream (java.io.InputStream)116 JarOutputStream (java.util.jar.JarOutputStream)102 FileOutputStream (java.io.FileOutputStream)96 FileInputStream (java.io.FileInputStream)79 JarInputStream (java.util.jar.JarInputStream)76 URL (java.net.URL)70 ArrayList (java.util.ArrayList)55 Manifest (java.util.jar.Manifest)50 JarURLConnection (java.net.JarURLConnection)42 Test (org.junit.Test)34 ZipFile (java.util.zip.ZipFile)30 ZipEntry (java.util.zip.ZipEntry)29 OutputStream (java.io.OutputStream)26 HashSet (java.util.HashSet)26 BufferedInputStream (java.io.BufferedInputStream)21 ByteArrayInputStream (java.io.ByteArrayInputStream)20