Search in sources :

Example 71 with JarEntry

use of java.util.jar.JarEntry in project tomcat by apache.

the class AbstractArchiveResourceSet method getResource.

@Override
public final WebResource getResource(String path) {
    checkPath(path);
    String webAppMount = getWebAppMount();
    WebResourceRoot root = getRoot();
    if (path.startsWith(webAppMount)) {
        String pathInJar = getInternalPath() + path.substring(webAppMount.length(), path.length());
        // Always strip off the leading '/' to get the JAR path
        if (pathInJar.length() > 0 && pathInJar.charAt(0) == '/') {
            pathInJar = pathInJar.substring(1);
        }
        if (pathInJar.equals("")) {
            // This is a directory resource so the path must end with /
            if (!path.endsWith("/")) {
                path = path + "/";
            }
            return new JarResourceRoot(root, new File(getBase()), baseUrlString, path);
        } else {
            Map<String, JarEntry> jarEntries = getArchiveEntries(true);
            JarEntry jarEntry = null;
            if (!(pathInJar.charAt(pathInJar.length() - 1) == '/')) {
                if (jarEntries == null) {
                    jarEntry = getArchiveEntry(pathInJar + '/');
                } else {
                    jarEntry = jarEntries.get(pathInJar + '/');
                }
                if (jarEntry != null) {
                    path = path + '/';
                }
            }
            if (jarEntry == null) {
                if (jarEntries == null) {
                    jarEntry = getArchiveEntry(pathInJar);
                } else {
                    jarEntry = jarEntries.get(pathInJar);
                }
            }
            if (jarEntry == null) {
                return new EmptyResource(root, path);
            } else {
                return createArchiveResource(jarEntry, path, getManifest());
            }
        }
    } else {
        return new EmptyResource(root, path);
    }
}
Also used : JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile) File(java.io.File) WebResourceRoot(org.apache.catalina.WebResourceRoot)

Example 72 with JarEntry

use of java.util.jar.JarEntry in project tomcat by apache.

the class AbstractSingleArchiveResourceSet method getArchiveEntries.

@Override
protected HashMap<String, JarEntry> getArchiveEntries(boolean single) {
    synchronized (archiveLock) {
        if (archiveEntries == null && !single) {
            JarFile jarFile = null;
            archiveEntries = new HashMap<>();
            try {
                jarFile = openJarFile();
                Enumeration<JarEntry> entries = jarFile.entries();
                while (entries.hasMoreElements()) {
                    JarEntry entry = entries.nextElement();
                    archiveEntries.put(entry.getName(), entry);
                }
            } catch (IOException ioe) {
                // Should never happen
                archiveEntries = null;
                throw new IllegalStateException(ioe);
            } finally {
                if (jarFile != null) {
                    closeJarFile();
                }
            }
        }
        return archiveEntries;
    }
}
Also used : IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry)

Example 73 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 74 with JarEntry

use of java.util.jar.JarEntry in project buck by facebook.

the class JarDirectoryStepHelper method createJarFile.

