incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r922216 - in /sling/trunk/contrib/scripting/java/src/main/java/org/apache/sling/scripting/java: CompilationContext.java JavaScriptEngineFactory.java ServletWrapper.java
Date Fri, 12 Mar 2010 11:12:41 GMT
Author: cziegeler
Date: Fri Mar 12 11:12:41 2010
New Revision: 922216

URL: http://svn.apache.org/viewvc?rev=922216&view=rev
Log:
SLING-1385 : Recompile java scripts on modifications and avoid periodic check - fix error
handling and sync problems.

Modified:
    sling/trunk/contrib/scripting/java/src/main/java/org/apache/sling/scripting/java/CompilationContext.java
    sling/trunk/contrib/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaScriptEngineFactory.java
    sling/trunk/contrib/scripting/java/src/main/java/org/apache/sling/scripting/java/ServletWrapper.java

Modified: sling/trunk/contrib/scripting/java/src/main/java/org/apache/sling/scripting/java/CompilationContext.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/java/src/main/java/org/apache/sling/scripting/java/CompilationContext.java?rev=922216&r1=922215&r2=922216&view=diff
==============================================================================
--- sling/trunk/contrib/scripting/java/src/main/java/org/apache/sling/scripting/java/CompilationContext.java
(original)
+++ sling/trunk/contrib/scripting/java/src/main/java/org/apache/sling/scripting/java/CompilationContext.java
Fri Mar 12 11:12:41 2010
@@ -16,7 +16,6 @@
  */
 package org.apache.sling.scripting.java;
 
-import java.io.FileNotFoundException;
 import java.util.List;
 
 import javax.servlet.ServletException;
@@ -135,8 +134,8 @@ public class CompilationContext {
         return this.lastModificationTest;
     }
 
