Search in sources :

Example 36 with Wire

use of org.osgi.resource.Wire in project felix by apache.

the class ResolveContext method getSubstitutionWires.

/**
 * Returns the subset of {@link Wiring#getRequiredResourceWires(String)
 * required} wires that provide wires to {@link Capability capabilities}
 * which substitute capabilities of the wiring. For example, when a
 * {@link PackageNamespace package} name is both provided and required by
 * the same resource. If the package requirement is resolved to a capability
 * provided by a different wiring then the package capability is considered
 * to be substituted.
 * <p>
 * The resolver asks the resolve context to return substitution wires for
 * each wiring that {@link Wiring#getResourceCapabilities(String) provides}
 * a {@link BundleNamespace bundle} namespace capability that is used to
 * resolve one or more bundle requirements.
 * <p>
 * Note that this method searches all the {@link PackageNamespace package}
 * capabilities declared as {@link Resource#getCapabilities(String)
 * provided} by the resource associated with the wiring and fragment
 * resources wired to the wiring with the {@link HostNamespace host}
 * namespace. The provided package names are compared against the
 * {@link Wiring#getRequiredResourceWires(String) required} package wires to
 * determine which wires are substitution wires. Subclasses of
 * <code>ResolveContext</code> should provide a more efficient
 * implementation of this method.
 *
 * @param wiring the wiring to get the substitution wires for. Must not be
 *            {@code null}.
 * @return A list containing a snapshot of the substitution {@link Wire}s
 *         for the {@link Requirement requirements} of the wiring, or an
 *         empty list if the wiring has no substitution wires. The list
 *         contains the wires in the order they are found in the
 *         {@link Wiring#getRequiredResourceWires(String) required} wires of
 *         the wiring.
 * @since 1.1
 */
public List<Wire> getSubstitutionWires(Wiring wiring) {
    // Keep track of the declared capability package names
    Set<String> exportNames = new HashSet<String>();
    // Add packages declared as provided by the wiring host
    for (Capability cap : wiring.getResource().getCapabilities(null)) {
        if (PackageNamespace.PACKAGE_NAMESPACE.equals(cap.getNamespace())) {
            exportNames.add((String) cap.getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE));
        }
    }
    // Add packages declared as provided by the attached fragments
    for (Wire wire : wiring.getProvidedResourceWires(null)) {
        if (HostNamespace.HOST_NAMESPACE.equals(wire.getCapability().getNamespace())) {
            Resource fragment = wire.getRequirement().getResource();
            for (Capability cap : fragment.getCapabilities(null)) {
                if (PackageNamespace.PACKAGE_NAMESPACE.equals(cap.getNamespace())) {
                    exportNames.add((String) cap.getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE));
                }
            }
        }
    }
    // collect the package wires that substitute one of the declared
    // export package names
    List<Wire> substitutionWires = new ArrayList<Wire>();
    for (Wire wire : wiring.getRequiredResourceWires(null)) {
        if (PackageNamespace.PACKAGE_NAMESPACE.equals(wire.getCapability().getNamespace())) {
            if (exportNames.contains(wire.getCapability().getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE))) {
                substitutionWires.add(wire);
            }
        }
    }
    return substitutionWires;
}
Also used : Capability(org.osgi.resource.Capability) Resource(org.osgi.resource.Resource) ArrayList(java.util.ArrayList) Wire(org.osgi.resource.Wire) HashSet(java.util.HashSet)

Example 37 with Wire

use of org.osgi.resource.Wire in project felix by apache.

the class ResolverTest method testScenario14.

