Search in sources :

Example 91 with JarInputStream

use of java.util.jar.JarInputStream in project jersey by jersey.

the class OsgiRegistry method getPackageResources.

/**
     * Get URLs of resources from a given package.
     *
     * @param packagePath package.
     * @param classLoader resource class loader.
     * @param recursive   whether the given package path should be scanned recursively by OSGi
     * @return URLs of the located resources.
     */
@SuppressWarnings("unchecked")
public Enumeration<URL> getPackageResources(final String packagePath, final ClassLoader classLoader, final boolean recursive) {
    final List<URL> result = new LinkedList<URL>();
    for (final Bundle bundle : bundleContext.getBundles()) {
        // Look for resources at the given <packagePath> and at WEB-INF/classes/<packagePath> in case a WAR is being examined.
        for (final String bundlePackagePath : new String[] { packagePath, WEB_INF_CLASSES + packagePath }) {
            final Enumeration<URL> enumeration = findEntries(bundle, bundlePackagePath, "*.class", recursive);
            if (enumeration != null) {
                while (enumeration.hasMoreElements()) {
                    final URL url = enumeration.nextElement();
                    final String path = url.getPath();
                    classToBundleMapping.put(bundleEntryPathToClassName(packagePath, path), bundle);
                    result.add(url);
                }
            }
        }
        // Now interested only in .jar provided by current bundle.
        final Enumeration<URL> jars = findEntries(bundle, "/", "*.jar", true);
        if (jars != null) {
            while (jars.hasMoreElements()) {
                final URL jar = jars.nextElement();
                final InputStream inputStream = classLoader.getResourceAsStream(jar.getPath());
                if (inputStream == null) {
                    LOGGER.config(LocalizationMessages.OSGI_REGISTRY_ERROR_OPENING_RESOURCE_STREAM(jar));
                    continue;
                }
                final JarInputStream jarInputStream;
                try {
                    jarInputStream = new JarInputStream(inputStream);
                } catch (final IOException ex) {
                    LOGGER.log(Level.CONFIG, LocalizationMessages.OSGI_REGISTRY_ERROR_PROCESSING_RESOURCE_STREAM(jar), ex);
                    try {
                        inputStream.close();
                    } catch (final IOException e) {
                    // ignored
                    }
                    continue;
                }
                try {
                    JarEntry jarEntry;
                    while ((jarEntry = jarInputStream.getNextJarEntry()) != null) {
                        final String jarEntryName = jarEntry.getName();
                        final String jarEntryNameLeadingSlash = jarEntryName.startsWith("/") ? jarEntryName : "/" + jarEntryName;
                        if (jarEntryName.endsWith(".class") && // explicitly instructs us to do so somehow (not implemented)
                        jarEntryNameLeadingSlash.contains("/" + normalizedPackagePath(packagePath))) {
                            if (!recursive && !isPackageLevelEntry(packagePath, jarEntryName)) {
                                continue;
                            }
                            classToBundleMapping.put(jarEntryName.replace(".class", "").replace('/', '.'), bundle);
                            result.add(bundle.getResource(jarEntryName));
                        }
                    }
                } catch (final Exception ex) {
                    LOGGER.log(Level.CONFIG, LocalizationMessages.OSGI_REGISTRY_ERROR_PROCESSING_RESOURCE_STREAM(jar), ex);
                } finally {
                    try {
                        jarInputStream.close();
                    } catch (final IOException e) {
                    // ignored
                    }
                }
            }
        }
    }
    return Collections.enumeration(result);
}
Also used : JarInputStream(java.util.jar.JarInputStream) ResourceBundle(java.util.ResourceBundle) Bundle(org.osgi.framework.Bundle) PropertyResourceBundle(java.util.PropertyResourceBundle) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) LinkedList(java.util.LinkedList) URL(java.net.URL) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) ProcessingException(javax.ws.rs.ProcessingException)

Example 92 with JarInputStream

use of java.util.jar.JarInputStream in project liquibase by liquibase.

the class DefaultPackageScanClassResolver method loadImplementationsInJar.

/**
     * Finds matching classes within a jar files that contains a folder
     * structure matching the package structure. If the File is not a JarFile or
     * does not exist a warning will be logged, but no error will be raised.
     *
     * Any nested JAR files found inside this JAR will be assumed to also be
     * on the classpath and will be recursively examined for classes in `parentPackage`.
     * @param parentPackage  the parent package under which classes must be in order to
     *                be considered
     * @param parentFileStream  the inputstream of the jar file to be examined for classes
     * @param loader a classloader which can load classes contained within the JAR file
     * @param parentFileName a unique name for the parentFileStream, to be used for caching.
*                       This is the URL of the parentFileStream, if it comes from a URL,
*                       or a composite ID if we are currently examining a nested JAR.
     */
