Search in sources :

Example 1 with Wire

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

the class Deployer method computeBundlesToRefresh.

private static void computeBundlesToRefresh(Map<Bundle, String> toRefresh, Collection<Bundle> bundles, Map<Resource, Bundle> resources, Map<Resource, List<Wire>> resolution) {
    // Compute the new list of fragments
    Map<Bundle, Set<Resource>> newFragments = new HashMap<>();
    for (Bundle bundle : bundles) {
        newFragments.put(bundle, new HashSet<>());
    }
    if (resolution != null) {
        for (Resource res : resolution.keySet()) {
            for (Wire wire : resolution.get(res)) {
                if (HOST_NAMESPACE.equals(wire.getCapability().getNamespace())) {
                    Bundle bundle;
                    if (wire.getProvider() instanceof BundleRevision) {
                        bundle = ((BundleRevision) wire.getProvider()).getBundle();
                    } else {
                        bundle = resources.get(wire.getProvider());
                    }
                    if (bundle != null) {
                        Bundle b = resources.get(wire.getRequirer());
                        Resource r = b != null ? b.adapt(BundleRevision.class) : wire.getRequirer();
                        newFragments.get(bundle).add(r);
                    }
                }
            }
        }
    }
    // Main loop
    int size;
    Map<Bundle, Resource> bndToRes = new HashMap<>();
    for (Map.Entry<Resource, Bundle> entry : resources.entrySet()) {
        bndToRes.put(entry.getValue(), entry.getKey());
    }
    do {
        size = toRefresh.size();
        main: for (Bundle bundle : bundles) {
            Resource resource = bndToRes.get(bundle);
            // This bundle is not managed
            if (resource == null) {
                resource = bundle.adapt(BundleRevision.class);
            }
            // Continue if we already know about this bundle
            if (toRefresh.containsKey(bundle)) {
                continue;
            }
            // Ignore non resolved bundle
            BundleWiring wiring = bundle.adapt(BundleWiring.class);
            if (wiring == null) {
                continue;
            }
            // Ignore bundles that won't be wired
            List<Wire> newWires = resolution != null ? resolution.get(resource) : null;
            if (newWires == null) {
                continue;
            }
            // Check if this bundle is a host and its fragments changed
            Set<Resource> oldFragments = new HashSet<>();
            for (BundleWire wire : wiring.getProvidedWires(null)) {
                if (HOST_NAMESPACE.equals(wire.getCapability().getNamespace())) {
                    oldFragments.add(wire.getRequirer());
                }
            }
            if (!oldFragments.containsAll(newFragments.get(bundle))) {
                toRefresh.put(bundle, "Attached fragments changed: " + new ArrayList<>(newFragments.get(bundle)));
                break;
            }
            // Compare the old and new resolutions
            Set<Resource> wiredBundles = new HashSet<>();
            for (BundleWire wire : wiring.getRequiredWires(null)) {
                BundleRevision rev = wire.getProvider();
                Bundle provider = rev.getBundle();
                if (toRefresh.containsKey(provider)) {
                    // The bundle is wired to a bundle being refreshed,
                    // so we need to refresh it too
                    toRefresh.put(bundle, "Wired to " + provider.getSymbolicName() + "/" + provider.getVersion() + " which is being refreshed");
                    continue main;
                }
                Resource res = bndToRes.get(provider);
                wiredBundles.add(res != null ? res : rev);
            }
            Map<Resource, Requirement> wiredResources = new HashMap<>();
            for (Wire wire : newWires) {
                // Handle only packages, hosts, and required bundles
                String namespace = wire.getRequirement().getNamespace();
                if (!namespace.equals(BundleNamespace.BUNDLE_NAMESPACE) && !namespace.equals(PackageNamespace.PACKAGE_NAMESPACE) && !namespace.equals(HostNamespace.HOST_NAMESPACE)) {
                    continue;
                }
                // Ignore non-resolution time requirements
                String effective = wire.getRequirement().getDirectives().get(Namespace.CAPABILITY_EFFECTIVE_DIRECTIVE);
                if (effective != null && !Namespace.EFFECTIVE_RESOLVE.equals(effective)) {
                    continue;
                }
                // Ignore non bundle resources
                if (!isBundle(wire.getProvider())) {
                    continue;
                }
                if (!wiredResources.containsKey(wire.getProvider())) {
                    wiredResources.put(wire.getProvider(), wire.getRequirement());
                }
            }
            if (!wiredBundles.containsAll(wiredResources.keySet())) {
                Map<Resource, Requirement> newResources = new HashMap<>(wiredResources);
                newResources.keySet().removeAll(wiredBundles);
                StringBuilder sb = new StringBuilder();
                sb.append("Should be wired to: ");
                boolean first = true;
                for (Map.Entry<Resource, Requirement> entry : newResources.entrySet()) {
                    if (!first) {
                        sb.append(", ");
                    } else {
                        first = false;
                    }
                    Resource res = entry.getKey();
                    Requirement req = entry.getValue();
                    sb.append(getSymbolicName(res)).append("/").append(getVersion(res));
                    sb.append(" (through ");
                    sb.append(req);
                    sb.append(")");
                }
                toRefresh.put(bundle, sb.toString());
            }
        }
    } while (toRefresh.size() > size);
}
Also used : MapUtils.removeFromMapSet(org.apache.karaf.features.internal.util.MapUtils.removeFromMapSet) EnumSet(java.util.EnumSet) Set(java.util.Set) MapUtils.addToMapSet(org.apache.karaf.features.internal.util.MapUtils.addToMapSet) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) FeatureResource(org.apache.karaf.features.internal.resolver.FeatureResource) Resource(org.osgi.resource.Resource) Wire(org.osgi.resource.Wire) BundleWire(org.osgi.framework.wiring.BundleWire) BundleWire(org.osgi.framework.wiring.BundleWire) Requirement(org.osgi.resource.Requirement) BundleRevision(org.osgi.framework.wiring.BundleRevision) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap)

