incubator-aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ga...@apache.org
Subject svn commit: r965942 - in /incubator/aries/trunk/jndi/jndi-url/src: main/java/org/apache/aries/jndi/services/ServiceHelper.java test/java/org/apache/aries/jndi/url/ServiceRegistryContextTest.java
Date Tue, 20 Jul 2010 18:15:18 GMT
Author: gawor
Date: Tue Jul 20 18:15:18 2010
New Revision: 965942

URL: http://svn.apache.org/viewvc?rev=965942&view=rev
Log:
ARIES-356: Always return raw services when using aries:services/ scheme

Modified:
    incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/ServiceHelper.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=965942&r1=965941&r2=965942&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
Tue Jul 20 18:15:18 2010
@@ -28,6 +28,7 @@ import java.security.AccessController;
 import java.security.PrivilegedAction;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.Comparator;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -208,9 +209,7 @@ public final class ServiceHelper
 
   public static Object getService(BundleContext ctx, OsgiName lookupName, String id,
                                   boolean dynamicRebind, Map<String, Object> env, boolean
requireProxy) throws NamingException
-  {
-    Object result = null;
-
+  {    
     String interfaceName = lookupName.getInterface();
     String filter = lookupName.getFilter();
     String serviceName = lookupName.getServiceName();
@@ -240,20 +239,25 @@ public final class ServiceHelper
       pair = findService(ctx, interfaceName, filter);
     }
 
+    Object result = null;
+    
     if (pair != null) {
-      result = proxy(interfaceName, filter, dynamicRebind, ctx, pair, requireProxy);
+      if (requireProxy) {
+        result = proxy(interfaceName, filter, dynamicRebind, ctx, pair);
+      } else {
+        result = pair.service;
+      }
     }
 
     return result;
   }
 
   private static Object proxy(final String interface1, final String filter, final boolean
rebind,
-                              final BundleContext ctx, final ServicePair pair, final boolean
requireProxy)
+                              final BundleContext ctx, final ServicePair pair)
   {
     Object result = null;
     Bundle owningBundle = ctx.getBundle();
-    ServiceKey k = new ServiceKey(owningBundle, pair.ref.getBundle(), (Long) pair.ref
-        .getProperty(Constants.SERVICE_ID));
+    ServiceKey k = new ServiceKey(owningBundle, pair.ref.getBundle(), (Long) pair.ref.getProperty(Constants.SERVICE_ID));
 
     WeakReference<Object> proxyRef = proxyCache.get(k);
 
@@ -268,7 +272,7 @@ public final class ServiceHelper
       result = AccessController.doPrivileged(new PrivilegedAction<Object>() {
         public Object run()
         {
-          return proxyPriviledged(interface1, filter, rebind, ctx, pair, requireProxy);
+          return proxyPriviledged(interface1, filter, rebind, ctx, pair);
         }
       });
 
@@ -282,7 +286,7 @@ public final class ServiceHelper
     return result;
   }
 
