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 2016/07/12 11:24:53 UTC

svn commit: r1752276 - in /sling/trunk/bundles/resourceresolver/src: main/java/org/apache/sling/resourceresolver/impl/ main/java/org/apache/sling/resourceresolver/impl/providers/ test/java/org/apache/sling/resourceresolver/impl/ test/java/org/apache/sl...

Author: cziegeler
Date: Tue Jul 12 11:24:53 2016
New Revision: 1752276

URL: http://svn.apache.org/viewvc?rev=1752276&view=rev
Log:
SLING-5830 : Allow required providers to be referenced through name (instead of PID)

Modified:
    sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/FactoryPreconditions.java
    sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryActivator.java
    sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/providers/ResourceProviderTracker.java
    sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/FactoryPreconditionsTest.java
    sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/providers/ResourceProviderTrackerTest.java

Modified: sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/FactoryPreconditions.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/FactoryPreconditions.java?rev=1752276&r1=1752275&r2=1752276&view=diff
==============================================================================
--- sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/FactoryPreconditions.java (original)
+++ sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/FactoryPreconditions.java Tue Jul 12 11:24:53 2016
@@ -39,6 +39,7 @@ import org.slf4j.LoggerFactory;
 public class FactoryPreconditions {
 
     private static final class RequiredProvider {
+        public String name;
         public String pid;
         public Filter filter;
     };
@@ -47,14 +48,17 @@ public class FactoryPreconditions {
 
     private volatile List<RequiredProvider> requiredProviders;
 
-    public void activate(final BundleContext bc, final String[] configuration, final ResourceProviderTracker tracker) {
+    public void activate(final BundleContext bc,
+            final String[] legycyConfiguration,
+            final String[] namesConfiguration,
+            final ResourceProviderTracker tracker) {
         synchronized ( this ) {
             this.tracker = tracker;
 
             final List<RequiredProvider> rps = new ArrayList<RequiredProvider>();
-            if ( configuration != null ) {
+            if ( legycyConfiguration != null ) {
                 final Logger logger = LoggerFactory.getLogger(getClass());
-                for(final String r : configuration) {
+                for(final String r : legycyConfiguration) {
                     if ( r != null && r.trim().length() > 0 ) {
                         final String value = r.trim();
                         RequiredProvider rp = new RequiredProvider();
@@ -74,6 +78,16 @@ public class FactoryPreconditions {
                     }
                 }
             }
+            if ( namesConfiguration != null ) {
+                for(final String r : namesConfiguration) {
+                    final String value = r.trim();
+                    if ( !value.isEmpty() ) {
+                        final RequiredProvider rp = new RequiredProvider();
+                        rp.name = value;
+                        rps.add(rp);
+                    }
+                }
+            }
             this.requiredProviders = rps;
         }
     }
@@ -85,7 +99,7 @@ public class FactoryPreconditions {
         }
     }
 
-    public boolean checkPreconditions(final String unavailableServicePid) {
+    public boolean checkPreconditions(final String unavailableName, final String unavailableServicePid) {
         synchronized ( this ) {
             final List<RequiredProvider> localRequiredProviders = this.requiredProviders;
             final ResourceProviderTracker localTracker = this.tracker;
@@ -100,7 +114,14 @@ public class FactoryPreconditions {
                             // ignore this service
                             continue;
                         }
-                        if (rp.filter != null && rp.filter.match(ref)) {
+                        if ( unavailableName != null && unavailableName.equals(h.getInfo().getName()) ) {
+                            // ignore this service
+                            continue;
+                        }
+                        if ( rp.name != null && rp.name.equals(h.getInfo().getName()) ) {
+                            canRegister = true;
+                            break;
+                        } else if (rp.filter != null && rp.filter.match(ref)) {
                             canRegister = true;
                             break;
                         } else if (rp.pid != null && rp.pid.equals(servicePid)){

Modified: sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryActivator.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryActivator.java?rev=1752276&r1=1752275&r2=1752276&view=diff
==============================================================================
--- sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryActivator.java (original)
+++ sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryActivator.java Tue Jul 12 11:24:53 2016
@@ -19,6 +19,7 @@
 package org.apache.sling.resourceresolver.impl;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Dictionary;
 import java.util.Hashtable;
 import java.util.List;
@@ -55,6 +56,8 @@ import org.osgi.framework.ServiceFactory
 import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.component.ComponentContext;
 import org.osgi.service.event.EventAdmin;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * The <code>ResourceResolverFactoryActivator/code> keeps track of required services for the
@@ -134,13 +137,20 @@ public class ResourceResolverFactoryActi
     private static final String PROP_ALLOW_DIRECT = "resource.resolver.allowDirect";
 
     @Property(unbounded=PropertyUnbounded.ARRAY,
-              value = "org.apache.sling.jcr.resource.internal.helper.jcr.JcrResourceProviderFactory",
-              label = "Required Providers",
+              label = "Required Providers (Deprecated)",
               description = "A resource resolver factory is only " +
                              "available (registered) if all resource providers mentioned in this configuration " +
                              "are available. Each entry is either a service PID or a filter expression.  " +
                              "Invalid filters are ignored.")
-    private static final String PROP_REQUIRED_PROVIDERS = "resource.resolver.required.providers";
+    private static final String PROP_REQUIRED_PROVIDERS_LEGACY = "resource.resolver.required.providers";
+
+    @Property(unbounded=PropertyUnbounded.ARRAY,
+            value = "JCR",
+            label = "Required Providers ",
+            description = "A resource resolver factory is only " +
+                           "available (registered) if all resource providers mentioned in this configuration " +
+                           "are available. Each entry is refers to the name of a registered provider.")
+    private static final String PROP_REQUIRED_PROVIDERS = "resource.resolver.required.providernames";
 
     /**
      * The resolver.virtual property has no default configuration. But the Sling
@@ -253,6 +263,9 @@ public class ResourceResolverFactoryActi
                   "production systems.")
     private static final String PROP_LOG_RESOURCE_RESOLVER_CLOSING = "resource.resolver.log.closing";
 
+    /** Logger. */
+    private final Logger logger = LoggerFactory.getLogger(this.getClass());
+
     /** Tracker for the resource decorators. */
     private final ResourceDecoratorTracker resourceDecoratorTracker = new ResourceDecoratorTracker();
 
@@ -524,13 +537,18 @@ public class ResourceResolverFactoryActi
         final BundleContext bc = componentContext.getBundleContext();
 
         // check for required property
-        final String[] requiredResourceProviders = PropertiesUtil.toStringArray(properties.get(PROP_REQUIRED_PROVIDERS));
+        final String[] requiredResourceProvidersLegacy = PropertiesUtil.toStringArray(properties.get(PROP_REQUIRED_PROVIDERS_LEGACY));
+        final String[] requiredResourceProviderNames = PropertiesUtil.toStringArray(properties.get(PROP_REQUIRED_PROVIDERS));
 
+        if ( requiredResourceProvidersLegacy != null && requiredResourceProvidersLegacy.length > 0 ) {
+            logger.error("ResourceResolverFactory is using deprecated required providers configuration (" + PROP_REQUIRED_PROVIDERS_LEGACY +
+                    "). Please change to use the property " + PROP_REQUIRED_PROVIDERS + " for values: " + Arrays.toString(requiredResourceProvidersLegacy));
+        }
         // for testing: if we run unit test, both trackers are set from the outside
         if ( this.resourceProviderTracker == null ) {
             this.resourceProviderTracker = new ResourceProviderTracker();
             this.changeListenerWhiteboard = new ResourceChangeListenerWhiteboard();
-            this.preconds.activate(bc, requiredResourceProviders, resourceProviderTracker);
+            this.preconds.activate(bc, requiredResourceProvidersLegacy, requiredResourceProviderNames, resourceProviderTracker);
             this.changeListenerWhiteboard.activate(this.componentContext.getBundleContext(),
                 this.resourceProviderTracker, searchPath);
             this.resourceProviderTracker.activate(this.componentContext.getBundleContext(),
@@ -540,24 +558,24 @@ public class ResourceResolverFactoryActi
                         @Override
                         public void providerAdded() {
                             if ( factoryRegistration == null ) {
-                                checkFactoryPreconditions(null);
+                                checkFactoryPreconditions(null, null);
                             }
 
                         }
 
                         @Override
-                        public void providerRemoved(final String pid, final boolean stateful, final boolean isUsed) {
+                        public void providerRemoved(final String name, final String pid, final boolean stateful, final boolean isUsed) {
                             if ( factoryRegistration != null ) {
                                 if ( isUsed && (stateful || paranoidProviderHandling) ) {
                                     unregisterFactory();
                                 }
-                                checkFactoryPreconditions(pid);
+                                checkFactoryPreconditions(name, pid);
                             }
                         }
                     });
         } else {
-            this.preconds.activate(bc, requiredResourceProviders, resourceProviderTracker);
-            this.checkFactoryPreconditions(null);
+            this.preconds.activate(bc, requiredResourceProvidersLegacy, requiredResourceProviderNames, resourceProviderTracker);
+            this.checkFactoryPreconditions(null, null);
          }
     }
 
@@ -657,10 +675,10 @@ public class ResourceResolverFactoryActi
     /**
      * Check the preconditions and if it changed, either register factory or unregister
      */
-    private void checkFactoryPreconditions(final String unavailableServicePid) {
+    private void checkFactoryPreconditions(final String unavailableName, final String unavailableServicePid) {
         final ComponentContext localContext = this.componentContext;
         if ( localContext != null ) {
-            final boolean result = this.preconds.checkPreconditions(unavailableServicePid);
+            final boolean result = this.preconds.checkPreconditions(unavailableName, unavailableServicePid);
             if ( result && this.factoryRegistration == null ) {
                 boolean create = true;
                 synchronized ( this ) {

Modified: sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/providers/ResourceProviderTracker.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/providers/ResourceProviderTracker.java?rev=1752276&r1=1752275&r2=1752276&view=diff
==============================================================================
--- sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/providers/ResourceProviderTracker.java (original)
+++ sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/providers/ResourceProviderTracker.java Tue Jul 12 11:24:53 2016
@@ -69,7 +69,7 @@ public class ResourceProviderTracker imp
 
         void providerAdded();
 
-        void providerRemoved(String pid, boolean stateful, boolean used);
+        void providerRemoved(String name, String pid, boolean stateful, boolean used);
     }
 
     private final Logger logger = LoggerFactory.getLogger(this.getClass());
@@ -207,7 +207,7 @@ public class ResourceProviderTracker imp
                    if ( pid != null && !(pid instanceof String) ) {
                        pid = null;
                    }
-                   cl.providerRemoved((String)pid,
+                   cl.providerRemoved(handlerInfo.getName(), (String)pid,
                                handlerInfo.getAuthType() != AuthType.no,
                                        deactivateHandler.isUsed());
                }
@@ -251,7 +251,7 @@ public class ResourceProviderTracker imp
             if ( deactivateHandler != null ) {
                 final ChangeListener cl = this.listener;
                 if ( cl != null ) {
-                    cl.providerRemoved(pid,
+                    cl.providerRemoved(info.getName(), pid,
                             info.getAuthType() != AuthType.no,
                                     deactivateHandler.isUsed());
                 }

Modified: sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/FactoryPreconditionsTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/FactoryPreconditionsTest.java?rev=1752276&r1=1752275&r2=1752276&view=diff
==============================================================================
--- sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/FactoryPreconditionsTest.java (original)
+++ sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/FactoryPreconditionsTest.java Tue Jul 12 11:24:53 2016
@@ -41,14 +41,14 @@ public class FactoryPreconditionsTest {
         Mockito.when(tracker.getResourceProviderStorage()).thenReturn(storage);
 
         FactoryPreconditions conditions = new FactoryPreconditions();
-        conditions.activate(null, null, tracker);
+        conditions.activate(null, null, null, tracker);
 
-        assertTrue(conditions.checkPreconditions(null));
+        assertTrue(conditions.checkPreconditions(null, null));
 
         conditions = new FactoryPreconditions();
-        conditions.activate(null, new String[0], tracker);
+        conditions.activate(null, new String[0], new String[0], tracker);
 
-        assertTrue(conditions.checkPreconditions(null));
+        assertTrue(conditions.checkPreconditions(null, null));
     }
 
     @Test public void testDeactivated() {
@@ -57,13 +57,13 @@ public class FactoryPreconditionsTest {
         Mockito.when(tracker.getResourceProviderStorage()).thenReturn(storage);
 
         FactoryPreconditions conditions = new FactoryPreconditions();
-        conditions.activate(null, null, tracker);
+        conditions.activate(null, null, null, tracker);
 
-        assertTrue(conditions.checkPreconditions(null));
+        assertTrue(conditions.checkPreconditions(null, null));
 
         conditions.deactivate();
 
-        assertFalse(conditions.checkPreconditions(null));
+        assertFalse(conditions.checkPreconditions(null, null));
     }
 
     private List<ResourceProviderHandler> getResourceProviderHandlers(String[] pids) {
@@ -83,25 +83,63 @@ public class FactoryPreconditionsTest {
         return result;
     }
 
+    private List<ResourceProviderHandler> getResourceProviderHandlersWithNames(String[] names) {
+        final List<ResourceProviderHandler> result = new ArrayList<ResourceProviderHandler>();
+
+        for(final String n : names) {
+            final ResourceProviderHandler handler = Mockito.mock(ResourceProviderHandler.class);
+            final ResourceProviderInfo info = Mockito.mock(ResourceProviderInfo.class);
+            Mockito.when(info.getName()).thenReturn(n);
+            final ServiceReference ref = Mockito.mock(ServiceReference.class);
+
+            Mockito.when(handler.getInfo()).thenReturn(info);
+            Mockito.when(info.getServiceReference()).thenReturn(ref);
+
+            result.add(handler);
+        }
+        return result;
+    }
+
     @Test public void testPIDs() {
         final ResourceProviderTracker tracker = Mockito.mock(ResourceProviderTracker.class);
         final ResourceProviderStorage storage = Mockito.mock(ResourceProviderStorage.class);
         Mockito.when(tracker.getResourceProviderStorage()).thenReturn(storage);
 
         FactoryPreconditions conditions = new FactoryPreconditions();
-        conditions.activate(null, new String[] {"pid1", "pid3"}, tracker);
+        conditions.activate(null, new String[] {"pid1", "pid3"}, null, tracker);
 
         final List<ResourceProviderHandler> handlers1 = getResourceProviderHandlers(new String[] {"pid2"});
         Mockito.when(storage.getAllHandlers()).thenReturn(handlers1);
-        assertFalse(conditions.checkPreconditions(null));
+        assertFalse(conditions.checkPreconditions(null, null));
 
         final List<ResourceProviderHandler> handlers2 = getResourceProviderHandlers(new String[] {"pid1", "pid2", "pid3"});
         Mockito.when(storage.getAllHandlers()).thenReturn(handlers2);
-        assertTrue(conditions.checkPreconditions(null));
+        assertTrue(conditions.checkPreconditions(null, null));
 
         final List<ResourceProviderHandler> handlers3 = getResourceProviderHandlers(new String[] {"pid1"});
         Mockito.when(storage.getAllHandlers()).thenReturn(handlers3);
-        assertFalse(conditions.checkPreconditions(null));
+        assertFalse(conditions.checkPreconditions(null, null));
+    }
+
+    @Test public void testNames() {
+        final ResourceProviderTracker tracker = Mockito.mock(ResourceProviderTracker.class);
+        final ResourceProviderStorage storage = Mockito.mock(ResourceProviderStorage.class);
+        Mockito.when(tracker.getResourceProviderStorage()).thenReturn(storage);
+
+        FactoryPreconditions conditions = new FactoryPreconditions();
+        conditions.activate(null, null, new String[] {"n1", "n2"}, tracker);
+
+        final List<ResourceProviderHandler> handlers1 = getResourceProviderHandlersWithNames(new String[] {"n2"});
+        Mockito.when(storage.getAllHandlers()).thenReturn(handlers1);
+        assertFalse(conditions.checkPreconditions(null, null));
+
+        final List<ResourceProviderHandler> handlers2 = getResourceProviderHandlersWithNames(new String[] {"n1", "n2", "n3"});
+        Mockito.when(storage.getAllHandlers()).thenReturn(handlers2);
+        assertTrue(conditions.checkPreconditions(null, null));
+
+        final List<ResourceProviderHandler> handlers3 = getResourceProviderHandlersWithNames(new String[] {"n1"});
+        Mockito.when(storage.getAllHandlers()).thenReturn(handlers3);
+        assertFalse(conditions.checkPreconditions(null, null));
     }
 
     @Test public void testUnregisteringService() {
@@ -110,14 +148,14 @@ public class FactoryPreconditionsTest {
         Mockito.when(tracker.getResourceProviderStorage()).thenReturn(storage);
 
         FactoryPreconditions conditions = new FactoryPreconditions();
-        conditions.activate(null, new String[] {"pid1", "pid3"}, tracker);
+        conditions.activate(null, new String[] {"pid1", "pid3"}, null, tracker);
 
         final List<ResourceProviderHandler> handlers2 = getResourceProviderHandlers(new String[] {"pid1", "pid2", "pid3"});
         Mockito.when(storage.getAllHandlers()).thenReturn(handlers2);
-        assertTrue(conditions.checkPreconditions(null));
+        assertTrue(conditions.checkPreconditions(null, null));
 
-        assertTrue(conditions.checkPreconditions("pid2"));
+        assertTrue(conditions.checkPreconditions(null, "pid2"));
 
-        assertFalse(conditions.checkPreconditions("pid1"));
+        assertFalse(conditions.checkPreconditions(null, "pid1"));
     }
 }

Modified: sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/providers/ResourceProviderTrackerTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/providers/ResourceProviderTrackerTest.java?rev=1752276&r1=1752275&r2=1752276&view=diff
==============================================================================
--- sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/providers/ResourceProviderTrackerTest.java (original)
+++ sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/providers/ResourceProviderTrackerTest.java Tue Jul 12 11:24:53 2016
@@ -119,7 +119,7 @@ public class ResourceProviderTrackerTest
             }
 
             @Override
-            public void providerRemoved(String pid, boolean stateful, boolean used) {
+            public void providerRemoved(String name, String pid, boolean stateful, boolean used) {
                 removedCalled.set(true);
             }
 
@@ -177,7 +177,7 @@ public class ResourceProviderTrackerTest
             }
 
             @Override
-            public void providerRemoved(String pid, boolean stateful, boolean used) {
+            public void providerRemoved(String name, String pid, boolean stateful, boolean used) {
                 removedCalled.set(true);
             }
 
@@ -291,7 +291,7 @@ public class ResourceProviderTrackerTest
         }
 
         @Override
-        public void providerRemoved(String pid, boolean stateful, boolean used) {
+        public void providerRemoved(String name, String pid, boolean stateful, boolean used) {
             // TODO Auto-generated method stub
 
         }