Example 2 with Wire

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

the class FeaturesDependenciesTest method dumpWiring.

private void dumpWiring(SubsystemResolver resolver) {
    System.out.println("Wiring");
    Map<Resource, List<Wire>> wiring = resolver.getWiring();
    List<Resource> resources = new ArrayList<>(wiring.keySet());
    resources.sort(Comparator.comparing(this::getName));
    for (Resource resource : resources) {
        System.out.println("    " + getName(resource));
        for (Wire wire : wiring.get(resource)) {
            System.out.println("        " + wire);
        }
    }
}
Also used : Resource(org.osgi.resource.Resource) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Wire(org.osgi.resource.Wire)

Example 3 with Wire

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

the class SubsystemResolver method resolve.

public Map<Resource, List<Wire>> resolve(Set<String> overrides, String featureResolutionRange, String serviceRequirements, final Repository globalRepository, String outputFile) throws Exception {
    if (root == null) {
        return Collections.emptyMap();
    }
    // Download bundles
    RepositoryManager repos = new RepositoryManager();
    root.downloadBundles(manager, overrides, featureResolutionRange, serviceRequirements, repos);
    // Populate digraph and resolve
    digraph = new StandardRegionDigraph(null, null);
    populateDigraph(digraph, root);
    Downloader downloader = manager.createDownloader();
    SubsystemResolveContext context = new SubsystemResolveContext(root, digraph, globalRepository, downloader, serviceRequirements);
    if (outputFile != null) {
        Map<String, Object> json = new HashMap<>();
        if (globalRepository != null) {
            json.put("globalRepository", toJson(globalRepository));
        }
        json.put("repository", toJson(context.getRepository()));
        try {
            wiring = resolver.resolve(context);
            json.put("success", "true");
            json.put("wiring", toJson(wiring));
        } catch (Exception e) {
            json.put("success", "false");
            json.put("exception", e.toString());
            throw e;
        } finally {
            try (Writer writer = Files.newBufferedWriter(Paths.get(outputFile), StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
                JsonWriter.write(writer, json, true);
            }
        }
    } else {
        wiring = resolver.resolve(context);
    }
    downloader.await();
    // Remove wiring to the fake environment resource
    if (environmentResource != null) {
        for (List<Wire> wires : wiring.values()) {
            wires.removeIf(wire -> wire.getProvider() == environmentResource);
        }
    }
    // Fragments are always wired to their host only, so create fake wiring to
    // the subsystem the host is wired to
    associateFragments();
    return wiring;
}
Also used : Downloader(org.apache.karaf.features.internal.download.Downloader) Wire(org.osgi.resource.Wire) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) JsonWriter(org.apache.karaf.util.json.JsonWriter) Writer(java.io.Writer) StandardRegionDigraph(org.eclipse.equinox.internal.region.StandardRegionDigraph)

Example 4 with Wire

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

the class SubsystemResolver method associateFragments.

