Search in sources :

Example 1 with WireImpl

use of aQute.bnd.osgi.resource.WireImpl in project bnd by bndtools.

the class ResolveProcess method resolveRequired.

public Map<Resource, List<Wire>> resolveRequired(Processor properties, Project project, Registry plugins, Resolver resolver, Collection<ResolutionCallback> callbacks, LogService log) throws ResolutionException {
    required = new HashMap<Resource, List<Wire>>();
    optional = new HashMap<Resource, List<Wire>>();
    BndrunResolveContext rc = new BndrunResolveContext(properties, project, plugins, log);
    rc.addCallbacks(callbacks);
    // 1. Resolve initial requirements
    Map<Resource, List<Wire>> wirings;
    try {
        wirings = resolver.resolve(rc);
    } catch (ResolutionException re) {
        throw augment(rc, re);
    }
    // 2. Save initial requirement resolution
    Pair<Resource, List<Wire>> initialRequirement = null;
    for (Map.Entry<Resource, List<Wire>> wiring : wirings.entrySet()) {
        if (rc.getInputResource() == wiring.getKey()) {
            initialRequirement = new Pair<Resource, List<Wire>>(wiring.getKey(), wiring.getValue());
            break;
        }
    }
    // 3. Save the resolved root resources
    final List<Resource> resources = new ArrayList<Resource>();
    for (Resource r : rc.getMandatoryResources()) {
        reqs: for (Requirement req : r.getRequirements(null)) {
            for (Resource found : wirings.keySet()) {
                String filterStr = req.getDirectives().get(Namespace.REQUIREMENT_FILTER_DIRECTIVE);
                try {
                    org.osgi.framework.Filter filter = filterStr != null ? org.osgi.framework.FrameworkUtil.createFilter(filterStr) : null;
                    for (Capability c : found.getCapabilities(req.getNamespace())) {
                        if (filter != null && filter.matches(c.getAttributes())) {
                            resources.add(found);
                            continue reqs;
                        }
                    }
                } catch (InvalidSyntaxException e) {
                }
            }
        }
    }
    // 4. Add any 'osgi.wiring.bundle' requirements
    List<Resource> wiredBundles = new ArrayList<Resource>();
    for (Resource resource : resources) {
        addWiredBundle(wirings, resource, wiredBundles);
    }
    for (Resource resource : wiredBundles) {
        if (!resources.contains(resource)) {
            resources.add(resource);
        }
    }
    final Map<Resource, List<Wire>> discoveredOptional = new LinkedHashMap<Resource, List<Wire>>();
    // 5. Resolve the rest
    BndrunResolveContext rc2 = new BndrunResolveContext(properties, project, plugins, log) {

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

        @Override
        public boolean isInputResource(Resource resource) {
            for (Resource r : resources) {
                if (GenericResolveContext.resourceIdentityEquals(r, resource)) {
                    return true;
                }
            }
            return false;
        }

        @Override
        public List<Capability> findProviders(Requirement requirement) {
            List<Capability> toReturn = super.findProviders(requirement);
            if (toReturn.isEmpty() && isEffective(requirement) && RESOLUTION_OPTIONAL.equals(requirement.getDirectives().get(REQUIREMENT_RESOLUTION_DIRECTIVE))) {
                for (Capability cap : findProvidersFromRepositories(requirement, new LinkedHashSet<Capability>())) {
                    Resource optionalRes = cap.getResource();
                    List<Wire> list = discoveredOptional.get(optionalRes);
                    if (list == null) {
                        list = new ArrayList<>();
                        discoveredOptional.put(optionalRes, list);
                    }
                    WireImpl candidateWire = new WireImpl(cap, requirement);
                    if (!list.contains(candidateWire))
                        list.add(candidateWire);
                }
            }
            return toReturn;
        }
    };
    rc2.addCallbacks(callbacks);
    try {
        wirings = resolver.resolve(rc2);
    } catch (ResolutionException re) {
        throw augment(rc2, re);
    }
    if (initialRequirement != null) {
        wirings.put(initialRequirement.getFirst(), initialRequirement.getSecond());
    }
    Map<Resource, List<Wire>> result = invertWirings(wirings, rc2);
    removeFrameworkAndInputResources(result, rc2);
    required.putAll(result);
    optional = tidyUpOptional(wirings, discoveredOptional, log);
    return result;
}
Also used : Capability(org.osgi.resource.Capability) Resource(org.osgi.resource.Resource) ArrayList(java.util.ArrayList) WireImpl(aQute.bnd.osgi.resource.WireImpl) Wire(org.osgi.resource.Wire) LinkedHashMap(java.util.LinkedHashMap) ResolutionException(org.osgi.service.resolver.ResolutionException) Requirement(org.osgi.resource.Requirement) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

WireImpl (aQute.bnd.osgi.resource.WireImpl)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1 Capability (org.osgi.resource.Capability)1 Requirement (org.osgi.resource.Requirement)1 Resource (org.osgi.resource.Resource)1 Wire (org.osgi.resource.Wire)1 ResolutionException (org.osgi.service.resolver.ResolutionException)1