Search in sources :

Example 1 with Jar

use of org.apache.tomcat.util.scan.Jar in project tomcat70 by apache.

the class TesterPerformance method testClassParserPerformance.

@Test
public void testClassParserPerformance() throws IOException {
    File libDir = new File(JAR_LOCATION);
    String[] libs = libDir.list();
    Assert.assertNotNull(libs);
    Set<URL> jarURLs = new HashSet<URL>();
    for (String lib : libs) {
        if (!lib.toLowerCase(Locale.ENGLISH).endsWith(".jar")) {
            continue;
        }
        jarURLs.add(new URL("jar:" + new File(libDir, lib).toURI().toURL().toExternalForm() + "!/"));
    }
    long duration = 0;
    for (URL jarURL : jarURLs) {
        Jar jar = JarFactory.newInstance(jarURL);
        try {
            jar.nextEntry();
            String jarEntryName = jar.getEntryName();
            while (jarEntryName != null) {
                if (jarEntryName.endsWith(".class")) {
                    InputStream is = jar.getEntryInputStream();
                    long start = System.nanoTime();
                    ClassParser cp = new ClassParser(is);
                    cp.parse();
                    duration += System.nanoTime() - start;
                }
                jar.nextEntry();
                jarEntryName = jar.getEntryName();
            }
        } finally {
            jar.close();
        }
    }
    System.out.println("ClassParser performance test took: " + duration + " ns");
}
Also used : InputStream(java.io.InputStream) Jar(org.apache.tomcat.util.scan.Jar) File(java.io.File) URL(java.net.URL) HashSet(java.util.HashSet) ClassParser(org.apache.tomcat.util.bcel.classfile.ClassParser) Test(org.junit.Test)

Example 2 with Jar

use of org.apache.tomcat.util.scan.Jar in project tomcat70 by apache.

the class TldLocationsCache method tldScanJar.

/*
     * Scans the given JarURLConnection for TLD files located in META-INF
     * (or a subdirectory of it), adding an implicit map entry to the taglib
     * map for any TLD that has a <uri> element.
     *
     * @param jarConn The JarURLConnection to the JAR file to scan
     * 
     * Keep in sync with o.a.c.startup.TldConfig
     */
