Search in sources :

Example 11 with BundleWire

use of org.osgi.framework.wiring.BundleWire 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 12 with BundleWire

use of org.osgi.framework.wiring.BundleWire 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 13 with BundleWire

use of org.osgi.framework.wiring.BundleWire in project karaf by apache.

the class Requirements method printMatchingRequirements.

private static boolean printMatchingRequirements(BundleWiring wiring, Pattern namespace) {
    List<BundleWire> wires = wiring.getRequiredWires(null);
    Map<BundleRequirement, List<BundleWire>> aggregateReqs = aggregateRequirements(namespace, wires);
    List<BundleRequirement> allReqs = wiring.getRequirements(null);
    boolean matches = false;
    for (BundleRequirement req : allReqs) {
        if (matchNamespace(namespace, req.getNamespace())) {
            matches = true;
            List<BundleWire> providers = aggregateReqs.get(req);
            if (providers != null) {
                System.out.println(req.getNamespace() + "; " + req.getDirectives().get(Constants.FILTER_DIRECTIVE) + " resolved by:");
                for (BundleWire wire : providers) {
                    String msg;
                    Object keyAttr = wire.getCapability().getAttributes().get(wire.getCapability().getNamespace());
                    if (keyAttr != null) {
                        msg = wire.getCapability().getNamespace() + "; " + keyAttr + " " + getVersionFromCapability(wire.getCapability());
                    } else {
                        msg = wire.getCapability().toString();
                    }
                    msg = "   " + msg + " from " + wire.getProviderWiring().getBundle();
                    System.out.println(msg);
                }
            } else {
                System.out.println(req.getNamespace() + "; " + req.getDirectives().get(Constants.FILTER_DIRECTIVE) + " " + UNRESOLVED_MESSAGE);
            }
        }
    }
    return matches;
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) BundleWire(org.osgi.framework.wiring.BundleWire) BundleRequirement(org.osgi.framework.wiring.BundleRequirement)

Example 14 with BundleWire

use of org.osgi.framework.wiring.BundleWire in project karaf by apache.

the class ShowBundleTree method printDuplicatePackages.

/*
     * Check for bundles in the tree exporting the same package
     * as a possible cause for 'Unresolved constraint...' on a uses-conflict
     */
private void printDuplicatePackages(Tree<Bundle> tree) {
    Set<Bundle> bundles = tree.flatten();
    Map<String, Set<Bundle>> exports = new HashMap<>();
    for (Bundle bundle : bundles) {
        for (BundleRevision revision : bundle.adapt(BundleRevisions.class).getRevisions()) {
            BundleWiring wiring = revision.getWiring();
            if (wiring != null) {
                List<BundleWire> wires = wiring.getProvidedWires(BundleRevision.PACKAGE_NAMESPACE);
                if (wires != null) {
                    for (BundleWire wire : wires) {
                        String name = wire.getCapability().getAttributes().get(BundleRevision.PACKAGE_NAMESPACE).toString();
                        exports.computeIfAbsent(name, k -> new HashSet<>()).add(bundle);
                    }
                }
            }
        }
    }
    for (String pkg : exports.keySet()) {
        if (exports.get(pkg).size() > 1) {
            System.out.printf("%n");
            System.out.printf("WARNING: multiple bundles are exporting package %s%n", pkg);
            for (Bundle bundle : exports.get(pkg)) {
                System.out.printf("- %s%n", bundle);
            }
        }
    }
}
Also used : Constants(org.osgi.framework.Constants) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Parser(org.apache.felix.utils.manifest.Parser) Command(org.apache.karaf.shell.api.action.Command) HashSet(java.util.HashSet) BundleCapability(org.osgi.framework.wiring.BundleCapability) Clause(org.apache.felix.utils.manifest.Clause) Map(java.util.Map) Node(org.apache.karaf.bundle.command.bundletree.Node) VersionTable(org.apache.felix.utils.version.VersionTable) Bundle(org.osgi.framework.Bundle) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleWiring(org.osgi.framework.wiring.BundleWiring) Logger(org.slf4j.Logger) Collection(java.util.Collection) Set(java.util.Set) VersionRange(org.apache.felix.utils.version.VersionRange) String.format(java.lang.String.format) BundleRevisions(org.osgi.framework.wiring.BundleRevisions) List(java.util.List) Service(org.apache.karaf.shell.api.action.lifecycle.Service) BundleWire(org.osgi.framework.wiring.BundleWire) Option(org.apache.karaf.shell.api.action.Option) Tree(org.apache.karaf.bundle.command.bundletree.Tree) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) BundleWire(org.osgi.framework.wiring.BundleWire) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleRevisions(org.osgi.framework.wiring.BundleRevisions) HashSet(java.util.HashSet)

Example 15 with BundleWire

use of org.osgi.framework.wiring.BundleWire in project karaf by apache.

the class Capabilities method printMatchingCapabilities.

private static boolean printMatchingCapabilities(BundleWiring wiring, Pattern namespace) {
    List<BundleWire> wires = wiring.getProvidedWires(null);
    Map<BundleCapability, List<BundleWire>> aggregateCaps = aggregateCapabilities(namespace, wires);
    List<BundleCapability> allCaps = wiring.getCapabilities(null);
    boolean matches = false;
    for (BundleCapability cap : allCaps) {
        if (matchNamespace(namespace, cap.getNamespace())) {
            matches = true;
            List<BundleWire> dependents = aggregateCaps.get(cap);
            Object keyAttr = cap.getAttributes().get(cap.getNamespace());
            if (dependents != null) {
                String msg;
                if (keyAttr != null) {
                    msg = cap.getNamespace() + "; " + keyAttr + " " + getVersionFromCapability(cap);
                } else {
                    msg = cap.toString();
                }
                msg = msg + " required by:";
                System.out.println(msg);
                for (BundleWire wire : dependents) {
                    System.out.println("   " + wire.getRequirerWiring().getBundle());
                }
            } else if (keyAttr != null) {
                System.out.println(cap.getNamespace() + "; " + cap.getAttributes().get(cap.getNamespace()) + " " + getVersionFromCapability(cap) + " " + UNUSED_MESSAGE);
            } else {
                System.out.println(cap + " " + UNUSED_MESSAGE);
            }
        }
    }
    return matches;
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) BundleCapability(org.osgi.framework.wiring.BundleCapability) BundleWire(org.osgi.framework.wiring.BundleWire)

Aggregations

BundleWire (org.osgi.framework.wiring.BundleWire)30 BundleWiring (org.osgi.framework.wiring.BundleWiring)22 Bundle (org.osgi.framework.Bundle)19 ArrayList (java.util.ArrayList)9 BundleRequirement (org.osgi.framework.wiring.BundleRequirement)8 Test (org.junit.Test)7 BundleCapability (org.osgi.framework.wiring.BundleCapability)7 BundleRevision (org.osgi.framework.wiring.BundleRevision)7 List (java.util.List)5 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 CompositeData (javax.management.openmbean.CompositeData)4 AbstractIntegrationTest (org.apache.aries.jmx.AbstractIntegrationTest)4 Version (org.osgi.framework.Version)4 BundleRevisions (org.osgi.framework.wiring.BundleRevisions)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Map (java.util.Map)3 JarOutputStream (java.util.jar.JarOutputStream)3 Manifest (java.util.jar.Manifest)3