protected void loadImplementationsInJar(String parentPackage, InputStream parentFileStream, ClassLoader loader, String parentFileName, String grandparentFileName) throws IOException {
    Set<String> classFiles = classFilesByLocation.get(parentFileName);
    if (classFiles == null) {
        classFiles = new HashSet<String>();
        classFilesByLocation.put(parentFileName, classFiles);
        Set<String> grandparentClassFiles = classFilesByLocation.get(grandparentFileName);
        if (grandparentClassFiles == null) {
            grandparentClassFiles = new HashSet<String>();
            classFilesByLocation.put(grandparentFileName, grandparentClassFiles);
        }
        JarInputStream jarStream;
        if (parentFileStream instanceof JarInputStream) {
            jarStream = (JarInputStream) parentFileStream;
        } else {
            jarStream = new JarInputStream(parentFileStream);
        }
        JarEntry entry;
        while ((entry = jarStream.getNextJarEntry()) != null) {
            String name = entry.getName();
            if (name != null) {
                if (name.endsWith(".jar")) {
                    //in a nested jar
                    log.debug("Found nested jar " + name);
                    // To avoid needing to unzip 'parentFile' in its entirety, as that
                    // may take a very long time (see CORE-2115) or not even be possible
                    // (see CORE-2595), we load the nested JAR from the classloader and
                    // read it as a zip.
                    //
                    // It is safe to assume that the nested JAR is readable by the classloader
                    // as a resource stream, because we have reached this point by scanning
                    // through packages located from `classloader` by using `getResource`.
                    // If loading this nested JAR as a resource fails, then certainly loading
                    // classes from inside it with `classloader` would fail and we are safe
                    // to exclude it form the PackageScan.
                    InputStream nestedJarResourceStream = loader.getResourceAsStream(name);
                    if (nestedJarResourceStream != null) {
                        JarInputStream nestedJarStream = new JarInputStream(nestedJarResourceStream);
                        try {
                            loadImplementationsInJar(parentPackage, nestedJarStream, loader, parentFileName + "!" + name, parentFileName);
                        } finally {
                            nestedJarStream.close();
                        }
                    }
                } else if (!entry.isDirectory() && name.endsWith(".class")) {
                    classFiles.add(name.trim());
                    grandparentClassFiles.add(name.trim());
                }
            }
        }
    }
    for (String name : classFiles) {
        if (name.contains(parentPackage)) {
            loadClass(name, loader);
        }
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) FileInputStream(java.io.FileInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) JarEntry(java.util.jar.JarEntry)

Example 93 with JarInputStream

use of java.util.jar.JarInputStream in project hadoop by apache.

the class TestJarFinder method testExistingManifest.

@Test
public void testExistingManifest() throws Exception {
    File dir = GenericTestUtils.getTestDir(TestJarFinder.class.getName() + "-testExistingManifest");
    delete(dir);
    dir.mkdirs();
    File metaInfDir = new File(dir, "META-INF");
    metaInfDir.mkdirs();
    File manifestFile = new File(metaInfDir, "MANIFEST.MF");
    Manifest manifest = new Manifest();
    OutputStream os = new FileOutputStream(manifestFile);
    manifest.write(os);
    os.close();
    File propsFile = new File(dir, "props.properties");
    Writer writer = new FileWriter(propsFile);
    new Properties().store(writer, "");
    writer.close();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JarOutputStream zos = new JarOutputStream(baos);
    JarFinder.jarDir(dir, "", zos);
    JarInputStream jis = new JarInputStream(new ByteArrayInputStream(baos.toByteArray()));
    Assert.assertNotNull(jis.getManifest());
    jis.close();
}
Also used : JarInputStream(java.util.jar.JarInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) FileOutputStream(java.io.FileOutputStream) FileWriter(java.io.FileWriter) JarOutputStream(java.util.jar.JarOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Manifest(java.util.jar.Manifest) Properties(java.util.Properties) File(java.io.File) FileWriter(java.io.FileWriter) Writer(java.io.Writer) Test(org.junit.Test)

Example 94 with JarInputStream

use of java.util.jar.JarInputStream in project hadoop by apache.

the class TestJarFinder method testNoManifest.

@Test
public void testNoManifest() throws Exception {
    File dir = GenericTestUtils.getTestDir(TestJarFinder.class.getName() + "-testNoManifest");
    delete(dir);
    dir.mkdirs();
    File propsFile = new File(dir, "props.properties");
    Writer writer = new FileWriter(propsFile);
    new Properties().store(writer, "");
    writer.close();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JarOutputStream zos = new JarOutputStream(baos);
    JarFinder.jarDir(dir, "", zos);
    JarInputStream jis = new JarInputStream(new ByteArrayInputStream(baos.toByteArray()));
    Assert.assertNotNull(jis.getManifest());
    jis.close();
}
Also used : JarInputStream(java.util.jar.JarInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileWriter(java.io.FileWriter) JarOutputStream(java.util.jar.JarOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Properties(java.util.Properties) File(java.io.File) FileWriter(java.io.FileWriter) Writer(java.io.Writer) Test(org.junit.Test)

Example 95 with JarInputStream

use of java.util.jar.JarInputStream in project flink by apache.

the class JarFileCreatorTest method validate.

private boolean validate(Set<String> expected, File out) throws IOException {
    int count = expected.size();
    try (JarInputStream jis = new JarInputStream(new FileInputStream(out))) {
        ZipEntry ze;
        while ((ze = jis.getNextEntry()) != null) {
            count--;
            expected.remove(ze.getName());
        }
    }
    return count == 0 && expected.size() == 0;
}
Also used : JarInputStream(java.util.jar.JarInputStream) ZipEntry(java.util.zip.ZipEntry) FileInputStream(java.io.FileInputStream)

Aggregations

JarInputStream (java.util.jar.JarInputStream)154 JarEntry (java.util.jar.JarEntry)72 IOException (java.io.IOException)66 FileInputStream (java.io.FileInputStream)63 Manifest (java.util.jar.Manifest)50 File (java.io.File)46 InputStream (java.io.InputStream)45 ZipEntry (java.util.zip.ZipEntry)29 JarOutputStream (java.util.jar.JarOutputStream)27 FileOutputStream (java.io.FileOutputStream)24 ByteArrayInputStream (java.io.ByteArrayInputStream)22 URL (java.net.URL)20 Test (org.junit.Test)20 OutputStream (java.io.OutputStream)13 JarFile (java.util.jar.JarFile)13 BufferedInputStream (java.io.BufferedInputStream)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 ArrayList (java.util.ArrayList)10 Attributes (java.util.jar.Attributes)10 HashSet (java.util.HashSet)8