Search in sources :

Example 11 with ResourceImpl

use of org.apache.felix.resolver.test.util.ResourceImpl in project felix by apache.

the class ResolverTest method testScenario13.

@Test
public void testScenario13() throws Exception {
    ResolverImpl resolver = new ResolverImpl(new Logger(Logger.LOG_DEBUG), 1);
    Map<Resource, Wiring> wirings = new HashMap<Resource, Wiring>();
    Map<Requirement, List<Capability>> candMap = new HashMap<Requirement, List<Capability>>();
    ResourceImpl a1 = new ResourceImpl("A");
    Capability a1_hostCap = addCap(a1, HostNamespace.HOST_NAMESPACE, "A");
    ResourceImpl f1 = new ResourceImpl("F1", IdentityNamespace.TYPE_FRAGMENT, Version.emptyVersion);
    Requirement f1_hostReq = addReq(f1, HostNamespace.HOST_NAMESPACE, "A");
    Capability f1_pkgCap = addCap(f1, PackageNamespace.PACKAGE_NAMESPACE, "org.foo.a");
    ResourceImpl b1 = new ResourceImpl("B");
    Requirement b_pkgReq1 = addReq(b1, PackageNamespace.PACKAGE_NAMESPACE, "org.foo.a");
    candMap.put(b_pkgReq1, Collections.singletonList(f1_pkgCap));
    candMap.put(f1_hostReq, Collections.singletonList(a1_hostCap));
    ResolveContextImpl rci = new ResolveContextImpl(wirings, candMap, Collections.<Resource>singletonList(b1), Collections.<Resource>emptyList());
    Map<Resource, List<Wire>> wireMap = resolver.resolve(rci);
    assertEquals(3, wireMap.size());
    List<Wire> wiresB = wireMap.get(b1);
    assertNotNull(wiresB);
    assertEquals(1, wiresB.size());
    // should be wired to A through the fragment capability
    assertEquals(a1, wiresB.get(0).getProvider());
    assertEquals(f1_pkgCap, wiresB.get(0).getCapability());
}
Also used : GenericCapability(org.apache.felix.resolver.test.util.GenericCapability) PackageCapability(org.apache.felix.resolver.test.util.PackageCapability) Capability(org.osgi.resource.Capability) BundleCapability(org.apache.felix.resolver.test.util.BundleCapability) HashMap(java.util.HashMap) Resource(org.osgi.resource.Resource) ResolverImpl(org.apache.felix.resolver.ResolverImpl) Logger(org.apache.felix.resolver.Logger) Wire(org.osgi.resource.Wire) Wiring(org.osgi.resource.Wiring) Requirement(org.osgi.resource.Requirement) BundleRequirement(org.apache.felix.resolver.test.util.BundleRequirement) GenericRequirement(org.apache.felix.resolver.test.util.GenericRequirement) PackageRequirement(org.apache.felix.resolver.test.util.PackageRequirement) ResourceImpl(org.apache.felix.resolver.test.util.ResourceImpl) ArrayList(java.util.ArrayList) List(java.util.List) ResolveContextImpl(org.apache.felix.resolver.test.util.ResolveContextImpl) Test(org.junit.Test)

Example 12 with ResourceImpl

use of org.apache.felix.resolver.test.util.ResourceImpl in project felix by apache.

the class BigResolutionTest method buildResolutionContext.

