incubator-aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n..@apache.org
Subject svn commit: r1041598 - in /incubator/aries/trunk/jndi/jndi-url: ./ src/main/java/org/apache/aries/jndi/services/ src/main/java/org/apache/aries/jndi/url/ src/test/java/org/apache/aries/jndi/url/
Date Thu, 02 Dec 2010 21:47:07 GMT
Author: not
Date: Thu Dec  2 21:47:07 2010
New Revision: 1041598

URL: http://svn.apache.org/viewvc?rev=1041598&view=rev
Log:
ARIES-468 Update to use the ProxyManager bundle for generating proxies.

Removed:
    incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/CgLibProxyFactory.java
    incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/JdkProxyFactory.java
    incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/services/ProxyFactory.java
Modified:
    incubator/aries/trunk/jndi/jndi-url/pom.xml
    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/Activator.java
    incubator/aries/trunk/jndi/jndi-url/src/test/java/org/apache/aries/jndi/url/ServiceRegistryContextTest.java

Modified: incubator/aries/trunk/jndi/jndi-url/pom.xml
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-url/pom.xml?rev=1041598&r1=1041597&r2=1041598&view=diff
==============================================================================
--- incubator/aries/trunk/jndi/jndi-url/pom.xml (original)
+++ incubator/aries/trunk/jndi/jndi-url/pom.xml Thu Dec  2 21:47:07 2010
@@ -58,16 +58,15 @@
             <artifactId>org.apache.aries.util</artifactId>
         </dependency>
         <dependency>
+            <groupId>org.apache.aries.proxy</groupId>
+            <artifactId>org.apache.aries.proxy.api</artifactId>
+        </dependency>
+        <dependency>
             <groupId>org.osgi</groupId>
             <artifactId>org.osgi.core</artifactId>
             <scope>provided</scope>
         </dependency>
         <dependency>
-            <groupId>org.apache.servicemix.bundles</groupId>
-            <artifactId>org.apache.servicemix.bundles.cglib</artifactId>
-            <optional>true</optional>
-        </dependency>
-        <dependency>
             <groupId>org.apache.aries.jndi</groupId>
             <artifactId>org.apache.aries.jndi.core</artifactId>
             <scope>test</scope>

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=1041598&r1=1041597&r2=1041598&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
Thu Dec  2 21:47:07 2010
@@ -33,7 +33,10 @@ import java.util.concurrent.ConcurrentMa
 
 import javax.naming.NamingException;
 
+import org.apache.aries.jndi.url.Activator;
 import org.apache.aries.jndi.url.OsgiName;
+import org.apache.aries.proxy.ProxyManager;
+import org.apache.aries.proxy.UnableToProxyException;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleEvent;
@@ -194,8 +197,6 @@ public final class ServiceHelper
   private static final ConcurrentMap<ServiceKey, WeakReference<Object>> proxyCache
= new ConcurrentHashMap<ServiceKey, WeakReference<Object>>();
   private static final CacheClearoutListener cacheClearoutListener = new CacheClearoutListener(proxyCache);
 
