Search in sources :

Example 11 with Reason

use of org.apache.felix.bundlerepository.Reason in project aries by apache.

the class OBRAriesResolver method refineUnsatisfiedRequirements.

/**
 * Refine the unsatisfied requirements ready for later human comsumption
 *
 * @param resolver The resolver to be used to refine the requirements
 * @param reasons The reasons
 * @return A map of the unsatifiedRequirement to the set of bundles that have that requirement unsatisfied (values associated with the keys can be null)
 */
private Map<String, Set<String>> refineUnsatisfiedRequirements(Resolver resolver, Reason[] reasons) {
    log.debug(LOG_ENTRY, "refineUnsatisfiedRequirements", new Object[] { resolver, Arrays.toString(reasons) });
    Map<Requirement, Set<String>> req_resources = new HashMap<Requirement, Set<String>>();
    // add the reasons to the map, use the requirement as the key, the resources required the requirement as the values
    Set<Resource> resources = new HashSet<Resource>();
    for (Reason reason : reasons) {
        resources.add(reason.getResource());
        Requirement key = reason.getRequirement();
        String value = reason.getResource().getSymbolicName() + "_" + reason.getResource().getVersion().toString();
        Set<String> values = req_resources.get(key);
        if (values == null) {
            values = new HashSet<String>();
        }
        values.add(value);
        req_resources.put(key, values);
    }
    // remove the requirements that can be satisifed by the resources. It is listed because the resources are not satisfied by other requirements.
    // For an instance, the unsatisfied reasons are [package a, required by bundle aa], [package b, required by bundle bb] and [package c, required by bundle cc],
    // If the bundle aa exports the package a and c. In our error message, we only want to display package a is needed by bundle aa.
    // Go through each requirement and find out whether the requirement can be satisfied by the reasons.
    Set<Capability> caps = new HashSet<Capability>();
    for (Resource res : resources) {
        if ((res != null) && (res.getCapabilities() != null)) {
            List<Capability> capList = Arrays.asList(res.getCapabilities());
            if (capList != null) {
                caps.addAll(capList);
            }
        }
    }
    Iterator<Map.Entry<Requirement, Set<String>>> iterator = req_resources.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry<Requirement, Set<String>> entry = iterator.next();
        Requirement req = entry.getKey();
        for (Capability cap : caps) {
            if (req.isSatisfied(cap)) {
                // remove the key from the map
                iterator.remove();
                break;
            }
        }
    }
    // Now the map only contains the necessary missing requirements
    Map<String, Set<String>> result = new HashMap<String, Set<String>>();
    for (Map.Entry<Requirement, Set<String>> req_res : req_resources.entrySet()) {
        result.put(req_res.getKey().getFilter(), req_res.getValue());
    }
    log.debug(LOG_EXIT, "refineUnsatisfiedRequirements", new Object[] { result });
    return result;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Capability(org.apache.felix.bundlerepository.Capability) HashMap(java.util.HashMap) ModelledBundleResource(org.apache.aries.application.resolver.obr.ext.ModelledBundleResource) ModelledResource(org.apache.aries.application.modelling.ModelledResource) Resource(org.apache.felix.bundlerepository.Resource) Reason(org.apache.felix.bundlerepository.Reason) Requirement(org.apache.felix.bundlerepository.Requirement) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Aggregations

Reason (org.apache.felix.bundlerepository.Reason)11 Resolver (org.apache.felix.bundlerepository.Resolver)7 Resource (org.apache.felix.bundlerepository.Resource)7 ArrayList (java.util.ArrayList)6 Requirement (org.apache.felix.bundlerepository.Requirement)4 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Set (java.util.Set)3 ModelledResource (org.apache.aries.application.modelling.ModelledResource)3 ModelledBundleResource (org.apache.aries.application.resolver.obr.ext.ModelledBundleResource)3 Capability (org.apache.felix.bundlerepository.Capability)3 Map (java.util.Map)2 ResolverException (org.apache.aries.application.management.ResolverException)2 Version (org.osgi.framework.Version)2 List (java.util.List)1 ApplicationMetadata (org.apache.aries.application.ApplicationMetadata)1 Content (org.apache.aries.application.Content)1 InvalidAttributeException (org.apache.aries.application.InvalidAttributeException)1 BundleInfo (org.apache.aries.application.management.BundleInfo)1 ResolveConstraint (org.apache.aries.application.management.ResolveConstraint)1