Author: cziegeler
Date: Tue Oct 11 06:52:24 2016
New Revision: 1764199

URL: http://svn.apache.org/viewvc?rev=1764199&view=rev
Log:
SLING-6002 : ScriptCacheImpl should move to new ResourceChangeListener API 

Modified:
    
sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ScriptCacheImpl.java

Modified: 
sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ScriptCacheImpl.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ScriptCacheImpl.java?rev=1764199&r1=1764198&r2=1764199&view=diff
==============================================================================
--- 
sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ScriptCacheImpl.java
 (original)
+++ 
sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ScriptCacheImpl.java
 Tue Oct 11 06:52:24 2016
@@ -30,9 +30,9 @@ import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
+
 import javax.annotation.Nonnull;
 import javax.script.Compilable;
-import javax.script.CompiledScript;
 import javax.script.ScriptEngine;
 import javax.script.ScriptEngineFactory;
 
@@ -51,6 +51,7 @@ import org.apache.sling.api.resource.Res
 import org.apache.sling.api.resource.ResourceResolverFactory;
 import 
org.apache.sling.api.resource.observation.ExternalResourceChangeListener;
 import org.apache.sling.api.resource.observation.ResourceChange;
+import org.apache.sling.api.resource.observation.ResourceChange.ChangeType;
 import org.apache.sling.api.resource.observation.ResourceChangeListener;
 import org.apache.sling.commons.osgi.PropertiesUtil;
 import org.apache.sling.commons.threads.ThreadPool;
@@ -93,7 +94,6 @@ import org.slf4j.LoggerFactory;
         referenceInterface = ScriptEngineFactory.class,
         policy = ReferencePolicy.DYNAMIC
 )
-@SuppressWarnings("unused")
 /**
  * The {@code ScriptCache} stores information about {@link CompiledScript} 
instances evaluated by various {@link ScriptEngine}s that
  * implement the {@link Compilable} interface.
@@ -108,7 +108,7 @@ public class ScriptCacheImpl implements
 
     private BundleContext bundleContext;
     private Map<String, SoftReference<CachedScript>> internalMap;
-    private ServiceRegistration resourceChangeListener = null;
+    private ServiceRegistration<ResourceChangeListener> resourceChangeListener;
     private Set<String> extensions = new HashSet<>();
     private String[] additionalExtensions = new String[]{};
     private String[] searchPaths = {};
@@ -195,8 +195,21 @@ public class ScriptCacheImpl implements
                     String path = change.getPath();
                     writeLock.lock();
                     try {
-                        internalMap.remove(path);
+                        final boolean removed = internalMap.remove(path) != 
null;
                         LOGGER.debug("Detected script change for {} - removed 
entry from the cache.", path);
+                        if ( !removed && change.getType() == 
ChangeType.REMOVED ) {
+                            final String prefix = path + "/";
+                            final Set<String> removal = new HashSet<>();
+                            for(final Map.Entry<String, 
SoftReference<CachedScript>> entry : internalMap.entrySet()) {
+                                if ( entry.getKey().startsWith(prefix) ) {
+                                    removal.add(entry.getKey());
+                                }
+                            }
+                            for(final String key : removal) {
+                                internalMap.remove(key);
+                                LOGGER.debug("Detected removal for {} - 
removed entry {} from the cache.", path, key);
+                            }
+                        }
                     } finally {
                         writeLock.unlock();
                     }
@@ -262,16 +275,13 @@ public class ScriptCacheImpl implements
                 for (String extension : extensions) {
                     globPatterns.add("glob:**/*." + extension);
                 }
-                Dictionary resourceChangeListenerProperties = new Hashtable();
+                Dictionary<String, Object> resourceChangeListenerProperties = 
new Hashtable<String, Object>();
                 
resourceChangeListenerProperties.put(ResourceChangeListener.PATHS, 
globPatterns.toArray(new String[globPatterns.size()]));
                 
resourceChangeListenerProperties.put(ResourceChangeListener.CHANGES,
                         new String[]{ResourceChange.ChangeType.CHANGED.name(), 
ResourceChange.ChangeType.REMOVED.name()});
                 resourceChangeListener =
                         bundleContext.registerService(
-                                new String[] {
-                                    ResourceChangeListener.class.getName(),
-                                    
ExternalResourceChangeListener.class.getName()
-                                },
+                                ResourceChangeListener.class,
                                 this,
                                 resourceChangeListenerProperties
                         );


Reply via email to