private ResolveContext buildResolutionContext() throws IOException, BundleException {
    Object resolution;
    InputStream is = getClass().getClassLoader().getResourceAsStream("resolution.json");
    try {
        resolution = JsonReader.read(is);
    } finally {
        is.close();
    }
    List<Resource> resources = new ArrayList<Resource>();
    ResourceImpl system = new ResourceImpl("system-bundle");
    parseCapability(system, "osgi.ee; osgi.ee=JavaSE; version=1.5");
    parseCapability(system, "osgi.ee; osgi.ee=JavaSE; version=1.6");
    parseCapability(system, "osgi.ee; osgi.ee=JavaSE; version=1.7");
    resources.add(system);
    for (Object r : (Collection) ((Map) resolution).get("resources")) {
        resources.add(parseResource(r));
    }
    final List<Resource> mandatory = new ArrayList<Resource>();
    for (Object r : (Collection) ((Map) resolution).get("mandatory")) {
        mandatory.add(parseResource(r));
    }
    final Map<String, CapabilitySet> capSets = new HashMap<String, CapabilitySet>();
    CapabilitySet svcSet = new CapabilitySet(Collections.singletonList("objectClass"));
    capSets.put("osgi.service", svcSet);
    for (Resource resource : resources) {
        for (Capability cap : resource.getCapabilities(null)) {
            String ns = cap.getNamespace();
            CapabilitySet set = capSets.get(ns);
            if (set == null) {
                set = new CapabilitySet(Collections.singletonList(ns));
                capSets.put(ns, set);
            }
            set.addCapability(cap);
        }
    }
    return new ResolveContext() {

        @Override
        public Collection<Resource> getMandatoryResources() {
            return mandatory;
        }

        @Override
        public List<Capability> findProviders(Requirement requirement) {
            SimpleFilter sf;
            if (requirement.getDirectives().containsKey("filter")) {
                sf = SimpleFilter.parse(requirement.getDirectives().get("filter"));
            } else {
                sf = SimpleFilter.convert(requirement.getAttributes());
            }
            CapabilitySet set = capSets.get(requirement.getNamespace());
            List<Capability> caps = new ArrayList<Capability>(set.match(sf, true));
            Collections.sort(caps, new CandidateComparator());
            return caps;
        }

        @Override
        public int insertHostedCapability(List<Capability> capabilities, HostedCapability hostedCapability) {
            capabilities.add(hostedCapability);
            return capabilities.size() - 1;
        }

        @Override
        public boolean isEffective(Requirement requirement) {
            return true;
        }

        @Override
        public Map<Resource, Wiring> getWirings() {
            return Collections.emptyMap();
        }
    };
}
Also used : ResolveContext(org.osgi.service.resolver.ResolveContext) HostedCapability(org.osgi.service.resolver.HostedCapability) GenericCapability(org.apache.felix.resolver.test.util.GenericCapability) Capability(org.osgi.resource.Capability) HashMap(java.util.HashMap) InputStream(java.io.InputStream) Resource(org.osgi.resource.Resource) ArrayList(java.util.ArrayList) HostedCapability(org.osgi.service.resolver.HostedCapability) Wiring(org.osgi.resource.Wiring) Requirement(org.osgi.resource.Requirement) GenericRequirement(org.apache.felix.resolver.test.util.GenericRequirement) ResourceImpl(org.apache.felix.resolver.test.util.ResourceImpl) CandidateComparator(org.apache.felix.resolver.test.util.CandidateComparator) SimpleFilter(org.apache.felix.resolver.test.util.SimpleFilter) Collection(java.util.Collection) CapabilitySet(org.apache.felix.resolver.test.util.CapabilitySet) ArrayList(java.util.ArrayList) List(java.util.List)

Example 13 with ResourceImpl

use of org.apache.felix.resolver.test.util.ResourceImpl in project felix by apache.

the class BigResolutionTest method parseResource.

@SuppressWarnings("unchecked")
public static Resource parseResource(Object resource) throws BundleException {
    ResourceImpl res = new ResourceImpl();
    Collection<String> caps = (Collection<String>) ((Map) resource).get("capabilities");
    if (caps != null) {
        for (String s : caps) {
            parseCapability(res, s);
        }
    }
    Collection<String> reqs = (Collection<String>) ((Map) resource).get("requirements");
    if (reqs != null) {
        for (String s : reqs) {
            parseRequirement(res, s);
        }
    }
    return res;
}
Also used : ResourceImpl(org.apache.felix.resolver.test.util.ResourceImpl) Collection(java.util.Collection)

Example 14 with ResourceImpl

use of org.apache.felix.resolver.test.util.ResourceImpl in project felix by apache.

the class Main method populateScenario5.

