Search in sources :

Example 21 with BundleCapability

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

the class SubsystemResolverHook method filterMatches.

public void filterMatches(BundleRequirement requirement, Collection<BundleCapability> candidates) {
    // Filter out candidates that don't come from preferred providers when
    // there is at least one preferred provider.
    // (1) Find the subsystem(s) containing requirement.getResource() as a
    // constituent.
    Collection<BasicSubsystem> requirers = subsystems.getSubsystemsReferencing(requirement.getResource());
    // (2) For each candidate, ask each subsystem if the candidate or any of
    // the candidate's containing subsystems is a preferred provider. If at
    // least one preferred provider exists, filter out all other candidates
    // that are not also preferred providers.
    Collection<BundleCapability> preferredProviders = new ArrayList<BundleCapability>(candidates.size());
    for (BundleCapability candidate : candidates) for (BasicSubsystem subsystem : requirers) {
        PreferredProviderHeader header = subsystem.getSubsystemManifest().getPreferredProviderHeader();
        if (header != null && (header.contains(candidate.getResource()) || isResourceConstituentOfPreferredSubsystem(candidate.getResource(), subsystem)))
            preferredProviders.add(candidate);
    }
    if (!preferredProviders.isEmpty())
        candidates.retainAll(preferredProviders);
}
Also used : ArrayList(java.util.ArrayList) BundleCapability(org.osgi.framework.wiring.BundleCapability) PreferredProviderHeader(org.apache.aries.subsystem.core.archive.PreferredProviderHeader)

Example 22 with BundleCapability

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

the class BundleWiresTest method testFilterMatches.

@Test
public void testFilterMatches() throws IOException {
    BundleWires wires = readFromFile();
    BundleRequirement req = packageRequirement(packageFilter);
    BundleCapability candidate1 = bundleCap(targetBundleId, targetBundleVersion);
    List<BundleCapability> candidates = new ArrayList<>();
    candidates.add(candidate1);
    BundleCapability matchingCandidate = bundleCap(targetBundleId, "1.1.0");
    candidates.add(matchingCandidate);
    c.replay();
    wires.filterMatches(req, candidates);
    assertEquals(1, candidates.size());
    assertEquals(candidate1, candidates.iterator().next());
    c.verify();
}
Also used : ArrayList(java.util.ArrayList) BundleCapability(org.osgi.framework.wiring.BundleCapability) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) Test(org.junit.Test)

Example 23 with BundleCapability

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

the class BundleWiresTest method bundleCap.

private BundleCapability bundleCap(long bundleId, String version) {
    BundleRevision rev = c.createMock(BundleRevision.class);
    Bundle bundle = c.createMock(Bundle.class);
    expect(bundle.getBundleId()).andReturn(bundleId);
    expect(rev.getBundle()).andReturn(bundle);
    BundleCapability cap = c.createMock(BundleCapability.class);
    expect(cap.getRevision()).andReturn(rev);
    Map<String, Object> attrs = new HashMap<>();
    attrs.put(Constants.VERSION_ATTRIBUTE, version);
    expect(cap.getAttributes()).andReturn(attrs);
    return cap;
}
Also used : HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleCapability(org.osgi.framework.wiring.BundleCapability)

Example 24 with BundleCapability

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

the class Headers method checkPackage.

private boolean checkPackage(String packageName, String version) {
    VersionRange range = VersionRange.parseVersionRange(version);
    Bundle[] bundles = bundleContext.getBundles();
    for (int i = 0; (bundles != null) && (i < bundles.length); i++) {
        BundleWiring wiring = bundles[i].adapt(BundleWiring.class);
        List<BundleCapability> caps = wiring != null ? wiring.getCapabilities(BundleRevision.PACKAGE_NAMESPACE) : null;
        if (caps != null) {
            for (BundleCapability cap : caps) {
                String n = getAttribute(cap, BundleRevision.PACKAGE_NAMESPACE);
                String v = getAttribute(cap, Constants.VERSION_ATTRIBUTE);
                if (packageName.equals(n) && range.contains(VersionTable.getVersion(v))) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) VersionRange(org.apache.felix.utils.version.VersionRange) BundleCapability(org.osgi.framework.wiring.BundleCapability)

Example 25 with BundleCapability

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

the class PackageServiceImpl method getExports.

public List<PackageVersion> getExports() {
    Bundle[] bundles = bundleContext.getBundles();
    SortedMap<String, PackageVersion> packageVersionMap = new TreeMap<>();
    for (Bundle bundle : bundles) {
        BundleRevision rev = bundle.adapt(BundleRevision.class);
        if (rev != null) {
            List<BundleCapability> caps = rev.getDeclaredCapabilities(BundleRevision.PACKAGE_NAMESPACE);
            for (BundleCapability cap : caps) {
                Map<String, Object> attr = cap.getAttributes();
                String packageName = (String) attr.get(BundleRevision.PACKAGE_NAMESPACE);
                Version version = (Version) attr.get("version");
                String key = packageName + ":" + version.toString();
                PackageVersion pVer = packageVersionMap.computeIfAbsent(key, k -> new PackageVersion(packageName, version));
                pVer.addBundle(bundle);
            }
        }
    }
    return new ArrayList<>(packageVersionMap.values());
}
Also used : Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) PackageVersion(org.apache.karaf.packages.core.PackageVersion) PackageVersion(org.apache.karaf.packages.core.PackageVersion) Version(org.osgi.framework.Version) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleCapability(org.osgi.framework.wiring.BundleCapability)

Aggregations

BundleCapability (org.osgi.framework.wiring.BundleCapability)27 Bundle (org.osgi.framework.Bundle)11 BundleRevision (org.osgi.framework.wiring.BundleRevision)10 BundleWiring (org.osgi.framework.wiring.BundleWiring)9 HashMap (java.util.HashMap)8 ArrayList (java.util.ArrayList)7 BundleRequirement (org.osgi.framework.wiring.BundleRequirement)7 CompositeData (javax.management.openmbean.CompositeData)6 BundleWire (org.osgi.framework.wiring.BundleWire)6 List (java.util.List)4 Map (java.util.Map)4 Region (org.eclipse.equinox.region.Region)3 Test (org.junit.Test)3 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 Set (java.util.Set)2 TreeMap (java.util.TreeMap)2 AbstractIntegrationTest (org.apache.aries.jmx.AbstractIntegrationTest)2 VersionRange (org.apache.felix.utils.version.VersionRange)2 PackageVersion (org.apache.karaf.packages.core.PackageVersion)2