incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1376410 - /sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceListener.java
Date Thu, 23 Aug 2012 08:52:30 GMT
Author: cziegeler
Date: Thu Aug 23 08:52:30 2012
New Revision: 1376410

URL: http://svn.apache.org/viewvc?rev=1376410&view=rev
Log:
SLING-2573 : JcrResourceListener activation is failing with NPE

Modified:
    sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceListener.java

Modified: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceListener.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceListener.java?rev=1376410&r1=1376409&r2=1376410&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceListener.java
(original)
+++ sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceListener.java
Thu Aug 23 08:52:30 2012
@@ -40,6 +40,7 @@ import org.apache.felix.scr.annotations.
 import org.apache.felix.scr.annotations.Properties;
 import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferencePolicy;
 import org.apache.jackrabbit.api.observation.JackrabbitEvent;
 import org.apache.sling.api.SlingConstants;
 import org.apache.sling.api.resource.LoginException;
@@ -68,7 +69,7 @@ public class JcrResourceListener impleme
     /** Logger */
     private final Logger logger = LoggerFactory.getLogger(JcrResourceListener.class);
 
-    @Reference
+    @Reference(policy=ReferencePolicy.DYNAMIC)
     private EventAdmin eventAdmin;
 
     @Reference
@@ -115,28 +116,47 @@ public class JcrResourceListener impleme
     }
 
     @Activate
-    protected void activate() throws RepositoryException, LoginException {
+    protected void activate() throws LoginException {
         this.resourceResolver = this.resourceResolverFactory.getAdministrativeResourceResolver(null);
-        try {
-            this.session = this.resourceResolver.adaptTo(Session.class);
-            this.startPath = "/";
-            this.mountPrefix = null;
-
-            this.osgiEventQueue = new LinkedBlockingQueue<Dictionary<String,Object>>();
-            Thread oeqt = new Thread(new Runnable() {
-                public void run() {
-                    processOsgiEventQueue();
-                }
-            }, "JCR Resource Event Queue Processor");
-            oeqt.start();
+        this.startPath = "/";
+        this.mountPrefix = null;
 
-            this.session.getWorkspace().getObservationManager().addEventListener(this,
-                Event.NODE_ADDED|Event.NODE_REMOVED|Event.PROPERTY_ADDED|Event.PROPERTY_CHANGED|Event.PROPERTY_REMOVED,
-                this.startPath, true, null, null, false);
-        } catch (final RepositoryException re) {
-            this.resourceResolver.close();
-            this.resourceResolver = null;
-            throw re;
+        this.osgiEventQueue = new LinkedBlockingQueue<Dictionary<String,Object>>();
+        final Thread oeqt = new Thread(new Runnable() {
+            public void run() {
+                init();
+                processOsgiEventQueue();
+            }
+        }, "JCR Resource Event Queue Processor");
+        oeqt.start();
+
+    }
+
+    private void init() {
+        // lazy polling
+        Session session = null;
+        ResourceResolver resolver = this.resourceResolver;
+        while ( resolver != null && session == null ) {
+            session = this.resourceResolver.adaptTo(Session.class);
+            if ( session == null ) {
+                try {
+                    Thread.sleep(100);
+                } catch (final InterruptedException ignore) {
+                    // we ignore this
+                }
+                resolver = this.resourceResolver;
+            }
+        }
+        if ( session != null ) {
+            try {
+                session.getWorkspace().getObservationManager().addEventListener(this,
+                                Event.NODE_ADDED|Event.NODE_REMOVED|Event.PROPERTY_ADDED|Event.PROPERTY_CHANGED|Event.PROPERTY_REMOVED,
+                                this.startPath, true, null, null, false);
+                this.session = session;
+            } catch (final RepositoryException re) {
+                logger.error("Unable to register event listener.", re);
+                this.deactivate();
+            }
         }
     }
 
@@ -155,6 +175,7 @@ public class JcrResourceListener impleme
         }
         if ( this.resourceResolver != null ) {
             this.resourceResolver.close();
+            this.resourceResolver = null;
         }
 
         // drop any remaining OSGi Events not processed yet
@@ -165,7 +186,7 @@ public class JcrResourceListener impleme
     /**
      * @see javax.jcr.observation.EventListener#onEvent(javax.jcr.observation.EventIterator)
      */
-    public void onEvent(EventIterator events) {
+    public void onEvent(final EventIterator events) {
         // if the event admin is currently not available, we just skip this
         final EventAdmin localEA = this.eventAdmin;
         if ( localEA == null ) {



Mime
View raw message