Search in sources :

Example 11 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 12 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 13 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)

Example 14 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 15 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)

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