Search in sources :

Example 1 with Jar

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

the class Compiler method isOutDated.

/**
     * Determine if a compilation is necessary by checking the time stamp of the
     * JSP page with that of the corresponding .class or .java file. If the page
     * has dependencies, the check is also extended to its dependents, and so
     * on. This method can by overridden by a subclasses of Compiler.
     *
     * @param checkClass
     *            If true, check against .class file, if false, check against
     *            .java file.
     * @return <code>true</code> if the source generation and compilation
     *  should occur
     */
public boolean isOutDated(boolean checkClass) {
    if (jsw != null && (ctxt.getOptions().getModificationTestInterval() > 0)) {
        if (jsw.getLastModificationTest() + (ctxt.getOptions().getModificationTestInterval() * 1000) > System.currentTimeMillis()) {
            return false;
        }
        jsw.setLastModificationTest(System.currentTimeMillis());
    }
    // Test the target file first. Unless there is an error checking the
    // last modified time of the source (unlikely) the target is going to
    // have to be checked anyway. If the target doesn't exist (likely during
    // startup) this saves an unnecessary check of the source.
    File targetFile;
    if (checkClass) {
        targetFile = new File(ctxt.getClassFileName());
    } else {
        targetFile = new File(ctxt.getServletJavaFileName());
    }
    if (!targetFile.exists()) {
        return true;
    }
    long targetLastModified = targetFile.lastModified();
    if (checkClass && jsw != null) {
        jsw.setServletClassLastModifiedTime(targetLastModified);
    }
    Long jspRealLastModified = ctxt.getLastModified(ctxt.getJspFile());
    if (jspRealLastModified.longValue() < 0) {
        // Something went wrong - assume modification
        return true;
    }
    if (targetLastModified != jspRealLastModified.longValue()) {
        if (log.isDebugEnabled()) {
            log.debug("Compiler: outdated: " + targetFile + " " + targetLastModified);
        }
        return true;
    }
    // directives) have been changed.
    if (jsw == null) {
        return false;
    }
    Map<String, Long> depends = jsw.getDependants();
    if (depends == null) {
        return false;
    }
    Iterator<Entry<String, Long>> it = depends.entrySet().iterator();
    while (it.hasNext()) {
        Entry<String, Long> include = it.next();
        try {
            String key = include.getKey();
            URL includeUrl;
            long includeLastModified = 0;
            if (key.startsWith("jar:jar:")) {
                // Assume we constructed this correctly
                int entryStart = key.lastIndexOf("!/");
                String entry = key.substring(entryStart + 2);
                try (Jar jar = JarFactory.newInstance(new URL(key.substring(4, entryStart)))) {
                    includeLastModified = jar.getLastModified(entry);
                }
            } else {
                if (key.startsWith("jar:") || key.startsWith("file:")) {
                    includeUrl = new URL(key);
                } else {
                    includeUrl = ctxt.getResource(include.getKey());
                }
                if (includeUrl == null) {
                    return true;
                }
                URLConnection iuc = includeUrl.openConnection();
                if (iuc instanceof JarURLConnection) {
                    includeLastModified = ((JarURLConnection) iuc).getJarEntry().getTime();
                } else {
                    includeLastModified = iuc.getLastModified();
                }
                iuc.getInputStream().close();
            }
            if (includeLastModified != include.getValue().longValue()) {
                return true;
            }
        } catch (Exception e) {
            if (log.isDebugEnabled())
                log.debug("Problem accessing resource. Treat as outdated.", e);
            return true;
        }
    }
    return false;
}
Also used : JarURLConnection(java.net.JarURLConnection) URL(java.net.URL) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection) FileNotFoundException(java.io.FileNotFoundException) JasperException(org.apache.jasper.JasperException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Entry(java.util.Map.Entry) Jar(org.apache.tomcat.Jar) File(java.io.File)

Example 2 with Jar

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

the class StandardJarScanner method process.

/*
     * Scan a URL for JARs with the optional extensions to look at all files
     * and all directories.
     */
private void process(JarScanType scanType, JarScannerCallback callback, URL url, String webappPath, boolean isWebapp, Deque<URL> classPathUrlsToProcess) throws IOException {
    if (log.isTraceEnabled()) {
        log.trace(sm.getString("jarScan.jarUrlStart", url));
    }
    if ("jar".equals(url.getProtocol()) || url.getPath().endsWith(Constants.JAR_EXT)) {
        try (Jar jar = JarFactory.newInstance(url)) {
            if (isScanManifest()) {
                processManifest(jar, isWebapp, classPathUrlsToProcess);
            }
            callback.scan(jar, webappPath, isWebapp);
        }
    } else if ("file".equals(url.getProtocol())) {
        File f;
        try {
            f = new File(url.toURI());
            if (f.isFile() && isScanAllFiles()) {
                // Treat this file as a JAR
                URL jarURL = UriUtil.buildJarUrl(f);
                try (Jar jar = JarFactory.newInstance(jarURL)) {
                    if (isScanManifest()) {
                        processManifest(jar, isWebapp, classPathUrlsToProcess);
                    }
                    callback.scan(jar, webappPath, isWebapp);
                }
            } else if (f.isDirectory()) {
                if (scanType == JarScanType.PLUGGABILITY) {
                    callback.scan(f, webappPath, isWebapp);
                } else {
                    File metainf = new File(f.getAbsoluteFile() + File.separator + "META-INF");
                    if (metainf.isDirectory()) {
                        callback.scan(f, webappPath, isWebapp);
                    }
                }
            }
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            // Wrap the exception and re-throw
            IOException ioe = new IOException();
            ioe.initCause(t);
            throw ioe;
        }
    }
}
Also used : Jar(org.apache.tomcat.Jar) IOException(java.io.IOException) File(java.io.File) URL(java.net.URL)

Example 3 with Jar

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

the class TestAbstractInputStreamJar method testNestedJarGetInputStream.

@Test
public void testNestedJarGetInputStream() throws Exception {
    File f = new File("test/webresources/war-url-connection.war");
    StringBuilder sb = new StringBuilder("war:");
    sb.append(f.toURI().toURL());
    sb.append("*/WEB-INF/lib/test.jar");
    Jar jar = JarFactory.newInstance(new URL(sb.toString()));
    InputStream is1 = jar.getInputStream("META-INF/resources/index.html");
    ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
    IOTools.flow(is1, baos1);
    InputStream is2 = jar.getInputStream("META-INF/resources/index.html");
    ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
    IOTools.flow(is2, baos2);
    Assert.assertArrayEquals(baos1.toByteArray(), baos2.toByteArray());
}
Also used : InputStream(java.io.InputStream) Jar(org.apache.tomcat.Jar) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) URL(java.net.URL) Test(org.junit.Test)

