Author: cziegeler
Date: Tue Aug 24 12:49:02 2010
New Revision: 988516

URL: http://svn.apache.org/viewvc?rev=988516&view=rev
Log:
SLING-1690 : Reduce startup time of jcr install

Modified:
    
sling/trunk/installer/jcr/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/impl/ConfigNodeConverter.java
    
sling/trunk/installer/jcr/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/impl/JcrInstaller.java
    
sling/trunk/installer/jcr/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/impl/WatchedFolder.java
    
sling/trunk/installer/jcr/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/impl/MiscUtil.java

Modified: 
sling/trunk/installer/jcr/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/impl/ConfigNodeConverter.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/installer/jcr/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/impl/ConfigNodeConverter.java?rev=988516&r1=988515&r2=988516&view=diff
==============================================================================
--- 
sling/trunk/installer/jcr/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/impl/ConfigNodeConverter.java
 (original)
+++ 
sling/trunk/installer/jcr/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/impl/ConfigNodeConverter.java
 Tue Aug 24 12:49:02 2010
@@ -51,7 +51,7 @@ class ConfigNodeConverter implements Jcr
         */
        public InstallableResource convertNode(final Node n,
                final int priority)
-       throws Exception {
+       throws RepositoryException {
                InstallableResource result = null;
 
                // We only consider CONFIG_NODE_TYPE nodes

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=988516&r1=988515&r2=988516&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
 Tue Aug 24 12:49:02 2010
@@ -36,6 +36,7 @@ import org.apache.sling.jcr.api.SlingRep
 import org.apache.sling.osgi.installer.InstallableResource;
 import org.apache.sling.osgi.installer.OsgiInstaller;
 import org.apache.sling.settings.SlingSettingsService;
+import org.osgi.service.component.ComponentConstants;
 import org.osgi.service.component.ComponentContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -123,10 +124,12 @@ public class JcrInstaller implements Eve
     /** The root folders that we watch */
     private String [] roots;
 
+    private ComponentContext componentContext;
+
     /** Convert Nodes to InstallableResources */
     static interface NodeConverter {
        InstallableResource convertNode(Node n, int priority)
-       throws Exception;
+       throws RepositoryException;
     }
 
     /** Our NodeConverters*/
@@ -152,7 +155,53 @@ public class JcrInstaller implements Eve
         @Override
         public final void run() {
             log.info("Background thread {} starting", 
Thread.currentThread().getName());
-            while(active) {
+            try {
+                // open session
+                session = 
repository.loginAdministrative(repository.getDefaultWorkspace());
+
+                for (String path : roots) {
+                    listeners.add(new RootFolderListener(session, 
folderNameFilter, path, updateFoldersListTimer));
+                    log.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,
+                        Event.NODE_ADDED | Event.NODE_REMOVED,
+                        "/",
+                        false, // isDeep
+                        null,
+                        null,
+                        true); // noLocal
+                log.debug("Watching for node events on / to detect removal/add 
of our root folders");
+
+
+                // Find paths to watch and create WatchedFolders to manage them
+                watchedFolders = new LinkedList<WatchedFolder>();
+                for(String root : roots) {
+                    findPathsToWatch(root, watchedFolders);
+                }
+
+                // Scan watchedFolders and register resources with installer
+                final List<InstallableResource> resources = new 
LinkedList<InstallableResource>();
+                for(WatchedFolder f : watchedFolders) {
+                    final WatchedFolder.ScanResult r = f.scan();
+                    log.debug("Startup: {} provides resources {}", f, r.toAdd);
+                    resources.addAll(r.toAdd);
+                }
+
+                log.debug("Registering {} resources with OSGi installer: {}", 
resources.size(), resources);
+                installer.registerResources(URL_SCHEME, resources);
+            } catch (final RepositoryException re) {
+                log.error("Repository exception during startup - deactivating 
installer!", re);
+                active = false;
+                final ComponentContext ctx = componentContext;
+                if ( ctx  != null ) {
+                    final String name = (String) 
componentContext.getProperties().get(
+                            ComponentConstants.COMPONENT_NAME);
+                    ctx.disableComponent(name);
+                }
+            }
+            while (active) {
                 runOneCycle();
             }
             log.info("Background thread {} done", 
Thread.currentThread().getName());
@@ -161,12 +210,16 @@ public class JcrInstaller implements Eve
     };
     private StoppableThread backgroundThread;
 
-    protected void activate(ComponentContext context) throws Exception {
+    /**
+     * Activate this component.
+     */
+    protected void activate(final ComponentContext context) {
+        if (backgroundThread != null) {
+            throw new IllegalStateException("Expected backgroundThread to be 
null in activate()");
+        }
+        this.componentContext = context;
         log.info("Activating Apache Sling JCR Installer");
 
-        // open session
-       session = 
repository.loginAdministrative(repository.getDefaultWorkspace());
-
        // Setup converters
        converters.add(new FileNodeConverter());
        converters.add(new ConfigNodeConverter());
@@ -196,75 +249,45 @@ public class JcrInstaller implements Eve
         folderNameFilter = new 
FolderNameFilter(OsgiUtil.toStringArray(context.getProperties().get(PROP_SEARCH_PATH),
 DEFAULT_SEARCH_PATH),
                 folderNameRegexp, settings.getRunModes());
         roots = folderNameFilter.getRootPaths();
-        for (String path : roots) {
-            listeners.add(new RootFolderListener(session, folderNameFilter, 
path, updateFoldersListTimer));
-            log.debug("Configured root folder: {}", path);
-        }
-
-        // Watch for events on the root - that might be one of our root folders
-        session.getWorkspace().getObservationManager().addEventListener(this,
-                Event.NODE_ADDED | Event.NODE_REMOVED,
-                "/",
-                false, // isDeep
-                null,
-                null,
-                true); // noLocal
-        log.debug("Watching for node events on / to detect removal/add of our 
root folders");
-
-
-       // Find paths to watch and create WatchedFolders to manage them
-       watchedFolders = new LinkedList<WatchedFolder>();
-       for(String root : roots) {
-               findPathsToWatch(root, watchedFolders);
-       }
-
-       // Scan watchedFolders and register resources with installer
-       final List<InstallableResource> resources = new 
LinkedList<InstallableResource>();
-       for(WatchedFolder f : watchedFolders) {
-               final WatchedFolder.ScanResult r = f.scan();
-               log.debug("Startup: {} provides resources {}", f, r.toAdd);
-               resources.addAll(r.toAdd);
-       }
-
-       log.debug("Registering {} resources with OSGi installer: {}", 
resources.size(), resources);
-       installer.registerResources(URL_SCHEME, resources);
-
-       if (backgroundThread != null) {
-           throw new IllegalStateException("Expected backgroundThread to be 
null in activate()");
-       }
         backgroundThread = new StoppableThread();
         backgroundThread.start();
     }
 
-    protected void deactivate(ComponentContext context) {
+    /**
+     * Deactivate this component
+     */
+    protected void deactivate(final ComponentContext context) {
        log.info("Deactivating Apache Sling JCR Installer");
 
        final long timeout = 30000L;
+        backgroundThread.active = false;
+        log.debug("Waiting for " + backgroundThread.getName() + " Thread to 
end...");
        try {
-            backgroundThread.active = false;
-            log.debug("Waiting for " + backgroundThread.getName() + " Thread 
to end...");
             backgroundThread.join(timeout);
-            backgroundThread = null;
        } catch(InterruptedException iex) {
            throw new IllegalStateException("backgroundThread.join interrupted 
after " + timeout + " msec");
        }
+        backgroundThread = null;
 
+        folderNameFilter = null;
+        watchedFolders = null;
+        converters.clear();
         try {
-            folderNameFilter = null;
-            watchedFolders = null;
-            converters.clear();
-            if(session != null) {
+            if (session != null) {
                 for(RootFolderListener wfc : listeners) {
                     wfc.cleanup(session);
                 }
                 
session.getWorkspace().getObservationManager().removeEventListener(this);
-                session.logout();
-                session = null;
             }
-            listeners.clear();
-        } catch(Exception e) {
+        } catch (final RepositoryException e) {
             log.warn("Exception in deactivate()", e);
         }
+        if ( session != null ) {
+            session.logout();
+            session = null;
+        }
+        listeners.clear();
+        this.componentContext = null;
     }
 
     /** Get a property value from the component context or bundle context */

Modified: 
sling/trunk/installer/jcr/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/impl/WatchedFolder.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/installer/jcr/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/impl/WatchedFolder.java?rev=988516&r1=988515&r2=988516&view=diff
==============================================================================
--- 
sling/trunk/installer/jcr/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/impl/WatchedFolder.java
 (original)
+++ 
sling/trunk/installer/jcr/jcrinstall/src/main/java/org/apache/sling/jcr/jcrinstall/impl/WatchedFolder.java
 Tue Aug 24 12:49:02 2010
@@ -126,7 +126,7 @@ class WatchedFolder implements EventList
     }
 
     /** Scan the contents of our folder and return the corresponding 
InstallableResource */
-    ScanResult scan() throws Exception {
+    ScanResult scan() throws RepositoryException {
         log.debug("Scanning {}", path);
         needsScan = false;
 

Modified: 
sling/trunk/installer/jcr/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/impl/MiscUtil.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/installer/jcr/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/impl/MiscUtil.java?rev=988516&r1=988515&r2=988516&view=diff
==============================================================================
--- 
sling/trunk/installer/jcr/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/impl/MiscUtil.java
 (original)
+++ 
sling/trunk/installer/jcr/jcrinstall/src/test/java/org/apache/sling/jcr/jcrinstall/impl/MiscUtil.java
 Tue Aug 24 12:49:02 2010
@@ -56,6 +56,7 @@ class MiscUtil {
         setField(installer, "settings", new MockSettings(RUN_MODES));
 
         installer.activate(getMockComponentContext());
+        Thread.sleep(1000);
         return installer;
     }
 


Reply via email to