public static int createJarFile(ProjectFilesystem filesystem, Path pathToOutputFile, CustomZipOutputStream outputFile, ImmutableSortedSet<Path> entriesToJar, ImmutableSet<String> alreadyAddedEntriesToOutputFile, Optional<String> mainClass, Optional<Path> manifestFile, boolean mergeManifests, Iterable<Pattern> blacklist, JavacEventSink eventSink, PrintStream stdErr) throws IOException {
    Set<String> alreadyAddedEntries = Sets.newHashSet(alreadyAddedEntriesToOutputFile);
    // Write the manifest first.
    JarEntry metaInf = new JarEntry("META-INF/");
    // We want deterministic JARs, so avoid mtimes. -1 is timzeone independent, 0 is not.
    metaInf.setTime(ZipConstants.getFakeTime());
    outputFile.putNextEntry(metaInf);
    outputFile.closeEntry();
    alreadyAddedEntries.add("META-INF/");
    Manifest manifest = createManifest(filesystem, entriesToJar, mainClass, manifestFile, mergeManifests);
    JarEntry manifestEntry = new JarEntry(JarFile.MANIFEST_NAME);
    // We want deterministic JARs, so avoid mtimes. -1 is timzeone independent, 0 is not.
    manifestEntry.setTime(ZipConstants.getFakeTime());
    outputFile.putNextEntry(manifestEntry);
    manifest.write(outputFile);
    outputFile.closeEntry();
    alreadyAddedEntries.add(JarFile.MANIFEST_NAME);
    Path absoluteOutputPath = filesystem.getPathForRelativePath(pathToOutputFile);
    for (Path entry : entriesToJar) {
        Path file = filesystem.getPathForRelativePath(entry);
        if (Files.isRegularFile(file)) {
            Preconditions.checkArgument(!file.equals(absoluteOutputPath), "Trying to put file %s into itself", file);
            // Assume the file is a ZIP/JAR file.
            copyZipEntriesToJar(file, pathToOutputFile, outputFile, alreadyAddedEntries, eventSink, blacklist);
        } else if (Files.isDirectory(file)) {
            addFilesInDirectoryToJar(filesystem, file, outputFile, alreadyAddedEntries, blacklist, eventSink);
        } else {
            throw new IllegalStateException("Must be a file or directory: " + file);
        }
    }
    if (mainClass.isPresent() && !mainClassPresent(mainClass.get(), alreadyAddedEntries)) {
        stdErr.print(String.format("ERROR: Main class %s does not exist.\n", mainClass.get()));
        return 1;
    }
    return 0;
}
Also used : Path(java.nio.file.Path) JarEntry(java.util.jar.JarEntry) Manifest(java.util.jar.Manifest)

Example 75 with JarEntry

use of java.util.jar.JarEntry in project enumerable by hraberg.

the class LambdaCompiler method unjar.

File unjar(JarFile jarFile) throws IOException {
    InputStream in = null;
    OutputStream out = null;
    File tempDir = new File(getProperty("java.io.tmpdir"), new File(jarFile.getName()).getName() + "-" + currentTimeMillis());
    ensureDirCreated(tempDir);
    try {
        Enumeration<JarEntry> entries = jarFile.entries();
        while (entries.hasMoreElements()) {
            JarEntry jarEntry = entries.nextElement();
            File file = new File(tempDir, jarEntry.getName());
            if (jarEntry.isDirectory()) {
                file.mkdir();
            } else {
                file.getParentFile().mkdirs();
                in = jarFile.getInputStream(jarEntry);
                out = new FileOutputStream(file);
                int read;
                while ((read = in.read(buffer)) != -1) out.write(buffer, 0, read);
                out.flush();
            }
        }
        return tempDir;
    } finally {
        if (out != null)
            out.close();
        if (in != null)
            in.close();
        jarFile.close();
    }
}
Also used : JarOutputStream(java.util.jar.JarOutputStream) JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile)

Aggregations

JarEntry (java.util.jar.JarEntry)594 JarFile (java.util.jar.JarFile)290 File (java.io.File)217 IOException (java.io.IOException)187 InputStream (java.io.InputStream)134 JarOutputStream (java.util.jar.JarOutputStream)112 FileOutputStream (java.io.FileOutputStream)109 FileInputStream (java.io.FileInputStream)92 URL (java.net.URL)87 JarInputStream (java.util.jar.JarInputStream)87 ArrayList (java.util.ArrayList)67 Manifest (java.util.jar.Manifest)58 JarURLConnection (java.net.JarURLConnection)53 Test (org.junit.Test)39 HashSet (java.util.HashSet)31 ZipEntry (java.util.zip.ZipEntry)31 ZipFile (java.util.zip.ZipFile)30 OutputStream (java.io.OutputStream)29 BufferedInputStream (java.io.BufferedInputStream)26 Enumeration (java.util.Enumeration)26