Search in sources :

Example 6 with Requirement

use of org.apache.felix.bundlerepository.Requirement in project karaf by apache.

the class InfoCommand method printResource.

private void printResource(PrintStream out, Resource resource) {
    // OBR R5 per Spec has no presentation name
    String resourceId = getResourceId(resource);
    printUnderline(out, resourceId.length());
    out.println(resourceId);
    printUnderline(out, resourceId.length());
    Map map = resource.getProperties();
    for (Object o : map.entrySet()) {
        Map.Entry entry = (Map.Entry) o;
        if (entry.getValue().getClass().isArray()) {
            out.println(entry.getKey() + ":");
            for (int j = 0; j < Array.getLength(entry.getValue()); j++) {
                out.println("   " + Array.get(entry.getValue(), j));
            }
        } else {
            out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
    Requirement[] reqs = resource.getRequirements();
    if ((reqs != null) && (reqs.length > 0)) {
        out.println("Requires:");
        for (Requirement req : reqs) {
            out.println("   " + req.getName() + ":" + req.getFilter());
        }
    }
    Capability[] caps = resource.getCapabilities();
    if ((caps != null) && (caps.length > 0)) {
        out.println("Capabilities:");
        for (Capability cap : caps) {
            out.println("   " + cap.getName() + ":" + cap.getPropertiesAsMap());
        }
    }
}
Also used : Requirement(org.apache.felix.bundlerepository.Requirement) Capability(org.apache.felix.bundlerepository.Capability) Map(java.util.Map)

Example 7 with Requirement

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

the class FelixRequirementAdapterTest method testCardinalityDirectiveMultiple.

@Test
public void testCardinalityDirectiveMultiple() {
    Requirement req = EasyMock.createNiceMock(Requirement.class);
    EasyMock.expect(req.getFilter()).andReturn("");
    EasyMock.expect(req.isMultiple()).andReturn(true);
    EasyMock.replay(req);
    FelixRequirementAdapter adapter = new FelixRequirementAdapter(req, EasyMock.createNiceMock(Resource.class));
    assertEquals("Wrong value for directive " + Namespace.REQUIREMENT_CARDINALITY_DIRECTIVE, Namespace.CARDINALITY_MULTIPLE, adapter.getDirectives().get(Namespace.REQUIREMENT_CARDINALITY_DIRECTIVE));
}
Also used : FelixRequirementAdapter(org.apache.aries.subsystem.util.felix.FelixRequirementAdapter) Requirement(org.apache.felix.bundlerepository.Requirement) Resource(org.osgi.resource.Resource) Test(org.junit.Test)

Example 8 with Requirement

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

the class FelixRequirementAdapterTest method testResolutionDirectiveOptional.

@Test
public void testResolutionDirectiveOptional() {
    Requirement req = EasyMock.createNiceMock(Requirement.class);
    EasyMock.expect(req.getFilter()).andReturn("");
    EasyMock.expect(req.isOptional()).andReturn(true);
    EasyMock.replay(req);
    FelixRequirementAdapter adapter = new FelixRequirementAdapter(req, EasyMock.createNiceMock(Resource.class));
    assertEquals("Wrong value for directive " + Namespace.REQUIREMENT_RESOLUTION_DIRECTIVE, Namespace.RESOLUTION_OPTIONAL, adapter.getDirectives().get(Namespace.REQUIREMENT_RESOLUTION_DIRECTIVE));
}
Also used : FelixRequirementAdapter(org.apache.aries.subsystem.util.felix.FelixRequirementAdapter) Requirement(org.apache.felix.bundlerepository.Requirement) Resource(org.osgi.resource.Resource) Test(org.junit.Test)

Example 9 with Requirement

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

the class FelixRequirementAdapterTest method testCardinalityDirectiveSingle.

@Test
public void testCardinalityDirectiveSingle() {
    Requirement req = EasyMock.createNiceMock(Requirement.class);
    EasyMock.expect(req.getFilter()).andReturn("");
    EasyMock.expect(req.isMultiple()).andReturn(false);
    EasyMock.replay(req);
    FelixRequirementAdapter adapter = new FelixRequirementAdapter(req, EasyMock.createNiceMock(Resource.class));
    assertEquals("Wrong value for directive " + Namespace.REQUIREMENT_CARDINALITY_DIRECTIVE, Namespace.CARDINALITY_SINGLE, adapter.getDirectives().get(Namespace.REQUIREMENT_CARDINALITY_DIRECTIVE));
}
Also used : FelixRequirementAdapter(org.apache.aries.subsystem.util.felix.FelixRequirementAdapter) Requirement(org.apache.felix.bundlerepository.Requirement) Resource(org.osgi.resource.Resource) Test(org.junit.Test)

Example 10 with Requirement

use of org.apache.felix.bundlerepository.Requirement 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

Requirement (org.apache.felix.bundlerepository.Requirement)11 Capability (org.apache.felix.bundlerepository.Capability)5 FelixRequirementAdapter (org.apache.aries.subsystem.util.felix.FelixRequirementAdapter)4 Test (org.junit.Test)4 Resource (org.osgi.resource.Resource)4 Map (java.util.Map)3 Reason (org.apache.felix.bundlerepository.Reason)3 ArrayList (java.util.ArrayList)2 Resource (org.apache.felix.bundlerepository.Resource)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 ModelledResource (org.apache.aries.application.modelling.ModelledResource)1 ModelledBundleResource (org.apache.aries.application.resolver.obr.ext.ModelledBundleResource)1 Repository (org.apache.felix.bundlerepository.Repository)1 Resolver (org.apache.felix.bundlerepository.Resolver)1 Element (org.w3c.dom.Element)1