Search in sources :

Example 1 with ResolverHook

use of aQute.bnd.service.resolve.hook.ResolverHook in project bnd by bndtools.

the class BndrunResolveContextTest method testResolverHookCannotFilterFrameworkCapabilities.

public static void testResolverHookCannotFilterFrameworkCapabilities() {
    MockRegistry registry = new MockRegistry();
    registry.addPlugin(createRepo(IO.getFile("testdata/osgi.cmpn-4.3.0.index.xml")));
    registry.addPlugin(createRepo(IO.getFile("testdata/org.apache.felix.framework-4.0.2.index.xml")));
    // Add a hook that tries to remove all capabilities from resource with
    // id "org.apache.felix.framework"
    registry.addPlugin(new ResolverHook() {

        public void filterMatches(Requirement requirement, List<Capability> candidates) {
            for (Iterator<Capability> iter = candidates.iterator(); iter.hasNext(); ) {
                Object id = iter.next().getResource().getCapabilities("osgi.identity").get(0).getAttributes().get("osgi.identity");
                if ("org.apache.felix.framework".equals(id)) {
                    fail("this line should not be reached");
                }
            }
        }
    });
    BndEditModel runModel = new BndEditModel();
    runModel.setRunFw("org.apache.felix.framework");
    Requirement requirement = new CapReqBuilder("osgi.wiring.package").addDirective("filter", "(&(osgi.wiring.package=org.osgi.util.tracker)(version>=1.5)(!(version>=1.6)))").buildSyntheticRequirement();
    BndrunResolveContext context = new BndrunResolveContext(runModel, registry, log);
    List<Capability> providers = context.findProviders(requirement);
    // The filter was ineffective
    assertEquals(2, providers.size());
    assertEquals(IO.getFile("testdata/org.apache.felix.framework-4.0.2.jar").toURI(), findContentURI(providers.get(0).getResource()));
    assertEquals(IO.getFile("testdata/osgi.cmpn-4.3.0.jar").toURI(), findContentURI(providers.get(1).getResource()));
}
Also used : CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) Requirement(org.osgi.resource.Requirement) Capability(org.osgi.resource.Capability) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) ResolverHook(aQute.bnd.service.resolve.hook.ResolverHook) MockRegistry(test.lib.MockRegistry) Iterator(java.util.Iterator) BndEditModel(aQute.bnd.build.model.BndEditModel)

Example 2 with ResolverHook

use of aQute.bnd.service.resolve.hook.ResolverHook in project bnd by bndtools.

the class BndrunResolveContext method postProcessProviders.

protected void postProcessProviders(Requirement requirement, Set<Capability> wired, List<Capability> candidates) {
    if (candidates.size() == 0)
        return;
    // Call resolver hooks
    for (ResolverHook resolverHook : registry.getPlugins(ResolverHook.class)) {
        resolverHook.filterMatches(requirement, candidates);
    }
    // Process the resolve preferences
    boolean prefsUsed = false;
    if (resolvePrefs != null && !resolvePrefs.isEmpty()) {
        List<Capability> insertions = new LinkedList<Capability>();
        for (Iterator<Capability> iterator = candidates.iterator(); iterator.hasNext(); ) {
            Capability cap = iterator.next();
            if (resolvePrefs.containsKey(getResourceIdentity(cap.getResource()))) {
                iterator.remove();
                insertions.add(cap);
            }
        }
        if (!insertions.isEmpty()) {
            candidates.addAll(0, insertions);
            prefsUsed = true;
        }
    }
    if (!prefsUsed) {
        for (ResolutionCallback callback : getCallbacks()) {
            callback.processCandidates(requirement, wired, candidates);
        }
    }
}
Also used : Capability(org.osgi.resource.Capability) ResolverHook(aQute.bnd.service.resolve.hook.ResolverHook) LinkedList(java.util.LinkedList)

Example 3 with ResolverHook

use of aQute.bnd.service.resolve.hook.ResolverHook in project bnd by bndtools.

the class BndrunResolveContextTest method testResolverHookFiltersResult.

public static void testResolverHookFiltersResult() {
    MockRegistry registry = new MockRegistry();
    registry.addPlugin(createRepo(IO.getFile("testdata/osgi.cmpn-4.3.0.index.xml")));
    registry.addPlugin(createRepo(IO.getFile("testdata/org.apache.felix.framework-4.0.2.index.xml")));
    // Add a hook that removes all capabilities from resource with id
    // "osgi.cmpn"
    registry.addPlugin(new ResolverHook() {

        public void filterMatches(Requirement requirement, List<Capability> candidates) {
            for (Iterator<Capability> iter = candidates.iterator(); iter.hasNext(); ) {
                Object id = iter.next().getResource().getCapabilities("osgi.identity").get(0).getAttributes().get("osgi.identity");
                if ("osgi.cmpn".equals(id))
                    iter.remove();
            }
        }
    });
    BndEditModel runModel = new BndEditModel();
    runModel.setRunFw("org.apache.felix.framework");
    Requirement requirement = new CapReqBuilder("osgi.wiring.package").addDirective("filter", "(&(osgi.wiring.package=org.osgi.util.tracker)(version>=1.5)(!(version>=1.6)))").buildSyntheticRequirement();
    BndrunResolveContext context = new BndrunResolveContext(runModel, registry, log);
    List<Capability> providers = context.findProviders(requirement);
    assertEquals(1, providers.size());
    assertEquals(IO.getFile("testdata/org.apache.felix.framework-4.0.2.jar").toURI(), findContentURI(providers.get(0).getResource()));
// The capability from osgi.cmpn is NOT here
}
Also used : CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) Requirement(org.osgi.resource.Requirement) Capability(org.osgi.resource.Capability) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) ResolverHook(aQute.bnd.service.resolve.hook.ResolverHook) MockRegistry(test.lib.MockRegistry) Iterator(java.util.Iterator) BndEditModel(aQute.bnd.build.model.BndEditModel)

Aggregations

ResolverHook (aQute.bnd.service.resolve.hook.ResolverHook)3 Capability (org.osgi.resource.Capability)3 BndEditModel (aQute.bnd.build.model.BndEditModel)2 CapReqBuilder (aQute.bnd.osgi.resource.CapReqBuilder)2 IdentityCapability (aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability)2 Iterator (java.util.Iterator)2 Requirement (org.osgi.resource.Requirement)2 MockRegistry (test.lib.MockRegistry)2 LinkedList (java.util.LinkedList)1