private void associateFragments() {
    SimpleFilter sf = createFilter(IDENTITY_NAMESPACE, "*", CAPABILITY_TYPE_ATTRIBUTE, TYPE_SUBSYSTEM);
    for (Map.Entry<Resource, List<Wire>> entry : wiring.entrySet()) {
        final Resource resource = entry.getKey();
        final Requirement requirement = getSubsystemRequirement(resource);
        if (ResolverUtil.isFragment(resource) && requirement != null) {
            List<Wire> wires = entry.getValue();
            final Resource host = wires.get(0).getProvider();
            final Wire wire = findMatchingWire(sf, wiring.get(host));
            if (wire != null) {
                wires.add(new Wire() {

                    @Override
                    public Capability getCapability() {
                        return wire.getCapability();
                    }

                    @Override
                    public Requirement getRequirement() {
                        return requirement;
                    }

                    @Override
                    public Resource getProvider() {
                        return wire.getProvider();
                    }

                    @Override
                    public Resource getRequirer() {
                        return resource;
                    }
                });
            }
        }
    }
}
Also used : Requirement(org.osgi.resource.Requirement) Capability(org.osgi.resource.Capability) SimpleFilter(org.apache.karaf.features.internal.resolver.SimpleFilter) Resource(org.osgi.resource.Resource) Wire(org.osgi.resource.Wire) DictionaryAsMap(org.apache.felix.utils.collections.DictionaryAsMap)

Example 5 with Wire

use of org.osgi.resource.Wire in project felix by apache.

the class DTOFactory method createBundleWiringNodeDTO.

private static void createBundleWiringNodeDTO(BundleWiring bw, Set<BundleRevisionDTO> resources, Set<NodeDTO> nodes) {
    NodeDTO node = new BundleWiringDTO.NodeDTO();
    node.id = getWiringID(bw);
    addNodeDTO(node, nodes);
    node.current = bw.isCurrent();
    node.inUse = bw.isInUse();
    node.resource = getResourceIDAndAdd(bw.getResource(), resources);
    node.capabilities = new ArrayList<CapabilityRefDTO>();
    for (Capability cap : bw.getCapabilities(null)) {
        CapabilityRefDTO cdto = new CapabilityRefDTO();
        cdto.capability = getCapabilityID(cap);
        cdto.resource = getResourceIDAndAdd(cap.getResource(), resources);
        node.capabilities.add(cdto);
    }
    node.requirements = new ArrayList<RequirementRefDTO>();
    for (Requirement req : bw.getRequirements(null)) {
        RequirementRefDTO rdto = new RequirementRefDTO();
        rdto.requirement = getRequirementID(req);
        rdto.resource = getResourceIDAndAdd(req.getResource(), resources);
        node.requirements.add(rdto);
    }
    node.providedWires = new ArrayList<WireDTO>();
    for (Wire pw : bw.getProvidedWires(null)) {
        node.providedWires.add(createBundleWireDTO(pw, resources, nodes));
    }
    node.requiredWires = new ArrayList<WireDTO>();
    for (Wire rw : bw.getRequiredWires(null)) {
        node.requiredWires.add(createBundleWireDTO(rw, resources, nodes));
    }
}
Also used : Requirement(org.osgi.resource.Requirement) CapabilityRefDTO(org.osgi.resource.dto.CapabilityRefDTO) Capability(org.osgi.resource.Capability) RequirementRefDTO(org.osgi.resource.dto.RequirementRefDTO) BundleWireDTO(org.osgi.framework.wiring.dto.BundleWireDTO) WireDTO(org.osgi.resource.dto.WireDTO) Wire(org.osgi.resource.Wire) BundleWire(org.osgi.framework.wiring.BundleWire) NodeDTO(org.osgi.framework.wiring.dto.BundleWiringDTO.NodeDTO)

Aggregations

Wire (org.osgi.resource.Wire)58 Resource (org.osgi.resource.Resource)49 ArrayList (java.util.ArrayList)35 List (java.util.List)33 HashMap (java.util.HashMap)31 Requirement (org.osgi.resource.Requirement)31 Capability (org.osgi.resource.Capability)29 Wiring (org.osgi.resource.Wiring)18 BundleCapability (org.apache.felix.resolver.test.util.BundleCapability)15 BundleRequirement (org.apache.felix.resolver.test.util.BundleRequirement)15 GenericCapability (org.apache.felix.resolver.test.util.GenericCapability)15 GenericRequirement (org.apache.felix.resolver.test.util.GenericRequirement)15 PackageCapability (org.apache.felix.resolver.test.util.PackageCapability)15 PackageRequirement (org.apache.felix.resolver.test.util.PackageRequirement)15 ResolverImpl (org.apache.felix.resolver.ResolverImpl)14 ResolveContextImpl (org.apache.felix.resolver.test.util.ResolveContextImpl)14 Logger (org.apache.felix.resolver.Logger)13 Test (org.junit.Test)13 Resolver (org.osgi.service.resolver.Resolver)12 HashSet (java.util.HashSet)10