private static List<Resource> populateScenario5(Map<Resource, Wiring> wirings, Map<Requirement, List<Capability>> candMap) {
    wirings.clear();
    candMap.clear();
    ResourceImpl x = new ResourceImpl("X");
    x.addRequirement(new BundleRequirement(x, "A"));
    ResourceImpl a = new ResourceImpl("A");
    a.addCapability(new BundleCapability(a, "A"));
    a.addRequirement(new BundleRequirement(a, "B"));
    a.addRequirement(new BundleRequirement(a, "C"));
    ResourceImpl b = new ResourceImpl("B");
    b.addCapability(new BundleCapability(b, "B"));
    b.addCapability(new PackageCapability(b, "p1"));
    ResourceImpl c = new ResourceImpl("C");
    c.addRequirement(new BundleRequirement(c, "D"));
    c.addCapability(new BundleCapability(c, "C"));
    PackageCapability p2 = new PackageCapability(c, "p2");
    p2.addDirective(Namespace.CAPABILITY_USES_DIRECTIVE, "p1");
    c.addCapability(p2);
    ResourceImpl d = new ResourceImpl("D");
    d.addCapability(new BundleCapability(d, "D"));
    d.addCapability(new PackageCapability(d, "p1"));
    candMap.put(x.getRequirements(null).get(0), a.getCapabilities(BundleNamespace.BUNDLE_NAMESPACE));
    candMap.put(a.getRequirements(null).get(0), b.getCapabilities(BundleNamespace.BUNDLE_NAMESPACE));
    candMap.put(a.getRequirements(null).get(1), c.getCapabilities(BundleNamespace.BUNDLE_NAMESPACE));
    candMap.put(c.getRequirements(null).get(0), d.getCapabilities(BundleNamespace.BUNDLE_NAMESPACE));
    List<Resource> resources = new ArrayList<Resource>();
    resources.add(x);
    return resources;
}
Also used : ResourceImpl(org.apache.felix.resolver.test.util.ResourceImpl) PackageCapability(org.apache.felix.resolver.test.util.PackageCapability) Resource(org.osgi.resource.Resource) ArrayList(java.util.ArrayList) BundleCapability(org.apache.felix.resolver.test.util.BundleCapability) BundleRequirement(org.apache.felix.resolver.test.util.BundleRequirement)

Example 15 with ResourceImpl

use of org.apache.felix.resolver.test.util.ResourceImpl in project felix by apache.

the class Main method populateScenario4.

private static List<Resource> populateScenario4(Map<Resource, Wiring> wirings, Map<Requirement, List<Capability>> candMap) {
    wirings.clear();
    candMap.clear();
    ResourceImpl a = new ResourceImpl("A");
    a.addRequirement(new BundleRequirement(a, "B"));
    a.addRequirement(new BundleRequirement(a, "C"));
    ResourceImpl b = new ResourceImpl("B");
    b.addCapability(new BundleCapability(b, "B"));
    b.addCapability(new PackageCapability(b, "p1"));
    ResourceImpl c = new ResourceImpl("C");
    c.addRequirement(new BundleRequirement(c, "D"));
    c.addCapability(new BundleCapability(c, "C"));
    PackageCapability p2 = new PackageCapability(c, "p2");
    p2.addDirective(Namespace.CAPABILITY_USES_DIRECTIVE, "p1");
    c.addCapability(p2);
    ResourceImpl d = new ResourceImpl("D");
    d.addCapability(new BundleCapability(d, "D"));
    d.addCapability(new PackageCapability(d, "p1"));
    candMap.put(a.getRequirements(null).get(0), b.getCapabilities(BundleNamespace.BUNDLE_NAMESPACE));
    candMap.put(a.getRequirements(null).get(1), c.getCapabilities(BundleNamespace.BUNDLE_NAMESPACE));
    candMap.put(c.getRequirements(null).get(0), d.getCapabilities(BundleNamespace.BUNDLE_NAMESPACE));
    List<Resource> resources = new ArrayList<Resource>();
    resources.add(a);
    return resources;
}
Also used : ResourceImpl(org.apache.felix.resolver.test.util.ResourceImpl) PackageCapability(org.apache.felix.resolver.test.util.PackageCapability) Resource(org.osgi.resource.Resource) ArrayList(java.util.ArrayList) BundleCapability(org.apache.felix.resolver.test.util.BundleCapability) BundleRequirement(org.apache.felix.resolver.test.util.BundleRequirement)

Aggregations

ResourceImpl (org.apache.felix.resolver.test.util.ResourceImpl)27 Resource (org.osgi.resource.Resource)26 ArrayList (java.util.ArrayList)25 PackageCapability (org.apache.felix.resolver.test.util.PackageCapability)23 BundleCapability (org.apache.felix.resolver.test.util.BundleCapability)21 BundleRequirement (org.apache.felix.resolver.test.util.BundleRequirement)21 GenericCapability (org.apache.felix.resolver.test.util.GenericCapability)20 GenericRequirement (org.apache.felix.resolver.test.util.GenericRequirement)20 PackageRequirement (org.apache.felix.resolver.test.util.PackageRequirement)19 Capability (org.osgi.resource.Capability)18 Requirement (org.osgi.resource.Requirement)18 HashMap (java.util.HashMap)11 List (java.util.List)11 ResolveContextImpl (org.apache.felix.resolver.test.util.ResolveContextImpl)9 Wire (org.osgi.resource.Wire)8 Logger (org.apache.felix.resolver.Logger)7 ResolverImpl (org.apache.felix.resolver.ResolverImpl)7 Test (org.junit.Test)7 Wiring (org.osgi.resource.Wiring)7 Collection (java.util.Collection)3