Author: gnodet
Date: Thu Oct 30 11:23:37 2014
New Revision: 1635466

URL: http://svn.apache.org/r1635466
Log:
[ARIES-1161] Use BundleWiring as the key for proxy classloaders

Modified:
    
aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/interfaces/InterfaceProxyGenerator.java
    
aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/InterfaceProxyingTest.java
    
aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/WovenProxyGeneratorTest.java
    
aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/WovenProxyPlusSubclassGeneratorTest.java

Modified: 
aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/interfaces/InterfaceProxyGenerator.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/interfaces/InterfaceProxyGenerator.java?rev=1635466&r1=1635465&r2=1635466&view=diff
==============================================================================
--- 
aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/interfaces/InterfaceProxyGenerator.java
 (original)
+++ 
aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/interfaces/InterfaceProxyGenerator.java
 Thu Oct 30 11:23:37 2014
@@ -36,6 +36,7 @@ import org.apache.aries.proxy.UnableToPr
 import org.objectweb.asm.ClassVisitor;
 import org.objectweb.asm.Opcodes;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.wiring.BundleWiring;
 
 /**
  * This class is used to aggregate several interfaces into a real class which 
implements all of them
@@ -51,8 +52,8 @@ public final class InterfaceProxyGenerat
     
   }
 
-  private static final Map<Bundle, WeakReference<ProxyClassLoader>> cache = 
-            new WeakHashMap<Bundle, WeakReference<ProxyClassLoader>>(128);
+  private static final Map<BundleWiring, WeakReference<ProxyClassLoader>> 
cache =
+            new WeakHashMap<BundleWiring, 
WeakReference<ProxyClassLoader>>(128);
   
   /**
    * Generate a new proxy instance implementing the supplied interfaces and 
using the supplied
@@ -65,7 +66,7 @@ public final class InterfaceProxyGenerat
    * @return
    * @throws UnableToProxyException
    */
