Search in sources :

Example 16 with BundleWiring

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

the class BaseActivator method getAllHeaders.

private List<String> getAllHeaders(String headerName, Bundle bundle) {
    List<Bundle> bundlesFragments = new ArrayList<Bundle>();
    bundlesFragments.add(bundle);
    BundleRevision rev = bundle.adapt(BundleRevision.class);
    if (rev != null) {
        BundleWiring wiring = rev.getWiring();
        if (wiring != null) {
            for (BundleWire wire : wiring.getProvidedWires("osgi.wiring.host")) {
                bundlesFragments.add(wire.getRequirement().getRevision().getBundle());
            }
        }
    }
    List<String> l = new ArrayList<String>();
    for (Bundle bf : bundlesFragments) {
        String header = bf.getHeaders().get(headerName);
        if (header != null) {
            l.add(header);
        }
    }
    return l;
}
Also used : Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleWire(org.osgi.framework.wiring.BundleWire)

Example 17 with BundleWiring

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

the class ClientWeavingHookGenericCapabilityTest method testHeadersFromFragment.

@Test
public void testHeadersFromFragment() throws Exception {
    // Register the bundle that provides the SPI implementation.
    Bundle providerBundle = mockProviderBundle("impl1", 1);
    activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle, new HashMap<String, Object>());
    Dictionary<String, String> fragmentConsumerHeaders = new Hashtable<String, String>();
    fragmentConsumerHeaders.put(SpiFlyConstants.REQUIRE_CAPABILITY, SpiFlyConstants.CLIENT_REQUIREMENT);
    Bundle fragment = EasyMock.createMock(Bundle.class);
    EasyMock.expect(fragment.getHeaders()).andReturn(fragmentConsumerHeaders).anyTimes();
    EasyMock.replay(fragment);
    BundleRevision frev = EasyMock.createMock(BundleRevision.class);
    EasyMock.expect(frev.getBundle()).andReturn(fragment).anyTimes();
    EasyMock.replay(frev);
    BundleRequirement req = EasyMock.createMock(BundleRequirement.class);
    EasyMock.expect(req.getRevision()).andReturn(frev).anyTimes();
    EasyMock.replay(req);
    BundleWire wire = EasyMock.createMock(BundleWire.class);
    EasyMock.expect(wire.getRequirement()).andReturn(req).anyTimes();
    EasyMock.replay(wire);
    List<BundleWire> wires = Collections.singletonList(wire);
    BundleWiring wiring = EasyMock.createMock(BundleWiring.class);
    EasyMock.expect(wiring.getProvidedWires("osgi.wiring.host")).andReturn(wires).anyTimes();
    EasyMock.replay(wiring);
    BundleRevision rev = EasyMock.createMock(BundleRevision.class);
    EasyMock.expect(rev.getWiring()).andReturn(wiring).anyTimes();
    EasyMock.replay(rev);
    Bundle consumerBundle = mockConsumerBundle(new Hashtable<String, String>(), rev, providerBundle);
    activator.addConsumerWeavingData(consumerBundle, SpiFlyConstants.REQUIRE_CAPABILITY);
    Bundle spiFlyBundle = mockSpiFlyBundle("spifly", Version.parseVersion("1.9.4"), consumerBundle, providerBundle);
    WeavingHook wh = new ClientWeavingHook(spiFlyBundle.getBundleContext(), activator);
    // Weave the TestClient class.
    URL clsUrl = getClass().getResource("TestClient.class");
    Assert.assertNotNull("Precondition", clsUrl);
    String clientClassName = "org.apache.aries.spifly.dynamic.TestClient";
    WovenClass wc = new MyWovenClass(clsUrl, clientClassName, consumerBundle);
    Assert.assertEquals("Precondition", 0, wc.getDynamicImports().size());
    wh.weave(wc);
    Assert.assertEquals(1, wc.getDynamicImports().size());
    String di1 = "org.apache.aries.spifly;bundle-symbolic-name=spifly;bundle-version=1.9.4";
    String di2 = "org.apache.aries.spifly;bundle-version=1.9.4;bundle-symbolic-name=spifly";
    String di = wc.getDynamicImports().get(0);
    Assert.assertTrue("Weaving should have added a dynamic import", di1.equals(di) || di2.equals(di));
    // Invoke the woven class and check that it properly sets the TCCL so that the
    // META-INF/services/org.apache.aries.mytest.MySPI file from impl1 is visible.
    Class<?> cls = wc.getDefinedClass();
    Method method = cls.getMethod("test", new Class[] { String.class });
    Object result = method.invoke(cls.newInstance(), "hello");
    Assert.assertEquals(Collections.singleton("olleh"), result);
}
Also used : Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) BundleWiring(org.osgi.framework.wiring.BundleWiring) WovenClass(org.osgi.framework.hooks.weaving.WovenClass) Method(java.lang.reflect.Method) BundleWire(org.osgi.framework.wiring.BundleWire) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) URL(java.net.URL) BundleRevision(org.osgi.framework.wiring.BundleRevision) WeavingHook(org.osgi.framework.hooks.weaving.WeavingHook) Test(org.junit.Test)

Example 18 with BundleWiring

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

the class BundleWiringState method getRevisionWiring.

private CompositeData getRevisionWiring(BundleRevision revision, int revisionID, String namespace, Map<BundleRevision, Integer> revisionIDMap) {
    BundleWiring wiring = revision.getWiring();
    List<BundleCapability> capabilities = wiring.getCapabilities(namespace);
    List<BundleRequirement> requirements = wiring.getRequirements(namespace);
    List<BundleWire> providedWires = wiring.getProvidedWires(namespace);
    List<BundleWire> requiredWires = wiring.getRequiredWires(namespace);
    BundleWiringData data = new BundleWiringData(wiring.getBundle().getBundleId(), revisionID, capabilities, requirements, providedWires, requiredWires, revisionIDMap);
    return data.toCompositeData();
}
Also used : BundleWiring(org.osgi.framework.wiring.BundleWiring) BundleWiringData(org.apache.aries.jmx.codec.BundleWiringData) BundleCapability(org.osgi.framework.wiring.BundleCapability) BundleWire(org.osgi.framework.wiring.BundleWire) BundleRequirement(org.osgi.framework.wiring.BundleRequirement)

Example 19 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 20 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)

Aggregations

BundleWiring (org.osgi.framework.wiring.BundleWiring)60 Bundle (org.osgi.framework.Bundle)38 BundleWire (org.osgi.framework.wiring.BundleWire)22 Test (org.junit.Test)14 BundleRevision (org.osgi.framework.wiring.BundleRevision)10 ArrayList (java.util.ArrayList)9 BundleCapability (org.osgi.framework.wiring.BundleCapability)9 Hashtable (java.util.Hashtable)8 BundleContext (org.osgi.framework.BundleContext)7 Dictionary (java.util.Dictionary)6 HashMap (java.util.HashMap)6 AbstractIntegrationTest (org.apache.aries.jmx.AbstractIntegrationTest)6 IAnswer (org.easymock.IAnswer)6 AsmProxyManager (org.apache.aries.proxy.impl.AsmProxyManager)5 ServiceRegistrationHolder (org.apache.karaf.service.guard.impl.GuardProxyCatalog.ServiceRegistrationHolder)5 ServiceReference (org.osgi.framework.ServiceReference)5 ServiceRegistration (org.osgi.framework.ServiceRegistration)5 Version (org.osgi.framework.Version)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 HashSet (java.util.HashSet)4