You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2011/10/17 17:16:07 UTC

svn commit: r1185243 - in /sling/trunk/bundles: api/ api/src/main/java/org/apache/sling/api/ jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/ jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/ jcr/resource/src/t...

Author: cziegeler
Date: Mon Oct 17 15:16:07 2011
New Revision: 1185243

URL: http://svn.apache.org/viewvc?rev=1185243&view=rev
Log:
SLING-2248 : Send an event when the mappings change

Modified:
    sling/trunk/bundles/api/pom.xml
    sling/trunk/bundles/api/src/main/java/org/apache/sling/api/SlingConstants.java
    sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverFactoryImpl.java
    sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/MapEntries.java
    sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverTest.java

Modified: sling/trunk/bundles/api/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/pom.xml?rev=1185243&r1=1185242&r2=1185243&view=diff
==============================================================================
--- sling/trunk/bundles/api/pom.xml (original)
+++ sling/trunk/bundles/api/pom.xml Mon Oct 17 15:16:07 2011
@@ -94,7 +94,7 @@
                             http://sling.apache.org/site/sling-api.html
                         </Bundle-DocURL>
                         <Export-Package>
-                            org.apache.sling.api;version=2.1,
+                            org.apache.sling.api;version=2.2,
                             org.apache.sling.api.adapter;version=2.2,
                             org.apache.sling.api.auth;version=1.0,
                             org.apache.sling.api.request;version=2.2,

