Search in sources :

Example 1 with JspServletWrapper

use of org.apache.jasper.servlet.JspServletWrapper 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)

Example 2 with JspServletWrapper

use of org.apache.jasper.servlet.JspServletWrapper in project tomcat by apache.

the class JspRuntimeContext method makeYoungest.

/**
     * Push unloadHandle for JspServletWrapper to front of the queue.
     *
     * @param unloadHandle the unloadHandle for the jsp.
     * */
public void makeYoungest(FastRemovalDequeue<JspServletWrapper>.Entry<JspServletWrapper> unloadHandle) {
    if (log.isTraceEnabled()) {
        JspServletWrapper jsw = unloadHandle.getContent();
        log.trace(Localizer.getMessage("jsp.message.jsp_queue_update", jsw.getJspUri(), context.getContextPath()));
    }
    jspQueue.moveFirst(unloadHandle);
}
Also used : JspServletWrapper(org.apache.jasper.servlet.JspServletWrapper)

Example 3 with JspServletWrapper

use of org.apache.jasper.servlet.JspServletWrapper in project tomcat by apache.

the class JspRuntimeContext method checkCompile.

/**
     * Method used by background thread to check the JSP dependencies
     * registered with this class for JSP's.
     */
public void checkCompile() {
    if (lastCompileCheck < 0) {
        // Checking was disabled
        return;
    }
    long now = System.currentTimeMillis();
    if (now > (lastCompileCheck + (options.getCheckInterval() * 1000L))) {
        lastCompileCheck = now;
    } else {
        return;
    }
    Object[] wrappers = jsps.values().toArray();
    for (int i = 0; i < wrappers.length; i++) {
        JspServletWrapper jsw = (JspServletWrapper) wrappers[i];
        JspCompilationContext ctxt = jsw.getJspEngineContext();
        // it detects it has to do a reload
        synchronized (jsw) {
            try {
                ctxt.compile();
            } catch (FileNotFoundException ex) {
                ctxt.incrementRemoved();
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
                jsw.getServletContext().log("Background compile failed", t);
            }
        }
    }
}
Also used : JspServletWrapper(org.apache.jasper.servlet.JspServletWrapper) JspCompilationContext(org.apache.jasper.JspCompilationContext) FileNotFoundException(java.io.FileNotFoundException)

Example 4 with JspServletWrapper

use of org.apache.jasper.servlet.JspServletWrapper in project tomcat by apache.

the class JspRuntimeContext method checkUnload.

/**
     * Method used by background thread to check if any JSP's should be unloaded.
     */
public void checkUnload() {
    if (log.isTraceEnabled()) {
        int queueLength = -1;
        if (jspQueue != null) {
            queueLength = jspQueue.getSize();
        }
        log.trace(Localizer.getMessage("jsp.message.jsp_unload_check", context.getContextPath(), "" + jsps.size(), "" + queueLength));
    }
    long now = System.currentTimeMillis();
    if (jspIdleTimeout > 0) {
        long unloadBefore = now - jspIdleTimeout;
        Object[] wrappers = jsps.values().toArray();
        for (int i = 0; i < wrappers.length; i++) {
            JspServletWrapper jsw = (JspServletWrapper) wrappers[i];
            synchronized (jsw) {
                if (jsw.getLastUsageTime() < unloadBefore) {
                    if (log.isDebugEnabled()) {
                        log.debug(Localizer.getMessage("jsp.message.jsp_removed_idle", jsw.getJspUri(), context.getContextPath(), "" + (now - jsw.getLastUsageTime())));
                    }
                    if (jspQueue != null) {
                        jspQueue.remove(jsw.getUnloadHandle());
                    }
                    unloadJspServletWrapper(jsw);
                }
            }
        }
    }
    lastJspQueueUpdate = now;
}
Also used : JspServletWrapper(org.apache.jasper.servlet.JspServletWrapper)

Example 5 with JspServletWrapper

use of org.apache.jasper.servlet.JspServletWrapper in project tomcat by apache.

the class JspRuntimeContext method push.

/**
     * Push a newly compiled JspServletWrapper into the queue at first
     * execution of jsp. Destroy any JSP that has been replaced in the queue.
     *
     * @param jsw Servlet wrapper for jsp.
     * @return an unloadHandle that can be pushed to front of queue at later execution times.
     * */
public FastRemovalDequeue<JspServletWrapper>.Entry<JspServletWrapper> push(JspServletWrapper jsw) {
    if (log.isTraceEnabled()) {
        log.trace(Localizer.getMessage("jsp.message.jsp_added", jsw.getJspUri(), context.getContextPath()));
    }
    FastRemovalDequeue<JspServletWrapper>.Entry<JspServletWrapper> entry = jspQueue.push(jsw);
    JspServletWrapper replaced = entry.getReplaced();
    if (replaced != null) {
        if (log.isDebugEnabled()) {
            log.debug(Localizer.getMessage("jsp.message.jsp_removed_excess", replaced.getJspUri(), context.getContextPath()));
        }
        unloadJspServletWrapper(replaced);
        entry.clearReplaced();
    }
    return entry;
}
Also used : JspServletWrapper(org.apache.jasper.servlet.JspServletWrapper) FastRemovalDequeue(org.apache.jasper.util.FastRemovalDequeue)

Aggregations

JspServletWrapper (org.apache.jasper.servlet.JspServletWrapper)5 JspCompilationContext (org.apache.jasper.JspCompilationContext)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Entry (java.util.Map.Entry)1 JasperException (org.apache.jasper.JasperException)1 JspSourceDependent (org.apache.jasper.runtime.JspSourceDependent)1 FastRemovalDequeue (org.apache.jasper.util.FastRemovalDequeue)1 Jar (org.apache.tomcat.Jar)1