@Test
public void testScenario14() throws Exception {
    ResolverImpl resolver = new ResolverImpl(new Logger(Logger.LOG_DEBUG), 1);
    Map<Resource, Wiring> wirings = new HashMap<Resource, Wiring>();
    Map<Requirement, List<Capability>> candMap = new HashMap<Requirement, List<Capability>>();
    ResourceImpl a1 = new ResourceImpl("A", IdentityNamespace.TYPE_BUNDLE, Version.parseVersion("1.0.0"));
    Capability a1_hostCap = addCap(a1, HostNamespace.HOST_NAMESPACE, "A");
    Capability a1_pkgCap = addCap(a1, PackageNamespace.PACKAGE_NAMESPACE, "a");
    Requirement a1_pkgReq = addReq(a1, PackageNamespace.PACKAGE_NAMESPACE, "a.impl");
    ResourceImpl a2 = new ResourceImpl("A", IdentityNamespace.TYPE_BUNDLE, Version.parseVersion("2.0.0"));
    Capability a2_hostCap = addCap(a2, HostNamespace.HOST_NAMESPACE, "A");
    Capability a2_pkgCap = addCap(a2, PackageNamespace.PACKAGE_NAMESPACE, "a");
    Requirement a2_pkgReq = addReq(a2, PackageNamespace.PACKAGE_NAMESPACE, "a.impl");
    ResourceImpl a3 = new ResourceImpl("A", IdentityNamespace.TYPE_BUNDLE, Version.parseVersion("3.0.0"));
    Capability a3_hostCap = addCap(a3, HostNamespace.HOST_NAMESPACE, "A");
    Capability a3_pkgCap = addCap(a3, PackageNamespace.PACKAGE_NAMESPACE, "a");
    Requirement a3_pkgReq = addReq(a3, PackageNamespace.PACKAGE_NAMESPACE, "a.impl");
    ResourceImpl f1 = new ResourceImpl("F1", IdentityNamespace.TYPE_FRAGMENT, Version.emptyVersion);
    Requirement f1_hostReq = addReq(f1, HostNamespace.HOST_NAMESPACE, "A");
    Capability f1_pkgCap = addCap(f1, PackageNamespace.PACKAGE_NAMESPACE, "a.impl");
    Requirement f1_pkgReq = addReq(f1, PackageNamespace.PACKAGE_NAMESPACE, "a");
    ResourceImpl b1 = new ResourceImpl("B");
    Requirement b_pkgReq1 = addReq(b1, PackageNamespace.PACKAGE_NAMESPACE, "a");
    candMap.put(a1_pkgReq, Collections.singletonList(f1_pkgCap));
    candMap.put(a2_pkgReq, Collections.singletonList(f1_pkgCap));
    candMap.put(a3_pkgReq, Collections.singletonList(f1_pkgCap));
    candMap.put(b_pkgReq1, Arrays.asList(a3_pkgCap, a2_pkgCap, a1_pkgCap));
    candMap.put(f1_pkgReq, Arrays.asList(a3_pkgCap, a2_pkgCap, a1_pkgCap));
    candMap.put(f1_hostReq, Arrays.asList(a3_hostCap, a2_hostCap, a1_hostCap));
    ResolveContextImpl rci = new ResolveContextImpl(wirings, candMap, Arrays.<Resource>asList(b1, a1, a2, a3), Collections.<Resource>emptyList());
    Map<Resource, List<Wire>> wireMap = resolver.resolve(rci);
    // all bundles should be resolved
    assertEquals(5, wireMap.size());
    List<Wire> wiresB = wireMap.get(b1);
    assertNotNull(wiresB);
    assertEquals(1, wiresB.size());
    assertEquals(a3, wiresB.get(0).getProvider());
    assertEquals(a3_pkgCap, wiresB.get(0).getCapability());
    // There should be three hosts
    List<Wire> wiresF1 = wireMap.get(f1);
    assertNotNull(wiresF1);
    assertEquals(3, wiresF1.size());
}
Also used : GenericCapability(org.apache.felix.resolver.test.util.GenericCapability) PackageCapability(org.apache.felix.resolver.test.util.PackageCapability) Capability(org.osgi.resource.Capability) BundleCapability(org.apache.felix.resolver.test.util.BundleCapability) HashMap(java.util.HashMap) Resource(org.osgi.resource.Resource) ResolverImpl(org.apache.felix.resolver.ResolverImpl) Logger(org.apache.felix.resolver.Logger) Wire(org.osgi.resource.Wire) Wiring(org.osgi.resource.Wiring) Requirement(org.osgi.resource.Requirement) BundleRequirement(org.apache.felix.resolver.test.util.BundleRequirement) GenericRequirement(org.apache.felix.resolver.test.util.GenericRequirement) PackageRequirement(org.apache.felix.resolver.test.util.PackageRequirement) ResourceImpl(org.apache.felix.resolver.test.util.ResourceImpl) ArrayList(java.util.ArrayList) List(java.util.List) ResolveContextImpl(org.apache.felix.resolver.test.util.ResolveContextImpl) Test(org.junit.Test)

