SLING-5784 : Use service property to identify the ServletContext registered by Sling

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/scripting/java@1765260 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/pom.xml b/pom.xml
index 329dc8c..fc1d892 100644
--- a/pom.xml
+++ b/pom.xml
@@ -24,7 +24,7 @@
   <parent>
     <groupId>org.apache.sling</groupId>
     <artifactId>sling</artifactId>
-    <version>28</version>
+    <version>29</version>
     <relativePath />
   </parent>
 
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 77a9309..851ec29 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
@@ -71,8 +71,8 @@
     @Property(name=JavaScriptEngineFactory.PROPERTY_COMPILER_TARGET_V_M, value=JavaScriptEngineFactory.VERSION_AUTO),
     @Property(name=JavaScriptEngineFactory.PROPERTY_CLASSDEBUGINFO, boolValue=true),
     @Property(name=JavaScriptEngineFactory.PROPERTY_ENCODING, value="UTF-8"),
-    @Property(name = ResourceChangeListener.CHANGES, value = {"CHANGED","REMOVED"}),
-    @Property(name = ResourceChangeListener.PATHS, value = {"glob:."}, propertyPrivate = true)
+    @Property(name = ResourceChangeListener.CHANGES, value = {"CHANGED", "REMOVED"}),
+    @Property(name = ResourceChangeListener.PATHS, value = {"."}, propertyPrivate = true)
 })
 public class JavaScriptEngineFactory
     extends AbstractScriptEngineFactory
@@ -93,7 +93,7 @@
     @Reference
     private JavaCompiler javaCompiler;
 
-    @Reference
+    @Reference(target="(name=org.apache.sling)")
     private ServletContext slingServletContext;
 
     private SlingIOProvider ioProvider;
@@ -254,7 +254,7 @@
 	}
 
     private void handleModification(final String scriptName, final boolean remove) {
-        this.ioProvider.getServletCache().removeWrapper(scriptName);
+        this.ioProvider.getServletCache().removeWrapper(scriptName, remove);
     }
 
     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 7f6aaab..0484283 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,6 +18,7 @@
 package org.apache.sling.scripting.java.impl;
 
 import java.util.Iterator;
+import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
 /**
@@ -58,16 +59,26 @@
      * Remove a  ServletWrapper.
      *
      * @param servletUri Servlet URI
+     * @param isRemove Is a remove
      */
-    public void removeWrapper(final String servletUri) {
+    public void removeWrapper(final String servletUri, final boolean isRemove) {
         final ServletWrapper wrapper = servlets.remove(servletUri);
         if ( wrapper != null ) {
             wrapper.destroy();
+        } else if ( isRemove ) {
+            final Iterator<Map.Entry<String, ServletWrapper>> iter = servlets.entrySet().iterator();
+            while ( iter.hasNext() ) {
+                final Map.Entry<String, ServletWrapper> entry = iter.next();
+                if ( entry.getKey().startsWith(servletUri) ) {
+                    iter.remove();
+                    entry.getValue().destroy();
+                }
+            }
         }
     }
 
     /**
-     * Process a "destory" event for this web application context.
+     * Process a "destroy" event for this web application context.
      */
     public void destroy() {
         Iterator<ServletWrapper> i = this.servlets.values().iterator();