incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r898227 - /sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceListener.java
Date Tue, 12 Jan 2010 08:12:44 GMT
Author: cziegeler
Date: Tue Jan 12 08:12:44 2010
New Revision: 898227

URL: http://svn.apache.org/viewvc?rev=898227&view=rev
Log:
Just code cleanup - no functional changes.

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=898227&r1=898226&r2=898227&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
Tue Jan 12 08:12:44 2010
@@ -39,10 +39,15 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+/**
+ * The <code>JcrResourceListener</code> listens for JCR observation
+ * events and creates resource events which are sent through the
+ * OSGi event admin.
+ */
 public class JcrResourceListener implements EventListener {
 
     /** Logger */
-    private static final Logger LOGGER = LoggerFactory.getLogger(JcrResourceListener.class);
+    private final Logger logger = LoggerFactory.getLogger(JcrResourceListener.class);
 
     /** The session for observation. */
     private final Session session;
@@ -57,8 +62,17 @@
     private final ResourceResolver resolver;
 
     /** The event admin tracker. */
-    private ServiceTracker eventAdminTracker;
+    private final ServiceTracker eventAdminTracker;
 
+    /**
+     * Constructor.
+     * @param repository The repository to observe.
+     * @param factory    The resource resolver factory.
+     * @param startPath  The observation root path
+     * @param mountPrefix The mount path in the repository
+     * @param eventAdminTracker The service tracker for the event admin.
+     * @throws RepositoryException
+     */
     public JcrResourceListener(final SlingRepository repository,
                                final JcrResourceResolverFactory factory,
                                final String startPath,
@@ -75,11 +89,14 @@
             this.startPath, true, null, null, false);
     }
 
+    /**
+     * Dispose this listener.
+     */
     public void dispose() {
         try {
             this.session.getWorkspace().getObservationManager().removeEventListener(this);
         } catch (RepositoryException e) {
-            LOGGER.warn("Unable to remove session listener: " + this, e);
+            logger.warn("Unable to remove session listener: " + this, e);
         }
         this.session.logout();
     }
@@ -88,6 +105,7 @@
      * @see javax.jcr.observation.EventListener#onEvent(javax.jcr.observation.EventIterator)
      */
     public void onEvent(EventIterator events) {
+        // if the event admin is currently not available, we just skip this
         final EventAdmin localEA = (EventAdmin) this.eventAdminTracker.getService();
         if ( localEA == null ) {
             return;
@@ -119,56 +137,45 @@
                     }
                 }
             } catch (RepositoryException e) {
-                LOGGER.error("Error during modification: {}", e.getMessage());
+                logger.error("Error during modification: {}", e.getMessage());
             }
         }
-        // remove is the strongest oberation, therefore remove all removed
+        // remove is the strongest operation, therefore remove all removed
         // paths from changed and added
         addedPaths.removeAll(removedPaths);
         changedPaths.removeAll(removedPaths);
         // add is stronger than changed
         changedPaths.removeAll(addedPaths);
 
-        // send events
-        for(final String path : addedPaths) {
-            final Resource resource = this.resolver.getResource(path);
-            if ( resource != null ) {
-                final Dictionary<String, String> properties = new Hashtable<String,
String>();
-                properties.put(SlingConstants.PROPERTY_PATH, path);
-                final String resourceType = resource.getResourceType();
-                final String resourceSuperType = resource.getResourceSuperType();
-                if ( resourceType != null ) {
-                    properties.put(SlingConstants.PROPERTY_RESOURCE_TYPE, resource.getResourceType());
-                }
-                if ( resourceSuperType != null ) {
-                    properties.put(SlingConstants.PROPERTY_RESOURCE_SUPER_TYPE, resource.getResourceSuperType());
-                }
+        // send events for added and changed
+        sendEvents(addedPaths, SlingConstants.TOPIC_RESOURCE_ADDED, localEA);
+        sendEvents(changedPaths, SlingConstants.TOPIC_RESOURCE_CHANGED, localEA);
 
-                localEA.postEvent(new org.osgi.service.event.Event(SlingConstants.TOPIC_RESOURCE_ADDED,
properties));
-            }
+        // send events for removed
+        for(final String path : removedPaths) {
+            final Dictionary<String, String> properties = new Hashtable<String,
String>();
+            properties.put(SlingConstants.PROPERTY_PATH, path);
+
+            localEA.postEvent(new org.osgi.service.event.Event(SlingConstants.TOPIC_RESOURCE_REMOVED,
properties));
         }
-        for(final String path : changedPaths) {
+    }
+
+    private void sendEvents(final Set<String> paths, final String topic, final EventAdmin
localEA) {
+        for(final String path : paths) {
             final Resource resource = this.resolver.getResource(path);
             if ( resource != null ) {
                 final Dictionary<String, String> properties = new Hashtable<String,
String>();
                 properties.put(SlingConstants.PROPERTY_PATH, path);
                 final String resourceType = resource.getResourceType();
-                final String resourceSuperType = resource.getResourceSuperType();
                 if ( resourceType != null ) {
                     properties.put(SlingConstants.PROPERTY_RESOURCE_TYPE, resource.getResourceType());
                 }
+                final String resourceSuperType = resource.getResourceSuperType();
                 if ( resourceSuperType != null ) {
                     properties.put(SlingConstants.PROPERTY_RESOURCE_SUPER_TYPE, resource.getResourceSuperType());
                 }
-
-                localEA.postEvent(new org.osgi.service.event.Event(SlingConstants.TOPIC_RESOURCE_CHANGED,
properties));
+                localEA.postEvent(new org.osgi.service.event.Event(topic, properties));
             }
         }
-        for(final String path : removedPaths) {
-            final Dictionary<String, String> properties = new Hashtable<String,
String>();
-            properties.put(SlingConstants.PROPERTY_PATH, path);
-
-            localEA.postEvent(new org.osgi.service.event.Event(SlingConstants.TOPIC_RESOURCE_REMOVED,
properties));
-        }
     }
 }



Mime
View raw message