Example 38 with Wire

use of org.osgi.resource.Wire in project felix by apache.

the class ResolverTest method testScenario15.

@Test
public void testScenario15() throws Exception {
    ResolverImpl resolver = new ResolverImpl(new Logger(Logger.LOG_DEBUG), 1);
    Map<Requirement, List<Capability>> candMap = new HashMap<Requirement, List<Capability>>();
    ResourceImpl exporter = new ResourceImpl("exporter", IdentityNamespace.TYPE_BUNDLE, Version.parseVersion("1.0.0"));
    Capability exporter_hostCap = addCap(exporter, HostNamespace.HOST_NAMESPACE, "exporter");
    Capability exporter_pkgCap = addCap(exporter, PackageNamespace.PACKAGE_NAMESPACE, "exporter");
    ResourceImpl exporterFrag = new ResourceImpl("exporter.frag", IdentityNamespace.TYPE_FRAGMENT, Version.emptyVersion);
    Requirement exporterFrag_hostReq = addReq(exporterFrag, HostNamespace.HOST_NAMESPACE, "exporter");
    ResourceImpl host1 = new ResourceImpl("host", IdentityNamespace.TYPE_BUNDLE, Version.parseVersion("1.0.0"));
    Capability host1_hostCap = addCap(host1, HostNamespace.HOST_NAMESPACE, "host");
    Requirement host1_pkgReq = addReq(host1, PackageNamespace.PACKAGE_NAMESPACE, "exporter");
    ResourceImpl host2 = new ResourceImpl("host", IdentityNamespace.TYPE_BUNDLE, Version.parseVersion("2.0.0"));
    Capability host2_hostCap = addCap(host2, HostNamespace.HOST_NAMESPACE, "host");
    Requirement host2_pkgReq = addReq(host2, PackageNamespace.PACKAGE_NAMESPACE, "exporter");
    ResourceImpl host3 = new ResourceImpl("host", IdentityNamespace.TYPE_BUNDLE, Version.parseVersion("3.0.0"));
    Capability host3_hostCap = addCap(host3, HostNamespace.HOST_NAMESPACE, "host");
    Requirement host3_pkgReq = addReq(host3, PackageNamespace.PACKAGE_NAMESPACE, "exporter");
    ResourceImpl host4 = new ResourceImpl("host", IdentityNamespace.TYPE_BUNDLE, Version.parseVersion("4.0.0"));
    Capability host4_hostCap = addCap(host4, HostNamespace.HOST_NAMESPACE, "host");
    Requirement host4_pkgReq = addReq(host4, PackageNamespace.PACKAGE_NAMESPACE, "exporter");
    ResourceImpl host5 = new ResourceImpl("host", IdentityNamespace.TYPE_BUNDLE, Version.parseVersion("5.0.0"));
    Capability host5_hostCap = addCap(host5, HostNamespace.HOST_NAMESPACE, "host");
    Requirement host5_pkgReq = addReq(host5, PackageNamespace.PACKAGE_NAMESPACE, "exporter");
    ResourceImpl host6 = new ResourceImpl("host", IdentityNamespace.TYPE_BUNDLE, Version.parseVersion("6.0.0"));
    Capability host6_hostCap = addCap(host6, HostNamespace.HOST_NAMESPACE, "host");
    Requirement host6_pkgReq = addReq(host6, PackageNamespace.PACKAGE_NAMESPACE, "exporter");
    ResourceImpl host7 = new ResourceImpl("host", IdentityNamespace.TYPE_BUNDLE, Version.parseVersion("7.0.0"));
    Capability host7_hostCap = addCap(host7, HostNamespace.HOST_NAMESPACE, "host");
    Requirement host7_pkgReq = addReq(host7, PackageNamespace.PACKAGE_NAMESPACE, "exporter");
    ResourceImpl host8 = new ResourceImpl("host", IdentityNamespace.TYPE_BUNDLE, Version.parseVersion("8.0.0"));
    Capability host8_hostCap = addCap(host8, HostNamespace.HOST_NAMESPACE, "host");
    Requirement host8_pkgReq = addReq(host8, PackageNamespace.PACKAGE_NAMESPACE, "exporter");
    ResourceImpl hostFrag = new ResourceImpl("host.frag", IdentityNamespace.TYPE_FRAGMENT, Version.emptyVersion);
    Requirement hostFrag_hostReq = addReq(hostFrag, HostNamespace.HOST_NAMESPACE, "host");
    Requirement hostFrag_pkgReq = addReq(hostFrag, PackageNamespace.PACKAGE_NAMESPACE, "exporter");
    candMap.put(exporterFrag_hostReq, Collections.singletonList(exporter_hostCap));
    candMap.put(host1_pkgReq, Collections.singletonList(exporter_pkgCap));
    candMap.put(host2_pkgReq, Collections.singletonList(exporter_pkgCap));
    candMap.put(host3_pkgReq, Collections.singletonList(exporter_pkgCap));
    candMap.put(host4_pkgReq, Collections.singletonList(exporter_pkgCap));
    candMap.put(host5_pkgReq, Collections.singletonList(exporter_pkgCap));
    candMap.put(host6_pkgReq, Collections.singletonList(exporter_pkgCap));
    candMap.put(host7_pkgReq, Collections.singletonList(exporter_pkgCap));
    candMap.put(host8_pkgReq, Collections.singletonList(exporter_pkgCap));
    candMap.put(hostFrag_pkgReq, Collections.singletonList(exporter_pkgCap));
    candMap.put(hostFrag_hostReq, Arrays.asList(host1_hostCap, host2_hostCap, host3_hostCap, host4_hostCap, host5_hostCap, host6_hostCap, host7_hostCap, host8_hostCap));
    ResolveContextImpl rci = new ResolveContextImpl(Collections.<Resource, Wiring>emptyMap(), candMap, Arrays.<Resource>asList(host1, host2, host3, host4, exporter, exporterFrag, host5, host6, host7, host8, hostFrag), Collections.<Resource>emptyList());
    Map<Resource, List<Wire>> wireMap = resolver.resolve(rci);
    // all bundles should be resolved
    assertEquals(11, wireMap.size());
    // There should be 8 hosts
    List<Wire> wiresHostFrag = wireMap.get(hostFrag);
    assertNotNull(wiresHostFrag);
    assertEquals(8, wiresHostFrag.size());
    List<Wire> wiresHost1 = wireMap.get(host1);
    assertNotNull(wiresHost1);
}
Also used : GenericCapability(org.apache.felix.resolver.test.util.GenericCapability) PackageCapability(org.apache.felix.resolver.test.util.PackageCapability) Capability(org.osgi.resource.Capability) BundleCapability(org.apache.felix.resolver.test.util.BundleCapability) HashMap(java.util.HashMap) Resource(org.osgi.resource.Resource) ResolverImpl(org.apache.felix.resolver.ResolverImpl) Logger(org.apache.felix.resolver.Logger) Wire(org.osgi.resource.Wire) Requirement(org.osgi.resource.Requirement) BundleRequirement(org.apache.felix.resolver.test.util.BundleRequirement) GenericRequirement(org.apache.felix.resolver.test.util.GenericRequirement) PackageRequirement(org.apache.felix.resolver.test.util.PackageRequirement) ResourceImpl(org.apache.felix.resolver.test.util.ResourceImpl) ArrayList(java.util.ArrayList) List(java.util.List) ResolveContextImpl(org.apache.felix.resolver.test.util.ResolveContextImpl) Test(org.junit.Test)

