Search in sources :

Example 1 with JspCompilationContext

use of org.apache.jasper.JspCompilationContext in project tomcat70 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 {
    JarResource tagJarResouce = null;
    if (tagFilePath.startsWith("/META-INF/")) {
        tagJarResouce = compiler.getCompilationContext().getTldLocation(tagInfo.getTagLibrary().getURI()).getJarResource();
    }
    String wrapperUri;
    if (tagJarResouce == null) {
        wrapperUri = tagFilePath;
    } else {
        wrapperUri = tagJarResouce.getEntry(tagFilePath).toString();
    }
    JspCompilationContext ctxt = compiler.getCompilationContext();
    JspRuntimeContext rctxt = ctxt.getRuntimeContext();
    synchronized (rctxt) {
        JspServletWrapper wrapper = rctxt.getWrapper(wrapperUri);
        if (wrapper == null) {
            wrapper = new JspServletWrapper(ctxt.getServletContext(), ctxt.getOptions(), tagFilePath, tagInfo, ctxt.getRuntimeContext(), tagJarResouce);
            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);
        }
        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(), tagJarResouce);
                // 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;
    }
}
Also used : JspServletWrapper(org.apache.jasper.servlet.JspServletWrapper) JspCompilationContext(org.apache.jasper.JspCompilationContext) JspSourceDependent(org.apache.jasper.runtime.JspSourceDependent) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) JasperException(org.apache.jasper.JasperException) Entry(java.util.Map.Entry)

Example 2 with JspCompilationContext

use of org.apache.jasper.JspCompilationContext 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);
                    // Use same classloader and classpath for compiling tag files
                    wrapper.getJspEngineContext().setClassLoader(ctxt.getClassLoader());
                    wrapper.getJspEngineContext().setClassPath(ctxt.getClassPath());
                    rctxt.addWrapper(wrapperUri, wrapper);
                } 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.getConstructor().newInstance();
                    if (tagIns instanceof JspSourceDependent) {
                        for (Entry<String, Long> entry : ((JspSourceDependent) tagIns).getDependants().entrySet()) {
                            parentPageInfo.addDependant(entry.getKey(), entry.getValue());
                        }
                    }
                } catch (RuntimeException | ReflectiveOperationException 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) JasperException(org.apache.jasper.JasperException) Jar(org.apache.tomcat.Jar)

Example 3 with JspCompilationContext

use of org.apache.jasper.JspCompilationContext in project tomcat70 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 JspCompilationContext

use of org.apache.jasper.JspCompilationContext 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;
    }
    List<JspServletWrapper> wrappersToReload = new ArrayList<>();
    // Tell JspServletWrapper to ignore the reload attribute while this
    // check is in progress. See BZ 62603.
    compileCheckInProgress = true;
    Object[] wrappers = jsps.values().toArray();
    for (Object wrapper : wrappers) {
        JspServletWrapper jsw = (JspServletWrapper) wrapper;
        JspCompilationContext ctxt = jsw.getJspEngineContext();
        // Sync on JspServletWrapper when calling ctxt.compile()
        synchronized (jsw) {
            try {
                ctxt.compile();
                if (jsw.getReload()) {
                    wrappersToReload.add(jsw);
                }
            } catch (FileNotFoundException ex) {
                ctxt.incrementRemoved();
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
                jsw.getServletContext().log(Localizer.getMessage("jsp.error.backgroundCompilationFailed"), t);
            }
        }
    }
    // See BZ 62603.
    // OK to process reload flag now.
    compileCheckInProgress = false;
    // Ensure all servlets and tags that need to be reloaded, are reloaded.
    for (JspServletWrapper jsw : wrappersToReload) {
        // Triggers reload
        try {
            if (jsw.isTagFile()) {
                // conditions during the reload.
                synchronized (this) {
                    jsw.loadTagFile();
                }
            } else {
                jsw.getServlet();
            }
        } catch (ServletException e) {
            jsw.getServletContext().log(Localizer.getMessage("jsp.error.reload"), e);
        }
    }
}
Also used : JspServletWrapper(org.apache.jasper.servlet.JspServletWrapper) ServletException(jakarta.servlet.ServletException) JspCompilationContext(org.apache.jasper.JspCompilationContext) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

JspCompilationContext (org.apache.jasper.JspCompilationContext)4 JspServletWrapper (org.apache.jasper.servlet.JspServletWrapper)4 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)2 JasperException (org.apache.jasper.JasperException)2 JspSourceDependent (org.apache.jasper.runtime.JspSourceDependent)2 ServletException (jakarta.servlet.ServletException)1 ArrayList (java.util.ArrayList)1 Entry (java.util.Map.Entry)1 Jar (org.apache.tomcat.Jar)1