Search in sources :

Example 46 with BundleWiring

use of org.osgi.framework.wiring.BundleWiring in project logging-log4j2 by apache.

the class ResolverUtil method loadImplementationsInBundle.

private void loadImplementationsInBundle(final Test test, final String packageName) {
    final BundleWiring wiring = FrameworkUtil.getBundle(ResolverUtil.class).adapt(BundleWiring.class);
    final Collection<String> list = wiring.listResources(packageName, "*.class", BundleWiring.LISTRESOURCES_RECURSE);
    for (final String name : list) {
        addIfMatching(test, name);
    }
}
Also used : BundleWiring(org.osgi.framework.wiring.BundleWiring)

Example 47 with BundleWiring

use of org.osgi.framework.wiring.BundleWiring in project aries by apache.

the class WovenProxyGeneratorTest method testWovenClassPlusInterfaces.

/**
 * This test checks that we can add interfaces to classes that don't implement
 * them using dynamic subclassing. This is a little odd, but it came for
 * free with support for proxying abstract classes!
 * @throws Exception
 */
@Test
public void testWovenClassPlusInterfaces() throws Exception {
    Bundle b = mock(Bundle.class);
    BundleWiring wiring = getWiring(weavingLoader);
    when(b.adapt(BundleWiring.class)).thenReturn(wiring);
    Object toCall = new AsmProxyManager().createDelegatingProxy(b, Arrays.asList(getProxyClass(ProxyTestClassAbstract.class), Callable.class), new Callable() {

        public Object call() throws Exception {
            return weavingLoader.loadClass(ProxyTestClassChildOfAbstract.class.getName()).newInstance();
        }
    }, null);
    // Should proxy the abstract method on the class
    Method m = getProxyClass(ProxyTestClassAbstract.class).getMethod("getMessage");
    assertEquals("Working", m.invoke(toCall));
    // Should be a callable too!
    assertEquals("Callable Works too!", ((Callable) toCall).call());
}
Also used : AsmProxyManager(org.apache.aries.proxy.impl.AsmProxyManager) Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) Method(java.lang.reflect.Method) Callable(java.util.concurrent.Callable) FinalModifierException(org.apache.aries.proxy.FinalModifierException) UnableToProxyException(org.apache.aries.proxy.UnableToProxyException) Test(org.junit.Test)

Example 48 with BundleWiring

use of org.osgi.framework.wiring.BundleWiring in project aries by apache.

the class ProxyWeavingHook method weave.

public final void weave(WovenClass wovenClass) {
    BundleWiring bw = wovenClass.getBundleWiring();
    if (bw != null) {
        Bundle b = bw.getBundle();
        if (b.getBundleId() == 0 || b.getSymbolicName().startsWith("org.apache.aries.proxy") || b.getSymbolicName().startsWith("org.apache.aries.util")) {
            return;
        }
    }
    if (!isEnabled(wovenClass.getClassName()) || isDisabled(wovenClass.getClassName())) {
        return;
    }
    if (shouldWeave(wovenClass)) {
        byte[] bytes = null;
        try {
            bytes = WovenProxyGenerator.getWovenProxy(wovenClass.getBytes(), wovenClass.getBundleWiring().getClassLoader());
        } catch (Exception e) {
            if (e instanceof RuntimeException && e.getCause() instanceof UnableToProxyException) {
                // This is a weaving failure that should be logged, but the class
                // can still be loaded
                LOGGER.trace(String.format("The class %s cannot be woven, it may not be possible for the runtime to proxy this class.", wovenClass.getClassName()), e);
            } else {
                throw weavingException(wovenClass, e);
            }
        }
        if (bytes != null && bytes.length != 0) {
            wovenClass.setBytes(bytes);
            List<String> imports = wovenClass.getDynamicImports();
            imports.add(IMPORT_A);
            imports.add(IMPORT_B);
        }
    }
}
Also used : Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) UnableToProxyException(org.apache.aries.proxy.UnableToProxyException) WeavingException(org.osgi.framework.hooks.weaving.WeavingException) UnableToProxyException(org.apache.aries.proxy.UnableToProxyException)

Example 49 with BundleWiring

use of org.osgi.framework.wiring.BundleWiring in project aries by apache.

the class InterfaceProxyingTest method testNoStaleProxiesForRefreshedBundle.

@Test
public void testNoStaleProxiesForRefreshedBundle() throws Exception {
    Bundle bundle = mock(Bundle.class);
    TestClassLoader loader = new TestClassLoader();
    when(bundle.getLastModified()).thenReturn(10l);
    BundleWiring wiring = AbstractProxyTest.getWiring(loader);
    when(bundle.adapt(BundleWiring.class)).thenReturn(wiring);
    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();
    when(bundle.getLastModified()).thenReturn(20l);
    // let's change the returned revision
    BundleWiring wiring2 = AbstractProxyTest.getWiring(loaderToo);
    when(bundle.adapt(BundleWiring.class)).thenReturn(wiring2);
    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();
    // 
    assertTrue("parents should be different, as the are the classloaders of different bundle revisions", parent1 != parent2);
}
Also used : Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) Test(org.junit.Test)

Example 50 with BundleWiring

use of org.osgi.framework.wiring.BundleWiring in project aries by apache.

the class BundleWiringStateMBeanTest method testRevisionsWiring.

@Test
public void testRevisionsWiring() throws Exception {
    TabularData jmxWiringTable = brsMBean.getRevisionsWiring(bundleA.getBundleId(), BundleRevision.PACKAGE_NAMESPACE);
    Assert.assertEquals(1, jmxWiringTable.size());
    CompositeData jmxWiring = (CompositeData) jmxWiringTable.values().iterator().next();
    Assert.assertEquals(BundleWiringStateMBean.BUNDLE_WIRING_TYPE, jmxWiring.getCompositeType());
    Assert.assertEquals(bundleA.getBundleId(), jmxWiring.get(BundleWiringStateMBean.BUNDLE_ID));
    BundleWiring bw = (BundleWiring) bundleA.adapt(BundleWiring.class);
    assertBundleWiring(bw, jmxWiring);
}
Also used : CompositeData(javax.management.openmbean.CompositeData) BundleWiring(org.osgi.framework.wiring.BundleWiring) TabularData(javax.management.openmbean.TabularData) Test(org.junit.Test) AbstractIntegrationTest(org.apache.aries.jmx.AbstractIntegrationTest)

Aggregations

BundleWiring (org.osgi.framework.wiring.BundleWiring)110 Bundle (org.osgi.framework.Bundle)61 BundleWire (org.osgi.framework.wiring.BundleWire)39 ArrayList (java.util.ArrayList)23 BundleRevision (org.osgi.framework.wiring.BundleRevision)23 BundleCapability (org.osgi.framework.wiring.BundleCapability)19 Test (org.junit.Test)15 HashMap (java.util.HashMap)13 Hashtable (java.util.Hashtable)12 List (java.util.List)12 BundleContext (org.osgi.framework.BundleContext)12 URL (java.net.URL)10 Dictionary (java.util.Dictionary)10 HashSet (java.util.HashSet)9 LinkedHashMap (java.util.LinkedHashMap)8 Version (org.osgi.framework.Version)7 BundleRevisions (org.osgi.framework.wiring.BundleRevisions)7 AbstractIntegrationTest (org.apache.aries.jmx.AbstractIntegrationTest)6 IAnswer (org.easymock.IAnswer)6 ServiceReference (org.osgi.framework.ServiceReference)6