This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.scripting.java-2.0.10
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-java.git

commit 321db862ee0e06b3e97b92332bcdfa78898972bc
Author: Carsten Ziegeler <cziege...@apache.org>
AuthorDate: Fri Jun 8 07:45:49 2012 +0000

    SLING-2506 : Destroy wrapper as soon as java file changes
    
    git-svn-id: 
https://svn.apache.org/repos/asf/sling/trunk/contrib/scripting/java@1347913 
13f79535-47bb-0310-9956-ffa450edef68
---
 .../java/impl/JavaScriptEngineFactory.java         | 28 +++++------------
 .../sling/scripting/java/impl/ServletCache.java    | 15 ++++++----
 .../sling/scripting/java/impl/ServletWrapper.java  | 35 ++++------------------
 3 files changed, 21 insertions(+), 57 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/scripting/java/impl/JavaScriptEngineFactory.java
 
b/src/main/java/org/apache/sling/scripting/java/impl/JavaScriptEngineFactory.java
index 2c0c4cc..e6b662a 100644
--- 
a/src/main/java/org/apache/sling/scripting/java/impl/JavaScriptEngineFactory.java
+++ 
b/src/main/java/org/apache/sling/scripting/java/impl/JavaScriptEngineFactory.java
@@ -234,20 +234,13 @@ public class JavaScriptEngineFactory
             return wrapper;
         }
 
-        synchronized (this) {
-            wrapper = this.ioProvider.getServletCache().getWrapper(scriptName);
-            if (wrapper != null) {
-                return wrapper;
-            }
-
-            wrapper = new ServletWrapper(servletConfig,
-                                         ioProvider,
-                                         scriptName,
-                                         scriptHelper);
-            this.ioProvider.getServletCache().addWrapper(scriptName, wrapper);
+        wrapper = new ServletWrapper(servletConfig,
+                                     ioProvider,
+                                     scriptName,
+                                     scriptHelper);
+        wrapper = this.ioProvider.getServletCache().addWrapper(scriptName, 
wrapper);
 
-            return wrapper;
-        }
+        return wrapper;
     }
 
     /**
@@ -262,14 +255,7 @@ public class JavaScriptEngineFactory
     }
 
     private void handleModification(final String scriptName, final boolean 
remove) {
-        if ( remove ) {
-            this.ioProvider.getServletCache().removeWrapper(scriptName);
-        } else {
-            final ServletWrapper wrapper = 
this.ioProvider.getServletCache().getWrapper(scriptName);
-            if ( wrapper != null ) {
-                wrapper.handleModification();
-            }
-        }
+        this.ioProvider.getServletCache().removeWrapper(scriptName);
     }
 
     private static class JavaScriptEngine extends AbstractSlingScriptEngine {
diff --git 
a/src/main/java/org/apache/sling/scripting/java/impl/ServletCache.java 
b/src/main/java/org/apache/sling/scripting/java/impl/ServletCache.java
index 7bed05f..7f6aaab 100644
--- a/src/main/java/org/apache/sling/scripting/java/impl/ServletCache.java
+++ b/src/main/java/org/apache/sling/scripting/java/impl/ServletCache.java
@@ -18,7 +18,6 @@
 package org.apache.sling.scripting.java.impl;
 
 import java.util.Iterator;
-import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
 /**
@@ -29,7 +28,7 @@ public final class ServletCache {
     /**
      * Maps servlet source urls to servlet wrappers.
      */
-    private Map<String, ServletWrapper> servlets = new 
ConcurrentHashMap<String, ServletWrapper>();
+    private ConcurrentHashMap<String, ServletWrapper> servlets = new 
ConcurrentHashMap<String, ServletWrapper>();
 
     /**
      * Add a new ServletWrapper.
@@ -37,8 +36,12 @@ public final class ServletCache {
      * @param servletUri Servlet URI
      * @param sw Servlet wrapper
      */
