incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1126982 - in /sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl: JcrInstaller.java WatchedFolder.java
Date Tue, 24 May 2011 11:24:24 GMT
Author: cziegeler
Date: Tue May 24 11:24:24 2011
New Revision: 1126982

URL: http://svn.apache.org/viewvc?rev=1126982&view=rev
Log:
SLING-2086 : Potential endless loop in jcr install

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/WatchedFolder.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=1126982&r1=1126981&r2=1126982&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 May 24 11:24:24 2011
@@ -228,6 +228,7 @@ public class JcrInstaller implements Eve
                 // Scan watchedFolders and register resources with installer
                 final List<InstallableResource> resources = new LinkedList<InstallableResource>();
                 for(WatchedFolder f : watchedFolders) {
+                    f.start();
                     final WatchedFolder.ScanResult r = f.scan();
                     logger.debug("Startup: {} provides resources {}", f, r.toAdd);
                     resources.addAll(r.toAdd);
@@ -360,7 +361,7 @@ public class JcrInstaller implements Eve
 
     /** Find the paths to watch under rootPath, according to our folderNameFilter,
      * 	and add them to result */
-    void findPathsToWatch(final String rootPath, final List<WatchedFolder> result)
throws RepositoryException {
+    private void findPathsToWatch(final String rootPath, final List<WatchedFolder>
result) throws RepositoryException {
         Session s = null;
 
         try {
@@ -401,7 +402,8 @@ public class JcrInstaller implements Eve
     }
 
     /** Add WatchedFolder to our list if it doesn't exist yet */
-    private void addWatchedFolder(final WatchedFolder toAdd) {
+    private void addWatchedFolder(final WatchedFolder toAdd)
+    throws RepositoryException {
         WatchedFolder existing = null;
         for(WatchedFolder wf : watchedFolders) {
             if (wf.getPath().equals(toAdd.getPath())) {
@@ -410,10 +412,8 @@ public class JcrInstaller implements Eve
             }
         }
         if (existing == null) {
+            toAdd.start();
             watchedFolders.add(toAdd);
-            toAdd.scheduleScan();
-        } else {
-            toAdd.cleanup();
         }
     }
 
@@ -441,7 +441,7 @@ public class JcrInstaller implements Eve
 
             if(!session.itemExists(wf.getPath())) {
                 result.addAll(wf.scan().toRemove);
-                wf.cleanup();
+                wf.stop();
                 toRemove.add(wf);
             }
         }
@@ -478,33 +478,33 @@ public class JcrInstaller implements Eve
         logger.debug("Running watch cycle.");
 
         try {
-            boolean didRefresh = true;
+            boolean didRefresh = false;
 
             // Rescan WatchedFolders if needed
-            final boolean scanWf = WatchedFolder.getRescanTimer().expired();
-            if (scanWf) {
-                session.refresh(false);
-                didRefresh = true;
-                for(WatchedFolder wf : watchedFolders) {
-                    if(!wf.needsScan()) {
-                        continue;
-                    }
-                    WatchedFolder.getRescanTimer().reset();
-                    counters[SCAN_FOLDERS_COUNTER]++;
-                    final WatchedFolder.ScanResult sr = wf.scan();
-                    boolean toDo = false;
-                    if ( sr.toAdd.size() > 0 ) {
-                        logger.info("Registering resource with OSGi installer: {}",sr.toAdd);
-                        toDo = true;
-                    }
-                    if ( sr.toRemove.size() > 0 ) {
-                        logger.info("Removing resource from OSGi installer: {}", sr.toRemove);
-                        toDo = true;
-                    }
-                    if ( toDo ) {
-                        installer.updateResources(URL_SCHEME, sr.toAdd.toArray(new InstallableResource[sr.toAdd.size()]),
-                            sr.toRemove.toArray(new String[sr.toRemove.size()]));
-                    }
+            boolean scanWf = false;
+            for(WatchedFolder wf : watchedFolders) {
+                if (!wf.needsScan()) {
+                    continue;
+                }
+                scanWf = true;
+                if ( !didRefresh ) {
+                    session.refresh(false);
+                    didRefresh = true;
+                }
+                counters[SCAN_FOLDERS_COUNTER]++;
+                final WatchedFolder.ScanResult sr = wf.scan();
+                boolean toDo = false;
+                if ( sr.toAdd.size() > 0 ) {
+                    logger.info("Registering resource with OSGi installer: {}",sr.toAdd);
+                    toDo = true;
+                }
+                if ( sr.toRemove.size() > 0 ) {
+                    logger.info("Removing resource from OSGi installer: {}", sr.toRemove);
+                    toDo = true;
+                }
+                if ( toDo ) {
+                    installer.updateResources(URL_SCHEME, sr.toAdd.toArray(new InstallableResource[sr.toAdd.size()]),
+                        sr.toRemove.toArray(new String[sr.toRemove.size()]));
                 }
             }
 
@@ -525,18 +525,14 @@ public class JcrInstaller implements Eve
                 }
             }
 
-            try {
-                Thread.sleep(RUN_LOOP_DELAY_MSEC);
-            } catch(InterruptedException ignore) {
-                // ignore
-            }
 
-        } catch(Exception e) {
-            logger.warn("Exception in run()", e);
-            try {
-                Thread.sleep(RUN_LOOP_DELAY_MSEC);
-            } catch(InterruptedException ignore) {
-            }
+        } catch (final Exception e) {
+            logger.warn("Exception in runOneCycle()", e);
+        }
+        try {
+            Thread.sleep(RUN_LOOP_DELAY_MSEC);
+        } catch (final InterruptedException ignore) {
+            // ignore
         }
         counters[RUN_LOOP_COUNTER]++;
     }

Modified: sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/WatchedFolder.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/WatchedFolder.java?rev=1126982&r1=1126981&r2=1126982&view=diff
==============================================================================
--- sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/WatchedFolder.java
(original)
+++ sling/trunk/installer/providers/jcr/src/main/java/org/apache/sling/installer/provider/jcr/impl/WatchedFolder.java
Tue May 24 11:24:24 2011
@@ -44,14 +44,15 @@ import org.slf4j.LoggerFactory;
  */
 class WatchedFolder implements EventListener{
 
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
     private final String path;
     private final int priority;
     private final Session session;
-    private static RescanTimer rescanTimer = new RescanTimer();
-    private boolean needsScan;
     private final Collection <JcrInstaller.NodeConverter> converters;
     private final Set<String> existingResourceUrls = new HashSet<String>();
-    protected final Logger log = LoggerFactory.getLogger(getClass());
+
+    private volatile boolean needsScan;
 
     static class ScanResult {
         List<InstallableResource> toAdd = new ArrayList<InstallableResource>();
@@ -66,7 +67,7 @@ class WatchedFolder implements EventList
             final int priority,
     		final Collection<JcrInstaller.NodeConverter> converters)
     throws RepositoryException {
-        if(priority < 1) {
+        if (priority < 1) {
             throw new IllegalArgumentException("Cannot watch folder with priority 0:" + path);
         }
 
@@ -75,7 +76,9 @@ class WatchedFolder implements EventList
         this.priority = priority;
 
         this.session = session;
+    }
 
+    public void start() throws RepositoryException {
         // observe any changes in our folder (and under it, as changes to properties
         // might be lower in the hierarchy)
         final int eventTypes = Event.NODE_ADDED | Event.NODE_REMOVED
@@ -84,11 +87,12 @@ class WatchedFolder implements EventList
         final boolean noLocal = true;
         session.getWorkspace().getObservationManager().addEventListener(this, eventTypes,
path,
                 isDeep, null, null, noLocal);
+        this.needsScan = true;
 
         log.info("Watching folder {} (priority {})", path, priority);
     }
 
-    void cleanup() {
+    public void stop() {
     	try {
 	    	session.getWorkspace().getObservationManager().removeEventListener(this);
     	} catch(RepositoryException re) {
@@ -101,38 +105,36 @@ class WatchedFolder implements EventList
     	return getClass().getSimpleName() + ":" + path;
     }
 
-    String getPath() {
+    public String getPath() {
         return path;
     }
 
-    /** Set a static "timer" whenever an event occurs */
-    public void onEvent(EventIterator it) {
-        log.debug("JCR event received for path {}", path);
-    	scheduleScan();
-    }
-
-    void scheduleScan() {
-        log.debug("Scheduling scan of {}", path);
-        rescanTimer.scheduleScan();
+    /**
+     * Update scan flag whenever an observation event occurs.
+     */
+    public void onEvent(final EventIterator it) {
+        log.debug("JCR events received for path {}", path);
         needsScan = true;
     }
 
-    boolean needsScan() {
+    /**
+     * Did an observation event occur in the meantime?
+     */
+    public boolean needsScan() {
     	return needsScan;
     }
 
-    static RescanTimer getRescanTimer() {
-    	return rescanTimer;
-    }
-
-    /** Scan the contents of our folder and return the corresponding InstallableResource
*/
-    ScanResult scan() throws RepositoryException {
+    /**
+     * Scan the contents of our folder and return the corresponding
+     * <code>ScanResult</code> containing the <code>InstallableResource</code>s.
+     */
+    public ScanResult scan() throws RepositoryException {
         log.debug("Scanning {}", path);
         needsScan = false;
 
         Node folder = null;
-        if(session.itemExists(path)) {
-        	Item i = session.getItem(path);
+        if (session.itemExists(path)) {
+        	final Item i = session.getItem(path);
         	if(i.isNode()) {
         		folder = (Node)i;
         	}



Mime
View raw message