Search in sources :

Example 26 with BundleRequirement

use of org.osgi.framework.wiring.BundleRequirement in project karaf by apache.

the class BundleServiceImpl method getDiag.

@Override
public String getDiag(Bundle bundle) {
    StringBuilder message = new StringBuilder();
    for (BundleStateService bundleStateService : stateServices) {
        String part = bundleStateService.getDiag(bundle);
        if (part != null) {
            message.append(bundleStateService.getName());
            message.append("\n");
            message.append(part);
        }
    }
    if (bundle.getState() == Bundle.INSTALLED) {
        System.out.println("Unsatisfied Requirements:");
        List<BundleRequirement> reqs = getUnsatisfiedRequirements(bundle, null);
        for (BundleRequirement req : reqs) {
            System.out.println(req);
        }
    }
    return message.toString();
}
Also used : BundleStateService(org.apache.karaf.bundle.core.BundleStateService) BundleRequirement(org.osgi.framework.wiring.BundleRequirement)

Example 27 with BundleRequirement

use of org.osgi.framework.wiring.BundleRequirement in project karaf by apache.

the class Requirements method printMatchingRequirements.

private static boolean printMatchingRequirements(BundleWiring wiring, Pattern namespace) {
    List<BundleWire> wires = wiring.getRequiredWires(null);
    Map<BundleRequirement, List<BundleWire>> aggregateReqs = aggregateRequirements(namespace, wires);
    List<BundleRequirement> allReqs = wiring.getRequirements(null);
    boolean matches = false;
    for (BundleRequirement req : allReqs) {
        if (matchNamespace(namespace, req.getNamespace())) {
            matches = true;
            List<BundleWire> providers = aggregateReqs.get(req);
            if (providers != null) {
                System.out.println(req.getNamespace() + "; " + req.getDirectives().get(Constants.FILTER_DIRECTIVE) + " resolved by:");
                for (BundleWire wire : providers) {
                    String msg;
                    Object keyAttr = wire.getCapability().getAttributes().get(wire.getCapability().getNamespace());
                    if (keyAttr != null) {
                        msg = wire.getCapability().getNamespace() + "; " + keyAttr + " " + getVersionFromCapability(wire.getCapability());
                    } else {
                        msg = wire.getCapability().toString();
                    }
                    msg = "   " + msg + " from " + wire.getProviderWiring().getBundle();
                    System.out.println(msg);
                }
            } else {
                System.out.println(req.getNamespace() + "; " + req.getDirectives().get(Constants.FILTER_DIRECTIVE) + " " + UNRESOLVED_MESSAGE);
            }
        }
    }
    return matches;
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) BundleWire(org.osgi.framework.wiring.BundleWire) BundleRequirement(org.osgi.framework.wiring.BundleRequirement)

Example 28 with BundleRequirement

use of org.osgi.framework.wiring.BundleRequirement in project karaf by apache.

the class BundleInstallSupportImpl method resolveBundles.

@Override
public void resolveBundles(Set<Bundle> bundles, final Map<Resource, List<Wire>> wiring, Map<Resource, Bundle> resToBnd) {
    // Make sure it's only used for us
    final Thread thread = Thread.currentThread();
    // Translate wiring
    final Map<Bundle, Resource> bndToRes = new HashMap<>();
    for (Resource res : resToBnd.keySet()) {
        bndToRes.put(resToBnd.get(res), res);
    }
    // Hook
    final ResolverHook hook = new ResolverHook() {

        @Override
        public void filterResolvable(Collection<BundleRevision> candidates) {
        }

        @Override
        public void filterSingletonCollisions(BundleCapability singleton, Collection<BundleCapability> collisionCandidates) {
        }

        @Override
        public void filterMatches(BundleRequirement requirement, Collection<BundleCapability> candidates) {
            if (Thread.currentThread() == thread) {
                // osgi.ee capabilities are provided by the system bundle, so just ignore those
                if (ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE.equals(requirement.getNamespace())) {
                    return;
                }
                Bundle sourceBundle = requirement.getRevision().getBundle();
                Resource sourceResource = bndToRes.get(sourceBundle);
                List<Wire> wires = wiring.get(sourceResource);
                if (sourceBundle == null || wires == null) {
                    // is being resolve at the same time, so do not interfere
                    return;
                }
                Set<Resource> wired = new HashSet<>();
                // Get a list of allowed wired resources
                wired.add(sourceResource);
                for (Wire wire : wires) {
                    wired.add(wire.getProvider());
                    if (HostNamespace.HOST_NAMESPACE.equals(wire.getRequirement().getNamespace())) {
                        for (Wire hostWire : wiring.get(wire.getProvider())) {
                            wired.add(hostWire.getProvider());
                        }
                    }
                }
                // Remove candidates that are not allowed
                for (Iterator<BundleCapability> candIter = candidates.iterator(); candIter.hasNext(); ) {
                    BundleCapability cand = candIter.next();
                    BundleRevision br = cand.getRevision();
                    if ((br.getTypes() & BundleRevision.TYPE_FRAGMENT) != 0) {
                        br = br.getWiring().getRequiredWires(null).get(0).getProvider();
                    }
                    Resource res = bndToRes.get(br.getBundle());
                    if (!wired.contains(br) && !wired.contains(res)) {
                        candIter.remove();
                    }
                }
            }
        }

        @Override
        public void end() {
        }
    };
    hooks.put(Thread.currentThread(), hook);
    try {
        FrameworkWiring frameworkWiring = systemBundleContext.getBundle().adapt(FrameworkWiring.class);
        frameworkWiring.resolveBundles(bundles);
    } finally {
        hooks.remove(Thread.currentThread());
    }
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ResolverHook(org.osgi.framework.hooks.resolver.ResolverHook) Bundle(org.osgi.framework.Bundle) Resource(org.osgi.resource.Resource) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) Wire(org.osgi.resource.Wire) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) BundleRevision(org.osgi.framework.wiring.BundleRevision) Collection(java.util.Collection) BundleCapability(org.osgi.framework.wiring.BundleCapability) HashSet(java.util.HashSet)

