sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1671754 - 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/
Date Tue, 07 Apr 2015 08:03:21 GMT
Author: cziegeler
Date: Tue Apr  7 08:03:20 2015
New Revision: 1671754

URL: http://svn.apache.org/r1671754
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/JcrInstaller.java
    sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/RootFolderListener.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/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=1671754&r1=1671753&r2=1671754&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 08:03:20 2015
@@ -77,7 +77,7 @@ import org.slf4j.LoggerFactory;
     @Property(name=Constants.SERVICE_RANKING, intValue=100)
 })
 @Service(value=UpdateHandler.class)
-public class JcrInstaller implements EventListener, UpdateHandler, ManagedService {
+public class JcrInstaller implements UpdateHandler, ManagedService {
 
 	public static final long RUN_LOOP_DELAY_MSEC = 500L;
 	public static final String URL_SCHEME = "jcrinstall";
@@ -183,7 +183,8 @@ public class JcrInstaller implements Eve
 
     /** Thread that can be cleanly stopped with a flag */
     private final static AtomicInteger bgThreadCounter = new AtomicInteger();
-    class StoppableThread extends Thread {
+
+    class StoppableThread extends Thread implements EventListener {
 
         /** Used for synchronizing. */
         final Object lock = new Object();
@@ -212,12 +213,12 @@ public class JcrInstaller implements Eve
                 session = repository.loginAdministrative(repository.getDefaultWorkspace());
 
                 for (String path : cfg.getRoots()) {
-                    listeners.add(new RootFolderListener(session, cfg.getFolderNameFilter(),
path, updateFoldersListTimer));
+                    listeners.add(new RootFolderListener(session, path, updateFoldersListTimer));
                     logger.debug("Configured root folder: {}", path);
                 }
 
                 // Watch for events on the root - that might be one of our root folders
-                session.getWorkspace().getObservationManager().addEventListener(JcrInstaller.this,
+                session.getWorkspace().getObservationManager().addEventListener(this,
                         Event.NODE_ADDED | Event.NODE_REMOVED,
                         "/",
                         false, // isDeep
@@ -234,8 +235,8 @@ public class JcrInstaller implements Eve
                         try {
                             while (events.hasNext()) {
                                 final Event e = events.nextEvent();
-                                JcrInstaller.this.checkChanges(cfg, e.getIdentifier());
-                                JcrInstaller.this.checkChanges(cfg, e.getPath());
+                                checkChanges(e.getIdentifier());
+                                checkChanges(e.getPath());
                             }
                         } catch (final RepositoryException re) {
                             logger.warn("RepositoryException in onEvent", re);
@@ -255,7 +256,7 @@ public class JcrInstaller implements Eve
 
 
                 // Find paths to watch and create WatchedFolders to manage them
-                for(String root : cfg.getRoots()) {
+                for(final String root : cfg.getRoots()) {
                     findPathsToWatch(cfg, session, root);
                 }
 
@@ -290,7 +291,7 @@ public class JcrInstaller implements Eve
                     for(RootFolderListener wfc : listeners) {
                         wfc.cleanup(session);
                     }
-                    session.getWorkspace().getObservationManager().removeEventListener(JcrInstaller.this);
+                    session.getWorkspace().getObservationManager().removeEventListener(this);
                     if ( moveEventListener != null ) {
                         session.getWorkspace().getObservationManager().removeEventListener(moveEventListener);
                         moveEventListener = null;
@@ -306,6 +307,36 @@ public class JcrInstaller implements Eve
             listeners.clear();
         }
 
+        /**
+         * @see javax.jcr.observation.EventListener#onEvent(javax.jcr.observation.EventIterator)
+         */
+        public void onEvent(final EventIterator it) {
+            // Got a DELETE or ADD on root - schedule folders rescan if one
+            // of our root folders is impacted
+            try {
+                while(it.hasNext()) {
+                    final Event e = it.nextEvent();
+                    logger.debug("Got event {}", e);
+
+                    this.checkChanges(e.getPath());
+                }
+            } catch(final RepositoryException re) {
+                logger.warn("RepositoryException in onEvent", re);
+            }
+        }
+
+        /**
+         * Check for changes in any of the root folders
+         */
+        private void checkChanges(final String path) {
+            for(final String root : cfg.getRoots()) {
+                if (path.startsWith(root)) {
+                    logger.info("Got event for root {}, scheduling scanning of new folders",
root);
+                    updateFoldersListTimer.scheduleScan();
+                }
+            }
+        }
+
         @Override
         public final void run() {
             logger.info("Background thread {} starting", Thread.currentThread().getName());
@@ -501,18 +532,6 @@ public class JcrInstaller implements Eve
         return removedResources;
     }
 
-    /**
-     * Check for changes in any of the root folders
-     */
-    private void checkChanges(final InstallerConfig cfg, final String path) {
-        for(String root : cfg.getRoots()) {
-            if (path.startsWith(root)) {
-                logger.info("Got event for root {}, scheduling scanning of new folders",
root);
-                updateFoldersListTimer.scheduleScan();
-            }
-        }
-    }
-
     InstallerConfig getConfiguration() {
         InstallerConfig cfg = null;
         final StoppableThread st = this.backgroundThread;
@@ -527,28 +546,6 @@ public class JcrInstaller implements Eve
     }
 
     /**
-     * @see javax.jcr.observation.EventListener#onEvent(javax.jcr.observation.EventIterator)
-     */
-    public void onEvent(final EventIterator it) {
-        // Got a DELETE or ADD on root - schedule folders rescan if one
-        // of our root folders is impacted
-        // get configuration
-        final InstallerConfig cfg = this.getConfiguration();
-        if ( cfg != null ) {
-            try {
-                while(it.hasNext()) {
-                    final Event e = it.nextEvent();
-                    logger.debug("Got event {}", e);
-
-                    this.checkChanges(cfg, e.getPath());
-                }
-            } catch(RepositoryException re) {
-                logger.warn("RepositoryException in onEvent", re);
-            }
-        }
-    }
-
-    /**
      * Run periodic scans of our watched folders, and watch for folders creations/deletions.
      */
     public void runOneCycle(final InstallerConfig cfg, final Session session) {

Modified: sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/RootFolderListener.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/RootFolderListener.java?rev=1671754&r1=1671753&r2=1671754&view=diff
==============================================================================
--- sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/RootFolderListener.java
(original)
+++ sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/RootFolderListener.java
Tue Apr  7 08:03:20 2015
@@ -27,16 +27,19 @@ import javax.jcr.observation.EventListen
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-/** Listen for JCR events under one of our roots, to find out
- *  when new WatchedFolders must be created, or when some might
- *  have been deleted.
+/**
+ * Listen for JCR events under one of our roots, to find out
+ * when new WatchedFolders must be created, or when some might
+ * have been deleted.
  */
 class RootFolderListener implements EventListener {
-    protected final Logger log = LoggerFactory.getLogger(this.getClass());
+
+    private final Logger logger = LoggerFactory.getLogger(this.getClass());
     private final RescanTimer timer;
     private final String watchedPath;
 
-    RootFolderListener(Session session, FolderNameFilter fnf, String path, RescanTimer timer)
throws RepositoryException {
+    RootFolderListener(final Session session, final String path, final RescanTimer timer)
+    throws RepositoryException {
         this.timer = timer;
         this.watchedPath = path;
 
@@ -46,7 +49,7 @@ class RootFolderListener implements Even
         session.getWorkspace().getObservationManager().addEventListener(this, eventTypes,
watchedPath,
                 isDeep, null, null, noLocal);
 
-        log.info("Watching {} to detect potential changes in subfolders", watchedPath);
+        logger.info("Watching {} to detect potential changes in subfolders", watchedPath);
     }
 
     @Override
@@ -54,15 +57,21 @@ class RootFolderListener implements Even
         return getClass().getSimpleName() + " (" + watchedPath + ")";
     }
 
-    void cleanup(Session session) throws RepositoryException {
+    void cleanup(final Session session) throws RepositoryException {
         session.getWorkspace().getObservationManager().removeEventListener(this);
     }
 
-    /** Schedule a scan */
-    public void onEvent(EventIterator it) {
-        while(it.hasNext()) {
-            final Event e = it.nextEvent();
-            log.debug("Got event {}", e);
+    /**
+     * Schedule a scan.
+     */
+    public void onEvent(final EventIterator it) {
+        // we don't care about the events
+        // they are only logged if debug log level
+        if ( logger.isDebugEnabled() ) {
+            while(it.hasNext()) {
+                final Event e = it.nextEvent();
+                logger.debug("Got event {}", e);
+            }
         }
         timer.scheduleScan();
     }

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=1671754&r1=1671753&r2=1671754&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 08:03:20 2015
@@ -25,7 +25,6 @@ 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;
@@ -99,15 +98,7 @@ class MiscUtil {
     /** Get the WatchedFolders of supplied JcrInstaller */
     @SuppressWarnings({ "unchecked"})
     static Collection<WatchedFolder> getWatchedFolders(JcrInstaller installer) throws
Exception {
-        // 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);
+        final InstallerConfig cfg = installer.getConfiguration();
 
         return cfg.getWatchedFolders();
     }



Mime
View raw message