Search in sources :

Example 96 with BundleCapability

use of org.osgi.framework.wiring.BundleCapability in project felix by apache.

the class StatefulResolver method indexCapabilities.

private synchronized void indexCapabilities(BundleRevision br) {
    List<BundleCapability> caps = (Util.isFragment(br) || (br.getWiring() == null)) ? br.getDeclaredCapabilities(null) : br.getWiring().getCapabilities(null);
    if (caps != null) {
        for (BundleCapability cap : caps) {
            // attached hosts for fragments.
            if (cap.getRevision() == br) {
                CapabilitySet capSet = m_capSets.get(cap.getNamespace());
                if (capSet == null) {
                    capSet = new CapabilitySet(null, true);
                    m_capSets.put(cap.getNamespace(), capSet);
                }
                capSet.addCapability(cap);
            }
        }
    }
}
Also used : CapabilitySet(org.apache.felix.framework.capabilityset.CapabilitySet) BundleCapability(org.osgi.framework.wiring.BundleCapability)

Example 97 with BundleCapability

use of org.osgi.framework.wiring.BundleCapability in project felix by apache.

the class StatefulResolver method groupSingletons.

private List<BundleRevision> groupSingletons(Map<BundleCapability, Collection<BundleCapability>> allCollisions, BundleCapability target, List<BundleRevision> group) {
    if (!group.contains(target.getRevision())) {
        // Add the target since it is implicitly part of the group.
        group.add(target.getRevision());
        // Recursively add the revisions of any singleton's in the
        // target's collisions.
        Collection<BundleCapability> collisions = allCollisions.remove(target);
        for (BundleCapability collision : collisions) {
            groupSingletons(allCollisions, collision, group);
        }
        // Need to check the values of other collisions for this target
        // and add those to the target's group too, since collisions are
        // treated as two-way relationships. Repeat until there are no
        // collision groups left that contain the target capability.
        boolean repeat;
        do {
            repeat = false;
            for (Entry<BundleCapability, Collection<BundleCapability>> entry : allCollisions.entrySet()) {
                if (entry.getValue().contains(target)) {
                    repeat = true;
                    groupSingletons(allCollisions, entry.getKey(), group);
                    break;
                }
            }
        } while (repeat);
    }
    return group;
}
Also used : ShrinkableCollection(org.apache.felix.framework.util.ShrinkableCollection) Collection(java.util.Collection) BundleCapability(org.osgi.framework.wiring.BundleCapability)

Example 98 with BundleCapability

use of org.osgi.framework.wiring.BundleCapability in project felix by apache.

the class RequirementsCapabilitiesTest method testIdentityCapabilityBundleFragment.