Example 29 with BundleRequirement

use of org.osgi.framework.wiring.BundleRequirement in project karaf by apache.

the class BundleWiresTest method testFilterCandidatesSelfSatisfiedCapability.

@Test
public void testFilterCandidatesSelfSatisfiedCapability() throws IOException {
    // no wires are created with itself
    Bundle bundle = wiredBundle(Collections.emptyList());
    BundleCapability candidate1 = bundleCap(1, targetBundleVersion, false);
    List<BundleCapability> candidates = new ArrayList<>();
    candidates.add(candidate1);
    BundleCapability matchingCandidate = bundleCap(2, "1.1.0", false);
    candidates.add(matchingCandidate);
    BundleRequirement req = packageRequirement(packageFilter);
    c.replay();
    BundleWires wires = new BundleWires(bundle);
    Set<BundleCapability> goodCandidates = wires.filterCandidates(req, candidates);
    assertEquals(1, goodCandidates.size());
    assertEquals(candidate1, goodCandidates.iterator().next());
    c.verify();
}
Also used : Bundle(org.osgi.framework.Bundle) BundleCapability(org.osgi.framework.wiring.BundleCapability) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) Test(org.junit.Test)

Example 30 with BundleRequirement

use of org.osgi.framework.wiring.BundleRequirement in project karaf by apache.

the class BundleWiresTest method testFilterCandidates.

@Test
public void testFilterCandidates() throws IOException {
    BundleWires wires = readFromFile();
    BundleRequirement req = packageRequirement(packageFilter);
    BundleCapability candidate1 = bundleCap(targetBundleId, targetBundleVersion, true);
    List<BundleCapability> candidates = new ArrayList<>();
    candidates.add(candidate1);
    BundleCapability matchingCandidate = bundleCap(targetBundleId, "1.1.0", true);
    candidates.add(matchingCandidate);
    c.replay();
    Set<BundleCapability> goodCandidates = wires.filterCandidates(req, candidates);
    assertEquals(1, goodCandidates.size());
    assertEquals(candidate1, goodCandidates.iterator().next());
    c.verify();
}
Also used : BundleCapability(org.osgi.framework.wiring.BundleCapability) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) Test(org.junit.Test)

Aggregations

BundleRequirement (org.osgi.framework.wiring.BundleRequirement)55 BundleCapability (org.osgi.framework.wiring.BundleCapability)23 ArrayList (java.util.ArrayList)20 BundleWire (org.osgi.framework.wiring.BundleWire)18 BundleRevision (org.osgi.framework.wiring.BundleRevision)17 HashMap (java.util.HashMap)15 Bundle (org.osgi.framework.Bundle)15 Test (org.junit.Test)14 BundleException (org.osgi.framework.BundleException)11 ResolverHook (org.osgi.framework.hooks.resolver.ResolverHook)10 BundleWiring (org.osgi.framework.wiring.BundleWiring)10 List (java.util.List)9 Collection (java.util.Collection)8 BundleRequirementImpl (org.apache.felix.framework.wiring.BundleRequirementImpl)8 ResolverHookFactory (org.osgi.framework.hooks.resolver.ResolverHookFactory)8 CompositeData (javax.management.openmbean.CompositeData)6 Module (org.eclipse.osgi.container.Module)6 ModuleContainer (org.eclipse.osgi.container.ModuleContainer)6 DummyContainerAdaptor (org.eclipse.osgi.tests.container.dummys.DummyContainerAdaptor)6 SimpleFilter (org.apache.felix.framework.capabilityset.SimpleFilter)5