-  private static Object proxyPriviledged(String interface1, String filter, boolean dynamicRebind,
BundleContext ctx, ServicePair pair, boolean requireProxy)
+  private static Object proxyPriviledged(String interface1, String filter, boolean dynamicRebind,
BundleContext ctx, ServicePair pair)
   {
     String[] interfaces = null;
     if (interface1 != null) {
@@ -323,32 +327,10 @@ public final class ServiceHelper
     for (String interfaceName : interfaces) {
       try {
         Class<?> potentialClass = serviceProviderBundle.loadClass(interfaceName);
-        if (Modifier.isFinal(potentialClass.getModifiers())) {
-          if (requireProxy) {
-            continue;
-          }
-          return pair.service;
-        }
-        if (!potentialClass.isInterface()) {
-          if (requireProxy && !proxyFactory.proxiesClasses()) {
-            continue;
-          }
-          try {
-            potentialClass.getConstructor(new Class[0]);
-          } catch (NoSuchMethodException e) {
-            if (requireProxy) {
-              continue;
-            }
-            return pair.service;
-          }
-        }
         if (pair.ref.isAssignableTo(owningBundle, interfaceName)) {
           clazz.add(potentialClass);
         }
       } catch (ClassNotFoundException e) {
-        if (!requireProxy) {
-          return pair.service;
-        }
       }
     }
 
@@ -364,13 +346,11 @@ public final class ServiceHelper
     // on this adapter.
 
     try {
-      return proxyFactory.createProxy(new BundleToClassLoaderAdapter(serviceProviderBundle),
clazz
-          .toArray(new Class<?>[clazz.size()]), ih);
+      return proxyFactory.createProxy(new BundleToClassLoaderAdapter(serviceProviderBundle),
toClassArray(clazz), ih);
     } catch (IllegalArgumentException e) {
-      if (requireProxy) {
-        throw e;
-      }
-      return pair.service;
+      throw e;
+    } catch (RuntimeException e) {
+      throw new IllegalArgumentException("Unable to create proxy for " + pair.ref, e);
     }
   }
 
@@ -453,7 +433,7 @@ public final class ServiceHelper
       pair.ref = ref;
       pair.service = service;
 
-      result = proxy(null, null, false, ctx, pair, true);
+      result = proxy(null, null, false, ctx, pair);
     }
 
     return result;
@@ -473,9 +453,9 @@ public final class ServiceHelper
     return proxyFactory;
   }
 
