Search in sources :

Example 21 with Requirement

use of org.osgi.resource.Requirement in project aries by apache.

the class RepositoryServiceRepositoryTest method testFindProviders.

@Test
public void testFindProviders() throws Exception {
    BundleContext bc = EasyMock.createNiceMock(BundleContext.class);
    RepositoryServiceRepository rsr = new RepositoryServiceRepository(bc);
    @SuppressWarnings("unchecked") ServiceReference<Object> sr = EasyMock.createMock(ServiceReference.class);
    @SuppressWarnings("unchecked") ServiceReference<Object> sr2 = EasyMock.createMock(ServiceReference.class);
    @SuppressWarnings("unchecked") ServiceReference<Object> sr3 = EasyMock.createMock(ServiceReference.class);
    EasyMock.expect(bc.getAllServiceReferences("org.osgi.service.repository.Repository", null)).andReturn(new ServiceReference[] { sr, sr2, sr3 }).anyTimes();
    TestRepository tr = new TestRepository();
    EasyMock.expect(bc.getService(sr)).andReturn(tr).anyTimes();
    ToastRepository tr2 = new ToastRepository();
    EasyMock.expect(bc.getService(sr2)).andReturn(tr2).anyTimes();
    Repository tr3 = Creator.create();
    EasyMock.expect(bc.getService(sr3)).andReturn(tr3).anyTimes();
    EasyMock.replay(bc);
    Map<String, String> dirs = Collections.singletonMap("filter", "(org.foo=bar)");
    Requirement req = new TestRequirement("org.foo", dirs);
    Collection<Capability> res = rsr.findProviders(req);
    assertEquals(1, res.size());
    Capability cap = res.iterator().next();
    assertEquals("org.foo", cap.getNamespace());
    assertEquals(1, cap.getAttributes().size());
    assertEquals("bar", cap.getAttributes().get("org.foo"));
    Map<String, String> dirs2 = Collections.singletonMap("filter", "(org.foo=b)");
    Requirement req2 = new TestRequirement("poing", dirs2);
    Collection<Capability> res2 = rsr.findProviders(req2);
    assertEquals(1, res2.size());
    Capability cap2 = res2.iterator().next();
    assertEquals("poing", cap2.getNamespace());
    assertEquals(1, cap2.getAttributes().size());
    assertEquals("b", cap2.getAttributes().get("org.foo"));
    Map<String, String> dirs3 = Collections.singletonMap("filter", "(x=y)");
    Requirement req3 = new TestRequirement("ns1", dirs3);
    Collection<Capability> res3 = rsr.findProviders(req3);
    assertEquals(1, res3.size());
    Capability cap3 = res3.iterator().next();
    assertEquals("ns1", cap3.getNamespace());
    assertEquals(1, cap3.getAttributes().size());
    assertEquals("y", cap3.getAttributes().get("x"));
}
Also used : Capability(org.osgi.resource.Capability) ServiceReference(org.osgi.framework.ServiceReference) Requirement(org.osgi.resource.Requirement) Repository(org.osgi.service.repository.Repository) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 22 with Requirement

use of org.osgi.resource.Requirement in project aries by apache.

the class ResourceHelperTest method testMandatoryDirectiveAndNullFilterDirective.

@Test
public void testMandatoryDirectiveAndNullFilterDirective() {
    Capability cap = new BasicCapability.Builder().namespace(PackageNamespace.PACKAGE_NAMESPACE).attribute(PackageNamespace.PACKAGE_NAMESPACE, "com.foo").attribute("a", "b").attribute("b", "c").attribute("c", "d").directive(PackageNamespace.CAPABILITY_MANDATORY_DIRECTIVE, "b").resource(EasyMock.createNiceMock(Resource.class)).build();
    Requirement req = new BasicRequirement.Builder().namespace(PackageNamespace.PACKAGE_NAMESPACE).resource(EasyMock.createNiceMock(Resource.class)).build();
    assertFalse("Capability should not match requirement", ResourceHelper.matches(req, cap));
}
Also used : Requirement(org.osgi.resource.Requirement) Capability(org.osgi.resource.Capability) Test(org.junit.Test)

Example 23 with Requirement

use of org.osgi.resource.Requirement in project aries by apache.

the class SubTestRepository method findProviders.

@Override
public Map<Requirement, Collection<Capability>> findProviders(Collection<? extends Requirement> requirements) {
    Map<Requirement, Collection<Capability>> res = new HashMap<Requirement, Collection<Capability>>();
    for (Requirement req : requirements) {
        if (req.getNamespace().equals("ns1") && req.getDirectives().equals(Collections.singletonMap("filter", "(x=y)"))) {
            TestCapability cap = new TestCapability("ns1", Collections.<String, Object>singletonMap("x", "y"));
            Collection<Capability> caps = Collections.<Capability>singleton(cap);
            res.put(req, caps);
        }
    }
    return res;
}
Also used : Requirement(org.osgi.resource.Requirement) Capability(org.osgi.resource.Capability) TestCapability(org.apache.aries.subsystem.core.internal.TestCapability) HashMap(java.util.HashMap) TestCapability(org.apache.aries.subsystem.core.internal.TestCapability) Collection(java.util.Collection)

