incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1347910 - in /sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp: ./ jasper/ jasper/compiler/ jasper/servlet/
Date Fri, 08 Jun 2012 07:41:51 GMT
Author: cziegeler
Date: Fri Jun  8 07:41:51 2012
New Revision: 1347910

URL: http://svn.apache.org/viewvc?rev=1347910&view=rev
Log:
SLING-2505 : Try to load jsp class only if available for dependency check

Modified:
    sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/JspScriptEngineFactory.java
    sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/jasper/IsolatedClassLoader.java
    sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/jasper/JspCompilationContext.java
    sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/jasper/compiler/JspRuntimeContext.java
    sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/jasper/compiler/TagFileProcessor.java
    sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/jasper/servlet/JspServletWrapper.java

Modified: sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/JspScriptEngineFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/JspScriptEngineFactory.java?rev=1347910&r1=1347909&r2=1347910&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/JspScriptEngineFactory.java
(original)
+++ sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/JspScriptEngineFactory.java
Fri Jun  8 07:41:51 2012
@@ -249,21 +249,13 @@ public class JspScriptEngineFactory
             rctxt.removeWrapper(wrapper.getJspUri());
             this.renewJspRuntimeContext();
             rctxt = this.getJspRuntimeContext();
-            wrapper = null;
         }
 
-        synchronized (rctxt) {
-            wrapper = rctxt.getWrapper(scriptName);
-            if (wrapper != null) {
-                return wrapper;
-            }
-
-            wrapper = new JspServletWrapper(servletConfig, options,
+        wrapper = new JspServletWrapper(servletConfig, options,
                 scriptName, false, rctxt);
-            rctxt.addWrapper(scriptName, wrapper);
+        wrapper = rctxt.addWrapper(scriptName, wrapper);
 
-            return wrapper;
-        }
+        return wrapper;
     }
 
     private JspServletWrapper getJspWrapper(final SlingScriptHelper scriptHelper, final SlingBindings
bindings)

Modified: sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/jasper/IsolatedClassLoader.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/jasper/IsolatedClassLoader.java?rev=1347910&r1=1347909&r2=1347910&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/jasper/IsolatedClassLoader.java
(original)
+++ sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/jasper/IsolatedClassLoader.java
Fri Jun  8 07:41:51 2012
@@ -33,9 +33,17 @@ public final class IsolatedClassLoader
 
     private final IOProvider ioProvider;
 
