aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r1214849 - in /aries/trunk/spi-fly: spi-fly-core/src/main/java/org/apache/aries/spifly/ spi-fly-dynamic-bundle/src/test/java/org/apache/aries/spifly/dynamic/
Date Thu, 15 Dec 2011 16:41:59 GMT
Author: davidb
Date: Thu Dec 15 16:41:58 2011
New Revision: 1214849

URL: http://svn.apache.org/viewvc?rev=1214849&view=rev
Log:
Add a strategy to obtain bundle classloader via OSGi 4.3 BundleRevision if this API is available.
It uses reflection and will be skipped when run in an 4.2 framework.


Modified:
    aries/trunk/spi-fly/spi-fly-core/src/main/java/org/apache/aries/spifly/Util.java
    aries/trunk/spi-fly/spi-fly-dynamic-bundle/src/test/java/org/apache/aries/spifly/dynamic/ClientWeavingHookGenericCapabilityTest.java
    aries/trunk/spi-fly/spi-fly-dynamic-bundle/src/test/java/org/apache/aries/spifly/dynamic/ClientWeavingHookTest.java

Modified: aries/trunk/spi-fly/spi-fly-core/src/main/java/org/apache/aries/spifly/Util.java
URL: http://svn.apache.org/viewvc/aries/trunk/spi-fly/spi-fly-core/src/main/java/org/apache/aries/spifly/Util.java?rev=1214849&r1=1214848&r2=1214849&view=diff
==============================================================================
--- aries/trunk/spi-fly/spi-fly-core/src/main/java/org/apache/aries/spifly/Util.java (original)
+++ aries/trunk/spi-fly/spi-fly-core/src/main/java/org/apache/aries/spifly/Util.java Thu Dec
15 16:41:58 2011
@@ -19,6 +19,7 @@
 package org.apache.aries.spifly;
 
 import java.io.IOException;
