Author: justin
Date: Sat Mar 20 02:22:25 2010
New Revision: 925531

URL: http://svn.apache.org/viewvc?rev=925531&view=rev
Log:
SLING-1447 - adding ability to configure multiple workspaces for event firing

Modified:
    
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceConstants.java
    
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceListener.java
    
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverFactoryImpl.java
    
sling/trunk/bundles/jcr/resource/src/main/resources/OSGI-INF/metatype/metatype.properties

Modified: 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceConstants.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceConstants.java?rev=925531&r1=925530&r2=925531&view=diff
==============================================================================
--- 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceConstants.java
 (original)
+++ 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceConstants.java
 Sat Mar 20 02:22:25 2010
@@ -52,4 +52,10 @@ public class JcrResourceConstants {
      * the primary node type is used as the resource super type.
      */
     public static final String SLING_RESOURCE_SUPER_TYPE_PROPERTY = 
"sling:resourceSuperType";
+
+    /**
+     * The name of the event property holding the workspace name.
+     * @since 2.0.8
+     */
+    public static final String PROPERTY_WORKSPACE = "workspace";
 }

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=925531&r1=925530&r2=925531&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
 Sat Mar 20 02:22:25 2010
@@ -35,6 +35,7 @@ import org.apache.sling.api.resource.Res
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ResourceUtil;
 import org.apache.sling.jcr.api.SlingRepository;
+import org.apache.sling.jcr.resource.JcrResourceConstants;
 import org.apache.sling.jcr.resource.JcrResourceResolverFactory;
 import org.osgi.service.event.EventAdmin;
 import org.osgi.util.tracker.ServiceTracker;
@@ -51,6 +52,9 @@ public class JcrResourceListener impleme
     /** Logger */
     private final Logger logger = 
LoggerFactory.getLogger(JcrResourceListener.class);
 
+    /** The workspace for observation. */
+    private final String workspaceName;
+
     /** The session for observation. */
     private final Session session;
 
@@ -69,6 +73,7 @@ public class JcrResourceListener impleme
     /**
      * Constructor.
      * @param repository The repository to observe.
+     * @param workspaceName The workspace name to observe
      * @param factory    The resource resolver factory.
      * @param startPath  The observation root path
      * @param mountPrefix The mount path in the repository
@@ -76,12 +81,14 @@ public class JcrResourceListener impleme
      * @throws RepositoryException
      */
     public JcrResourceListener(final SlingRepository repository,
+                               final String workspaceName,
                                final JcrResourceResolverFactory factory,
                                final String startPath,
                                final String mountPrefix,
                                final ServiceTracker eventAdminTracker)
     throws RepositoryException {
-        this.session = repository.loginAdministrative(null);
+        this.workspaceName = workspaceName;
+        this.session = repository.loginAdministrative(workspaceName);
         this.resolver = factory.getResourceResolver(this.session);
         this.startPath = startPath;
         this.eventAdminTracker = eventAdminTracker;
@@ -184,6 +191,9 @@ public class JcrResourceListener impleme
                 }
                 final Dictionary<String, String> properties = new 
Hashtable<String, String>();
                 properties.put(SlingConstants.PROPERTY_PATH, 
resource.getPath());
+                if (workspaceName != null) {
+                    properties.put(JcrResourceConstants.PROPERTY_WORKSPACE, 
workspaceName);
+                }
                 final String resourceType = resource.getResourceType();
                 if ( resourceType != null ) {
                     properties.put(SlingConstants.PROPERTY_RESOURCE_TYPE, 
resource.getResourceType());

Modified: 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverFactoryImpl.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverFactoryImpl.java?rev=925531&r1=925530&r2=925531&view=diff
==============================================================================
--- 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverFactoryImpl.java
 (original)
+++ 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverFactoryImpl.java
 Sat Mar 20 02:22:25 2010
@@ -19,10 +19,14 @@
 package org.apache.sling.jcr.resource.internal;
 
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
 import java.util.Dictionary;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.regex.Pattern;
 
 import javax.jcr.Credentials;
@@ -95,6 +99,12 @@ public class JcrResourceResolverFactoryI
     }
 
     /**
+     * Special value which, if passed to listener.workspaces, will have 
resource
+     * events fired for all workspaces.
+     */
+    public static final String ALL_WORKSPACES = "*";
+
+    /**
      * @scr.property values.1="/apps" values.2="/libs"
      */
     public static final String PROP_PATH = "resource.resolver.searchpath";