public void testIdentityCapabilityBundleFragment() throws Exception {
    String bmf = "Bundle-SymbolicName: cap.bundle\n" + "Bundle-Version: 1.2.3.Blah\n" + "Bundle-ManifestVersion: 2\n" + "Import-Package: org.osgi.framework\n";
    File bundleFile = createBundle(bmf);
    String fmf = "Bundle-SymbolicName: cap.frag\n" + "Bundle-Version: 1.0.0\n" + "Fragment-Host: cap.bundle\n" + "Bundle-ManifestVersion: 2\n" + "Export-Package: org.foo.bar;version=\"2.0.0\"\n" + "Import-Package: org.osgi.util.tracker\n";
    File fragFile = createBundle(fmf);
    Bundle b = felix.getBundleContext().installBundle(bundleFile.toURI().toASCIIString());
    Bundle f = felix.getBundleContext().installBundle(fragFile.toURI().toASCIIString());
    // Check the bundle capabilities.
    // First check the capabilities on the Bundle Revision, which is available on installed bundles
    BundleRevision bbr = b.adapt(BundleRevision.class);
    List<Capability> bwbCaps = bbr.getCapabilities("osgi.wiring.bundle");
    assertEquals(1, bwbCaps.size());
    Map<String, Object> expectedBWBAttrs = new HashMap<String, Object>();
    expectedBWBAttrs.put("osgi.wiring.bundle", "cap.bundle");
    expectedBWBAttrs.put("bundle-version", Version.parseVersion("1.2.3.Blah"));
    Capability expectedBWBCap = new TestCapability("osgi.wiring.bundle", expectedBWBAttrs, Collections.<String, String>emptyMap());
    assertCapsEquals(expectedBWBCap, bwbCaps.get(0));
    List<Capability> bwhCaps = bbr.getCapabilities("osgi.wiring.host");
    assertEquals(1, bwhCaps.size());
    Map<String, Object> expectedBWHAttrs = new HashMap<String, Object>();
    expectedBWHAttrs.put("osgi.wiring.host", "cap.bundle");
    expectedBWHAttrs.put("bundle-version", Version.parseVersion("1.2.3.Blah"));
    Capability expectedBWHCap = new TestCapability("osgi.wiring.host", expectedBWHAttrs, Collections.<String, String>emptyMap());
    assertCapsEquals(expectedBWHCap, bwhCaps.get(0));
    List<Capability> bwiCaps = bbr.getCapabilities("osgi.identity");
    assertEquals(1, bwiCaps.size());
    Map<String, Object> expectedBWIAttrs = new HashMap<String, Object>();
    expectedBWIAttrs.put("osgi.identity", "cap.bundle");
    expectedBWIAttrs.put("type", "osgi.bundle");
    expectedBWIAttrs.put("version", Version.parseVersion("1.2.3.Blah"));
    Capability expectedBWICap = new TestCapability("osgi.identity", expectedBWIAttrs, Collections.<String, String>emptyMap());
    assertCapsEquals(expectedBWICap, bwiCaps.get(0));
    assertEquals("The Bundle should not directly expose osgi.wiring.package", 0, bbr.getCapabilities("osgi.wiring.package").size());
    // Check the fragment's capabilities.
    // First check the capabilities on the Bundle Revision, which is available on installed fragments
    BundleRevision fbr = f.adapt(BundleRevision.class);
    List<Capability> fwpCaps = fbr.getCapabilities("osgi.wiring.package");
    assertEquals(1, fwpCaps.size());
    Map<String, Object> expectedFWAttrs = new HashMap<String, Object>();
    expectedFWAttrs.put("osgi.wiring.package", "org.foo.bar");
    expectedFWAttrs.put("version", Version.parseVersion("2"));
    expectedFWAttrs.put("bundle-symbolic-name", "cap.frag");
    expectedFWAttrs.put("bundle-version", Version.parseVersion("1.0.0"));
    Capability expectedFWCap = new TestCapability("osgi.wiring.package", expectedFWAttrs, Collections.<String, String>emptyMap());
    assertCapsEquals(expectedFWCap, fwpCaps.get(0));
    List<Capability> fiCaps = fbr.getCapabilities("osgi.identity");
    assertEquals(1, fiCaps.size());
    Map<String, Object> expectedFIAttrs = new HashMap<String, Object>();
    expectedFIAttrs.put("osgi.identity", "cap.frag");
    expectedFIAttrs.put("type", "osgi.fragment");
    expectedFIAttrs.put("version", Version.parseVersion("1.0.0"));
    Capability expectedFICap = new TestCapability("osgi.identity", expectedFIAttrs, Collections.<String, String>emptyMap());
    assertCapsEquals(expectedFICap, fiCaps.get(0));
    // Start the bundle. This will make the BundleWiring available on both the bundle and the fragment
    b.start();
    // Check the Bundle Wiring on the fragment. It should only contain the osgi.identity capability
    // All the other capabilities should have migrated to the bundle's BundleWiring.
    BundleWiring fbw = f.adapt(BundleWiring.class);
    List<BundleCapability> fbwCaps = fbw.getCapabilities(null);
    assertEquals("Fragment should only have 1 capability: it's osgi.identity", 1, fbwCaps.size());
    assertCapsEquals(expectedFICap, fbwCaps.get(0));
    // Check the Bundle Wiring on the bundle. It should contain all the capabilities originally on the
    // bundle and also contain the osgi.wiring.package capability from the fragment.
    BundleWiring bbw = b.adapt(BundleWiring.class);
    List<BundleCapability> bwbCaps2 = bbw.getCapabilities("osgi.wiring.bundle");
    assertEquals(1, bwbCaps2.size());
    assertCapsEquals(expectedBWBCap, bwbCaps2.get(0));
    List<BundleCapability> bwhCaps2 = bbw.getCapabilities("osgi.wiring.host");
    assertEquals(1, bwhCaps2.size());
    assertCapsEquals(expectedBWHCap, bwhCaps2.get(0));
    List<BundleCapability> bwiCaps2 = bbw.getCapabilities("osgi.identity");
    assertEquals(1, bwiCaps2.size());
    assertCapsEquals(expectedBWICap, bwiCaps2.get(0));
    List<BundleCapability> bwpCaps2 = bbw.getCapabilities("osgi.wiring.package");
    assertEquals("Bundle should have inherited the osgi.wiring.package capability from the fragment", 1, bwpCaps2.size());
    assertCapsEquals(expectedFWCap, bwpCaps2.get(0));
}
Also used : BundleCapability(org.osgi.framework.wiring.BundleCapability) Capability(org.osgi.resource.Capability) HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleCapability(org.osgi.framework.wiring.BundleCapability) File(java.io.File)