+import java.lang.reflect.Method;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -119,6 +120,15 @@ public class Util {
         // be 4.2 compliant.
         // Here we're just finding any class in the bundle, load that and then use its classloader.
 
+        try {
+            Method adaptMethod = b.getClass().getMethod("adapt", Class.class);
+            if (adaptMethod != null) {
+                return getBundleClassLoaderViaAdapt(b, adaptMethod);
+            }
+        } catch (Exception e) {
+            // No Bundle.adapt(), use the fallback approach to find the bundle classloader
+        }
+
         List<String> rootPaths = new ArrayList<String>();
         rootPaths.add("/");
 
@@ -157,6 +167,27 @@ public class Util {
         throw new RuntimeException("Could not obtain classloader for bundle " + b);
     }
 
+    private static ClassLoader getBundleClassLoaderViaAdapt(Bundle b, Method adaptMethod)
{
+        // This method uses reflection to avoid a hard dependency on OSGi 4.3 APIs
+        try {
+            // Load the BundleRevision and BundleWiring classes from the System Bundle.
+            Bundle systemBundle = b.getBundleContext().getBundle(0);
+
+            Class<?> bundleRevisionClass = systemBundle.loadClass("org.osgi.framework.wiring.BundleRevision");
+            Object bundleRevision = adaptMethod.invoke(b, bundleRevisionClass);
+
+            Method getWiringMethod = bundleRevisionClass.getDeclaredMethod("getWiring");
+            Object bundleWiring = getWiringMethod.invoke(bundleRevision);
+
+            Class<?> bundleWiringClass = systemBundle.loadClass("org.osgi.framework.wiring.BundleWiring");
+            Method getClassLoaderMethod = bundleWiringClass.getDeclaredMethod("getClassLoader");
+
+            return (ClassLoader) getClassLoaderMethod.invoke(bundleWiring);
+        } catch (Exception e) {
+            throw new RuntimeException("Can't obtain Bundle Class Loader for bundle: " +
b, e);
+        }
+    }
+
     private static ClassLoader getClassLoaderViaBundleClassPath(Bundle b, URL url) {
         try {
             JarInputStream jis = null;

Modified: aries/trunk/spi-fly/spi-fly-dynamic-bundle/src/test/java/org/apache/aries/spifly/dynamic/ClientWeavingHookGenericCapabilityTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/spi-fly/spi-fly-dynamic-bundle/src/test/java/org/apache/aries/spifly/dynamic/ClientWeavingHookGenericCapabilityTest.java?rev=1214849&r1=1214848&r2=1214849&view=diff
==============================================================================
--- aries/trunk/spi-fly/spi-fly-dynamic-bundle/src/test/java/org/apache/aries/spifly/dynamic/ClientWeavingHookGenericCapabilityTest.java
(original)
+++ aries/trunk/spi-fly/spi-fly-dynamic-bundle/src/test/java/org/apache/aries/spifly/dynamic/ClientWeavingHookGenericCapabilityTest.java
Thu Dec 15 16:41:58 2011
@@ -551,6 +551,10 @@ public class ClientWeavingHookGenericCap
         for(String className : classNames) {
             classResources.add("/" + className.replace('.', '/') + ".class");
         }
+
+        BundleContext bc = EasyMock.createNiceMock(BundleContext.class);
+        EasyMock.replay(bc);
+
         Bundle providerBundle = EasyMock.createMock(Bundle.class);
         String bsn = subdir;
         int idx = bsn.indexOf('_');
@@ -559,6 +563,7 @@ public class ClientWeavingHookGenericCap
         }
         EasyMock.expect(providerBundle.getSymbolicName()).andReturn(bsn).anyTimes();
         EasyMock.expect(providerBundle.getBundleId()).andReturn(id).anyTimes();
+        EasyMock.expect(providerBundle.getBundleContext()).andReturn(bc).anyTimes();
         EasyMock.expect(providerBundle.getVersion()).andReturn(version).anyTimes();
         EasyMock.expect(providerBundle.getEntryPaths("/")).andAnswer(new IAnswer<Enumeration<String>>()
{
             @Override

Modified: aries/trunk/spi-fly/spi-fly-dynamic-bundle/src/test/java/org/apache/aries/spifly/dynamic/ClientWeavingHookTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/spi-fly/spi-fly-dynamic-bundle/src/test/java/org/apache/aries/spifly/dynamic/ClientWeavingHookTest.java?rev=1214849&r1=1214848&r2=1214849&view=diff
==============================================================================
--- aries/trunk/spi-fly/spi-fly-dynamic-bundle/src/test/java/org/apache/aries/spifly/dynamic/ClientWeavingHookTest.java
(original)
+++ aries/trunk/spi-fly/spi-fly-dynamic-bundle/src/test/java/org/apache/aries/spifly/dynamic/ClientWeavingHookTest.java
Thu Dec 15 16:41:58 2011
@@ -614,6 +614,10 @@ public class ClientWeavingHookTest {
         for(String className : classNames) {
             classResources.add("/" + className.replace('.', '/') + ".class");
         }
+
+        BundleContext bc = EasyMock.createNiceMock(BundleContext.class);
+        EasyMock.replay(bc);
+
         Bundle providerBundle = EasyMock.createMock(Bundle.class);
         String bsn = subdir;
         int idx = bsn.indexOf('_');
@@ -622,6 +626,7 @@ public class ClientWeavingHookTest {
         }
         EasyMock.expect(providerBundle.getSymbolicName()).andReturn(bsn).anyTimes();
         EasyMock.expect(providerBundle.getBundleId()).andReturn(id).anyTimes();
+        EasyMock.expect(providerBundle.getBundleContext()).andReturn(bc).anyTimes();
         EasyMock.expect(providerBundle.getVersion()).andReturn(version).anyTimes();
         EasyMock.expect(providerBundle.getEntryPaths("/")).andAnswer(new IAnswer<Enumeration<String>>()
{
             @Override



Mime
View raw message