Author: bdelacretaz
Date: Mon Sep  7 14:48:47 2009
New Revision: 812166

URL: http://svn.apache.org/viewvc?rev=812166&view=rev
Log:
SLING-1078 - more robust definition of root folders priorities

Modified:
    
sling/trunk/installer/jcr/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/impl/FolderNameFilter.java
    
sling/trunk/installer/jcr/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/impl/JcrInstaller.java
    
sling/trunk/installer/jcr/jcrinstall/src/main/resources/OSGI-INF/metatype/metatype.properties
    
sling/trunk/installer/jcr/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/impl/FolderNameFilterTest.java

Modified: 
sling/trunk/installer/jcr/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/impl/FolderNameFilter.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/installer/jcr/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/impl/FolderNameFilter.java?rev=812166&r1=812165&r2=812166&view=diff
==============================================================================
--- 
sling/trunk/installer/jcr/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/impl/FolderNameFilter.java
 (original)
+++ 
sling/trunk/installer/jcr/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/impl/FolderNameFilter.java
 Mon Sep  7 14:48:47 2009
@@ -39,27 +39,57 @@
     private final Pattern pattern;
     private final String regexp;
     private final RunMode runMode;
+    private final String [] rootPaths;
     private Map<String, Integer> rootPriorities = new HashMap<String, 
Integer>();
     private final Logger log = LoggerFactory.getLogger(getClass());
     
-    /** getPriority computes priorities as follows: each root gets (N+1) * 
ROOT_PRIORITY_FACTOR,
-     *         where N is the index in the roots array, and paths that match 
one or several
-     *         run level get an additional RUNMODE_PRIORITY_OFFSET per matched 
run level
+    /** getPriority computes priorities as follows: each root gets its own 
base priority,
+     *         and paths that match one or several run levels get an 
additional RUNMODE_PRIORITY_OFFSET 
+     *  per matched run level
      */
-    public static final int ROOT_PRIORITY_FACTOR = 100;
     public static final int RUNMODE_PRIORITY_BOOST = 1;
+    public static final int DEFAULT_ROOT_PRIORITY = 99;
     