Example 4 with Jar

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

the class ContextConfig method processAnnotationsJar.

protected void processAnnotationsJar(URL url, WebXml fragment, boolean handlesTypesOnly, Map<String, JavaClassCacheEntry> javaClassCache) {
    try (Jar 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")) {
                try (InputStream is = jar.getEntryInputStream()) {
                    processAnnotationsStream(is, fragment, handlesTypesOnly, javaClassCache);
                } catch (IOException e) {
                    log.error(sm.getString("contextConfig.inputStreamJar", entryName, url), e);
                } catch (ClassFormatException e) {
                    log.error(sm.getString("contextConfig.inputStreamJar", entryName, url), e);
                }
            }
            jar.nextEntry();
            entryName = jar.getEntryName();
        }
    } catch (IOException e) {
        log.error(sm.getString("contextConfig.jarFile", url), e);
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Jar(org.apache.tomcat.Jar) IOException(java.io.IOException) ClassFormatException(org.apache.tomcat.util.bcel.classfile.ClassFormatException)

Example 5 with Jar

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

the class TagFileProcessor method loadTagFile.

/**
     * Compiles and loads a tagfile.
     */
private Class<?> loadTagFile(Compiler compiler, String tagFilePath, TagInfo tagInfo, PageInfo parentPageInfo) throws JasperException {
    Jar tagJar = null;
    Jar tagJarOriginal = null;
    try {
        if (tagFilePath.startsWith("/META-INF/")) {
            try {
                tagJar = compiler.getCompilationContext().getTldResourcePath(tagInfo.getTagLibrary().getURI()).openJar();
            } catch (IOException ioe) {
                throw new JasperException(ioe);
            }
        }
        String wrapperUri;
        if (tagJar == null) {
            wrapperUri = tagFilePath;
        } else {
            wrapperUri = tagJar.getURL(tagFilePath);
        }
        JspCompilationContext ctxt = compiler.getCompilationContext();
        JspRuntimeContext rctxt = ctxt.getRuntimeContext();
        synchronized (rctxt) {
            JspServletWrapper wrapper = null;
            try {
                wrapper = rctxt.getWrapper(wrapperUri);
                if (wrapper == null) {
                    wrapper = new JspServletWrapper(ctxt.getServletContext(), ctxt.getOptions(), tagFilePath, tagInfo, ctxt.getRuntimeContext(), tagJar);
                    rctxt.addWrapper(wrapperUri, wrapper);
                    // Use same classloader and classpath for compiling tag files
                    wrapper.getJspEngineContext().setClassLoader(ctxt.getClassLoader());
                    wrapper.getJspEngineContext().setClassPath(ctxt.getClassPath());
                } else {
                    // Make sure that JspCompilationContext gets the latest TagInfo
                    // for the tag file. TagInfo instance was created the last
                    // time the tag file was scanned for directives, and the tag
                    // file may have been modified since then.
                    wrapper.getJspEngineContext().setTagInfo(tagInfo);
                    // This compilation needs to use the current tagJar.
                    // Compilation may be nested in which case the old tagJar
                    // will need to be restored
                    tagJarOriginal = wrapper.getJspEngineContext().getTagFileJar();
                    wrapper.getJspEngineContext().setTagFileJar(tagJar);
                }
                Class<?> tagClazz;
                int tripCount = wrapper.incTripCount();
                try {
                    if (tripCount > 0) {
                        // When tripCount is greater than zero, a circular
                        // dependency exists. The circularly dependent tag
                        // file is compiled in prototype mode, to avoid infinite
                        // recursion.
                        JspServletWrapper tempWrapper = new JspServletWrapper(ctxt.getServletContext(), ctxt.getOptions(), tagFilePath, tagInfo, ctxt.getRuntimeContext(), tagJar);
                        // Use same classloader and classpath for compiling tag files
                        tempWrapper.getJspEngineContext().setClassLoader(ctxt.getClassLoader());
                        tempWrapper.getJspEngineContext().setClassPath(ctxt.getClassPath());
                        tagClazz = tempWrapper.loadTagFilePrototype();
                        tempVector.add(tempWrapper.getJspEngineContext().getCompiler());
                    } else {
                        tagClazz = wrapper.loadTagFile();
                    }
                } finally {
                    wrapper.decTripCount();
                }
                // can only be obtained from the tag instance.
                try {
                    Object tagIns = tagClazz.newInstance();
                    if (tagIns instanceof JspSourceDependent) {
                        Iterator<Entry<String, Long>> iter = ((JspSourceDependent) tagIns).getDependants().entrySet().iterator();
                        while (iter.hasNext()) {
                            Entry<String, Long> entry = iter.next();
                            parentPageInfo.addDependant(entry.getKey(), entry.getValue());
                        }
                    }
                } catch (Exception e) {
                // ignore errors
                }
                return tagClazz;
            } finally {
                if (wrapper != null && tagJarOriginal != null) {
                    wrapper.getJspEngineContext().setTagFileJar(tagJarOriginal);
                }
            }
        }
    } finally {
        if (tagJar != null) {
            tagJar.close();
        }
    }
}
Also used : JspServletWrapper(org.apache.jasper.servlet.JspServletWrapper) JspCompilationContext(org.apache.jasper.JspCompilationContext) IOException(java.io.IOException) JspSourceDependent(org.apache.jasper.runtime.JspSourceDependent) IOException(java.io.IOException) JasperException(org.apache.jasper.JasperException) Entry(java.util.Map.Entry) JasperException(org.apache.jasper.JasperException) Jar(org.apache.tomcat.Jar)

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