Author: radu Date: Wed Jul 13 09:57:20 2016 New Revision: 1752382 URL: http://svn.apache.org/viewvc?rev=1752382&view=rev Log: SLING-5837 - Allow ResourceChangeListeners to define glob patterns for resource matching
* added support for glob pattern matching in o.a.s.api.resource.path.Path * expanded documentation for the ResourceChangeListener#PATHS configuration property Modified: sling/trunk/bundles/api/pom.xml sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/observation/ResourceChangeListener.java sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/observation/package-info.java sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/path/Path.java sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/path/package-info.java sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/path/PathTest.java Modified: sling/trunk/bundles/api/pom.xml URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/pom.xml?rev=1752382&r1=1752381&r2=1752382&view=diff ============================================================================== --- sling/trunk/bundles/api/pom.xml (original) +++ sling/trunk/bundles/api/pom.xml Wed Jul 13 09:57:20 2016 @@ -51,6 +51,7 @@ <properties> <site.jira.version.id>12314252</site.jira.version.id> + <sling.java.version>7</sling.java.version> </properties> <dependencies> Modified: sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/observation/ResourceChangeListener.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/observation/ResourceChangeListener.java?rev=1752382&r1=1752381&r2=1752382&view=diff ============================================================================== --- sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/observation/ResourceChangeListener.java (original) +++ sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/observation/ResourceChangeListener.java Wed Jul 13 09:57:20 2016 @@ -53,14 +53,24 @@ import aQute.bnd.annotation.ConsumerType public interface ResourceChangeListener { /** - * Array of paths - required. - * A path is either absolute or relative. If it's a relative path, the relative path - * will be appended to all search paths of the resource resolver. If the whole tree - * of all search paths should be observed, the special value {@code .} should be used. - * If one of the paths is a sub resource of another specified path, - * the sub path is ignored. - * If this property is missing or invalid, the listener is ignored. The type of the - * property must either be String, or a String array. + * <p>Array of paths or glob patterns - required.</p> + * <p>A path is either absolute or relative. If it's a relative path, the relative path + * will be appended to all search paths of the resource resolver.</p> + * <p>If the whole tree + * of all search paths should be observed, the special value {@code .} should be used.</p> + * <p> + * The following rules are used to interpret glob patterns: + * <ul> + * <li>The {@code *} character matches zero or more characters of a name component without crossing directory boundaries.</li> + * <li>The {@code **} characters matches zero or more characters crossing directory boundaries.</li> + * <li>The {@code ?} character matches exactly one character of a name component.</li> + * </ul> + * + * </p> + * <p>If one of the paths is a sub resource of another specified path, + * the sub path is ignored.</p> + * <p>If this property is missing or invalid, the listener is ignored. The type of the + * property must either be String, or a String array.</p> */ String PATHS = "resource.paths"; Modified: sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/observation/package-info.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/observation/package-info.java?rev=1752382&r1=1752381&r2=1752382&view=diff ============================================================================== --- sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/observation/package-info.java (original) +++ sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/observation/package-info.java Wed Jul 13 09:57:20 2016 @@ -17,7 +17,7 @@ * under the License. */ -@Version("1.0.0") +@Version("1.1.0") package org.apache.sling.api.resource.observation; import aQute.bnd.annotation.Version; Modified: sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/path/Path.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/path/Path.java?rev=1752382&r1=1752381&r2=1752382&view=diff ============================================================================== --- sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/path/Path.java (original) +++ sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/path/Path.java Wed Jul 13 09:57:20 2016 @@ -18,6 +18,10 @@ */ package org.apache.sling.api.resource.path; +import java.nio.file.FileSystems; +import java.nio.file.PathMatcher; +import java.nio.file.Paths; + /** * Simple helper class for path matching. * @@ -29,26 +33,42 @@ public class Path implements Comparable< private final String prefix; + private final boolean isPattern; + /** - * Create a new path object. - * @param path The resource path. + * <p> + * Create a new path object either from a concrete path or from a glob pattern. The following rules are used to interpret glob patterns: + * <ul> + * <li>The {@code *} character matches zero or more characters of a name component without crossing directory boundaries.</li> + * <li>The {@code **} characters matches zero or more characters crossing directory boundaries.</li> + * <li>The {@code ?} character matches exactly one character of a name component.</li> + * </ul> + * </p> + * + * @param path the resource path or a glob pattern. */ public Path(final String path) { this.path = path; this.prefix = path.equals("/") ? "/" : path.concat("/"); + if (path.contains("?") || path.contains("*")) { + isPattern = true; + } else { + isPattern = false; + } + } /** - * Check whether the provided path is equal to this path or a sub path - * of it. + * Check whether the provided path is equal to this path or a sub path of it. * @param otherPath Path to check * @return {@code true} If other path is within the sub tree of this path. */ public boolean matches(final String otherPath) { - if ( this.path.equals(otherPath) || otherPath.startsWith(this.prefix) ) { - return true; + if (isPattern) { + PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:" + path); + return matcher.matches(Paths.get(otherPath)); } - return false; + return this.path.equals(otherPath) || otherPath.startsWith(this.prefix); } /** @@ -80,4 +100,4 @@ public class Path implements Comparable< return this.getPath().equals(((Path)obj).getPath()); } -} \ No newline at end of file +} Modified: sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/path/package-info.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/path/package-info.java?rev=1752382&r1=1752381&r2=1752382&view=diff ============================================================================== --- sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/path/package-info.java (original) +++ sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/path/package-info.java Wed Jul 13 09:57:20 2016 @@ -17,7 +17,7 @@ * under the License. */ -@Version("1.0.0") +@Version("1.1.0") package org.apache.sling.api.resource.path; import aQute.bnd.annotation.Version; Modified: sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/path/PathTest.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/path/PathTest.java?rev=1752382&r1=1752381&r2=1752382&view=diff ============================================================================== --- sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/path/PathTest.java (original) +++ sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/path/PathTest.java Wed Jul 13 09:57:20 2016 @@ -47,4 +47,13 @@ public class PathTest { assertFalse(path.matches("/content1")); } + + @Test public void testPatternMatching() { + final Path path = new Path("/apps/**/*.html"); + assertTrue(path.matches("/apps/project/a.html")); + assertTrue(path.matches("/apps/project/1/a.html")); + assertTrue(path.matches("/apps/project/1/2/a.html")); + assertFalse(path.matches("/apps/a.html")); + assertFalse(path.matches("/apps/project/a.html/b")); + } }