-  public static final Object getProxyInstance(Bundle client, Class<?> 
superclass,
+  public static Object getProxyInstance(Bundle client, Class<?> superclass,
       Collection<Class<?>> ifaces, Callable<Object> dispatcher, 
InvocationListener listener) throws UnableToProxyException{
     
     if(superclass != null && (superclass.getModifiers() & Modifier.FINAL) != 0)
@@ -76,24 +77,25 @@ public final class InterfaceProxyGenerat
     SortedSet<Class<?>> interfaces = createSet(ifaces);
     
     synchronized (cache) {
-      WeakReference<ProxyClassLoader> ref = cache.get(client);
+      BundleWiring wiring = client == null ? null : 
(BundleWiring)client.adapt(BundleWiring.class);
+      WeakReference<ProxyClassLoader> ref = cache.get(wiring);
       
       if(ref != null)
         pcl = ref.get();
       
       if (pcl != null && pcl.isInvalid(interfaces)) {
           pcl = null;
-          cache.remove(client);
+          cache.remove(wiring);
       }
       
       if(pcl == null) {
         pcl = new ProxyClassLoader(client);
-        cache.put(client, new WeakReference<ProxyClassLoader>(pcl));
+        cache.put(wiring, new WeakReference<ProxyClassLoader>(pcl));
       }
     }
-    
+
     Class<?> c = pcl.createProxyClass(superclass, interfaces);
-        
+
     try {
       Constructor<?> con = c.getDeclaredConstructor(Callable.class, 
InvocationListener.class);
       con.setAccessible(true);

Modified: 
aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/InterfaceProxyingTest.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/InterfaceProxyingTest.java?rev=1635466&r1=1635465&r2=1635466&view=diff
==============================================================================
--- 
aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/InterfaceProxyingTest.java
 (original)
+++ 
aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/InterfaceProxyingTest.java
 Thu Oct 30 11:23:37 2014
@@ -27,10 +27,12 @@ import static org.junit.Assert.fail;
 
 import java.io.ByteArrayOutputStream;
 import java.io.Closeable;
+import java.io.File;
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Dictionary;
 import java.util.Hashtable;
 import java.util.List;
 import java.util.Map;
@@ -45,6 +47,9 @@ import org.apache.aries.util.ClassLoader
 import org.junit.Before;
 import org.junit.Test;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.wiring.BundleRevision;
+import org.osgi.framework.wiring.BundleWiring;
 
 
 public class InterfaceProxyingTest {
@@ -67,12 +72,34 @@ public class InterfaceProxyingTest {
       list = o;
     }
   }
-  
+
   private Bundle testBundle;
-  
+
+  /**
+   * Extended BundleMock which handles update() and adapt() methods
+   */
+  public static class BundleMockEx extends BundleMock {
+    private BundleWiring currentWiring = Skeleton.newMock(BundleWiring.class);
+
+    public BundleMockEx(String name, Dictionary<?, ?> properties) {
+      super(name, properties);
+    }
+
+    public <A> A adapt(Class<A> type) {
+      if (type == BundleWiring.class) {
+        return (A) currentWiring;
+      }
+      return null;
+    }
+
+    public void update() throws BundleException {
+      this.currentWiring = Skeleton.newMock(BundleWiring.class);
+    }
+  }
+
   @Before
   public void setup() {
-    testBundle = Skeleton.newMock(new BundleMock("test", 
+    testBundle = Skeleton.newMock(new BundleMockEx("test",
         new Hashtable<Object, Object>()), Bundle.class);
   }
   
@@ -249,25 +276,35 @@ public class InterfaceProxyingTest {
       TestClassLoader loader = new TestClassLoader();
       skel.setReturnValue(new MethodCall(ClassLoaderProxy.class, 
"getClassLoader"), loader);
       skel.setReturnValue(new MethodCall(Bundle.class, "getLastModified"), 
10l);
-      
+      skel.setReturnValue(new MethodCall(Bundle.class, "adapt", 
BundleWiring.class), Skeleton.newMock(BundleWiring.class));
+
       Class<?> clazz = 
loader.loadClass("org.apache.aries.blueprint.proxy.TestInterface");
       
       Object proxy = InterfaceProxyGenerator.getProxyInstance(bundle, null, 
Arrays.<Class<?>>asList(clazz), constantly(null), null);
       assertTrue(clazz.isInstance(proxy));
+
+      ClassLoader parent1 = proxy.getClass().getClassLoader().getParent();
       
       /* Now again but with a changed classloader as if the bundle had 
refreshed */
       
       TestClassLoader loaderToo = new TestClassLoader();
       skel.setReturnValue(new MethodCall(ClassLoaderProxy.class, 
"getClassLoader"), loaderToo);
       skel.setReturnValue(new MethodCall(Bundle.class, "getLastModified"), 
20l);
+
+      // let's change the returned revision
+      skel.setReturnValue(new MethodCall(Bundle.class, "adapt", 
BundleWiring.class), Skeleton.newMock(BundleWiring.class));
       
       Class<?> clazzToo = 
loaderToo.loadClass("org.apache.aries.blueprint.proxy.TestInterface");
       
       Object proxyToo = InterfaceProxyGenerator.getProxyInstance(bundle, null, 
Arrays.<Class<?>>asList(clazzToo), constantly(null), null);
       assertTrue(clazzToo.isInstance(proxyToo));
+
+      ClassLoader parent2= proxyToo.getClass().getClassLoader().getParent();
+
+      // parents should be different, as the are the classloaders of different 
bundle revisions
+      assertTrue(parent1 != parent2);
   }
-  
-  
+
   protected void assertCalled(TestListener listener, boolean pre, boolean 
post, boolean ex) {
     assertEquals(pre, listener.preInvoke);
     assertEquals(post, listener.postInvoke);

Modified: 
aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/WovenProxyGeneratorTest.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/WovenProxyGeneratorTest.java?rev=1635466&r1=1635465&r2=1635466&view=diff
==============================================================================
--- 
aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/WovenProxyGeneratorTest.java
 (original)
+++ 
aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/WovenProxyGeneratorTest.java
 Thu Oct 30 11:23:37 2014
@@ -55,6 +55,7 @@ import org.apache.aries.util.ClassLoader
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.wiring.BundleWiring;
 
 
 public class WovenProxyGeneratorTest extends AbstractProxyTest
@@ -496,10 +497,13 @@ public class WovenProxyGeneratorTest ext
   @Test
   public void testWovenClassPlusInterfaces() throws Exception {
     Bundle b = (Bundle) Skeleton.newMock(new Class<?>[] {Bundle.class, 
ClassLoaderProxy.class});
-    
+    BundleWiring bw = (BundleWiring) Skeleton.newMock(BundleWiring.class);
+
     Skeleton.getSkeleton(b).setReturnValue(new MethodCall(
         ClassLoaderProxy.class, "getClassLoader"), weavingLoader);
-    
+    Skeleton.getSkeleton(b).setReturnValue(new MethodCall(
+        ClassLoaderProxy.class, "adapt", BundleWiring.class), bw);
+
     Object toCall = new AsmProxyManager().createDelegatingProxy(b, 
Arrays.asList(
         getProxyClass(ProxyTestClassAbstract.class), Callable.class), new 
Callable() {
 

Modified: 
aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/WovenProxyPlusSubclassGeneratorTest.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/WovenProxyPlusSubclassGeneratorTest.java?rev=1635466&r1=1635465&r2=1635466&view=diff
==============================================================================
--- 
aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/WovenProxyPlusSubclassGeneratorTest.java
 (original)
+++ 
aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/WovenProxyPlusSubclassGeneratorTest.java
 Thu Oct 30 11:23:37 2014
@@ -49,6 +49,7 @@ import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.wiring.BundleWiring;
 
 /**
  * This class uses the {@link ProxySubclassGenerator} to test
@@ -64,13 +65,17 @@ public class WovenProxyPlusSubclassGener
   private Callable<Object> testCallable = null;
   
   private static Bundle testBundle;
-  
+  private static BundleWiring testBundleWiring;
+
   @BeforeClass
   public static void createTestBundle() {
          testBundle = (Bundle) Skeleton.newMock(new Class<?>[] {Bundle.class, 
ClassLoaderProxy.class});
-           
+         testBundleWiring = (BundleWiring) 
Skeleton.newMock(BundleWiring.class);
+
            Skeleton.getSkeleton(testBundle).setReturnValue(new MethodCall(
                ClassLoaderProxy.class, "getClassLoader"), weavingLoader);
+           Skeleton.getSkeleton(testBundle).setReturnValue(new MethodCall(
+               ClassLoaderProxy.class, "adapt", BundleWiring.class), 
testBundleWiring);
   }
 
   //Avoid running four weaving tests that don't apply to us


Reply via email to