-    public IsolatedClassLoader(final IOProvider ioProvider) {
+    private final String className;
+
+    private final InputStream classInputStream;
+
+    public IsolatedClassLoader(final IOProvider ioProvider,
+                    final String className,
+                    final InputStream is) {
         super(ioProvider.getClassLoader());
         this.ioProvider = ioProvider;
+        this.classInputStream = is;
+        this.className = className;
     }
 
     //---------- Class loader overwrites -------------------------------------
@@ -52,7 +60,8 @@ public final class IsolatedClassLoader
      * @return    the resulting <code>Class</code> object
      * @exception ClassNotFoundException if the class could not be found
      */
-    public final Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException
{
+    public final Class<?> loadClass(final String name, final boolean resolve)
+    throws ClassNotFoundException {
         // First check if it's already loaded
         Class<?> clazz = findLoadedClass(name);
 
@@ -120,8 +129,18 @@ public final class IsolatedClassLoader
         final String path = ":/" + name.replace('.', '/') + ".class";
         InputStream is = null;
         try {
-            is = this.ioProvider.getInputStream(path);
-            final Class<?> c = defineClass(name, is);
+            final Class<?> c;
+            if ( name.equals( this.className ) ) {
+                c = defineClass(name, this.classInputStream);
+            } else {
+                is = this.ioProvider.getInputStream(path);
+                if ( is == null ) {
+                    c = null;
+                } else {
+                    c = defineClass(name, is);
+                }
+            }
+
             if (c == null) {
                 throw new ClassNotFoundException(name);
             }
@@ -130,6 +149,10 @@ public final class IsolatedClassLoader
             throw cnfe;
         } catch (final Throwable t) {
             throw new ClassNotFoundException(name, t);
+        } finally {
+            if ( is != null ) {
+                try { is.close(); } catch (final IOException ignore) {}
+            }
         }
      }
 

Modified: sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/jasper/JspCompilationContext.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/jasper/JspCompilationContext.java?rev=1347910&r1=1347909&r2=1347910&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/jasper/JspCompilationContext.java
(original)
+++ sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/jasper/JspCompilationContext.java
Fri Jun  8 07:41:51 2012
@@ -503,7 +503,6 @@ public class JspCompilationContext {
         try {
             c.removeGeneratedFiles();
             c.compile();
-            this.getRuntimeContext().addJspDependencies(jsw);
         } catch (final JasperException ex) {
             return ex;
         } catch (final IOException ioe) {

Modified: sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/jasper/compiler/JspRuntimeContext.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/jasper/compiler/JspRuntimeContext.java?rev=1347910&r1=1347909&r2=1347910&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/jasper/compiler/JspRuntimeContext.java
(original)
+++ sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/jasper/compiler/JspRuntimeContext.java
Fri Jun  8 07:41:51 2012
@@ -222,7 +222,7 @@ public final class JspRuntimeContext {
     /**
      * Maps JSP pages to their JspServletWrapper's
      */
-    private final Map<String, JspServletWrapper> jsps = new ConcurrentHashMap<String,
JspServletWrapper>();
+    private final ConcurrentHashMap<String, JspServletWrapper> jsps = new ConcurrentHashMap<String,
JspServletWrapper>();
 
     /**
      * Maps dependencies to the using jsp.
@@ -231,8 +231,7 @@ public final class JspRuntimeContext {
 
     // ------------------------------------------------------ Public Methods
 
-    public void addJspDependencies(final JspServletWrapper jsw) {
-        final List<String> deps = jsw.getDependants();
+    public void addJspDependencies(final JspServletWrapper jsw, List<String> deps)
{
         if ( deps != null ) {
             final String jspUri = jsw.getJspUri();
             synchronized ( depToJsp ) {
@@ -289,9 +288,13 @@ public final class JspRuntimeContext {
      * @param jspUri JSP URI
      * @param jsw Servlet wrapper for JSP
      */
-    public void addWrapper(final String jspUri, final JspServletWrapper jsw) {
-        jsps.put(jspUri, jsw);
-        addJspDependencies(jsw);
+    public JspServletWrapper addWrapper(final String jspUri, final JspServletWrapper jsw)
{
+        final JspServletWrapper previous = jsps.putIfAbsent(jspUri, jsw);
+        if ( previous == null ) {
+            addJspDependencies(jsw, jsw.getDependants());
+            return jsw;
+        }
+        return previous;
     }
 
     /**

Modified: sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/jasper/compiler/TagFileProcessor.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/jasper/compiler/TagFileProcessor.java?rev=1347910&r1=1347909&r2=1347910&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/jasper/compiler/TagFileProcessor.java
(original)
+++ sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/jasper/compiler/TagFileProcessor.java
Fri Jun  8 07:41:51 2012
@@ -541,7 +541,7 @@ class TagFileProcessor {
                 wrapper = new JspServletWrapper(ctxt.getServletContext(), ctxt
                         .getOptions(), tagFilePath, tagInfo, ctxt
                         .getRuntimeContext(), ctxt.getTagFileJarUrl(tagFilePath));
-                rctxt.addWrapper(tagFilePath, wrapper);
+                wrapper = rctxt.addWrapper(tagFilePath, wrapper);
 
                 // Use same classloader and classpath for compiling tag files
                 //wrapper.getJspEngineContext().setClassLoader(ctxt.getClassLoader());

Modified: sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/jasper/servlet/JspServletWrapper.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/jasper/servlet/JspServletWrapper.java?rev=1347910&r1=1347909&r2=1347910&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/jasper/servlet/JspServletWrapper.java
(original)
+++ sling/trunk/bundles/scripting/jsp/src/main/java/org/apache/sling/scripting/jsp/jasper/servlet/JspServletWrapper.java
Fri Jun  8 07:41:51 2012
@@ -18,8 +18,9 @@
 package org.apache.sling.scripting.jsp.jasper.servlet;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.URL;
-import java.net.URLConnection;
+import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 
@@ -85,7 +86,6 @@ public class JspServletWrapper {
     private final Options options;
     private final boolean isTagFile;
     private volatile JasperException compileException;
-    private volatile long lastModificationTest = -1L;
     private volatile int tripCount;
 
     private volatile List<String> dependents;
@@ -139,6 +139,7 @@ public class JspServletWrapper {
         return false;
     }
 
+    @SuppressWarnings("unchecked")
     private Servlet loadServlet()
     throws ServletException, IOException {
         Servlet servlet = null;
@@ -150,6 +151,11 @@ public class JspServletWrapper {
                annotationProcessor.processAnnotations(servlet);
                annotationProcessor.postConstruct(servlet);
             }
+            // update dependents
+            if (servlet != null && servlet instanceof JspSourceDependent) {
+                this.dependents = (List<String>) ((JspSourceDependent) servlet).getDependants();
+                this.ctxt.getRuntimeContext().addJspDependencies(this, this.dependents);
+            }
         } catch (IllegalAccessException e) {
             throw new JasperException(e);
         } catch (InstantiationException e) {
@@ -167,21 +173,9 @@ public class JspServletWrapper {
      * Compile (if needed) and load a tag file
      */
     public Class<?> loadTagFile() throws JasperException {
-        if (this.lastModificationTest <= 0) {
-            synchronized (this) {
-                if (this.lastModificationTest <= 0 ) {
-                    this.compileException = ctxt.compile();
-                    this.lastModificationTest = System.currentTimeMillis();
-                    if ( compileException != null ) {
-                        throw compileException;
-                    }
-                } else if ( compileException != null ) {
-                    // Throw cached compilation exception
-                    throw compileException;
-                }
-            }
-        } else {
-            if (compileException != null) {
+        synchronized (this) {
+            this.compileException = ctxt.compile();
+            if ( compileException != null ) {
                 throw compileException;
             }
         }
@@ -211,14 +205,31 @@ public class JspServletWrapper {
     public List<String> getDependants() {
         if ( this.dependents == null ) {
             try {
-                Object target;
+                Object target = null;
                 if (isTagFile) {
                     target = ctxt.load().newInstance();
                 } else {
                     // we load the servlet with a separate class loader
                     final String name = this.ctxt.getServletPackageName() + "." + this.ctxt.getServletClassName();
-                    final ClassLoader cl = new IsolatedClassLoader(this.ctxt.getRuntimeContext().getIOProvider());
-                    target = cl.loadClass(name).newInstance();
+                    // first check if the class is available
+                    final String path = ":/" + name.replace('.', '/') + ".class";
+                    InputStream is = null;
+                    try {
+                        is = this.ctxt.getRuntimeContext().getIOProvider().getInputStream(path);
+                        if ( is != null ) {
+                            // class file is available, try to load it
+                            final ClassLoader cl = new IsolatedClassLoader(this.ctxt.getRuntimeContext().getIOProvider(),
+                                            name, is);
+                            target = cl.loadClass(name).newInstance();
+                        }
+                    } catch ( final IOException ignore ) {
+                        // excepted
+                    } finally {
+                        if ( is != null ) {
+                            try { is.close(); } catch ( final IOException ioe ) {}
+                        }
+                    }
+
                 }
                 if (target != null && target instanceof JspSourceDependent) {
                     this.dependents = (List<String>) ((JspSourceDependent) target).getDependants();
@@ -226,6 +237,10 @@ public class JspServletWrapper {
             } catch (final Throwable ex) {
                 // ignore
             }
+            // use empty list, until servlet is compiled and loaded
+            if ( this.dependents == null ) {
+                this.dependents = Collections.emptyList();
+            }
         }
         return this.dependents;
     }
@@ -279,20 +294,13 @@ public class JspServletWrapper {
             if ( include.startsWith("tld:") ) {
                 continue;
             }
-            try {
-                final URL includeUrl = ctxt.getResource(include);
-                if (includeUrl == null) {
-                    return true;
-                }
-
-                URLConnection includeUconn = includeUrl.openConnection();
-                long includeLastModified = includeUconn.getLastModified();
-                includeUconn.getInputStream().close();
+            final long includeLastModified = ctxt.getRuntimeContext().getIOProvider().lastModified(include);
 
-                if (includeLastModified > targetLastModified) {
-                    return true;
+            if (includeLastModified > targetLastModified) {
+                if (log.isDebugEnabled()) {
+                    log.debug("Compiler: outdated: " + targetFile + " because of dependency
" + include + " : "
+                            + targetLastModified + " - " + includeLastModified);
                 }
-            } catch (Exception e) {
                 return true;
             }
         }



Mime
View raw message