You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ra...@apache.org on 2016/07/27 15:52:40 UTC

svn commit: r1754283 - in /sling/trunk/bundles/resourceresolver/src: main/java/org/apache/sling/resourceresolver/impl/observation/ test/java/org/apache/sling/resourceresolver/impl/observation/

Author: radu
Date: Wed Jul 27 15:52:40 2016
New Revision: 1754283

URL: http://svn.apache.org/viewvc?rev=1754283&view=rev
Log:
SLING-5903 - ResourceChangeListenerInfo does not expand glob patterns correctly

* made sure glob patterns that start with relative paths are expanded correctly by prepending the search paths

Modified:
    sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/observation/ResourceChangeListenerInfo.java
    sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/observation/ResourceChangeListenerInfoTest.java

Modified: sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/observation/ResourceChangeListenerInfo.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/observation/ResourceChangeListenerInfo.java?rev=1754283&r1=1754282&r2=1754283&view=diff
==============================================================================
--- sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/observation/ResourceChangeListenerInfo.java (original)
+++ sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/observation/ResourceChangeListenerInfo.java Wed Jul 27 15:52:40 2016
@@ -18,10 +18,6 @@
  */
 package org.apache.sling.resourceresolver.impl.observation;
 
-import static org.apache.sling.api.resource.observation.ResourceChangeListener.CHANGES;
-import static org.apache.sling.api.resource.observation.ResourceChangeListener.PATHS;
-import static org.apache.sling.commons.osgi.PropertiesUtil.toStringArray;
-
 import java.util.Collections;
 import java.util.EnumSet;
 import java.util.HashSet;
@@ -31,10 +27,14 @@ import java.util.Set;
 import org.apache.sling.api.resource.ResourceUtil;
 import org.apache.sling.api.resource.observation.ExternalResourceChangeListener;
 import org.apache.sling.api.resource.observation.ResourceChange.ChangeType;
-import org.apache.sling.api.resource.path.PathSet;
 import org.apache.sling.api.resource.observation.ResourceChangeListener;
+import org.apache.sling.api.resource.path.PathSet;
 import org.osgi.framework.ServiceReference;
 
+import static org.apache.sling.api.resource.observation.ResourceChangeListener.CHANGES;
+import static org.apache.sling.api.resource.observation.ResourceChangeListener.PATHS;
+import static org.apache.sling.commons.osgi.PropertiesUtil.toStringArray;
+
 /**
  * Information about a resource change listener.
  */
@@ -56,23 +56,36 @@ public class ResourceChangeListenerInfo
 
     private volatile ResourceChangeListener listener;
 
+    private static final String GLOB_PREFIX = "glob:";
+
     public ResourceChangeListenerInfo(final ServiceReference ref, final String[] searchPaths) {
         boolean configValid = true;
         final Set<String> pathsSet = new HashSet<String>();
         final String paths[] = toStringArray(ref.getProperty(PATHS), null);
         if ( paths != null ) {
             for(final String p : paths) {
+                boolean isGlobPattern = false;
                 String normalisedPath = ResourceUtil.normalize(p);
+                if (p.startsWith(GLOB_PREFIX)) {
+                    isGlobPattern = true;
+                    normalisedPath =  ResourceUtil.normalize(p.substring(GLOB_PREFIX.length()));
+                }
                 if (!".".equals(p) && normalisedPath.isEmpty()) {
                     configValid = false;
-                } else if ( normalisedPath.startsWith("/") ) {
+                } else if ( normalisedPath.startsWith("/") && !isGlobPattern ) {
                     pathsSet.add(normalisedPath);
+                } else if (normalisedPath.startsWith("/") && isGlobPattern) {
+                    pathsSet.add(GLOB_PREFIX + normalisedPath);
                 } else {
                     for(final String sp : searchPaths) {
                         if ( p.equals(".") ) {
                             pathsSet.add(sp);
                         } else {
-                            pathsSet.add(ResourceUtil.normalize(sp + normalisedPath));
+                            if (isGlobPattern) {
+                                pathsSet.add(GLOB_PREFIX + ResourceUtil.normalize(sp + normalisedPath));
+                            } else {
+                                pathsSet.add(ResourceUtil.normalize(sp + normalisedPath));
+                            }
                         }
                     }
                 }

Modified: sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/observation/ResourceChangeListenerInfoTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/observation/ResourceChangeListenerInfoTest.java?rev=1754283&r1=1754282&r2=1754283&view=diff
==============================================================================
--- sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/observation/ResourceChangeListenerInfoTest.java (original)
+++ sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/observation/ResourceChangeListenerInfoTest.java Wed Jul 27 15:52:40 2016
@@ -31,11 +31,13 @@ public class ResourceChangeListenerInfoT
     @Test
     public void testGetExpandedRelativePaths() {
         ServiceReference reference = mock(ServiceReference.class);
-        when(reference.getProperty(ResourceChangeListener.PATHS)).thenReturn(new String[] {"./**/*.html"});
+        when(reference.getProperty(ResourceChangeListener.PATHS)).thenReturn(new String[] {"project/components/page/page.html"});
         final ResourceChangeListenerInfo rcli = new ResourceChangeListenerInfo(reference, new String[] {"/apps/", "/libs/"});
         Set<String> paths = rcli.getPaths().toStringSet();
-        assertTrue("PathSet " + paths.toString() + " does not contain /apps/**/*.html.", paths.contains("/apps/**/*.html"));
-        assertTrue("PathSet " + paths.toString() + " does not contain /libs/**/*.html.", paths.contains("/libs/**/*.html"));
+        assertTrue("PathSet " + paths.toString() + " does not contain /apps/project/components/page/page.html.",
+                paths.contains("/apps/project/components/page/page.html"));
+        assertTrue("PathSet " + paths.toString() + " does not contain /libs/project/components/page/page.html.",
+                paths.contains("/libs/project/components/page/page.html"));
     }
 
     @Test
@@ -47,4 +49,14 @@ public class ResourceChangeListenerInfoT
         assertTrue("PathSet " + paths.toString() + " does not contain /apps/", paths.contains("/apps/"));
         assertTrue("PathSet " + paths.toString() + " does not contain /libs/.", paths.contains("/libs/"));
     }
+
+    @Test
+    public void testGlobPatternExpansion() {
+        ServiceReference reference = mock(ServiceReference.class);
+        when(reference.getProperty(ResourceChangeListener.PATHS)).thenReturn(new String[] {"glob:./**/*.html"});
+        final ResourceChangeListenerInfo rcli = new ResourceChangeListenerInfo(reference, new String[] {"/apps/", "/libs/"});
+        Set<String> paths = rcli.getPaths().toStringSet();
+        assertTrue("PathSet " + paths.toString() + " does not contain glob:/apps/**/*.html.", paths.contains("glob:/apps/**/*.html"));
+        assertTrue("PathSet " + paths.toString() + " does not contain glob:/libs/**/*.html.", paths.contains("glob:/libs/**/*.html"));
+    }
 }