Example 39 with Wire

use of org.osgi.resource.Wire in project felix by apache.

the class ResolverTest method testScenario8.

@Test
public void testScenario8() throws Exception {
    Resolver resolver = new ResolverImpl(new Logger(Logger.LOG_DEBUG));
    Map<Resource, Wiring> wirings = new HashMap<Resource, Wiring>();
    Map<Requirement, List<Capability>> candMap = new HashMap<Requirement, List<Capability>>();
    List<Resource> mandatory = populateScenario8(wirings, candMap);
    ResolveContextImpl rci = new ResolveContextImpl(wirings, candMap, mandatory, Collections.<Resource>emptyList());
    Map<Resource, List<Wire>> wireMap = resolver.resolve(rci);
    Resource res2 = findResource("res2", wireMap.keySet());
    Resource res4 = findResource("res4", wireMap.keySet());
    Resource res5 = findResource("res5", wireMap.keySet());
    assertNotNull(res2);
    assertNotNull(res4);
    assertNotNull(res5);
    List<Wire> wires2 = wireMap.get(res2);
    assertEquals(2, wires2.size());
    // should be wired to res4 and res5
    List<Wire> wires4 = wireMap.get(res4);
    assertEquals(1, wires4.size());
    // should be wired to res5
    List<Wire> wires5 = wireMap.get(res5);
    assertEquals(0, wires5.size());
    // should not be wired to any of its optional requirements to res6
    assertEquals(3, wireMap.size());
}
Also used : Resolver(org.osgi.service.resolver.Resolver) GenericCapability(org.apache.felix.resolver.test.util.GenericCapability) PackageCapability(org.apache.felix.resolver.test.util.PackageCapability) Capability(org.osgi.resource.Capability) BundleCapability(org.apache.felix.resolver.test.util.BundleCapability) HashMap(java.util.HashMap) Resource(org.osgi.resource.Resource) ResolverImpl(org.apache.felix.resolver.ResolverImpl) Logger(org.apache.felix.resolver.Logger) Wire(org.osgi.resource.Wire) Wiring(org.osgi.resource.Wiring) Requirement(org.osgi.resource.Requirement) BundleRequirement(org.apache.felix.resolver.test.util.BundleRequirement) GenericRequirement(org.apache.felix.resolver.test.util.GenericRequirement) PackageRequirement(org.apache.felix.resolver.test.util.PackageRequirement) ArrayList(java.util.ArrayList) List(java.util.List) ResolveContextImpl(org.apache.felix.resolver.test.util.ResolveContextImpl) Test(org.junit.Test)