-    public void addWrapper(String servletUri, ServletWrapper sw) {
-        servlets.put(servletUri, sw);
+    public ServletWrapper addWrapper(final String servletUri, final 
ServletWrapper sw) {
+        ServletWrapper previous = servlets.putIfAbsent(servletUri, sw);
+        if ( previous != null ) {
+            return previous;
+        }
+        return sw;
     }
 
     /**
@@ -47,7 +50,7 @@ public final class ServletCache {
      * @param servletUri Servlet URI
      * @return ServletWrapper
      */
-    public ServletWrapper getWrapper(String servletUri) {
+    public ServletWrapper getWrapper(final String servletUri) {
         return servlets.get(servletUri);
     }
 
@@ -56,7 +59,7 @@ public final class ServletCache {
      *
      * @param servletUri Servlet URI
      */
-    public void removeWrapper(String servletUri) {
+    public void removeWrapper(final String servletUri) {
         final ServletWrapper wrapper = servlets.remove(servletUri);
         if ( wrapper != null ) {
             wrapper.destroy();
diff --git 
a/src/main/java/org/apache/sling/scripting/java/impl/ServletWrapper.java 
b/src/main/java/org/apache/sling/scripting/java/impl/ServletWrapper.java
index 9a430e1..c1e0233 100644
--- a/src/main/java/org/apache/sling/scripting/java/impl/ServletWrapper.java
+++ b/src/main/java/org/apache/sling/scripting/java/impl/ServletWrapper.java
@@ -64,9 +64,6 @@ public class ServletWrapper {
 
     private final SlingScriptHelper scriptHelper;
 
-    /** Flag for handling modifications. */
-    private volatile long lastModificationTest = 0L;
-
     /**
      * The compiled and instantiated servlet. This field may be null in which 
case a new servlet
      * instance is created per request.
@@ -116,21 +113,6 @@ public class ServletWrapper {
                 available = 0;
             }
 
-            // check for compilation
-            if (this.lastModificationTest <= 0 ) {
-                synchronized (this) {
-                    if (this.lastModificationTest <= 0 ) {
-                        this.compile();
-                    } else if (compileException != null) {
-                        // Throw cached compilation exception
-                        throw compileException;
-                    }
-                }
-            } else if (compileException != null) {
-                // Throw cached compilation exception
-                throw compileException;
-            }
-
             final Servlet servlet = this.getServlet();
 
             // invoke the servlet
@@ -169,21 +151,13 @@ public class ServletWrapper {
     }
 
     /**
-     * Handle the modification.
-     */
-    public void handleModification() {
-        logger.debug("Received modification event for {}", this.sourcePath);
-        this.lastModificationTest = -1;
-    }
-
-    /**
      * Check if the used classloader is still valid
      */
     private boolean checkReload() {
         if ( theServlet != null && theServlet.getClass().getClassLoader() 
instanceof DynamicClassLoader ) {
             return 
!((DynamicClassLoader)theServlet.getClass().getClassLoader()).isLive();
         }
-        return false;
+        return theServlet == null;
     }
 
     /**
@@ -192,6 +166,9 @@ public class ServletWrapper {
      */
     public Servlet getServlet()
     throws Exception {
+        if ( this.compileException != null ) {
+            throw this.compileException;
+        }
         // check if the used class loader is still alive
         if (this.checkReload()) {
             synchronized (this) {
@@ -265,7 +242,7 @@ public class ServletWrapper {
         // clear exception
         this.compileException = null;
         try {
-            final CompilerOptions opts = (this.lastModificationTest == -1 ? 
this.ioProvider.getForceCompileOptions() : this.ioProvider.getOptions());
+            final CompilerOptions opts = 
this.ioProvider.getForceCompileOptions();
             final CompilationUnit unit = new CompilationUnit(this.sourcePath, 
className, ioProvider);
             final CompilationResult result = 
this.ioProvider.getCompiler().compile(new 
org.apache.sling.commons.compiler.CompilationUnit[] {unit},
                     opts);
@@ -287,8 +264,6 @@ public class ServletWrapper {
             // store exception for futher access attempts
             this.compileException = ex;
             throw ex;
-        } finally {
-            this.lastModificationTest = System.currentTimeMillis();
         }
     }
 

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <commits@sling.apache.org>.

Reply via email to