Search in sources :

Example 1 with JspSourceDependent

use of org.apache.sling.scripting.jsp.jasper.runtime.JspSourceDependent in project sling by apache.

the class JspServletWrapper method loadServlet.

@SuppressWarnings("unchecked")
private Servlet loadServlet() throws ServletException, IOException {
    Servlet servlet = null;
    try {
        if (log.isDebugEnabled()) {
            log.debug("Loading servlet " + jspUri);
        }
        servlet = (Servlet) ctxt.load().newInstance();
        AnnotationProcessor annotationProcessor = (AnnotationProcessor) config.getServletContext().getAttribute(AnnotationProcessor.class.getName());
        if (annotationProcessor != null) {
            annotationProcessor.processAnnotations(servlet);
            annotationProcessor.postConstruct(servlet);
        }
        // update dependents
        final List<String> oldDeps = this.dependents;
        if (servlet != null && servlet instanceof JspSourceDependent) {
            this.dependents = (List<String>) ((JspSourceDependent) servlet).getDependants();
            if (this.dependents == null) {
                this.dependents = Collections.EMPTY_LIST;
            }
            this.ctxt.getRuntimeContext().addJspDependencies(this, this.dependents);
        }
        if (!equals(oldDeps, this.dependents)) {
            this.persistDependencies();
        }
    } catch (final IllegalAccessException e) {
        throw new JasperException(e);
    } catch (final InstantiationException e) {
        throw new JasperException(e);
    } catch (final Exception e) {
        throw new JasperException(e);
    }
    servlet.init(config);
    return servlet;
}
Also used : JasperException(org.apache.sling.scripting.jsp.jasper.JasperException) Servlet(javax.servlet.Servlet) AnnotationProcessor(org.apache.sling.scripting.jsp.jasper.runtime.AnnotationProcessor) JspSourceDependent(org.apache.sling.scripting.jsp.jasper.runtime.JspSourceDependent) ServletException(javax.servlet.ServletException) SlingServletException(org.apache.sling.api.SlingServletException) SlingException(org.apache.sling.api.SlingException) IOException(java.io.IOException) SlingIOException(org.apache.sling.api.SlingIOException) UnavailableException(javax.servlet.UnavailableException) ScriptEvaluationException(org.apache.sling.api.scripting.ScriptEvaluationException) SlingPageException(org.apache.sling.scripting.jsp.SlingPageException) JasperException(org.apache.sling.scripting.jsp.jasper.JasperException)

Example 2 with JspSourceDependent

use of org.apache.sling.scripting.jsp.jasper.runtime.JspSourceDependent in project sling by apache.

the class TagFileProcessor method loadTagFile.

/**
     * Compiles and loads a tagfile.
     */
private void loadTagFile(Compiler compiler, String tagFilePath, Node.CustomTag n, PageInfo parentPageInfo) throws JasperException {
    JspCompilationContext ctxt = compiler.getCompilationContext();
    JspRuntimeContext rctxt = ctxt.getRuntimeContext();
    rctxt.lockTagFileLoading(tagFilePath);
    try {
        JspServletWrapper wrapper = rctxt.getWrapper(tagFilePath);
        if (wrapper == null) {
            wrapper = new JspServletWrapper(ctxt.getServletContext(), ctxt.getOptions(), tagFilePath, n.getTagInfo(), ctxt.getRuntimeContext(), compiler.getDefaultIsSession(), ctxt.getTagFileJarUrl(tagFilePath));
            wrapper = rctxt.addWrapper(tagFilePath, 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(n.getTagInfo());
        }
        Class tagClazz;
        int tripCount = wrapper.incTripCount();
        try {
            if (tripCount > 0) {
                final String postfix = "_" + String.valueOf(tripCount);
                // When tripCount is greater than zero, a circular
                // dependency exists. The circularily dependant tag
                // file is compiled in prototype mode, to avoid infinite
                // recursion.
                final String tempTagFilePath = tagFilePath + postfix;
                final TagInfo tempTagInfo = new JasperTagInfo(n.getTagInfo().getTagName(), n.getTagInfo().getTagClassName() + postfix, n.getTagInfo().getBodyContent(), n.getTagInfo().getInfoString(), n.getTagInfo().getTagLibrary(), n.getTagInfo().getTagExtraInfo(), n.getTagInfo().getAttributes(), n.getTagInfo().getDisplayName(), n.getTagInfo().getSmallIcon(), n.getTagInfo().getLargeIcon(), n.getTagInfo().getTagVariableInfos(), ((JasperTagInfo) n.getTagInfo()).getDynamicAttributesMapName());
                JspServletWrapper tempWrapper = new JspServletWrapper(ctxt.getServletContext(), ctxt.getOptions(), tagFilePath, tempTagInfo, ctxt.getRuntimeContext(), compiler.getDefaultIsSession(), ctxt.getTagFileJarUrl(tempTagFilePath));
                tagClazz = tempWrapper.loadTagFilePrototype();
                tempVector.add(tempWrapper.getJspEngineContext().getCompiler());
                String name = JspUtil.getCanonicalName(tagClazz);
                final int underscorePos = name.lastIndexOf(postfix);
                if (underscorePos > -1) {
                    n.setTagHandlerClassName(name.substring(0, underscorePos));
                }
            } 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 iter = ((List) ((JspSourceDependent) tagIns).getDependants()).iterator();
                while (iter.hasNext()) {
                    parentPageInfo.addDependant((String) iter.next());
                }
            }
        } catch (Exception e) {
        // ignore errors
        }
        n.setTagHandlerClass(tagClazz);
    } finally {
        rctxt.unlockTagFileLoading(tagFilePath);
    }
}
Also used : JspServletWrapper(org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper) JspCompilationContext(org.apache.sling.scripting.jsp.jasper.JspCompilationContext) JspSourceDependent(org.apache.sling.scripting.jsp.jasper.runtime.JspSourceDependent) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) JasperException(org.apache.sling.scripting.jsp.jasper.JasperException) TagInfo(javax.servlet.jsp.tagext.TagInfo) Iterator(java.util.Iterator) List(java.util.List)

Aggregations

IOException (java.io.IOException)2 JasperException (org.apache.sling.scripting.jsp.jasper.JasperException)2 JspSourceDependent (org.apache.sling.scripting.jsp.jasper.runtime.JspSourceDependent)2 FileNotFoundException (java.io.FileNotFoundException)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Servlet (javax.servlet.Servlet)1 ServletException (javax.servlet.ServletException)1 UnavailableException (javax.servlet.UnavailableException)1 TagInfo (javax.servlet.jsp.tagext.TagInfo)1 SlingException (org.apache.sling.api.SlingException)1 SlingIOException (org.apache.sling.api.SlingIOException)1 SlingServletException (org.apache.sling.api.SlingServletException)1 ScriptEvaluationException (org.apache.sling.api.scripting.ScriptEvaluationException)1 SlingPageException (org.apache.sling.scripting.jsp.SlingPageException)1 JspCompilationContext (org.apache.sling.scripting.jsp.jasper.JspCompilationContext)1 AnnotationProcessor (org.apache.sling.scripting.jsp.jasper.runtime.AnnotationProcessor)1 JspServletWrapper (org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper)1