Modified: sling/trunk/bundles/api/src/main/java/org/apache/sling/api/SlingConstants.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/SlingConstants.java?rev=1185243&r1=1185242&r2=1185243&view=diff
==============================================================================
--- sling/trunk/bundles/api/src/main/java/org/apache/sling/api/SlingConstants.java (original)
+++ sling/trunk/bundles/api/src/main/java/org/apache/sling/api/SlingConstants.java Mon Oct 17 15:16:07 2011
@@ -301,6 +301,12 @@ public class SlingConstants {
     public static final String TOPIC_RESOURCE_PROVIDER_REMOVED = "org/apache/sling/api/resource/ResourceProvider/REMOVED";
 
     /**
+     * The topic for the OSGi event which is sent when the resource mapping changes.
+     * @since 2.2.0
+     */
+    public static final String TOPIC_RESOURCE_RESOLVER_MAPPING_CHANGED = "org/apache/sling/api/resource/ResourceResolverMapping/CHANGED";
+
+    /**
      * The name of the event property holding the resource path.
      * @since 2.0.6
      */

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=1185243&r1=1185242&r2=1185243&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 Mon Oct 17 15:16:07 2011
@@ -464,7 +464,7 @@ public class JcrResourceResolverFactoryI
 
         // set up the map entries from configuration
         try {
-            mapEntries = new MapEntries(this, componentContext.getBundleContext());
+            mapEntries = new MapEntries(this, componentContext.getBundleContext(), this.eventAdminTracker);
         } catch (Exception e) {
             log.error(
                 "activate: Cannot access repository, failed setting up Mapping Support",

Modified: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/MapEntries.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/MapEntries.java?rev=1185243&r1=1185242&r2=1185243&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/MapEntries.java (original)
+++ sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/MapEntries.java Mon Oct 17 15:16:07 2011
@@ -46,7 +46,10 @@ import org.apache.sling.jcr.resource.int
 import org.apache.sling.jcr.resource.internal.JcrResourceResolverFactoryImpl;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventAdmin;
 import org.osgi.service.event.EventHandler;
+import org.osgi.util.tracker.ServiceTracker;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -77,6 +80,8 @@ public class MapEntries implements Event
 
     private final ServiceRegistration registration;
 
+    private final ServiceTracker eventAdminTracker;
+
     private MapEntries() {
         factory = null;
         resolver = null;
@@ -86,15 +91,18 @@ public class MapEntries implements Event
         resolveMaps = Collections.<MapEntry> emptyList();
         mapMaps = Collections.<MapEntry> emptyList();
         this.registration = null;
+        this.eventAdminTracker = null;
     }
 
     public MapEntries(final JcrResourceResolverFactoryImpl factory,
-                      final BundleContext bundleContext)
+                      final BundleContext bundleContext,
+                      final ServiceTracker eventAdminTracker)
     throws LoginException {
         this.resolver = factory.getAdministrativeResourceResolver(null);
         this.factory = factory;
         this.mapRoot = factory.getMapRoot();
         this.mapRootPrefix = this.mapRoot + "/";
+        this.eventAdminTracker = eventAdminTracker;
 
         init();
         final Dictionary<String, String> props = new Hashtable<String, String>();
@@ -136,6 +144,8 @@ public class MapEntries implements Event
             this.resolveMaps = newResolveMaps;
             this.mapMaps = new TreeSet<MapEntry>(newMapMaps.values());
 
+            sendChangeEvent();
+
         } finally {
 
             // reset the flag and notify listeners
@@ -187,7 +197,7 @@ public class MapEntries implements Event
 
     // ---------- EventListener interface
 
-    public void handleEvent(final org.osgi.service.event.Event event) {
+    public void handleEvent(final Event event) {
         boolean handleEvent = false;
         final String path = (String) event.getProperty(SlingConstants.PROPERTY_PATH);
         if ( this.resolver != null && path != null ) {
@@ -212,6 +222,19 @@ public class MapEntries implements Event
 
     // ---------- internal
 
+    /**
+     * Send an OSGi event
+     */
+    private void sendChangeEvent() {
+        final EventAdmin ea = (EventAdmin) this.eventAdminTracker.getService();
+        if ( ea != null ) {
+            // we hard code the topic here and don't use SlingConstants.TOPIC_RESOURCE_RESOLVER_MAPPING_CHANGED
+            // to avoid requiring the latest API version for this bundle to work
+            final Event event = new Event("org/apache/sling/api/resource/ResourceResolverMapping/CHANGED", (Dictionary<?,?>)null);
+            ea.postEvent(event);
+        }
+    }
+
     private void loadResolverMap(final ResourceResolver resolver,
             Collection<MapEntry> resolveEntries,
             Map<String, MapEntry> mapEntries) {

Modified: sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverTest.java?rev=1185243&r1=1185242&r2=1185243&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverTest.java (original)
+++ sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverTest.java Mon Oct 17 15:16:07 2011
@@ -87,7 +87,7 @@ public class JcrResourceResolverTest ext
     private Node rootWs2Node;
 
     private JcrResourceListener listener;
-    
+
     String vanity;
 
     protected void setUp() throws Exception {
@@ -122,7 +122,7 @@ public class JcrResourceResolverTest ext
         vanity = "testVanity";
         rootNode.setProperty("sling:vanityPath", vanity);
         rootNode.addMixin("sling:VanityPath");
-        
+
         session.save();
 
         resFac = new JcrResourceResolverFactoryImpl();
@@ -148,9 +148,22 @@ public class JcrResourceResolverTest ext
         mapRootField.setAccessible(true);
         mapRootField.set(resFac, "/etc/map");
 
+        final EventAdmin mockVoidEA = new EventAdmin() {
+
+            public void postEvent(Event event) {
+                // nothing to do
+            }
+
+            public void sendEvent(Event event) {
+                // nothing to do
+            }
+        };
+        final ServiceTracker voidTracker = mock(ServiceTracker.class);
+        when(voidTracker.getService()).thenReturn(mockVoidEA);
+
         Field mapEntriesField = resFac.getClass().getDeclaredField("mapEntries");
         mapEntriesField.setAccessible(true);
-        mapEntries = new MapEntries(resFac, mock(BundleContext.class));
+        mapEntries = new MapEntries(resFac, mock(BundleContext.class), voidTracker);
         mapEntriesField.set(resFac, mapEntries);
 
         try {
@@ -1292,7 +1305,7 @@ public class JcrResourceResolverTest ext
         assertNotNull(res.adaptTo(Node.class));
         assertTrue(rootNode.isSame(res.adaptTo(Node.class)));
     }
-    
+
     public void testResolveVanityPath() throws Exception {
         String path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath)
                 + "/" + vanity + ".print.html");