Search in sources :

Example 1 with JspSourceDependent

use of org.apache.jasper.runtime.JspSourceDependent 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 JspSourceDependent

use of org.apache.jasper.runtime.JspSourceDependent 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)

Aggregations

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