Return-Path: Delivered-To: apmail-sling-commits-archive@www.apache.org Received: (qmail 89248 invoked from network); 12 Jan 2010 08:13:09 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 12 Jan 2010 08:13:09 -0000 Received: (qmail 2681 invoked by uid 500); 12 Jan 2010 08:13:09 -0000 Delivered-To: apmail-sling-commits-archive@sling.apache.org Received: (qmail 2623 invoked by uid 500); 12 Jan 2010 08:13:09 -0000 Mailing-List: contact commits-help@sling.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@sling.apache.org Delivered-To: mailing list commits@sling.apache.org Received: (qmail 2614 invoked by uid 99); 12 Jan 2010 08:13:09 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 12 Jan 2010 08:13:09 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 12 Jan 2010 08:13:06 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id E413423889FA; Tue, 12 Jan 2010 08:12:44 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@sling.apache.org From: cziegeler@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100112081244.E413423889FA@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 JcrResourceListener 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 properties = new Hashtable(); - 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 properties = new Hashtable(); + 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 paths, final String topic, final EventAdmin localEA) { + for(final String path : paths) { final Resource resource = this.resolver.getResource(path); if ( resource != null ) { final Dictionary properties = new Hashtable(); 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 properties = new Hashtable(); - properties.put(SlingConstants.PROPERTY_PATH, path); - - localEA.postEvent(new org.osgi.service.event.Event(SlingConstants.TOPIC_RESOURCE_REMOVED, properties)); - } } }