-  private static Class[] getInterfaces(Class[] classes) {
-    Set<Class> interfaces = new HashSet<Class>();
-    for (Class clazz : classes) {
+  private static Class<?>[] getInterfaces(Class<?>[] classes) {
+    Set<Class<?>> interfaces = new HashSet<Class<?>>();
+    for (Class<?> clazz : classes) {
       if (clazz.isInterface()) {
         interfaces.add(clazz);
       }
@@ -483,7 +463,7 @@ public final class ServiceHelper
     return toClassArray(interfaces);
   }
 
-  private static Class[] toClassArray(Set<Class> classes) {
+  private static Class<?>[] toClassArray(Collection<Class<?>> classes)
{
     return classes.toArray(new Class[classes.size()]);
   }
 
@@ -497,7 +477,7 @@ public final class ServiceHelper
   public static class JdkProxyFactory implements ProxyFactory {
 
     public Object createProxy(final ClassLoader classLoader, final Class[] classes, final
Callable<Object> dispatcher) {
-      return Proxy.newProxyInstance(classLoader, getInterfaces(classes), new InvocationHandler()
{
+      return Proxy.newProxyInstance(classLoader, classes, new InvocationHandler() {
         public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
           try {
             return method.invoke(dispatcher.call(), args);

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=965942&r1=965941&r2=965942&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
Tue Jul 20 18:15:18 2010
@@ -25,6 +25,7 @@ import static org.junit.Assert.assertTru
 import static org.junit.Assert.fail;
 
 import java.lang.reflect.Field;
+import java.lang.reflect.Proxy;
 import java.sql.SQLException;
 import java.util.Hashtable;
 import java.util.Properties;
@@ -129,9 +130,13 @@ public class ServiceRegistryContextTest
      
      Context ctx2 = (Context) ctx.lookup("osgi:service");
      
-     Runnable r = (Runnable) ctx2.lookup("java.lang.Runnable");
+     Runnable r1 = (Runnable) ctx2.lookup("java.lang.Runnable");   
+     assertNotNull(r1);
+     assertTrue("expected proxied service class", r1 != service);
      
-     assertNotNull(r);
+     Runnable r2 = (Runnable) ctx.lookup("aries:services/java.lang.Runnable");
+     assertNotNull(r2);
+     assertTrue("expected non-proxied service class", r2 == service);
   }
   
   /**
@@ -200,7 +205,7 @@ public class ServiceRegistryContextTest
     
     Thread.currentThread().setContextClassLoader(mock.getClassLoader());
     
-    Runnable s = (Runnable) ctx.lookup("aries:services/java.lang.Runnable");
+    Runnable s = (Runnable) ctx.lookup("osgi:service/java.lang.Runnable");
     
     assertNotNull("We didn't get a service back from our lookup :(", s);
     
@@ -253,7 +258,7 @@ public class ServiceRegistryContextTest
 
     InitialContext ctx = new InitialContext();
     
-    Object s = ctx.lookup("aries:services/java.lang.Runnable/(rubbish=smelly)");
+    Object s = ctx.lookup("osgi:service/java.lang.Runnable/(rubbish=smelly)");
     
     assertNotNull("We didn't get a service back from our lookup :(", s);
     
@@ -281,7 +286,7 @@ public class ServiceRegistryContextTest
 
     InitialContext ctx = new InitialContext();
     
-    ctx.lookup("aries:services/java.lang.Runnable");
+    ctx.lookup("osgi:service/java.lang.Runnable");
   }
   
   /**
@@ -299,7 +304,7 @@ public class ServiceRegistryContextTest
 
     InitialContext ctx = new InitialContext();
     
-    ctx.lookup("aries:services/java.lang.Integer");
+    ctx.lookup("osgi:service/java.lang.Integer");
   }
   
   /**
@@ -312,7 +317,7 @@ public class ServiceRegistryContextTest
   {
     InitialContext ctx = new InitialContext();
     
-    NamingEnumeration<NameClassPair> serviceList = ctx.list("aries:services/java.lang.Runnable/(rubbish=smelly)");
+    NamingEnumeration<NameClassPair> serviceList = ctx.list("osgi:service/java.lang.Runnable/(rubbish=smelly)");
     
     checkThreadRetrievedViaListMethod(serviceList);
     
@@ -322,7 +327,7 @@ public class ServiceRegistryContextTest
     
     registerService(new Thread());
     
-    serviceList = ctx.list("aries:services/java.lang.Runnable/(rubbish=smelly)");
+    serviceList = ctx.list("osgi:service/java.lang.Runnable/(rubbish=smelly)");
     
     checkThreadRetrievedViaListMethod(serviceList);
     
@@ -591,7 +596,7 @@ public class ServiceRegistryContextTest
     
     assertEquals("The service retrieved was not of the correct type", "java.lang.Thread",
ncp.getClassName());
     
-    assertEquals("aries:services/java.lang.Runnable/(rubbish=smelly)", ncp.getName().toString());
+    assertEquals("osgi:service/java.lang.Runnable/(rubbish=smelly)", ncp.getName().toString());
   }
   
   /**
@@ -604,7 +609,7 @@ public class ServiceRegistryContextTest
   {
     InitialContext ctx = new InitialContext();
     
-    NamingEnumeration<Binding> serviceList = ctx.listBindings("aries:services/java.lang.Runnable/(rubbish=smelly)");
+    NamingEnumeration<Binding> serviceList = ctx.listBindings("osgi:service/java.lang.Runnable/(rubbish=smelly)");
     
     Object returnedService = checkThreadRetrievedViaListBindingsMethod(serviceList);
     
@@ -616,7 +621,7 @@ public class ServiceRegistryContextTest
     Thread secondService = new Thread();
     registerService(secondService);
     
-    serviceList = ctx.listBindings("aries:services/java.lang.Runnable/(rubbish=smelly)");
+    serviceList = ctx.listBindings("osgi:service/java.lang.Runnable/(rubbish=smelly)");
     
     Object returnedService1 = checkThreadRetrievedViaListBindingsMethod(serviceList);
     
@@ -647,7 +652,7 @@ public class ServiceRegistryContextTest
     
     assertTrue("The service retrieved was not of the correct type", binding.getObject() instanceof
Thread);
     
-    assertEquals("aries:services/java.lang.Runnable/(rubbish=smelly)", binding.getName().toString());
+    assertEquals("osgi:service/java.lang.Runnable/(rubbish=smelly)", binding.getName().toString());
     
     return binding.getObject();
   }



Mime
View raw message