@@ -151,6 +161,11 @@ public class JcrResourceResolverFactoryI
      */
     private static final String PROP_MAP_LOCATION = 
"resource.resolver.map.location";
 
+    /**
+     * @scr.property cardinality="+"
+     */
+    private static final String PROP_LISTENER_WORKSPACES = 
"resource.resolver.listener.workspaces";
+
     /** default log */
     private final Logger log = LoggerFactory.getLogger(getClass());
 
@@ -187,8 +202,8 @@ public class JcrResourceResolverFactoryI
     // whether to mangle paths with namespaces or not
     private boolean mangleNamespacePrefixes;
 
-    /** The resource listenr for the observation events. */
-    private JcrResourceListener resourceListener;
+    /** The resource listeners for the observation events. */
+    private Set<JcrResourceListener> resourceListeners;
 
     /** The service tracker for the event admin
      */
@@ -360,9 +375,24 @@ public class JcrResourceResolverFactoryI
                 e);
         }
 
+        String[] listenerWorkspaces = 
OsgiUtil.toStringArray(properties.get(PROP_LISTENER_WORKSPACES));
+
         // start observation listener
         try {
-            this.resourceListener = new JcrResourceListener(this.repository, 
this, "/", "/", this.eventAdminTracker);
+            if (listenerWorkspaces == null) {
+                this.resourceListeners =
+                    Collections.singleton(new 
JcrResourceListener(this.repository, null, this, "/", "/", 
this.eventAdminTracker));
+            } else {
+                if 
(Arrays.asList(listenerWorkspaces).contains(ALL_WORKSPACES)) {
+                    listenerWorkspaces = getAllWorkspaces();
+                }
+
+                this.resourceListeners = new 
HashSet<JcrResourceListener>(listenerWorkspaces.length);
+                for (String wspName : listenerWorkspaces) {
+                    this.resourceListeners.add(
+                            new JcrResourceListener(this.repository, wspName, 
this, "/", "/", this.eventAdminTracker));
+                }
+            }
         } catch (Exception e) {
             log.error(
                 "activate: Cannot create resource listener; resource events 
for JCR resources will be disabled.",
@@ -395,9 +425,11 @@ public class JcrResourceResolverFactoryI
             this.eventAdminTracker.close();
             this.eventAdminTracker = null;
         }
-        if ( this.resourceListener != null ) {
-            this.resourceListener.dispose();
-            this.resourceListener = null;
+        if ( this.resourceListeners != null && 
!this.resourceListeners.isEmpty() ) {
+            for ( JcrResourceListener resourceListener : 
this.resourceListeners ) {
+                resourceListener.dispose();
+            }
+            this.resourceListeners = null;
         }
         this.resourceDecoratorTracker.close();
     }
@@ -479,6 +511,21 @@ public class JcrResourceResolverFactoryI
     }
 
     /**
+     * Get an array of all workspaces.
+     */
+    private String[] getAllWorkspaces() throws RepositoryException {
+        Session session =  null;
+        try {
+            session = repository.loginAdministrative(null);
+            return session.getWorkspace().getAccessibleWorkspaceNames();
+        } finally {
+            if (session != null) {
+                session.logout();
+            }
+        }
+    }
+
+    /**
      * Return the workspace name.
      * If the workspace name is provided, it is returned, otherwise
      * <code>null</code> is returned.

Modified: 
sling/trunk/bundles/jcr/resource/src/main/resources/OSGI-INF/metatype/metatype.properties
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/resources/OSGI-INF/metatype/metatype.properties?rev=925531&r1=925530&r2=925531&view=diff
==============================================================================
--- 
sling/trunk/bundles/jcr/resource/src/main/resources/OSGI-INF/metatype/metatype.properties
 (original)
+++ 
sling/trunk/bundles/jcr/resource/src/main/resources/OSGI-INF/metatype/metatype.properties
 Sat Mar 20 02:22:25 2010
@@ -70,3 +70,9 @@ resource.resolver.manglenamespaces.descr
  containing colons, even though they are perfectly valid characters in the \
  path part of URI references with a scheme. The default value of this property 
\
  if no configuration is provided is "true".
+
+resource.resolver.listener.workspaces.name = Listener Workspaces
+resource.resolver.listener.workspaces.description = A list of workspaces \
+ to which the JCR Resource Resolver should observe and fire OSGi events \
+ upon workspace changes. The special value '*' results in observation 
happening \
+ for all workspaces.


Reply via email to