You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by no...@apache.org on 2010/01/23 21:38:24 UTC

svn commit: r902482 - in /incubator/aries/trunk/jndi/jndi-url/src: main/java/org/apache/aries/jndi/services/ main/java/org/apache/aries/jndi/url/ test/java/org/apache/aries/jndi/url/

Author: not
Date: Sat Jan 23 20:38:24 2010
New Revision: 902482

URL: http://svn.apache.org/viewvc?rev=902482&view=rev
Log:
ARIES-17 Fixed to use getServiceReferences and to correctly respect service ordering

Modified:
    incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/ServiceHelper.java
    incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/ServiceRegistryContext.java
    incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/ServiceRegistryContextTest.java

Modified: incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/ServiceHelper.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/ServiceHelper.java?rev=902482&r1=902481&r2=902482&view=diff
==============================================================================
--- incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/ServiceHelper.java (original)
+++ incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/ServiceHelper.java Sat Jan 23 20:38:24 2010
@@ -18,6 +18,8 @@
  */
 package org.apache.aries.jndi.services;
 
+import java.util.Arrays;
+import java.util.Comparator;
 import java.util.HashSet;
 import java.util.LinkedList;
 import java.util.List;
@@ -146,9 +148,19 @@
     Object service = null;
     try {
       BundleContext callerCtx = getBundleContext();
-      ServiceReference[] refs = callerCtx.getAllServiceReferences(className, filter);
+      ServiceReference[] refs = callerCtx.getServiceReferences(className, filter);
       
       if (refs != null) {
+        // we need to sort the references returned in case they are out of order
+        // we need to sort in the reverse natural order, services with higher 
+        // ranking or lower id should be processed first so should be earlier in the array.
+        Arrays.sort(refs, new Comparator<ServiceReference>() {
+          public int compare(ServiceReference o1, ServiceReference o2)
+          {
+            return o2.compareTo(o1);
+          }
+        });
+        
         for (ServiceReference ref : refs) {
           List<Object> services = getServices(callerCtx, className, filter, ref);
           if (!!!services.isEmpty()) {

Modified: incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/ServiceRegistryContext.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/ServiceRegistryContext.java?rev=902482&r1=902481&r2=902482&view=diff
==============================================================================
--- incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/ServiceRegistryContext.java (original)
+++ incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/ServiceRegistryContext.java Sat Jan 23 20:38:24 2010
@@ -54,7 +54,7 @@
    * 
    * @param environment
    */
-  public ServiceRegistryContext(@SuppressWarnings("unused") Hashtable<?, ?> environment)
+  public ServiceRegistryContext(Hashtable<?, ?> environment)
   {
     env = new HashMap<Object, Object>();
     env.putAll(environment);

Modified: incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/ServiceRegistryContextTest.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/ServiceRegistryContextTest.java?rev=902482&r1=902481&r2=902482&view=diff
==============================================================================
--- incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/ServiceRegistryContextTest.java (original)
+++ incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/ServiceRegistryContextTest.java Sat Jan 23 20:38:24 2010
@@ -21,6 +21,7 @@
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 
 import java.lang.reflect.Field;
@@ -41,6 +42,7 @@
 import org.junit.Test;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceFactory;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
@@ -165,7 +167,7 @@
     
     Skeleton skel = Skeleton.getSkeleton(mock.getBundleContext());
     
-    skel.assertCalled(new MethodCall(BundleContext.class, "getAllServiceReferences", "java.lang.Runnable", null));
+    skel.assertCalled(new MethodCall(BundleContext.class, "getServiceReferences", "java.lang.Runnable", null));
 
     mock = new BundleMock("scooby.doo", new Properties());
     
@@ -185,7 +187,7 @@
     assertEquals("The SR did not return the object we expected", service, s);
 
     skel = Skeleton.getSkeleton(mock.getBundleContext());
-    skel.assertCalled(new MethodCall(BundleContext.class, "getAllServiceReferences", "java.lang.Runnable", null));
+    skel.assertCalled(new MethodCall(BundleContext.class, "getServiceReferences", "java.lang.Runnable", null));
   }
 
   /**
@@ -207,7 +209,7 @@
     
     assertEquals("The SR did not return the object we expected", service, s);
     
-    Skeleton.getSkeleton(bc).assertCalled(new MethodCall(BundleContext.class, "getAllServiceReferences", "java.lang.Runnable", "(rubbish=smelly)"));
+    Skeleton.getSkeleton(bc).assertCalled(new MethodCall(BundleContext.class, "getServiceReferences", "java.lang.Runnable", "(rubbish=smelly)"));
   }
   
   /**
@@ -267,9 +269,42 @@
     checkThreadRetrievedViaListMethod(serviceList);
     
     assertFalse("The repository contained more objects than we expected", serviceList.hasMoreElements());
-    
   }
 
+  @Test
+  public void checkServiceOrderObserved() throws NamingException
+  {
+    InitialContext ctx = new InitialContext();
+    
+    String className = Runnable.class.getName();
+    
+    Thread t = new Thread();
+    Thread t2 = new Thread();
+    
+    // we don't want the default service
+    reg.unregister();
+    
+    ServiceRegistration reg = bc.registerService(className, t, null);
+    ServiceRegistration reg2 = bc.registerService(className, t2, null);
+    
+    Runnable r = (Runnable) ctx.lookup("osgi:services/java.lang.Runnable");
+    
+    assertSame("The wrong runnable was returned", t, r);
+    
+    reg.unregister();
+    reg2.unregister();
+    
+    Hashtable<String, Object> props = new Hashtable<String, Object>();
+    props.put(Constants.SERVICE_RANKING, 55);
+    
+    reg = bc.registerService(className, t, null);
+    reg2 = bc.registerService(className, t2, props);
+    
+    r = (Runnable) ctx.lookup("osgi:services/java.lang.Runnable");
+    
+    assertSame("The wrong runnable was returned", t2, r);
+  }
+  
   /**
    * Check that the NamingEnumeration passed in has another element, which represents a java.lang.Thread
    * @param serviceList