-    FolderNameFilter(String [] roots, String regexp, RunMode runMode) {
+    FolderNameFilter(String [] rootsConfig, String regexp, RunMode runMode) {
         this.regexp = regexp;
         this.pattern = Pattern.compile(regexp);
         this.runMode = runMode;
         
-        // Assign priorities to our root folders
-        for(int i = 0; i < roots.length; i++) {
-               rootPriorities.put(roots[i], new Integer((i + 1) * 
ROOT_PRIORITY_FACTOR));
+        // Each entry in rootsConfig is like /libs:100, where 100
+        // is the priority.
+        // Break that up into paths and priorities
+        rootPaths = new String[rootsConfig.length];
+        for(int i = 0; i < rootsConfig.length; i++) {
+               final String [] parts = rootsConfig[i].split(":");
+               rootPaths[i] = cleanupRootPath(parts[0]);
+               Integer priority = new Integer(DEFAULT_ROOT_PRIORITY);
+               if(parts.length > 1) {
+                       try {
+                               priority = Integer.parseInt(parts[1].trim());
+                       } catch(NumberFormatException nfe) {
+                               log.warn("Invalid priority in path definition 
'{}'", rootsConfig[i]);
+                       }
+               }
+               rootPriorities.put(rootPaths[i], priority);
+               log.debug("Root path {} has priority {}", rootPaths[i], 
priority);
         }
     }
     
+    String [] getRootPaths() {
+       return rootPaths;
+    }
+    
+    static String cleanupRootPath(final String str) {
+       String result = str.trim();
+       if(!result.startsWith("/")) {
+               result = "/" + result;
+       }
+       if(result.endsWith("/")) {
+               result = result.substring(0, result.length() - 1);
+       }
+       return result;
+    }
+    
     /** If a folder at given path can contain installable resources
      *         (according to our regexp and current RunMode), return the
      *         priority to use for InstallableResource found in that folder.
@@ -110,6 +140,10 @@
         return result;
     }
     
+    public String toString() {
+        return getClass().getSimpleName() + " (" + regexp + "), RunMode=" + 
runMode;
+    }
+    
     int getRootPriority(String path) {
        for(Map.Entry<String, Integer> e : rootPriorities.entrySet()) {
                if(path.startsWith(e.getKey())) {
@@ -118,8 +152,4 @@
        }
        return 0;
     }
-
-    public String toString() {
-        return getClass().getSimpleName() + " (" + regexp + "), RunMode=" + 
runMode;
-    }
 }
\ No newline at end of file

Modified: 
sling/trunk/installer/jcr/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/impl/JcrInstaller.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/installer/jcr/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/impl/JcrInstaller.java?rev=812166&r1=812165&r2=812166&view=diff
==============================================================================
--- 
sling/trunk/installer/jcr/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/impl/JcrInstaller.java
 (original)
+++ 
sling/trunk/installer/jcr/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/impl/JcrInstaller.java
 Mon Sep  7 14:48:47 2009
@@ -102,14 +102,14 @@
      */
     public static final String PROP_INSTALL_FOLDER_MAX_DEPTH = 
"sling.jcrinstall.folder.max.depth";
     
-    /**        Configurable search path. We could get it from the 
ResourceResolver, but
-     *         introducing a dependency on this just to get those values is 
too much
-     *         for this module that's meant to bootstrap other services.
+    /**        Configurable search path, with per-path priorities. 
+     *  We could get it from the ResourceResolver, but introducing a 
dependency on this just to get those 
+     *  values is too much for this module that's meant to bootstrap other 
services.
      * 
-     *         @scr.property values.1="/apps" values.2="/libs"
+     *         @scr.property values.1="/libs:100" values.2="/apps:200"
      */
     public static final String PROP_SEARCH_PATH = 
"sling.jcrinstall.search.path";
-    public static final String [] DEFAULT_SEARCH_PATH = { "/apps/", "/libs/" };
+    public static final String [] DEFAULT_SEARCH_PATH = { "/libs:100", 
"/apps:200" };
 
     public static final int DEFAULT_FOLDER_MAX_DEPTH = 4;
     private int maxWatchedFolderDepth;
@@ -157,32 +157,6 @@
        converters.add(new FileNodeConverter());
        converters.add(new ConfigNodeConverter());
        
-       // Get search paths, and make sure each part starts and ends with a /
-        roots = 
OsgiUtil.toStringArray(context.getProperties().get(PROP_SEARCH_PATH));
-        if (roots == null) {
-               roots = DEFAULT_SEARCH_PATH;
-        }
-        for (int i = 0; i < roots.length; i++) {
-            if (!roots[i].startsWith("/")) {
-               roots[i] = "/" + roots[i];
-            }
-            if (!roots[i].endsWith("/")) {
-               roots[i] += "/";
-            }
-        }
-        for(int i = 0; i < roots.length; i++) {
-               log.info("Configured root folder: {}", roots[i]);
-       }
-        
-        // Watch for DELETE events on the root - that might be one of our root 
folders
-        int eventTypes = Event.NODE_ADDED | Event.NODE_REMOVED;
-        boolean isDeep = false;
-        boolean noLocal = true;
-        session.getWorkspace().getObservationManager().addEventListener(this, 
eventTypes, "/",
-                isDeep, null, null, noLocal);
-        log.info("Watching for NODE_REMOVED events on / to detect removal of 
our root folders");
-
-       
        // Configurable max depth, system property (via bundle context) 
overrides default value
        Object obj = getPropertyValue(context, PROP_INSTALL_FOLDER_MAX_DEPTH);
        if(obj != null) {
@@ -204,11 +178,40 @@
        }
        
        // Setup folder filtering and watching
-        folderNameFilter = new FolderNameFilter(roots, folderNameRegexp, 
runMode);
+       String [] rootsConfig = 
OsgiUtil.toStringArray(context.getProperties().get(PROP_SEARCH_PATH));
+       if(rootsConfig == null) {
+               rootsConfig = DEFAULT_SEARCH_PATH;
+       }
+        folderNameFilter = new FolderNameFilter(rootsConfig, folderNameRegexp, 
runMode);
+        roots = folderNameFilter.getRootPaths();
         for (String path : roots) {
             listeners.add(new RootFolderListener(session, folderNameFilter, 
path, updateFoldersListTimer));
         }
         
+       // Get search paths, and make sure each part starts and ends with a /
+        if (roots == null) {
+        }
+        for (int i = 0; i < roots.length; i++) {
+            if (!roots[i].startsWith("/")) {
+               roots[i] = "/" + roots[i];
+            }
+            if (!roots[i].endsWith("/")) {
+               roots[i] += "/";
+            }
+        }
+        for(int i = 0; i < roots.length; i++) {
+               log.info("Configured root folder: {}", roots[i]);
+       }
+        
+        // Watch for DELETE events on the root - that might be one of our root 
folders
+        int eventTypes = Event.NODE_ADDED | Event.NODE_REMOVED;
+        boolean isDeep = false;
+        boolean noLocal = true;
+        session.getWorkspace().getObservationManager().addEventListener(this, 
eventTypes, "/",
+                isDeep, null, null, noLocal);
+        log.info("Watching for NODE_REMOVED events on / to detect removal of 
our root folders");
+
+       
        // Find paths to watch and create WatchedFolders to manage them
        watchedFolders = new LinkedList<WatchedFolder>();
        for(String root : roots) {
@@ -471,4 +474,5 @@
     long [] getCounters() {
         return counters;
     }
+    
 }
\ No newline at end of file

Modified: 
sling/trunk/installer/jcr/jcrinstall/src/main/resources/OSGI-INF/metatype/metatype.properties
URL: 
http://svn.apache.org/viewvc/sling/trunk/installer/jcr/jcrinstall/src/main/resources/OSGI-INF/metatype/metatype.properties?rev=812166&r1=812165&r2=812166&view=diff
==============================================================================
--- 
sling/trunk/installer/jcr/jcrinstall/src/main/resources/OSGI-INF/metatype/metatype.properties
 (original)
+++ 
sling/trunk/installer/jcr/jcrinstall/src/main/resources/OSGI-INF/metatype/metatype.properties
 Mon Sep  7 14:48:47 2009
@@ -41,4 +41,6 @@
 sling.jcrinstall.search.path.name = Search Path
 sling.jcrinstall.search.path.description = List of paths under which 
jcrinstall looks \
   for installable resources. Combined with the installations folders name 
regexp \
-  to select folders for scanning. 
\ No newline at end of file
+  to select folders for scanning. Each path is followed by a colon and the 
priority \
+  of resources found under that path, resources with higher values override 
resources \
+  with lower values which represent the same OSGi entity (configuration, 
bundle, etc). 
\ No newline at end of file

Modified: 
sling/trunk/installer/jcr/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/impl/FolderNameFilterTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/installer/jcr/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/impl/FolderNameFilterTest.java?rev=812166&r1=812165&r2=812166&view=diff
==============================================================================
--- 
sling/trunk/installer/jcr/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/impl/FolderNameFilterTest.java
 (original)
+++ 
sling/trunk/installer/jcr/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/impl/FolderNameFilterTest.java
 Mon Sep  7 14:48:47 2009
@@ -25,9 +25,38 @@
 
 public class FolderNameFilterTest {
     public static final String DEFAULT_REGEXP =  ".*/install$";
-    public static final String [] ROOTS = { "/libs", "/apps" };
+    public static final String [] ROOTS = JcrInstaller.DEFAULT_SEARCH_PATH;
 
     @Test
+    public void testParseRootPaths() {
+       {
+               final String [] paths = { "a", "b/" };
+            final FolderNameFilter f = new FolderNameFilter(paths, 
DEFAULT_REGEXP, new MockRunMode(new String[0]));
+            assertEquals("/a", f.getRootPaths()[0]);
+            assertEquals("/b", f.getRootPaths()[1]);
+            assertEquals(FolderNameFilter.DEFAULT_ROOT_PRIORITY, 
f.getRootPriority("/a/foo"));
+            assertEquals(FolderNameFilter.DEFAULT_ROOT_PRIORITY, 
f.getRootPriority("/b/foo"));
+            assertEquals(0, f.getRootPriority("/notInThoseRoots"));
+       }
+       {
+               final String [] paths = { "a:100", "/b/: 200 " };
+            final FolderNameFilter f = new FolderNameFilter(paths, 
DEFAULT_REGEXP, new MockRunMode(new String[0]));
+            assertEquals("/a", f.getRootPaths()[0]);
+            assertEquals("/b", f.getRootPaths()[1]);
+            assertEquals(100, f.getRootPriority("/a/foo"));
+            assertEquals(200, f.getRootPriority("/b/foo"));
+       }
+       {
+               final String [] paths = { "a/:NOT_AN_INTEGER", "/b/: 200 " };
+            final FolderNameFilter f = new FolderNameFilter(paths, 
DEFAULT_REGEXP, new MockRunMode(new String[0]));
+            assertEquals("/a", f.getRootPaths()[0]);
+            assertEquals("/b", f.getRootPaths()[1]);
+            assertEquals(FolderNameFilter.DEFAULT_ROOT_PRIORITY, 
f.getRootPriority("/a/foo"));
+            assertEquals(200, f.getRootPriority("/b/foo"));
+       }
+    }
+    
+    @Test
     public void testNoRunMode() {
         final FolderNameFilter f = new FolderNameFilter(ROOTS, DEFAULT_REGEXP, 
new MockRunMode(new String[0]));
         assertTrue("Test 1", f.getPriority("/libs/install") > 0);


Reply via email to