Example 99 with BundleCapability

use of org.osgi.framework.wiring.BundleCapability in project felix by apache.

the class ManifestParserTest method testIdentityCapabilityMinimal.

public void testIdentityCapabilityMinimal() throws BundleException {
    Map<String, Object> headers = new HashMap<String, Object>();
    headers.put(Constants.BUNDLE_MANIFESTVERSION, "2");
    headers.put(Constants.BUNDLE_SYMBOLICNAME, "foo.bar");
    ManifestParser mp = new ManifestParser(null, null, null, headers);
    BundleCapability ic = findCapability(mp.getCapabilities(), IdentityNamespace.IDENTITY_NAMESPACE);
    assertEquals("foo.bar", ic.getAttributes().get(IdentityNamespace.IDENTITY_NAMESPACE));
    assertEquals(IdentityNamespace.TYPE_BUNDLE, ic.getAttributes().get(IdentityNamespace.CAPABILITY_TYPE_ATTRIBUTE));
    assertEquals(0, ic.getDirectives().size());
}
Also used : HashMap(java.util.HashMap) BundleCapability(org.osgi.framework.wiring.BundleCapability)

Example 100 with BundleCapability

use of org.osgi.framework.wiring.BundleCapability in project felix by apache.

the class ManifestParserTest method testNativeCapability.

@SuppressWarnings("unchecked")
public void testNativeCapability() throws BundleException {
    Map<String, Object> headers = new HashMap<String, Object>();
    headers.put(Constants.BUNDLE_MANIFESTVERSION, "2");
    headers.put(Constants.BUNDLE_SYMBOLICNAME, FelixConstants.SYSTEM_BUNDLE_SYMBOLICNAME);
    headers.put(Constants.PROVIDE_CAPABILITY, " osgi.native;" + "osgi.native.osname:List<String>=\"Windows7,Windows 7,Win7,Win32\";" + "osgi.native.osversion:Version=\"7.0\";" + "osgi.native.processor:List<String>=\"x86-64,amd64,em64t,x86_64\";" + "osgi.native.language=\"en\"");
    BundleRevision mockBundleRevision = mock(BundleRevision.class);
    when(mockBundleRevision.getSymbolicName()).thenReturn(FelixConstants.SYSTEM_BUNDLE_SYMBOLICNAME);
    ManifestParser mp = new ManifestParser(null, null, mockBundleRevision, headers);
    BundleCapability ic = findCapability(mp.getCapabilities(), NativeNamespace.NATIVE_NAMESPACE);
    assertEquals("en", ic.getAttributes().get(NativeNamespace.CAPABILITY_LANGUAGE_ATTRIBUTE));
    List<String> osList = (List<String>) ic.getAttributes().get(NativeNamespace.CAPABILITY_OSNAME_ATTRIBUTE);
    assertEquals(4, osList.size());
    assertEquals(new Version("7.0"), ic.getAttributes().get(NativeNamespace.CAPABILITY_OSVERSION_ATTRIBUTE));
    List<String> nativeProcesserList = (List<String>) ic.getAttributes().get(NativeNamespace.CAPABILITY_PROCESSOR_ATTRIBUTE);
    assertEquals(4, nativeProcesserList.size());
}
Also used : HashMap(java.util.HashMap) Version(org.osgi.framework.Version) BundleRevision(org.osgi.framework.wiring.BundleRevision) ArrayList(java.util.ArrayList) List(java.util.List) BundleCapability(org.osgi.framework.wiring.BundleCapability)

Aggregations

BundleCapability (org.osgi.framework.wiring.BundleCapability)164 ArrayList (java.util.ArrayList)65 Bundle (org.osgi.framework.Bundle)57 BundleRevision (org.osgi.framework.wiring.BundleRevision)52 HashMap (java.util.HashMap)48 BundleWire (org.osgi.framework.wiring.BundleWire)47 BundleRequirement (org.osgi.framework.wiring.BundleRequirement)44 BundleWiring (org.osgi.framework.wiring.BundleWiring)44 List (java.util.List)41 Test (org.junit.Test)28 HashSet (java.util.HashSet)27 BundleException (org.osgi.framework.BundleException)25 ResolverHook (org.osgi.framework.hooks.resolver.ResolverHook)25 Collection (java.util.Collection)24 Set (java.util.Set)24 Version (org.osgi.framework.Version)22 Map (java.util.Map)18 ResolverHookFactory (org.osgi.framework.hooks.resolver.ResolverHookFactory)18 File (java.io.File)16 Module (org.eclipse.osgi.container.Module)14