Search in sources :

Example 1 with BundleCapability

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

the class HttpExtension method getAttributes.

private Map<String, Object> getAttributes() {
    BundleWiring bundleWiring = _bundle.adapt(BundleWiring.class);
    List<BundleWire> wires = bundleWiring.getRequiredWires(EXTENDER_NAMESPACE);
    Map<String, Object> cdiAttributes = Collections.emptyMap();
    for (BundleWire wire : wires) {
        BundleCapability capability = wire.getCapability();
        Map<String, Object> attributes = capability.getAttributes();
        String extender = (String) attributes.get(EXTENDER_NAMESPACE);
        if (extender.equals(CDI_CAPABILITY_NAME)) {
            BundleRequirement requirement = wire.getRequirement();
            cdiAttributes = requirement.getAttributes();
            break;
        }
    }
    return cdiAttributes;
}
Also used : BundleWiring(org.osgi.framework.wiring.BundleWiring) BundleCapability(org.osgi.framework.wiring.BundleCapability) BundleWire(org.osgi.framework.wiring.BundleWire) BundleRequirement(org.osgi.framework.wiring.BundleRequirement)

Example 2 with BundleCapability

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

the class CandidateComparator method compare.

public int compare(Capability cap1, Capability cap2) {
    int c = 0;
    // Always prefer system bundle
    if (cap1 instanceof BundleCapability && !(cap2 instanceof BundleCapability)) {
        c = -1;
    } else if (!(cap1 instanceof BundleCapability) && cap2 instanceof BundleCapability) {
        c = 1;
    }
    // Always prefer mandatory resources
    if (c == 0) {
        int c1 = cost.applyAsInt(cap1.getResource());
        int c2 = cost.applyAsInt(cap2.getResource());
        c = Integer.compare(c1, c2);
    }
    // Compare revision capabilities.
    if ((c == 0) && cap1.getNamespace().equals(BundleNamespace.BUNDLE_NAMESPACE)) {
        c = compareNames(cap1, cap2, BundleNamespace.BUNDLE_NAMESPACE);
        if (c == 0) {
            c = compareVersions(cap1, cap2, BundleNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE);
        }
    // Compare package capabilities.
    } else if ((c == 0) && cap1.getNamespace().equals(PackageNamespace.PACKAGE_NAMESPACE)) {
        c = compareNames(cap1, cap2, PackageNamespace.PACKAGE_NAMESPACE);
        if (c == 0) {
            c = compareVersions(cap1, cap2, PackageNamespace.CAPABILITY_VERSION_ATTRIBUTE);
            // if same version, rather compare on the bundle version
            if (c == 0) {
                c = compareVersions(cap1, cap2, BundleNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE);
            }
        }
    // Compare feature capabilities
    } else if ((c == 0) && cap1.getNamespace().equals(IdentityNamespace.IDENTITY_NAMESPACE)) {
        c = compareNames(cap1, cap2, IdentityNamespace.IDENTITY_NAMESPACE);
        if (c == 0) {
            c = compareVersions(cap1, cap2, IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE);
        }
    }
    if (c == 0) {
        // We just want to have a deterministic heuristic
        final Resource resource1 = cap1.getResource();
        final Resource resource2 = cap2.getResource();
        String n1 = ResolverUtil.getSymbolicName(resource1);
        String n2 = ResolverUtil.getSymbolicName(resource2);
        c = n1.compareTo(n2);
        // so use this difference for deterministic heuristic
        if (c == 0) {
            String o1 = ResolverUtil.getOwnerName(resource1);
            String o2 = ResolverUtil.getOwnerName(resource2);
            if (o1 != null && o2 != null) {
                // In case the owners are the same but with different version, prefer the latest one
                // TODO: this may not be fully correct, as we'd need to separate names/versions
                // TODO: and do a real version comparison
                c = -o1.compareTo(o2);
            }
        }
    }
    return c;
}
Also used : Resource(org.osgi.resource.Resource) BundleCapability(org.osgi.framework.wiring.BundleCapability)

Example 3 with BundleCapability

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

the class ShowBundleTree method createNodeForImport.

/*
     * Create a child node for a given import (by finding a matching export in the currently installed bundles)
     */
private void createNodeForImport(Node<Bundle> node, Bundle bundle, Clause i) {
    VersionRange range = VersionRange.parseVersionRange(i.getAttribute(Constants.VERSION_ATTRIBUTE));
    boolean foundMatch = false;
    for (Bundle b : bundleContext.getBundles()) {
        BundleWiring wiring = b.adapt(BundleWiring.class);
        if (wiring != null) {
            List<BundleCapability> caps = wiring.getCapabilities(BundleRevision.PACKAGE_NAMESPACE);
            if (caps != null) {
                for (BundleCapability cap : caps) {
                    String n = getAttribute(cap, BundleRevision.PACKAGE_NAMESPACE);
                    String v = getAttribute(cap, Constants.VERSION_ATTRIBUTE);
                    if (i.getName().equals(n) && range.contains(VersionTable.getVersion(v))) {
                        boolean existing = tree.flatten().contains(b);
                        System.out.printf("- import %s: resolved using %s%n", i, b);
                        foundMatch = true;
                        if (!node.hasChild(b)) {
                            Node<Bundle> child = node.addChild(b);
                            if (!existing) {
                                createNode(child);
                            }
                        }
                    }
                }
            }
        }
    }
    if (!foundMatch) {
        System.out.printf("- import %s: WARNING - unable to find matching export%n", i);
    }
}
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 4 with BundleCapability

use of org.osgi.framework.wiring.BundleCapability 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)

Example 5 with BundleCapability

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

the class Activator method canSee.

/**
     * Check if bundle can see the given class
     */
protected boolean canSee(Bundle bundle, Class<?> clazz) {
    if (bundle.getBundleId() == bundleId) {
        // Need extra handling of camel core as it does not import the api
        return true;
    }
    BundleCapability packageCap = packageCapabilities.get(clazz.getPackage().getName());
    if (packageCap != null) {
        BundleWiring wiring = bundle.adapt(BundleWiring.class);
        List<BundleWire> imports = wiring.getRequiredWires(PACKAGE_NAMESPACE);
        for (BundleWire importWire : imports) {
            if (packageCap.equals(importWire.getCapability())) {
                return true;
            }
        }
    }
    // then we need to use a different canSee algorithm that works outside real OSGi
    if (bundle.getBundleId() > 0) {
        Bundle root = bundle.getBundleContext().getBundle(0);
        if (root != null && "org.apache.felix.connect".equals(root.getSymbolicName())) {
            return checkCompat(bundle, clazz);
        }
    }
    return false;
}
Also used : Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) BundleCapability(org.osgi.framework.wiring.BundleCapability) BundleWire(org.osgi.framework.wiring.BundleWire)

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