Example 24 with Requirement

use of org.osgi.resource.Requirement in project aries by apache.

the class BundleRequiredExecutionEnvironmentHeaderTest method assertRequirement.

private void assertRequirement(Requirement requirement, String filter, Resource resource) {
    Requirement r = new BasicRequirement.Builder().namespace(ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE).directive(ExecutionEnvironmentNamespace.REQUIREMENT_FILTER_DIRECTIVE, filter).resource(resource).build();
    assertEquals("Wrong requirement", r, requirement);
}
Also used : Requirement(org.osgi.resource.Requirement) BasicRequirement(org.apache.aries.subsystem.core.internal.BasicRequirement)

Example 25 with Requirement

use of org.osgi.resource.Requirement in project karaf by apache.

the class SubsystemResolveContext method findProviders.

@Override
public List<Capability> findProviders(Requirement requirement) {
    List<Capability> caps = new ArrayList<>();
    Region requirerRegion = getRegion(requirement.getResource());
    if (requirerRegion != null) {
        Map<Requirement, Collection<Capability>> resMap = repository.findProviders(Collections.singleton(requirement));
        Collection<Capability> res = resMap != null ? resMap.get(requirement) : null;
        if (res != null && !res.isEmpty()) {
            caps.addAll(res);
        } else if (globalRepository != null) {
            // Only bring in external resources for non optional requirements
            if (!RESOLUTION_OPTIONAL.equals(requirement.getDirectives().get(RESOLUTION_DIRECTIVE))) {
                resMap = globalRepository.findProviders(Collections.singleton(requirement));
                res = resMap != null ? resMap.get(requirement) : null;
                if (res != null && !res.isEmpty()) {
                    caps.addAll(res);
                }
            }
        }
        // Use the digraph to prune non visible capabilities
        Visitor visitor = new Visitor(caps);
        requirerRegion.visitSubgraph(visitor);
        Collection<Capability> allowed = visitor.getAllowed();
        caps.retainAll(allowed);
        // the parent one
        if (caps.size() > 1) {
            Set<Resource> providers = new HashSet<>();
            for (Capability cap : caps) {
                Resource resource = cap.getResource();
                String id = ResolverUtil.getSymbolicName(resource) + "|" + ResolverUtil.getVersion(resource);
                if (!providers.contains(resource)) {
                    Set<Resource> oldRes = new HashSet<>(providers);
                    providers.clear();
                    String r1 = getRegion(resource).getName();
                    boolean superceded = false;
                    for (Resource r : oldRes) {
                        String id2 = ResolverUtil.getSymbolicName(r) + "|" + ResolverUtil.getVersion(r);
                        if (id.equals(id2)) {
                            String r2 = getRegion(r).getName();
                            if (r1.equals(r2)) {
                                if (r instanceof BundleRevision) {
                                    providers.add(r);
                                    superceded = true;
                                } else if (resource instanceof BundleRevision) {
                                    providers.add(resource);
                                } else {
                                    throw new InternalError();
                                }
                            } else if (r1.startsWith(r2)) {
                                providers.add(r);
                                superceded = true;
                            } else if (r2.startsWith(r1)) {
                                providers.add(resource);
                            } else {
                                providers.add(r);
                            }
                        } else {
                            providers.add(r);
                        }
                    }
                    if (!superceded) {
                        providers.add(resource);
                    }
                }
            }
            caps.removeIf(cap -> !providers.contains(cap.getResource()));
        }
        // Sort caps
        if (distance != null) {
            caps.sort(candidateComparator);
        }
    }
    return caps;
}
Also used : HostedCapability(org.osgi.service.resolver.HostedCapability) Capability(org.osgi.resource.Capability) ArrayList(java.util.ArrayList) Resource(org.osgi.resource.Resource) Requirement(org.osgi.resource.Requirement) ResourceUtils.addIdentityRequirement(org.apache.karaf.features.internal.resolver.ResourceUtils.addIdentityRequirement) BundleRevision(org.osgi.framework.wiring.BundleRevision) Region(org.eclipse.equinox.region.Region) Collection(java.util.Collection) HashSet(java.util.HashSet)

Aggregations

Requirement (org.osgi.resource.Requirement)193 Capability (org.osgi.resource.Capability)102 Resource (org.osgi.resource.Resource)59 CapReqBuilder (aQute.bnd.osgi.resource.CapReqBuilder)53 ArrayList (java.util.ArrayList)46 Collection (java.util.Collection)44 BndEditModel (aQute.bnd.build.model.BndEditModel)35 IdentityCapability (aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability)34 HashMap (java.util.HashMap)34 MockRegistry (test.lib.MockRegistry)32 List (java.util.List)19 Repository (org.osgi.service.repository.Repository)19 Test (org.junit.Test)18 File (java.io.File)14 Map (java.util.Map)12 Resolver (org.osgi.service.resolver.Resolver)11 LinkedHashSet (java.util.LinkedHashSet)10 LinkedList (java.util.LinkedList)10 Wire (org.osgi.resource.Wire)10 Parameters (aQute.bnd.header.Parameters)9