Search in sources :

Example 66 with JarEntry

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

the class JarWarResource method getJarInputStreamWrapper.

@Override
protected JarInputStreamWrapper getJarInputStreamWrapper() {
    JarFile warFile = null;
    JarInputStream jarIs = null;
    JarEntry entry = null;
    try {
        warFile = getArchiveResourceSet().openJarFile();
        JarEntry jarFileInWar = warFile.getJarEntry(archivePath);
        InputStream isInWar = warFile.getInputStream(jarFileInWar);
        jarIs = new JarInputStream(isInWar);
        entry = jarIs.getNextJarEntry();
        while (entry != null && !entry.getName().equals(getResource().getName())) {
            entry = jarIs.getNextJarEntry();
        }
        if (entry == null) {
            return null;
        }
        return new JarInputStreamWrapper(entry, jarIs);
    } catch (IOException e) {
        if (log.isDebugEnabled()) {
            log.debug(sm.getString("jarResource.getInputStreamFail", getResource().getName(), getBaseUrl()), e);
        }
        return null;
    } finally {
        if (entry == null) {
            if (jarIs != null) {
                try {
                    jarIs.close();
                } catch (IOException ioe) {
                // Ignore
                }
            }
            if (warFile != null) {
                getArchiveResourceSet().closeJarFile();
            }
        }
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry)

Example 67 with JarEntry

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

the class Utils method extractDirFromJarImpl.

public void extractDirFromJarImpl(String jarpath, String dir, File destdir) {
    try (JarFile jarFile = new JarFile(jarpath)) {
        Enumeration<JarEntry> jarEnums = jarFile.entries();
        while (jarEnums.hasMoreElements()) {
            JarEntry entry = jarEnums.nextElement();
            if (!entry.isDirectory() && entry.getName().startsWith(dir)) {
                File aFile = new File(destdir, entry.getName());
                aFile.getParentFile().mkdirs();
                try (FileOutputStream out = new FileOutputStream(aFile);
                    InputStream in = jarFile.getInputStream(entry)) {
                    IOUtils.copy(in, out);
                }
            }
        }
    } catch (IOException e) {
        LOG.info("Could not extract {} from {}", dir, jarpath);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) ClassLoaderObjectInputStream(org.apache.commons.io.input.ClassLoaderObjectInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) JarFile(java.util.jar.JarFile) ZipFile(java.util.zip.ZipFile)

Example 68 with JarEntry

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

the class DefaultShader method addRemappedClass.

private void addRemappedClass(RelocatorRemapper remapper, JarOutputStream jos, String name, InputStream is) throws IOException {
    LOG.debug("Remapping class... " + name);
    if (!remapper.hasRelocators()) {
        try {
            LOG.debug("Just copy class...");
            jos.putNextEntry(new JarEntry(name));
            IOUtil.copy(is, jos);
        } catch (ZipException e) {
            LOG.info("zip exception ", e);
        }
        return;
    }
    ClassReader cr = new ClassReader(is);
    // We don't pass the ClassReader here. This forces the ClassWriter to rebuild the constant pool.
    // Copying the original constant pool should be avoided because it would keep references
    // to the original class names. This is not a problem at runtime (because these entries in the
    // constant pool are never used), but confuses some tools such as Felix' maven-bundle-plugin
    // that use the constant pool to determine the dependencies of a class.
    ClassWriter cw = new ClassWriter(0);
    final String pkg = name.substring(0, name.lastIndexOf('/') + 1);
    ClassVisitor cv = new RemappingClassAdapter(cw, remapper) {

        @Override
        public void visitSource(final String source, final String debug) {
            LOG.debug("visitSource " + source);
            if (source == null) {
                super.visitSource(source, debug);
            } else {
                final String fqSource = pkg + source;
                final String mappedSource = remapper.map(fqSource);
                final String filename = mappedSource.substring(mappedSource.lastIndexOf('/') + 1);
                LOG.debug("Remapped to " + filename);
                super.visitSource(filename, debug);
            }
        }
    };
    try {
        cr.accept(cv, ClassReader.EXPAND_FRAMES);
    } catch (Throwable ise) {
        throw new IOException("Error in ASM processing class " + name, ise);
    }
    byte[] renamedClass = cw.toByteArray();
    // Need to take the .class off for remapping evaluation
    String mappedName = remapper.map(name.substring(0, name.indexOf('.')));
    LOG.debug("Remapped class name to " + mappedName);
    try {
        // Now we put it back on so the class file is written out with the right extension.
        jos.putNextEntry(new JarEntry(mappedName + ".class"));
        jos.write(renamedClass);
    } catch (ZipException e) {
        LOG.info("zip exception ", e);
    }
}
Also used : RemappingClassAdapter(org.objectweb.asm.commons.RemappingClassAdapter) ClassReader(org.objectweb.asm.ClassReader) ZipException(java.util.zip.ZipException) ClassVisitor(org.objectweb.asm.ClassVisitor) JarEntry(java.util.jar.JarEntry) ClassWriter(org.objectweb.asm.ClassWriter)

Example 69 with JarEntry

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

the class DefaultShader method addJavaSource.

private void addJavaSource(Set<String> resources, JarOutputStream jos, String name, InputStream is, List<Relocator> relocators) throws IOException {
    jos.putNextEntry(new JarEntry(name));
    String sourceContent = IOUtil.toString(new InputStreamReader(is, "UTF-8"));
    for (Relocator relocator : relocators) {
        sourceContent = relocator.applyToSourceContent(sourceContent);
    }
    OutputStreamWriter writer = new OutputStreamWriter(jos, "UTF-8");
    writer.append(sourceContent);
    writer.flush();
    resources.add(name);
}
Also used : JarEntry(java.util.jar.JarEntry) Relocator(org.apache.storm.hack.relocation.Relocator)

Example 70 with JarEntry

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

the class DefaultShader method addDirectory.

private void addDirectory(Set<String> resources, JarOutputStream jos, String name) throws IOException {
    if (name.lastIndexOf('/') > 0) {
        String parent = name.substring(0, name.lastIndexOf('/'));
        if (!resources.contains(parent)) {
            addDirectory(resources, jos, parent);
        }
    }
    // directory entries must end in "/"
    JarEntry entry = new JarEntry(name + "/");
    LOG.debug("Adding JAR directory " + entry);
    jos.putNextEntry(entry);
    resources.add(name);
}
Also used : JarEntry(java.util.jar.JarEntry)

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