Search in sources :

Example 6 with Jar

use of org.apache.tomcat.Jar in project tomcat by apache.

the class TestTldScanner method scan.

private static void scan(TldScanner.TldScannerCallback callback, File webapp, String path) throws Exception {
    String fullPath = new File(webapp, path).toURI().toString();
    URL jarUrl = new URL("jar:" + fullPath + "!/");
    try (Jar jar = JarFactory.newInstance(jarUrl)) {
        callback.scan(jar, path, true);
    }
}
Also used : Jar(org.apache.tomcat.Jar) File(java.io.File) URL(java.net.URL)

Example 7 with Jar

use of org.apache.tomcat.Jar in project tomcat 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.
     * @param fragments The set of fragments that will be scanned for
     *  static resources
     */
protected void processResourceJARs(Set<WebXml> fragments) {
    for (WebXml fragment : fragments) {
        URL url = fragment.getURL();
        try {
            if ("jar".equals(url.getProtocol()) || url.toString().endsWith(".jar")) {
                try (Jar jar = JarFactory.newInstance(url)) {
                    jar.nextEntry();
                    String entryName = jar.getEntryName();
                    while (entryName != null) {
                        if (entryName.startsWith("META-INF/resources/")) {
                            context.getResources().createWebResourceSet(WebResourceRoot.ResourceSetType.RESOURCE_JAR, "/", url, "/META-INF/resources");
                            break;
                        }
                        jar.nextEntry();
                        entryName = jar.getEntryName();
                    }
                }
            } else if ("file".equals(url.getProtocol())) {
                File file = new File(url.toURI());
                File resources = new File(file, "META-INF/resources/");
                if (resources.isDirectory()) {
                    context.getResources().createWebResourceSet(WebResourceRoot.ResourceSetType.RESOURCE_JAR, "/", resources.getAbsolutePath(), null, "/");
                }
            }
        } catch (IOException ioe) {
            log.error(sm.getString("contextConfig.resourceJarFail", url, context.getName()));
        } catch (URISyntaxException e) {
            log.error(sm.getString("contextConfig.resourceJarFail", url, context.getName()));
        }
    }
}
Also used : WebXml(org.apache.tomcat.util.descriptor.web.WebXml) Jar(org.apache.tomcat.Jar) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) File(java.io.File) URL(java.net.URL)

Example 8 with Jar

use of org.apache.tomcat.Jar in project tomcat by apache.

the class TldCache method getLastModified.

private long[] getLastModified(TldResourcePath tldResourcePath) {
    long[] result = new long[2];
    result[0] = -1;
    result[1] = -1;
    try {
        String webappPath = tldResourcePath.getWebappPath();
        if (webappPath != null) {
            // webappPath will be null for JARs containing TLDs that are on
            // the class path but not part of the web application
            URL url = servletContext.getResource(tldResourcePath.getWebappPath());
            URLConnection conn = url.openConnection();
            result[0] = conn.getLastModified();
            if ("file".equals(url.getProtocol())) {
                // Reading the last modified time opens an input stream so we
                // need to make sure it is closed again otherwise the TLD file
                // will be locked until GC runs.
                conn.getInputStream().close();
            }
        }
        try (Jar jar = tldResourcePath.openJar()) {
            if (jar != null) {
                result[1] = jar.getLastModified(tldResourcePath.getEntryName());
            }
        }
    } catch (IOException e) {
    // Ignore (shouldn't happen)
    }
    return result;
}
Also used : Jar(org.apache.tomcat.Jar) IOException(java.io.IOException) URL(java.net.URL) URLConnection(java.net.URLConnection)

Example 9 with Jar

use of org.apache.tomcat.Jar in project tomcat 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<>();
    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) {
        try (Jar jar = JarFactory.newInstance(jarURL)) {
            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();
            }
        }
    }
    System.out.println("ClassParser performance test took: " + duration + " ns");
}
Also used : InputStream(java.io.InputStream) Jar(org.apache.tomcat.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)

Aggregations

Jar (org.apache.tomcat.Jar)9 URL (java.net.URL)7 File (java.io.File)6 IOException (java.io.IOException)5 InputStream (java.io.InputStream)3 URLConnection (java.net.URLConnection)2 Entry (java.util.Map.Entry)2 JasperException (org.apache.jasper.JasperException)2 Test (org.junit.Test)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 JarURLConnection (java.net.JarURLConnection)1 URISyntaxException (java.net.URISyntaxException)1 HashSet (java.util.HashSet)1 JspCompilationContext (org.apache.jasper.JspCompilationContext)1 JspSourceDependent (org.apache.jasper.runtime.JspSourceDependent)1 JspServletWrapper (org.apache.jasper.servlet.JspServletWrapper)1 ClassFormatException (org.apache.tomcat.util.bcel.classfile.ClassFormatException)1