Search in sources :

Example 56 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 57 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 58 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)

Example 59 with BundleCapability

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

the class CapabilitySet method dump.

public void dump() {
    for (Entry<String, Map<Object, Set<BundleCapability>>> entry : m_indices.entrySet()) {
        boolean header1 = false;
        for (Entry<Object, Set<BundleCapability>> entry2 : entry.getValue().entrySet()) {
            boolean header2 = false;
            for (BundleCapability cap : entry2.getValue()) {
                if (cap.getRevision().getBundle().getBundleId() != 0) {
                    if (!header1) {
                        System.out.println(entry.getKey() + ":");
                        header1 = true;
                    }
                    if (!header2) {
                        System.out.println("   " + entry2.getKey());
                        header2 = true;
                    }
                    System.out.println("      " + cap);
                }
            }
        }
    }
}
Also used : Set(java.util.Set) BundleCapability(org.osgi.framework.wiring.BundleCapability) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 60 with BundleCapability

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

the class CandidateComparator method compare.

public int compare(Capability cap1, Capability cap2) {
    // First check resolved state, since resolved capabilities have priority
    // over unresolved ones. Compare in reverse order since we want to sort
    // in descending order.
    int c = 0;
    BundleCapability bcap1 = null;
    BundleCapability bcap2 = null;
    if (cap1 instanceof BundleCapability && cap2 instanceof BundleCapability) {
        bcap1 = (BundleCapability) cap1;
        bcap2 = (BundleCapability) cap2;
        if ((bcap1.getRevision().getWiring() != null) && (bcap2.getRevision().getWiring() == null)) {
            c = -1;
        } else if ((bcap1.getRevision().getWiring() == null) && (bcap2.getRevision().getWiring() != null)) {
            c = 1;
        }
    }
    // Compare revision capabilities.
    if ((c == 0) && cap1.getNamespace().equals(BundleRevision.BUNDLE_NAMESPACE)) {
        c = ((Comparable) cap1.getAttributes().get(BundleRevision.BUNDLE_NAMESPACE)).compareTo(cap2.getAttributes().get(BundleRevision.BUNDLE_NAMESPACE));
        if (c == 0) {
            Version v1 = (!cap1.getAttributes().containsKey(Constants.BUNDLE_VERSION_ATTRIBUTE)) ? Version.emptyVersion : (Version) cap1.getAttributes().get(Constants.BUNDLE_VERSION_ATTRIBUTE);
            Version v2 = (!cap2.getAttributes().containsKey(Constants.BUNDLE_VERSION_ATTRIBUTE)) ? Version.emptyVersion : (Version) cap2.getAttributes().get(Constants.BUNDLE_VERSION_ATTRIBUTE);
            // Compare these in reverse order, since we want
            // highest version to have priority.
            c = v2.compareTo(v1);
        }
    } else // Compare package capabilities.
    if ((c == 0) && cap1.getNamespace().equals(BundleRevision.PACKAGE_NAMESPACE)) {
        c = ((Comparable) cap1.getAttributes().get(BundleRevision.PACKAGE_NAMESPACE)).compareTo(cap2.getAttributes().get(BundleRevision.PACKAGE_NAMESPACE));
        if (c == 0) {
            Version v1 = (!cap1.getAttributes().containsKey(BundleCapabilityImpl.VERSION_ATTR)) ? Version.emptyVersion : (Version) cap1.getAttributes().get(BundleCapabilityImpl.VERSION_ATTR);
            Version v2 = (!cap2.getAttributes().containsKey(BundleCapabilityImpl.VERSION_ATTR)) ? Version.emptyVersion : (Version) cap2.getAttributes().get(BundleCapabilityImpl.VERSION_ATTR);
            // Compare these in reverse order, since we want
            // highest version to have priority.
            c = v2.compareTo(v1);
        }
    }
    // Finally, compare bundle identity.
    if (c == 0 && bcap1 != null && bcap2 != null) {
        if (bcap1.getRevision().getBundle().getBundleId() < bcap2.getRevision().getBundle().getBundleId()) {
            c = -1;
        } else if (bcap1.getRevision().getBundle().getBundleId() > bcap2.getRevision().getBundle().getBundleId()) {
            c = 1;
        }
    }
    return c;
}
Also used : Version(org.osgi.framework.Version) BundleCapability(org.osgi.framework.wiring.BundleCapability)

Aggregations

BundleCapability (org.osgi.framework.wiring.BundleCapability)83 ArrayList (java.util.ArrayList)30 BundleRevision (org.osgi.framework.wiring.BundleRevision)26 HashMap (java.util.HashMap)24 Bundle (org.osgi.framework.Bundle)24 BundleRequirement (org.osgi.framework.wiring.BundleRequirement)21 BundleWiring (org.osgi.framework.wiring.BundleWiring)19 BundleWire (org.osgi.framework.wiring.BundleWire)18 List (java.util.List)15 Version (org.osgi.framework.Version)12 BundleException (org.osgi.framework.BundleException)11 ResolverHook (org.osgi.framework.hooks.resolver.ResolverHook)11 Collection (java.util.Collection)10 Test (org.junit.Test)10 HashSet (java.util.HashSet)9 Set (java.util.Set)8 ResolverHookFactory (org.osgi.framework.hooks.resolver.ResolverHookFactory)8 LinkedHashMap (java.util.LinkedHashMap)6 Module (org.eclipse.osgi.container.Module)6 CompositeData (javax.management.openmbean.CompositeData)5