-    public void setLastModificationTest(final long value) {
-        this.lastModificationTest = value;
+    public void clearLastModificationTest() {
+        this.lastModificationTest = 0;
     }
 
     /**
@@ -146,8 +145,6 @@ public class CompilationContext {
         if ( this.lastModificationTest > 0 ) {
             return false;
         }
-        this.lastModificationTest = System.currentTimeMillis();
-
         final long sourceLastModified = this.ioProvider.lastModified(getSourcePath());
 
         final long targetLastModified = this.ioProvider.lastModified(getCompleteClassPath());
@@ -173,33 +170,32 @@ public class CompilationContext {
 
     // ==================== Compile and reload ====================
 
-    public void compile() throws ServletException, FileNotFoundException {
+    public boolean compile() throws Exception {
         if (this.isOutDated()) {
             try {
                 final List<CompilerError> errors = this.compiler.compile();
                 if ( errors != null ) {
-                    //this.ioProvider.delete(getCompleteClassPath());
                     throw CompilerException.create(errors);
                 }
-                this.wrapper.setReload(true);
                 this.wrapper.setCompilationException(null);
-            } catch (ServletException se) {
-                this.wrapper.setCompilationException(se);
-                throw se;
+                return true;
             } catch (Exception ex) {
-                final ServletException se = new ServletException("Unable to compile servlet.",
ex);
                 // Cache compilation exception
-                this.wrapper.setCompilationException(se);
-                throw se;
+                this.wrapper.setCompilationException(ex);
+                throw ex;
+            } finally {
+                this.lastModificationTest = System.currentTimeMillis();
             }
         }
+        this.lastModificationTest = System.currentTimeMillis();
+        return false;
     }
 
     /**
      * Load the class.
      */
     public Class<?> load()
-    throws ServletException, FileNotFoundException {
+    throws ServletException {
         final String name = this.getClassFilePath().substring(1).replace('/', '.');
         try {
             servletClass = this.options.getClassLoader().loadClass(name);

Modified: sling/trunk/contrib/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaScriptEngineFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaScriptEngineFactory.java?rev=922216&r1=922215&r2=922216&view=diff
==============================================================================
--- sling/trunk/contrib/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaScriptEngineFactory.java
(original)
+++ sling/trunk/contrib/scripting/java/src/main/java/org/apache/sling/scripting/java/JavaScriptEngineFactory.java
Fri Mar 12 11:12:41 2010
@@ -168,10 +168,13 @@ public class JavaScriptEngineFactory
             this.eventHandlerRegistration.unregister();
             this.eventHandlerRegistration = null;
         }
+        if ( this.servletCache != null ) {
+            this.servletCache.destroy();
+            this.servletCache = null;
+        }
         ioProvider = null;
         javaServletContext = null;
         servletConfig = null;
-        servletCache = null;
         compilerOptions = null;
     }
 
@@ -279,7 +282,7 @@ public class JavaScriptEngineFactory
     private void handleModification(final String scriptName) {
         final ServletWrapper wrapper = this.servletCache.getWrapper(scriptName);
         if ( wrapper != null ) {
-            wrapper.getCompilationContext().setLastModificationTest(0);
+            wrapper.getCompilationContext().clearLastModificationTest();
         }
     }
 

Modified: sling/trunk/contrib/scripting/java/src/main/java/org/apache/sling/scripting/java/ServletWrapper.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/java/src/main/java/org/apache/sling/scripting/java/ServletWrapper.java?rev=922216&r1=922215&r2=922216&view=diff
==============================================================================
--- sling/trunk/contrib/scripting/java/src/main/java/org/apache/sling/scripting/java/ServletWrapper.java
(original)
+++ sling/trunk/contrib/scripting/java/src/main/java/org/apache/sling/scripting/java/ServletWrapper.java
Fri Mar 12 11:12:41 2010
@@ -50,13 +50,9 @@ public class ServletWrapper {
     /** The servlet config. */
     private ServletConfig config;
 
-    /** Reload flag. */
-    private boolean reload = true;
-
     private Servlet theServlet;
     private long available = 0L;
-    private boolean firstTime = true;
-    private ServletException compileException;
+    private Exception compileException;
     private CompilationContext ctxt;
 
     /**
@@ -73,53 +69,38 @@ public class ServletWrapper {
                 ioProvider, servletCache, this);
     }
 
-    /**
-     * Set the reload flag.
-     * @param reload
-     */
-    public void setReload(boolean reload) {
-        this.reload = reload;
-    }
-
     public CompilationContext getCompilationContext() {
         return this.ctxt;
     }
 
     /**
      * Get the servlet.
-     * @return The servlet.
      * @throws ServletException
      * @throws IOException
      * @throws FileNotFoundException
      */
-    private Servlet getServlet()
-    throws ServletException, IOException, FileNotFoundException {
-        if (reload) {
-            synchronized (this) {
-                if (reload) {
-                    destroy();
-
-                    Servlet servlet = null;
-
-                    try {
-                        final Class<?> servletClass = ctxt.load();
-                        servlet = (Servlet) servletClass.newInstance();
-                    } catch (IllegalAccessException e) {
-                        throw new ServletException(e);
-                    } catch (InstantiationException e) {
-                        throw new ServletException(e);
-                    } catch (Exception e) {
-                        throw new ServletException(e);
-                    }
+    private void getServlet()
+    throws ServletException {
+        destroy();
 
-                    servlet.init(config);
+        Servlet servlet = null;
 
-                    theServlet = servlet;
-                    reload = false;
-                }
-            }
+        try {
+            final Class<?> servletClass = ctxt.load();
+            servlet = (Servlet) servletClass.newInstance();
+        } catch (IllegalAccessException e) {
+            throw new ServletException(e);
+        } catch (InstantiationException e) {
+            throw new ServletException(e);
+        } catch (ServletException se) {
+            throw se;
+        } catch (Exception e) {
+            throw new ServletException(e);
         }
-        return theServlet;
+
+        servlet.init(config);
+
+        theServlet = servlet;
     }
 
     /**
@@ -127,7 +108,7 @@ public class ServletWrapper {
      *
      * @param je The compilation exception
      */
-    public void setCompilationException(ServletException je) {
+    public void setCompilationException(final Exception je) {
         this.compileException = je;
     }
 
@@ -164,10 +145,9 @@ public class ServletWrapper {
      * @throws IOException
      * @throws FileNotFoundException
      */
-    public void service(HttpServletRequest request,
+    private void service(HttpServletRequest request,
                         HttpServletResponse response)
-    throws ServletException, IOException, FileNotFoundException {
-
+    throws ServletException, IOException {
         try {
 
             if (ctxt.isRemoved()) {
@@ -180,43 +160,43 @@ public class ServletWrapper {
                     response.sendError
                         (HttpServletResponse.SC_SERVICE_UNAVAILABLE,
                          "Servlet unavailable.");
+                    logger.error("Java servlet {} is unavailable.", this.servletUri);
                     return;
                 }
                 // Wait period has expired. Reset.
                 available = 0;
             }
 
-            /*
-             * (1) Compile
-             */
-            if (firstTime || ctxt.getLastModificationTest() == 0 ) {
+            // check for compilation
+            if (ctxt.getLastModificationTest() == 0 ) {
                 synchronized (this) {
-                    firstTime = false;
-
-                    // The following sets reload to true, if necessary
-                    ctxt.compile();
-                }
-            } else {
-                if (compileException != null) {
-                    // Throw cached compilation exception
-                    throw compileException;
+                    if (ctxt.getLastModificationTest() == 0 ) {
+                        if ( ctxt.compile() ) {
+                            // (re)load the servlet class
+                            getServlet();
+                        }
+
+                    } else if (compileException != null) {
+                        // Throw cached compilation exception
+                        throw compileException;
+                    }
                 }
+            } else if (compileException != null) {
+                // Throw cached compilation exception
+                throw compileException;
             }
 
-            /*
-             * (2) (Re)load servlet class file
-             */
-            getServlet();
-
         } catch (FileNotFoundException ex) {
             ctxt.incrementRemoved();
             try {
                 response.sendError(HttpServletResponse.SC_NOT_FOUND,
                                   ex.getMessage());
+                logger.error("Java servlet {} not found.", this.servletUri);
             } catch (IllegalStateException ise) {
                 logger.error("Java servlet source not found." +
                        ex.getMessage(), ex);
             }
+            return;
         } catch (SlingException ex) {
             throw ex;
         } catch (ServletException ex) {
@@ -231,9 +211,7 @@ public class ServletWrapper {
 
         try {
 
-            /*
-             * (3) Service request
-             */
+            // invoke the servlet
             if (theServlet instanceof SingleThreadModel) {
                // sync on the wrapper so that the freshness
                // of the page is determined right before servicing
@@ -254,6 +232,8 @@ public class ServletWrapper {
             response.sendError
                 (HttpServletResponse.SC_SERVICE_UNAVAILABLE,
                  ex.getMessage());
+            logger.error("Java servlet {} is unavailable.", this.servletUri);
+            return;
         } catch (SlingException ex) {
             throw ex;
         } catch (ServletException ex) {



Mime
View raw message