-  private static ProxyFactory proxyFactory;
-
   public static Object getService(BundleContext ctx, OsgiName lookupName, String id,
                                   boolean dynamicRebind, Map<String, Object> env, boolean
requireProxy) throws NamingException
   {    
@@ -311,7 +312,7 @@ public final class ServiceHelper
     Bundle serviceProviderBundle = pair.ref.getBundle();
     Bundle owningBundle = ctx.getBundle();
 
-    ProxyFactory proxyFactory = getProxyFactory();
+    ProxyManager proxyManager = Activator.getProxyManager();
 
     for (String interfaceName : interfaces) {
       try {
@@ -335,9 +336,9 @@ public final class ServiceHelper
     // on this adapter.
 
     try {
-      return proxyFactory.createProxy(serviceProviderBundle, clazz, ih);
-    } catch (IllegalArgumentException e) {
-      throw e;
+      return proxyManager.createProxy(serviceProviderBundle, clazz, ih);
+    } catch (UnableToProxyException e) {
+      throw new IllegalArgumentException(e);
     } catch (RuntimeException e) {
       throw new IllegalArgumentException("Unable to create proxy for " + pair.ref, e);
     }
@@ -427,19 +428,5 @@ public final class ServiceHelper
 
     return result;
   }
-
-  protected static synchronized ProxyFactory getProxyFactory() {
-    if (proxyFactory == null) {
-      try {
-        // Try load load a cglib class (to make sure it's actually available
-        // then create the cglib factory
-        Class.forName("net.sf.cglib.proxy.Enhancer");
-        proxyFactory = new CgLibProxyFactory();
-      } catch (Throwable t) {
-        proxyFactory = new JdkProxyFactory();
-      }
-    }
-    return proxyFactory;
-  }
  
 }

Modified: incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/Activator.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/Activator.java?rev=1041598&r1=1041597&r2=1041598&view=diff
==============================================================================
--- incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/Activator.java
(original)
+++ incubator/aries/trunk/jndi/jndi-url/src/main/java/org/apache/aries/jndi/url/Activator.java
Thu Dec  2 21:47:07 2010
@@ -22,23 +22,53 @@ import java.util.Hashtable;
 
 import javax.naming.spi.ObjectFactory;
 
+import org.apache.aries.proxy.ProxyManager;
+import org.apache.aries.util.SingleServiceTracker;
+import org.apache.aries.util.SingleServiceTracker.SingleServiceListener;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.jndi.JNDIConstants;
 
-public class Activator implements BundleActivator {
+public class Activator implements BundleActivator, SingleServiceListener 
+{
+    private BundleContext ctx;
+    private volatile ServiceRegistration reg;
+    private static SingleServiceTracker<ProxyManager> proxyManager;
+
+    public void start(BundleContext context) 
+    {
+        ctx = context;
+        proxyManager = new SingleServiceTracker<ProxyManager>(context, ProxyManager.class,
this);
+        proxyManager.open();
+    }
 
-    private ServiceRegistration reg;
+    public void stop(BundleContext context) 
+    {
+        proxyManager.close();
+        if (reg != null) reg.unregister();
+    }
 
-    public void start(BundleContext context) {
-        Hashtable<Object, Object> props = new Hashtable<Object, Object>();
-        props.put(JNDIConstants.JNDI_URLSCHEME, new String[] { "osgi", "aries" });
-        reg = context.registerService(ObjectFactory.class.getName(), new OsgiURLContextServiceFactory(),
props);
+    public void serviceFound() 
+    {
+      Hashtable<Object, Object> props = new Hashtable<Object, Object>();
+      props.put(JNDIConstants.JNDI_URLSCHEME, new String[] { "osgi", "aries" });
+      reg = ctx.registerService(ObjectFactory.class.getName(), new OsgiURLContextServiceFactory(),
props);
     }
 
-    public void stop(BundleContext context) {
-        reg.unregister();
+    public void serviceLost() 
+    {
+      if (reg != null) reg.unregister();
+      reg = null;
     }
 
-}
+    public void serviceReplaced() 
+    {
+      
+    }
+    
+    public static ProxyManager getProxyManager()
+    {
+      return proxyManager == null ? null : proxyManager.getService();
+    }
+}
\ No newline at end of file

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=1041598&r1=1041597&r2=1041598&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
Thu Dec  2 21:47:07 2010
@@ -24,9 +24,15 @@ import static org.junit.Assert.assertNot
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
 import java.sql.SQLException;
+import java.util.Collection;
 import java.util.Hashtable;
+import java.util.Iterator;
 import java.util.Properties;
+import java.util.concurrent.Callable;
 
 import javax.naming.Binding;
 import javax.naming.Context;
@@ -40,7 +46,9 @@ import javax.sql.DataSource;
 
 import org.apache.aries.mocks.BundleContextMock;
 import org.apache.aries.mocks.BundleMock;
+import org.apache.aries.proxy.ProxyManager;
 import org.apache.aries.unittest.mocks.MethodCall;
+import org.apache.aries.unittest.mocks.MethodCallHandler;
 import org.apache.aries.unittest.mocks.Skeleton;
 import org.junit.After;
 import org.junit.Before;
@@ -77,6 +85,7 @@ public class ServiceRegistryContextTest
   public void registerService() throws NamingException, SecurityException, NoSuchFieldException,
IllegalArgumentException, IllegalAccessException 
   {
     bc =  Skeleton.newMock(new BundleContextMock(), BundleContext.class);
+    registerProxyManager();
     new org.apache.aries.jndi.startup.Activator().start(bc);
     new Activator().start(bc);
         
@@ -85,6 +94,43 @@ public class ServiceRegistryContextTest
     registerService(service);
   }
   
+  private void registerProxyManager() 
+  {
+    ProxyManager mgr = Skeleton.newMock(ProxyManager.class);
+    
+    //   public Object createProxy(Bundle clientBundle, Collection<Class<?>>
classes, Callable<Object> dispatcher) throws UnableToProxyException;
+
+    Skeleton.getSkeleton(mgr).registerMethodCallHandler(new MethodCall(ProxyManager.class,
"createProxy", Bundle.class, Collection.class, Callable.class),
+        new MethodCallHandler() 
+        {
+          public Object handle(MethodCall methodCall, Skeleton skeleton) throws Exception

+          {
+            @SuppressWarnings("unchecked")
+            Collection<Class<?>> interfaceClasses = (Collection<Class<?>>)
methodCall.getArguments()[1];
+            Class<?>[] classes = new Class<?>[interfaceClasses.size()];
+            
+            Iterator<Class<?>> it = interfaceClasses.iterator(); 
+            for (int i = 0; it.hasNext(); i++) {
+              classes[i] = it.next();
+            }
+            
+            @SuppressWarnings("unchecked")
+            final Callable<Object> target = (Callable<Object>) methodCall.getArguments()[2];
+            
+            return Proxy.newProxyInstance(this.getClass().getClassLoader(), classes, new
InvocationHandler() 
+            {
+              public Object invoke(Object mock, Method method, Object[] arguments)
+                  throws Throwable 
+              {
+                return method.invoke(target.call(), arguments);
+              }
+            });
+          }
+        });
+    
+    bc.registerService(ProxyManager.class.getName(), mgr, null);
+  }
+
   /**
    * Register a service in our map.
    * 



Mime
View raw message