You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2015/04/07 09:45:51 UTC

svn commit: r1671752 - in /sling/trunk/installer/providers/jcr/src: main/java/org/apache/sling/installer/provider/jcr/impl/ test/java/org/apache/sling/installer/provider/jcr/impl/

Author: cziegeler
Date: Tue Apr  7 07:45:51 2015
New Revision: 1671752

URL: http://svn.apache.org/r1671752
Log:
SLING-4564 : Use a single listener registered for multiple path in JCR installer. Refactor watched folder to be part of the configuration

Modified:
    sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/InstallerConfig.java
    sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/JcrInstaller.java
    sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/MiscUtil.java

Modified: sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/InstallerConfig.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/InstallerConfig.java?rev=1671752&r1=1671751&r2=1671752&view=diff
==============================================================================
--- sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/InstallerConfig.java (original)
+++ sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/InstallerConfig.java Tue Apr  7 07:45:51 2015
@@ -21,6 +21,8 @@ package org.apache.sling.installer.provi
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Dictionary;
+import java.util.LinkedList;
+import java.util.List;
 
 import org.apache.sling.commons.osgi.PropertiesUtil;
 import org.apache.sling.installer.provider.jcr.impl.JcrInstaller.NodeConverter;
@@ -43,7 +45,7 @@ public class InstallerConfig {
     private final FolderNameFilter folderNameFilter;
 
     /** The root folders that we watch */
-    private final String [] roots;
+    private final String[] roots;
 
     /** The path for new configurations. */
     private final String newConfigPath;
@@ -51,6 +53,9 @@ public class InstallerConfig {
     /** The path for pauseInstallation property */
     private final String pauseScanNodePath;
 
+    /** List of watched folders */
+    private final List<WatchedFolder> watchedFolders = new LinkedList<WatchedFolder>();
+
     public InstallerConfig(
             final Logger logger,
             final ComponentContext ctx,
@@ -75,7 +80,7 @@ public class InstallerConfig {
 
         // Configurable folder regexp, system property overrides default value
         String folderNameRegexp = (String)getPropertyValue(logger, ctx, cfg, JcrInstaller.FOLDER_NAME_REGEXP_PROPERTY);
-        if(folderNameRegexp != null) {
+        if (folderNameRegexp != null) {
             folderNameRegexp = folderNameRegexp.trim();
             logger.debug("Using configured ({}) folder name regexp '{}'", JcrInstaller.FOLDER_NAME_REGEXP_PROPERTY, folderNameRegexp);
         } else {
@@ -150,4 +155,8 @@ public class InstallerConfig {
     public String getNewConfigPath() {
         return this.newConfigPath;
     }
+
+    public List<WatchedFolder> getWatchedFolders() {
+        return this.watchedFolders;
+    }
 }

Modified: sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/JcrInstaller.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/JcrInstaller.java?rev=1671752&r1=1671751&r2=1671752&view=diff
==============================================================================
--- sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/JcrInstaller.java (original)
+++ sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/JcrInstaller.java Tue Apr  7 07:45:51 2015
@@ -26,6 +26,7 @@ import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Dictionary;
 import java.util.Hashtable;
+import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
@@ -162,9 +163,6 @@ public class JcrInstaller implements Eve
     @Reference
     private OsgiInstaller installer;
 
-    /** List of watched folders */
-    private volatile List<WatchedFolder> watchedFolders;
-
     /** The component context. */
     private volatile ComponentContext componentContext;
 
@@ -257,14 +255,13 @@ public class JcrInstaller implements Eve
 
 
                 // Find paths to watch and create WatchedFolders to manage them
-                watchedFolders = new LinkedList<WatchedFolder>();
                 for(String root : cfg.getRoots()) {
-                    findPathsToWatch(cfg, session, root, watchedFolders);
+                    findPathsToWatch(cfg, session, root);
                 }
 
                 // Scan watchedFolders and register resources with installer
                 final List<InstallableResource> resources = new LinkedList<InstallableResource>();
-                for(final WatchedFolder f : watchedFolders) {
+                for(final WatchedFolder f : cfg.getWatchedFolders()) {
                     f.start();
                     final WatchedFolder.ScanResult r = f.scan();
                     logger.debug("Startup: {} provides resources {}", f, r.toAdd);
@@ -284,7 +281,7 @@ public class JcrInstaller implements Eve
             while ( running.get() ) {
                 try {
                     Thread.sleep(10);
-                } catch (InterruptedException e) {
+                } catch (final InterruptedException e) {
                     Thread.currentThread().interrupt();
                 }
             }
@@ -307,8 +304,6 @@ public class JcrInstaller implements Eve
                 session = null;
             }
             listeners.clear();
-
-            watchedFolders = null;
         }
 
         @Override
@@ -416,7 +411,7 @@ public class JcrInstaller implements Eve
     /** Find the paths to watch under rootPath, according to our folderNameFilter,
      * 	and add them to result */
     private void findPathsToWatch(final InstallerConfig cfg, final Session session,
-            final String rootPath, final List<WatchedFolder> result) throws RepositoryException {
+            final String rootPath) throws RepositoryException {
         Session s = null;
 
         try {
@@ -426,7 +421,7 @@ public class JcrInstaller implements Eve
             } else {
                 logger.debug("Bundles root node {} found, looking for bundle folders inside it", rootPath);
                 final Node n = (Node)s.getItem(rootPath);
-                findPathsUnderNode(cfg, session, n, result);
+                findPathsUnderNode(cfg, session, n);
             }
         } finally {
             if (s != null) {
@@ -440,11 +435,11 @@ public class JcrInstaller implements Eve
      * to do the same.
      */
     void findPathsUnderNode(final InstallerConfig cfg, final Session session,
-            final Node n, final List<WatchedFolder> result) throws RepositoryException {
+            final Node n) throws RepositoryException {
         final String path = n.getPath();
         final int priority = cfg.getFolderNameFilter().getPriority(path);
         if (priority > 0) {
-            result.add(new WatchedFolder(session, path, priority, cfg.getConverters()));
+            addWatchedFolder(cfg, new WatchedFolder(session, path, priority, cfg.getConverters()));
         }
         final int depth = path.split("/").length;
         if(depth > cfg.getMaxWatchedFolderDepth()) {
@@ -453,17 +448,17 @@ public class JcrInstaller implements Eve
         }
         final NodeIterator it = n.getNodes();
         while (it.hasNext()) {
-            findPathsUnderNode(cfg, session, it.nextNode(), result);
+            findPathsUnderNode(cfg, session, it.nextNode());
         }
     }
 
     /**
      * Add WatchedFolder to our list if it doesn't exist yet.
      */
-    private void addWatchedFolder(final WatchedFolder toAdd)
+    private void addWatchedFolder(final InstallerConfig cfg, final WatchedFolder toAdd)
     throws RepositoryException {
         WatchedFolder existing = null;
-        for(WatchedFolder wf : watchedFolders) {
+        for(WatchedFolder wf : cfg.getWatchedFolders()) {
             if (wf.getPath().equals(toAdd.getPath())) {
                 existing = wf;
                 break;
@@ -471,44 +466,39 @@ public class JcrInstaller implements Eve
         }
         if (existing == null) {
             toAdd.start();
-            watchedFolders.add(toAdd);
+            cfg.getWatchedFolders().add(toAdd);
         }
     }
 
-    /** Add new folders to watch if any have been detected
-     *  @return a list of InstallableResource that must be unregistered,
+    /**
+     * Add new folders to watch if any have been detected
+     * @return a list of InstallableResource that must be unregistered,
      *  	for folders that have been removed
      */
     private List<String> updateFoldersList(final InstallerConfig cfg, final Session session) throws Exception {
         logger.debug("Updating folder list.");
 
-        final List<String> result = new LinkedList<String>();
-
-        final List<WatchedFolder> newFolders = new ArrayList<WatchedFolder>();
 	    for(String root : cfg.getRoots()) {
-	        findPathsToWatch(cfg, session, root, newFolders);
-	    }
-	    for(WatchedFolder wf : newFolders) {
-	        addWatchedFolder(wf);
+	        findPathsToWatch(cfg, session, root);
 	    }
 
         // Check all WatchedFolder, in case some were deleted
-        final List<WatchedFolder> toRemove = new ArrayList<WatchedFolder>();
-        for(WatchedFolder wf : watchedFolders) {
-            logger.debug("Item {} exists? {}", wf.getPath(), session.itemExists(wf.getPath()));
+        final List<String> removedResources = new LinkedList<String>();
+
+        final Iterator<WatchedFolder> i = cfg.getWatchedFolders().iterator();
+        while ( i.hasNext() ) {
+            final WatchedFolder wf = i.next();
 
-            if(!session.itemExists(wf.getPath())) {
-                result.addAll(wf.scan().toRemove);
+            logger.debug("Item {} exists? {}", wf.getPath(), session.itemExists(wf.getPath()));
+            if (!session.itemExists(wf.getPath())) {
+                logger.info("Deleting {}, path does not exist anymore", wf);
+                removedResources.addAll(wf.scan().toRemove);
                 wf.stop();
-                toRemove.add(wf);
+                i.remove();
             }
         }
-        for(final WatchedFolder wf : toRemove) {
-            logger.info("Deleting {}, path does not exist anymore", wf);
-            watchedFolders.remove(wf);
-        }
 
-        return result;
+        return removedResources;
     }
 
     /**
@@ -567,7 +557,7 @@ public class JcrInstaller implements Eve
         try {
             boolean didRefresh = false;
 
-            if (anyWatchFolderNeedsScan()) {
+            if (anyWatchFolderNeedsScan(cfg)) {
                 session.refresh(false);
                 didRefresh = true;
                 if (scanningIsPaused(cfg, session)) {
@@ -585,7 +575,7 @@ public class JcrInstaller implements Eve
 
             // Rescan WatchedFolders if needed
             boolean scanWf = false;
-            for(WatchedFolder wf : watchedFolders) {
+            for(WatchedFolder wf : cfg.getWatchedFolders()) {
                 if (!wf.needsScan()) {
                     continue;
                 }
@@ -662,8 +652,8 @@ public class JcrInstaller implements Eve
         return false;
     }
 
-    private boolean anyWatchFolderNeedsScan() {
-        for (WatchedFolder wf : watchedFolders) {
+    private boolean anyWatchFolderNeedsScan(final InstallerConfig cfg) {
+        for (WatchedFolder wf : cfg.getWatchedFolders()) {
             if (wf.needsScan()) {
                 return true;
             }

Modified: sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/MiscUtil.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/MiscUtil.java?rev=1671752&r1=1671751&r2=1671752&view=diff
==============================================================================
--- sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/MiscUtil.java (original)
+++ sling/trunk/installer/providers/jcr/src/test/java/org/apache/sling/installer/provider/jcr/impl/MiscUtil.java Tue Apr  7 07:45:51 2015
@@ -25,6 +25,7 @@ import java.util.Hashtable;
 
 import org.apache.sling.commons.testing.jcr.EventHelper;
 import org.apache.sling.installer.api.OsgiInstaller;
+import org.apache.sling.installer.provider.jcr.impl.JcrInstaller.StoppableThread;
 import org.apache.sling.jcr.api.SlingRepository;
 import org.jmock.Expectations;
 import org.jmock.Mockery;
@@ -98,9 +99,17 @@ class MiscUtil {
     /** Get the WatchedFolders of supplied JcrInstaller */
     @SuppressWarnings({ "unchecked"})
     static Collection<WatchedFolder> getWatchedFolders(JcrInstaller installer) throws Exception {
-        final Field f = installer.getClass().getDeclaredField("watchedFolders");
-        f.setAccessible(true);
-        return (Collection<WatchedFolder>)f.get(installer);
+        // get background thread
+        final Field threadField = installer.getClass().getDeclaredField("backgroundThread");
+        threadField.setAccessible(true);
+        final JcrInstaller.StoppableThread thread = (StoppableThread) threadField.get(installer);
+
+        // get configuration from thread
+        final Field configField = thread.getClass().getDeclaredField("cfg");
+        configField.setAccessible(true);
+        final InstallerConfig cfg = (InstallerConfig) configField.get(thread);
+
+        return cfg.getWatchedFolders();
     }
 
     /** Wait long enough for all changes in content to be processed by JcrInstaller */