Example 40 with Wire

use of org.osgi.resource.Wire in project felix by apache.

the class ResolverTest method testScenario11.

/**
 * Test dynamic resolution with an unresolved fragment
 */
@Test
public void testScenario11() throws Exception {
    ResolverImpl resolver = new ResolverImpl(new Logger(Logger.LOG_DEBUG), 1);
    Map<Resource, Wiring> wirings = new HashMap<Resource, Wiring>();
    Map<Requirement, List<Capability>> candMap = new HashMap<Requirement, List<Capability>>();
    ResourceImpl a1 = new ResourceImpl("A");
    Capability a1_hostCap = addCap(a1, HostNamespace.HOST_NAMESPACE, "A");
    ResourceImpl f1 = new ResourceImpl("F1", IdentityNamespace.TYPE_FRAGMENT, Version.emptyVersion);
    Requirement f1_hostReq = addReq(f1, HostNamespace.HOST_NAMESPACE, "A");
    Capability f1_pkgCap = addCap(f1, PackageNamespace.PACKAGE_NAMESPACE, "org.foo.a");
    ResourceImpl b1 = new ResourceImpl("B");
    Requirement b_pkgReq1 = addReq(b1, PackageNamespace.PACKAGE_NAMESPACE, "org.foo.a");
    candMap.put(b_pkgReq1, Collections.singletonList(f1_pkgCap));
    candMap.put(f1_hostReq, Collections.singletonList(a1_hostCap));
    Map<Resource, List<Wire>> wires = new HashMap<Resource, List<Wire>>();
    wires.put(a1, new ArrayList<Wire>());
    wires.put(b1, new ArrayList<Wire>());
    Map<Resource, List<Wire>> invertedWires = new HashMap<Resource, List<Wire>>();
    invertedWires.put(a1, new ArrayList<Wire>());
    invertedWires.put(b1, new ArrayList<Wire>());
    wirings.put(a1, new SimpleWiring(a1, Collections.<Capability>emptyList(), wires, invertedWires));
    wirings.put(b1, new SimpleWiring(b1, Collections.<Capability>emptyList(), wires, invertedWires));
    ResolveContextImpl rci = new ResolveContextImpl(wirings, candMap, Collections.<Resource>emptyList(), Collections.<Resource>emptyList());
    List<Capability> caps = new ArrayList<Capability>();
    caps.add(f1_pkgCap);
    try {
        resolver.resolve(rci, b1, b_pkgReq1, caps);
        fail("Should fail to dynamic requirement to fragment when host is resolved already.");
    } catch (ResolutionException e) {
        // expected
        assertTrue(e.getUnresolvedRequirements().contains(b_pkgReq1));
    }
    // now remove host wiring
    wirings.remove(a1);
    caps.clear();
    caps.add(f1_pkgCap);
    Map<Resource, List<Wire>> wireMap = resolver.resolve(rci, b1, b_pkgReq1, caps);
    assertEquals(3, wireMap.size());
    List<Wire> wiresB = wireMap.get(b1);
    assertNotNull(wiresB);
    assertEquals(1, wiresB.size());
    // should be wired to A through the fragment capability
    assertEquals(a1, wiresB.get(0).getProvider());
    assertEquals(f1_pkgCap, wiresB.get(0).getCapability());
}
Also used : GenericCapability(org.apache.felix.resolver.test.util.GenericCapability) PackageCapability(org.apache.felix.resolver.test.util.PackageCapability) Capability(org.osgi.resource.Capability) BundleCapability(org.apache.felix.resolver.test.util.BundleCapability) HashMap(java.util.HashMap) Resource(org.osgi.resource.Resource) ArrayList(java.util.ArrayList) ResolverImpl(org.apache.felix.resolver.ResolverImpl) Logger(org.apache.felix.resolver.Logger) Wire(org.osgi.resource.Wire) Wiring(org.osgi.resource.Wiring) ResolutionException(org.osgi.service.resolver.ResolutionException) Requirement(org.osgi.resource.Requirement) BundleRequirement(org.apache.felix.resolver.test.util.BundleRequirement) GenericRequirement(org.apache.felix.resolver.test.util.GenericRequirement) PackageRequirement(org.apache.felix.resolver.test.util.PackageRequirement) ResourceImpl(org.apache.felix.resolver.test.util.ResourceImpl) ArrayList(java.util.ArrayList) List(java.util.List) ResolveContextImpl(org.apache.felix.resolver.test.util.ResolveContextImpl) Test(org.junit.Test)

Aggregations

Wire (org.osgi.resource.Wire)58 Resource (org.osgi.resource.Resource)49 ArrayList (java.util.ArrayList)38 List (java.util.List)36 HashMap (java.util.HashMap)35 Requirement (org.osgi.resource.Requirement)31 Capability (org.osgi.resource.Capability)29 Wiring (org.osgi.resource.Wiring)18 BundleCapability (org.apache.felix.resolver.test.util.BundleCapability)15 BundleRequirement (org.apache.felix.resolver.test.util.BundleRequirement)15 GenericCapability (org.apache.felix.resolver.test.util.GenericCapability)15 GenericRequirement (org.apache.felix.resolver.test.util.GenericRequirement)15 PackageCapability (org.apache.felix.resolver.test.util.PackageCapability)15 PackageRequirement (org.apache.felix.resolver.test.util.PackageRequirement)15 ResolverImpl (org.apache.felix.resolver.ResolverImpl)14 ResolveContextImpl (org.apache.felix.resolver.test.util.ResolveContextImpl)14 Logger (org.apache.felix.resolver.Logger)13 Test (org.junit.Test)13 Resolver (org.osgi.service.resolver.Resolver)12 Map (java.util.Map)11