private void tldScanJar(JarURLConnection jarConn) throws IOException {
    Jar jar = null;
    InputStream is;
    boolean foundTld = false;
    URL resourceURL = jarConn.getJarFileURL();
    String resourcePath = resourceURL.toString();
    try {
        jar = JarFactory.newInstance(jarConn.getURL());
        jar.nextEntry();
        String entryName = jar.getEntryName();
        while (entryName != null) {
            if (entryName.startsWith("META-INF/") && entryName.endsWith(".tld")) {
                is = null;
                try {
                    is = jar.getEntryInputStream();
                    foundTld = true;
                    tldScanStream(resourcePath, entryName, is);
                } finally {
                    if (is != null) {
                        try {
                            is.close();
                        } catch (IOException ioe) {
                        // Ignore
                        }
                    }
                }
            }
            jar.nextEntry();
            entryName = jar.getEntryName();
        }
    } finally {
        if (jar != null) {
            jar.close();
        }
    }
    if (!foundTld) {
        if (log.isDebugEnabled()) {
            log.debug(Localizer.getMessage("jsp.tldCache.noTldInJar", resourcePath));
        } else if (showTldScanWarning) {
            // Not entirely thread-safe but a few duplicate log messages are
            // not a huge issue
            showTldScanWarning = false;
            log.info(Localizer.getMessage("jsp.tldCache.noTldSummary"));
        }
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Jar(org.apache.tomcat.util.scan.Jar) IOException(java.io.IOException) URL(java.net.URL)

Example 3 with Jar

use of org.apache.tomcat.util.scan.Jar in project tomcat70 by apache.

the class ContextConfig method processAnnotationsJar.

protected void processAnnotationsJar(URL url, WebXml fragment, boolean handlesTypesOnly) {
    Jar jar = null;
    InputStream is;
    try {
        jar = JarFactory.newInstance(url);
        if (log.isDebugEnabled()) {
            log.debug(sm.getString("contextConfig.processAnnotationsJar.debug", url));
        }
        jar.nextEntry();
        String entryName = jar.getEntryName();
        while (entryName != null) {
            if (entryName.endsWith(".class")) {
                is = null;
                try {
                    is = jar.getEntryInputStream();
                    processAnnotationsStream(is, fragment, handlesTypesOnly);
                } catch (IOException e) {
                    log.error(sm.getString("contextConfig.inputStreamJar", entryName, url), e);
                } catch (ClassFormatException e) {
                    log.error(sm.getString("contextConfig.inputStreamJar", entryName, url), e);
                } finally {
                    if (is != null) {
                        try {
                            is.close();
                        } catch (IOException ioe) {
                        // Ignore
                        }
                    }
                }
            }
            jar.nextEntry();
            entryName = jar.getEntryName();
        }
    } catch (IOException e) {
        log.error(sm.getString("contextConfig.jarFile", url), e);
    } finally {
        if (jar != null) {
            jar.close();
        }
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Jar(org.apache.tomcat.util.scan.Jar) IOException(java.io.IOException) ClassFormatException(org.apache.tomcat.util.bcel.classfile.ClassFormatException)

Example 4 with Jar

use of org.apache.tomcat.util.scan.Jar in project tomcat70 by apache.

the class ContextConfig method processResourceJARs.

/**
 * Scan JARs that contain web-fragment.xml files that will be used to
 * configure this application to see if they also contain static resources.
 * If static resources are found, add them to the context. Resources are
 * added in web-fragment.xml priority order.
 */
protected void processResourceJARs(Set<WebXml> fragments) {
    for (WebXml fragment : fragments) {
        URL url = fragment.getURL();
        Jar jar = null;
        try {
            // Note: Ignore file URLs for now since only jar URLs will be accepted
            if ("jar".equals(url.getProtocol())) {
                jar = JarFactory.newInstance(url);
                jar.nextEntry();
                String entryName = jar.getEntryName();
                while (entryName != null) {
                    if (entryName.startsWith("META-INF/resources/")) {
                        context.addResourceJarUrl(url);
                        break;
                    }
                    jar.nextEntry();
                    entryName = jar.getEntryName();
                }
            } else if ("file".equals(url.getProtocol())) {
                FileDirContext fileDirContext = new FileDirContext();
                fileDirContext.setDocBase(new File(url.toURI()).getAbsolutePath());
                try {
                    fileDirContext.lookup("META-INF/resources/");
                    // lookup succeeded
                    if (context instanceof StandardContext) {
                        ((StandardContext) context).addResourcesDirContext(fileDirContext);
                    }
                } catch (NamingException e) {
                // not found, ignore
                }
            }
        } catch (IOException ioe) {
            log.error(sm.getString("contextConfig.resourceJarFail", url, context.getName()));
        } catch (URISyntaxException e) {
            log.error(sm.getString("contextConfig.resourceJarFail", url, context.getName()));
        } finally {
            if (jar != null) {
                jar.close();
            }
        }
    }
}
Also used : WebXml(org.apache.catalina.deploy.WebXml) FileDirContext(org.apache.naming.resources.FileDirContext) StandardContext(org.apache.catalina.core.StandardContext) Jar(org.apache.tomcat.util.scan.Jar) NamingException(javax.naming.NamingException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) File(java.io.File) URL(java.net.URL)

Example 5 with Jar

use of org.apache.tomcat.util.scan.Jar in project tomcat70 by apache.

the class TldConfig method tldScanJar.

/*
     * Scans the given JarURLConnection for TLD files located in META-INF
     * (or a sub-directory of it).
     *
     * @param jarConn The JarURLConnection to the JAR file to scan
     * 
     * Keep in sync with o.a.j.comiler.TldLocationsCache
     */
private boolean tldScanJar(JarURLConnection jarConn) {
    Jar jar = null;
    InputStream is;
    boolean isFound = false;
    try {
        jar = JarFactory.newInstance(jarConn.getURL());
        jar.nextEntry();
        String entryName = jar.getEntryName();
        while (entryName != null) {
            if (entryName.startsWith("META-INF/") && entryName.endsWith(".tld")) {
                isFound = true;
                is = null;
                try {
                    is = jar.getEntryInputStream();
                    XmlErrorHandler handler = tldScanStream(is);
                    handler.logFindings(log, jarConn.getURL() + entryName);
                } finally {
                    if (is != null) {
                        try {
                            is.close();
                        } catch (IOException ioe) {
                        // Ignore
                        }
                    }
                }
            }
            jar.nextEntry();
            entryName = jar.getEntryName();
        }
        if (!isFound) {
            if (log.isDebugEnabled()) {
                log.debug(sm.getString("tldConfig.noTldInJar", jarConn.getURL().getFile()));
            }
        }
    } catch (IOException ioe) {
        log.warn(sm.getString("tldConfig.jarFail", jarConn.getURL()), ioe);
    } finally {
        if (jar != null) {
            jar.close();
        }
    }
    return isFound;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Jar(org.apache.tomcat.util.scan.Jar) IOException(java.io.IOException) XmlErrorHandler(org.apache.tomcat.util.descriptor.XmlErrorHandler)

Aggregations

Jar (org.apache.tomcat.util.scan.Jar)5 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 FileInputStream (java.io.FileInputStream)3 URL (java.net.URL)3 File (java.io.File)2 URISyntaxException (java.net.URISyntaxException)1 HashSet (java.util.HashSet)1 NamingException (javax.naming.NamingException)1 StandardContext (org.apache.catalina.core.StandardContext)1 WebXml (org.apache.catalina.deploy.WebXml)1 FileDirContext (org.apache.naming.resources.FileDirContext)1 ClassFormatException (org.apache.tomcat.util.bcel.classfile.ClassFormatException)1 ClassParser (org.apache.tomcat.util.bcel.classfile.ClassParser)1 XmlErrorHandler (org.apache.tomcat.util.descriptor.XmlErrorHandler)1 Test (org.junit.Test)1