incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1565659 - in /sling/trunk/bundles/scripting/core/src: main/java/org/apache/sling/scripting/core/impl/ test/java/org/apache/sling/scripting/core/impl/
Date Fri, 07 Feb 2014 14:00:33 GMT
Author: bdelacretaz
Date: Fri Feb  7 14:00:32 2014
New Revision: 1565659

URL: http://svn.apache.org/r1565659
Log:
SLING-3320 - sort BindingsValuesProviders by service ranking. Patch contributed by Radu Cotescu,
thanks!

Modified:
    sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/BindingsValuesProvidersByContextImpl.java
    sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ContextBvpCollector.java
    sling/trunk/bundles/scripting/core/src/test/java/org/apache/sling/scripting/core/impl/BindingsValuesProvidersByContextIT.java

Modified: sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/BindingsValuesProvidersByContextImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/BindingsValuesProvidersByContextImpl.java?rev=1565659&r1=1565658&r2=1565659&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/BindingsValuesProvidersByContextImpl.java
(original)
+++ sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/BindingsValuesProvidersByContextImpl.java
Fri Feb  7 14:00:32 2014
@@ -145,7 +145,7 @@ public class BindingsValuesProvidersByCo
         if (factoryProps != null) {
             String[] compatibleLangs = PropertiesUtil.toStringArray(factoryProps.get("compatible.javax.script.name"),
new String[0]);
             for (final String name : compatibleLangs) {
-                final Map<Object, BindingsValuesProvider> langProviders = bvpc.getLangBindingsValuesProviders().get(name);
+                final Map<ServiceReference, BindingsValuesProvider> langProviders =
bvpc.getLangBindingsValuesProviders().get(name);
                 if (langProviders != null) {
                     results.addAll(langProviders.values());
                 }
@@ -154,7 +154,7 @@ public class BindingsValuesProvidersByCo
         }
 
         for (final String name : scriptEngineFactory.getNames()) {
-            final Map<Object, BindingsValuesProvider> langProviders = bvpc.getLangBindingsValuesProviders().get(name);
+            final Map<ServiceReference, BindingsValuesProvider> langProviders = bvpc.getLangBindingsValuesProviders().get(name);
             if (langProviders != null) {
                 results.addAll(langProviders.values());
             }
@@ -196,7 +196,7 @@ public class BindingsValuesProvidersByCo
         new ContextLoop() {
             @Override
             protected void applyInContext(ContextBvpCollector c) {
-                c.modifiedService(reference, service);
+                c.modifiedService(reference);
                 if (eventAdmin != null) {
                     eventAdmin.postEvent(newEvent(TOPIC_MODIFIED, reference));
                 }
@@ -214,7 +214,7 @@ public class BindingsValuesProvidersByCo
         new ContextLoop() {
             @Override
             protected void applyInContext(ContextBvpCollector c) {
-                c.removedService(reference, service);
+                c.removedService(reference);
                 if (eventAdmin != null) {
                     eventAdmin.postEvent(newEvent(TOPIC_REMOVED, reference));
                 }

Modified: sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ContextBvpCollector.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ContextBvpCollector.java?rev=1565659&r1=1565658&r2=1565659&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ContextBvpCollector.java
(original)
+++ sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ContextBvpCollector.java
Fri Feb  7 14:00:32 2014
@@ -20,6 +20,7 @@ import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentSkipListMap;
 
 import javax.script.Bindings;
 import javax.script.ScriptEngine;
@@ -41,67 +42,66 @@ class ContextBvpCollector {
     /**
      * The BindingsValuesProvider impls which apply to all languages. Keys are serviceIds.
      */
-    private final Map<Object, BindingsValuesProvider> genericBindingsValuesProviders;
+    private final Map<ServiceReference, BindingsValuesProvider> genericBindingsValuesProviders;
 
     /**
      * The BindingsValuesProvider impls which apply to a specific language.
      */
-    private final Map<String, Map<Object, BindingsValuesProvider>> langBindingsValuesProviders;
+    private final Map<String, Map<ServiceReference, BindingsValuesProvider>>
langBindingsValuesProviders;
     
     ContextBvpCollector(BundleContext bc) {
         bundleContext = bc;
-        genericBindingsValuesProviders = new ConcurrentHashMap<Object, BindingsValuesProvider>();
-        langBindingsValuesProviders = new ConcurrentHashMap<String, Map<Object, BindingsValuesProvider>>();
+        genericBindingsValuesProviders = new ConcurrentSkipListMap<ServiceReference, BindingsValuesProvider>();
+        langBindingsValuesProviders = new ConcurrentHashMap<String, Map<ServiceReference,
BindingsValuesProvider>>();
     }
 
     @SuppressWarnings("unchecked")
     public Object addingService(final ServiceReference ref) {
         final String[] engineNames = PropertiesUtil
                 .toStringArray(ref.getProperty(ScriptEngine.NAME), new String[0]);
-        final Object serviceId = ref.getProperty(Constants.SERVICE_ID);
         Object service = bundleContext.getService(ref);
         if (service != null) {
             if (service instanceof Map) {
                 service = new MapWrappingBindingsValuesProvider((Map<String, Object>)
service);
             }
             if (engineNames.length == 0) {
-                genericBindingsValuesProviders.put(serviceId, (BindingsValuesProvider) service);
+                genericBindingsValuesProviders.put(ref, (BindingsValuesProvider) service);
             } else if (engineNames.length == 1 && ANY_ENGINE.contains(engineNames[0].toUpperCase()))
{
-                genericBindingsValuesProviders.put(serviceId, (BindingsValuesProvider) service);
+                genericBindingsValuesProviders.put(ref, (BindingsValuesProvider) service);
             } else {
                 for (String engineName : engineNames) {
-                    Map<Object, BindingsValuesProvider> langProviders = langBindingsValuesProviders.get(engineName);
+                    Map<ServiceReference, BindingsValuesProvider> langProviders = langBindingsValuesProviders.get(engineName);
                     if (langProviders == null) {
-                        langProviders = new ConcurrentHashMap<Object, BindingsValuesProvider>();
+                        langProviders = new ConcurrentSkipListMap<ServiceReference, BindingsValuesProvider>();
                         langBindingsValuesProviders.put(engineName, langProviders);
                     }
 
-                    langProviders.put(serviceId, (BindingsValuesProvider) service);
+                    langProviders.put(ref, (BindingsValuesProvider) service);
                 }
             }
         }
         return service;
     }
 
-    public void modifiedService(final ServiceReference ref, final Object service) {
-        removedService(ref, service);
+    public void modifiedService(final ServiceReference ref) {
+        removedService(ref);
         addingService(ref);
     }
 
-    public void removedService(final ServiceReference ref, final Object service) {
+    public void removedService(final ServiceReference ref) {
         Object serviceId = ref.getProperty(Constants.SERVICE_ID);
         if (genericBindingsValuesProviders.remove(serviceId) == null) {
-            for (Map<Object, BindingsValuesProvider> coll : langBindingsValuesProviders.values())
{
-                coll.remove(serviceId);
+            for (Map<ServiceReference, BindingsValuesProvider> coll : langBindingsValuesProviders.values())
{
+                coll.remove(ref);
             }
         }
     }
     
-    Map<Object, BindingsValuesProvider> getGenericBindingsValuesProviders() {
+    Map<ServiceReference, BindingsValuesProvider> getGenericBindingsValuesProviders()
{
         return genericBindingsValuesProviders;
     }
 
-    Map<String, Map<Object, BindingsValuesProvider>> getLangBindingsValuesProviders()
{
+    Map<String, Map<ServiceReference, BindingsValuesProvider>> getLangBindingsValuesProviders()
{
         return langBindingsValuesProviders;
     }
 

Modified: sling/trunk/bundles/scripting/core/src/test/java/org/apache/sling/scripting/core/impl/BindingsValuesProvidersByContextIT.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/core/src/test/java/org/apache/sling/scripting/core/impl/BindingsValuesProvidersByContextIT.java?rev=1565659&r1=1565658&r2=1565659&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/core/src/test/java/org/apache/sling/scripting/core/impl/BindingsValuesProvidersByContextIT.java
(original)
+++ sling/trunk/bundles/scripting/core/src/test/java/org/apache/sling/scripting/core/impl/BindingsValuesProvidersByContextIT.java
Fri Feb  7 14:00:32 2014
@@ -52,6 +52,7 @@ import org.junit.runner.RunWith;
 import org.ops4j.pax.exam.Option;
 import org.ops4j.pax.exam.junit.PaxExam;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceRegistration;
 
 @RunWith(PaxExam.class)
@@ -133,6 +134,21 @@ public class BindingsValuesProvidersByCo
         regs.add(bundleContext.registerService(BindingsValuesProvider.class.getName(), bvp,
getProperties(context, engineName)));
     }
     
+    private void addBVPWithServiceRanking(final String id, String context, String engineName,
int serviceRanking) {
+        final BindingsValuesProvider bvp = new BindingsValuesProvider() {
+            @Override
+            public String toString() {
+                return id;
+            }
+            
+            public void addBindings(Bindings b) {
+            }
+        };
+        final Dictionary<String, Object> properties = getProperties(context, engineName);
+        properties.put(Constants.SERVICE_RANKING, serviceRanking);
+        regs.add(bundleContext.registerService(BindingsValuesProvider.class.getName(), bvp,
properties));
+    }
+    
     private void addMap(final String id, String context, String engineName) {
         final Map<String, Object> result = new HashMap<String, Object>() {
             private static final long serialVersionUID = 1L;
@@ -281,4 +297,31 @@ public class BindingsValuesProvidersByCo
         final String unsorted = asString(bvpProvider.getBindingsValuesProviders(factory("python"),
"python"), false);
         assertTrue("Expecting python language-specific BVP at the end", unsorted.endsWith("M_python"));
     }
+    
+    @Test
+    public void testBVPsWithServiceRankingA() {
+        addBVPWithServiceRanking("last", null, "js", Integer.MAX_VALUE);
+        addBVPWithServiceRanking("second", null, "js", 0);
+        addBVPWithServiceRanking("first", null, "js", Integer.MIN_VALUE);
+        assertEquals("first,second,last", asString(bvpProvider.getBindingsValuesProviders(factory("js"),
null), false));
+    }
+    
+    @Test
+    public void testBVPsWithServiceRankingB() {
+        addBVPWithServiceRanking("first", null, "js", Integer.MIN_VALUE);
+        addBVPWithServiceRanking("second", null, "js", 0);
+        addBVPWithServiceRanking("last", null, "js", Integer.MAX_VALUE);
+        assertEquals("first,second,last", asString(bvpProvider.getBindingsValuesProviders(factory("js"),
null), false));
+    }
+    
+    @Test
+    public void testBVPsWithServiceRankingC() {
+        addBVPWithServiceRanking("second", "request", "js", 0);
+        addBVPWithServiceRanking("first", "request", "js", Integer.MIN_VALUE);
+        addBVPWithServiceRanking("genericThree", "request", null, 42);
+        addBVPWithServiceRanking("genericTwo", "request", null, 0);
+        addBVPWithServiceRanking("last", "request", "js", Integer.MAX_VALUE);
+        addBVPWithServiceRanking("genericOne", "request", null, -42);
+        assertEquals("genericOne,genericTwo,genericThree,first,second,last", asString(bvpProvider.getBindingsValuesProviders(factory("js"),
"request"), false));
+    }
 }
\ No newline at end of file



Mime
View raw message