Search in sources :

Example 51 with BundleCapability

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

the class Felix method getExportedPackages.

/**
 * Adds any current active exported packages from the specified bundle
 * to the passed in list.
 * @param bundle The bundle from which to retrieve exported packages.
 * @param list The list to which the exported packages are added
 */
private void getExportedPackages(Bundle bundle, List list) {
    // to get all exports.
    for (BundleRevision br : bundle.adapt(BundleRevisions.class).getRevisions()) {
        List<BundleCapability> caps = (br.getWiring() == null) ? br.getDeclaredCapabilities(null) : br.getWiring().getCapabilities(null);
        if ((caps != null) && (caps.size() > 0)) {
            for (BundleCapability cap : caps) {
                if (cap.getNamespace().equals(BundleRevision.PACKAGE_NAMESPACE)) {
                    String pkgName = (String) cap.getAttributes().get(BundleRevision.PACKAGE_NAMESPACE);
                    list.add(new ExportedPackageImpl(this, (BundleImpl) bundle, br, cap));
                }
            }
        }
    }
}
Also used : BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleCapability(org.osgi.framework.wiring.BundleCapability) BundleRevisions(org.osgi.framework.wiring.BundleRevisions)

Example 52 with BundleCapability

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

the class StatefulResolver method selectSingletonsUsingHooks.

/*
     * Groups singletons based on resolver hook filtering and then selects
     * the singleton from each group with the highest version that is in
     * the resolver hook whitelist. No selection is made if a group already
     * has a resolved singleton in it.
     */
private void selectSingletonsUsingHooks(ResolverHookRecord record) throws BundleException {
    // Convert singleton bundle revision map into a map using
    // bundle capabilities instead, since this is what the resolver
    // hooks require.
    Map<BundleCapability, Collection<BundleCapability>> allCollisions = new HashMap<BundleCapability, Collection<BundleCapability>>();
    for (Entry<String, List<BundleRevision>> entry : m_singletons.entrySet()) {
        Collection<BundleCapability> bundleCaps = new ArrayList<BundleCapability>();
        for (BundleRevision br : entry.getValue()) {
            List<BundleCapability> caps = br.getDeclaredCapabilities(BundleRevision.BUNDLE_NAMESPACE);
            if (!caps.isEmpty()) {
                bundleCaps.add(caps.get(0));
            }
        }
        for (BundleCapability bc : bundleCaps) {
            Collection<BundleCapability> capCopy = new ShrinkableCollection<BundleCapability>(new ArrayList<BundleCapability>(bundleCaps));
            capCopy.remove(bc);
            allCollisions.put(bc, capCopy);
        }
    }
    // Invoke hooks to allow them to filter singleton collisions.
    for (ResolverHook hook : record.getResolverHooks()) {
        for (Entry<BundleCapability, Collection<BundleCapability>> entry : allCollisions.entrySet()) {
            try {
                Felix.m_secureAction.invokeResolverHookSingleton(hook, entry.getKey(), entry.getValue());
            } catch (Throwable ex) {
                throw new BundleException("Resolver hook exception: " + ex.getMessage(), BundleException.REJECTED_BY_HOOK, ex);
            }
        }
    }
    // Create groups according to how the resolver hooks filtered the
    // collisions.
    List<List<BundleRevision>> groups = new ArrayList<List<BundleRevision>>();
    while (!allCollisions.isEmpty()) {
        BundleCapability target = allCollisions.entrySet().iterator().next().getKey();
        groups.add(groupSingletons(allCollisions, target, new ArrayList<BundleRevision>()));
    }
    // Now select the singletons available for this resolve operation.
    for (List<BundleRevision> group : groups) {
        selectSingleton(record, group);
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ResolverHook(org.osgi.framework.hooks.resolver.ResolverHook) ArrayList(java.util.ArrayList) BundleRevision(org.osgi.framework.wiring.BundleRevision) ShrinkableCollection(org.apache.felix.framework.util.ShrinkableCollection) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) ShrinkableCollection(org.apache.felix.framework.util.ShrinkableCollection) BundleException(org.osgi.framework.BundleException) BundleCapability(org.osgi.framework.wiring.BundleCapability)

Example 53 with BundleCapability

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

the class StatefulResolver method isAllowedDynamicImport.

// This method duplicates a lot of logic from:
// ResolverImpl.getDynamicImportCandidates()
// This is only a rough check since it doesn't include resolver hooks.
boolean isAllowedDynamicImport(BundleRevision revision, String pkgName) {
    // package be dynamically imported.
    if ((revision.getWiring() == null) || pkgName.length() == 0) {
        return false;
    }
    // If the revision doesn't have dynamic imports, then just return
    // immediately.
    List<BundleRequirement> dynamics = Util.getDynamicRequirements(revision.getWiring().getRequirements(null));
    if ((dynamics == null) || dynamics.isEmpty()) {
        return false;
    }
    // attempt to dynamically import it.
    for (BundleCapability cap : revision.getWiring().getCapabilities(null)) {
        if (cap.getNamespace().equals(BundleRevision.PACKAGE_NAMESPACE) && cap.getAttributes().get(BundleRevision.PACKAGE_NAMESPACE).equals(pkgName)) {
            return false;
        }
    }
    // we cannot dynamically import it.
    if (((BundleWiringImpl) revision.getWiring()).hasPackageSource(pkgName)) {
        return false;
    }
    // Loop through the importer's dynamic requirements to determine if
    // there is a matching one for the package from which we want to
    // load a class.
    Map<String, Object> attrs = Collections.singletonMap(BundleRevision.PACKAGE_NAMESPACE, (Object) pkgName);
    BundleRequirementImpl req = new BundleRequirementImpl(revision, BundleRevision.PACKAGE_NAMESPACE, Collections.EMPTY_MAP, attrs);
    List<BundleCapability> candidates = findProviders(req, false);
    // Try to find a dynamic requirement that matches the capabilities.
    BundleRequirementImpl dynReq = null;
    for (int dynIdx = 0; (candidates.size() > 0) && (dynReq == null) && (dynIdx < dynamics.size()); dynIdx++) {
        for (Iterator<BundleCapability> itCand = candidates.iterator(); (dynReq == null) && itCand.hasNext(); ) {
            Capability cap = itCand.next();
            if (CapabilitySet.matches(cap, ((BundleRequirementImpl) dynamics.get(dynIdx)).getFilter())) {
                dynReq = (BundleRequirementImpl) dynamics.get(dynIdx);
            }
        }
    }
    // any candidates that do not match it.
    if (dynReq != null) {
        for (Iterator<BundleCapability> itCand = candidates.iterator(); itCand.hasNext(); ) {
            Capability cap = itCand.next();
            if (!CapabilitySet.matches(cap, dynReq.getFilter())) {
                itCand.remove();
            }
        }
    } else {
        candidates.clear();
    }
    return !candidates.isEmpty();
}
Also used : BundleCapability(org.osgi.framework.wiring.BundleCapability) Capability(org.osgi.resource.Capability) BundleCapability(org.osgi.framework.wiring.BundleCapability) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) BundleRequirementImpl(org.apache.felix.framework.wiring.BundleRequirementImpl)

Example 54 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 55 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)

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