Search in sources :

Example 1 with CapabilityImpl

use of org.apache.karaf.features.internal.resolver.CapabilityImpl in project karaf by apache.

the class StaxParser method parseResource.

private static ResourceImpl parseResource(URI repositoryUrl, XMLStreamReader reader) {
    try {
        if (reader.getAttributeCount() > 0) {
            throw new IllegalStateException("Unexpected attribute '" + reader.getAttributeLocalName(0) + "'");
        }
        ResourceImpl resource = new ResourceImpl();
        int event;
        while ((event = reader.nextTag()) == START_ELEMENT) {
            String element = reader.getLocalName();
            switch(element) {
                case CAPABILITY:
                    CapabilityImpl cap = parseCapability(reader, resource);
                    // Resolve relative resource urls now
                    if (repositoryUrl != null && ContentNamespace.CONTENT_NAMESPACE.equals(cap.getNamespace())) {
                        Object url = cap.getAttributes().get(ContentNamespace.CAPABILITY_URL_ATTRIBUTE);
                        if (url instanceof String) {
                            url = repositoryUrl.resolve(url.toString()).toString();
                            cap.getAttributes().put(ContentNamespace.CAPABILITY_URL_ATTRIBUTE, url);
                        }
                    }
                    resource.addCapability(cap);
                    break;
                case REQUIREMENT:
                    resource.addRequirement(parseRequirement(reader, resource));
                    break;
                default:
                    while ((event = reader.next()) != END_ELEMENT) {
                        switch(event) {
                            case START_ELEMENT:
                                throw new IllegalStateException("Unexpected element '" + reader.getLocalName() + "' inside 'resource' element");
                            case CHARACTERS:
                                throw new IllegalStateException("Unexpected text inside 'resource' element");
                            default:
                                break;
                        }
                    }
                    break;
            }
        }
        // Sanity check
        sanityCheckEndElement(reader, event, RESOURCE);
        return resource;
    } catch (Exception e) {
        Location loc = reader.getLocation();
        if (loc != null) {
            throw new IllegalStateException("Error while parsing resource at line " + loc.getLineNumber() + " and column " + loc.getColumnNumber(), e);
        } else {
            throw new IllegalStateException("Error while parsing resource", e);
        }
    }
}
Also used : ResourceImpl(org.apache.karaf.features.internal.resolver.ResourceImpl) CapabilityImpl(org.apache.karaf.features.internal.resolver.CapabilityImpl) XMLStreamException(javax.xml.stream.XMLStreamException) Location(javax.xml.stream.Location)

Example 2 with CapabilityImpl

use of org.apache.karaf.features.internal.resolver.CapabilityImpl in project karaf by apache.

the class SubsystemResolver method prepare.

public void prepare(Collection<Feature> allFeatures, Map<String, Set<String>> requirements, Map<String, Set<BundleRevision>> system) throws Exception {
    // Build subsystems on the fly
    for (Map.Entry<String, Set<String>> entry : requirements.entrySet()) {
        String[] parts = entry.getKey().split("/");
        if (root == null) {
            root = new Subsystem(parts[0]);
        } else if (!root.getName().equals(parts[0])) {
            throw new IllegalArgumentException("Can not use multiple roots: " + root.getName() + ", " + parts[0]);
        }
        Subsystem ss = root;
        for (int i = 1; i < parts.length; i++) {
            ss = getOrCreateChild(ss, parts[i]);
        }
        for (String requirement : entry.getValue()) {
            ss.require(requirement);
        }
    }
    if (root == null) {
        return;
    }
    // Pre-resolve
    root.build(allFeatures);
    // Add system resources
    BundleRevision sysBundleRev = null;
    boolean hasEeCap = false;
    for (Map.Entry<String, Set<BundleRevision>> entry : system.entrySet()) {
        Subsystem ss = null;
        String[] parts = entry.getKey().split("/");
        String path = parts[0];
        if (path.equals(root.getName())) {
            ss = root;
        }
        for (int i = 1; ss != null && i < parts.length; i++) {
            path += "/" + parts[i];
            ss = ss.getChild(path);
        }
        if (ss != null) {
            ResourceImpl dummy = new ResourceImpl("dummy", "dummy", Version.emptyVersion);
            for (BundleRevision res : entry.getValue()) {
                // We need to explicitely provide service capabilities for bundles
                // We use both actual services and services declared from the headers
                // TODO: use actual services
                Map<String, String> headers = new DictionaryAsMap<>(res.getBundle().getHeaders());
                Resource tmp = ResourceBuilder.build(res.getBundle().getLocation(), headers);
                for (Capability cap : tmp.getCapabilities(ServiceNamespace.SERVICE_NAMESPACE)) {
                    dummy.addCapability(new CapabilityImpl(dummy, cap.getNamespace(), cap.getDirectives(), cap.getAttributes()));
                }
                ss.addSystemResource(res);
                for (Capability cap : res.getCapabilities(null)) {
                    hasEeCap |= cap.getNamespace().equals(EXECUTION_ENVIRONMENT_NAMESPACE);
                }
                if (res.getBundle().getBundleId() == 0) {
                    sysBundleRev = res;
                }
            }
            ss.addSystemResource(dummy);
        }
    }
    // Under Equinox, the osgi.ee capabilities are not provided by the system bundle
    if (!hasEeCap && sysBundleRev != null) {
        String provideCaps = sysBundleRev.getBundle().getHeaders().get(PROVIDE_CAPABILITY);
        environmentResource = new ResourceImpl("environment", "karaf.environment", Version.emptyVersion);
        environmentResource.addCapabilities(ResourceBuilder.parseCapability(environmentResource, provideCaps));
        root.addSystemResource(environmentResource);
    }
}
Also used : CapabilitySet(org.apache.karaf.features.internal.resolver.CapabilitySet) Capability(org.osgi.resource.Capability) DictionaryAsMap(org.apache.felix.utils.collections.DictionaryAsMap) Resource(org.osgi.resource.Resource) ResourceImpl(org.apache.karaf.features.internal.resolver.ResourceImpl) CapabilityImpl(org.apache.karaf.features.internal.resolver.CapabilityImpl) BundleRevision(org.osgi.framework.wiring.BundleRevision) DictionaryAsMap(org.apache.felix.utils.collections.DictionaryAsMap)

Example 3 with CapabilityImpl

use of org.apache.karaf.features.internal.resolver.CapabilityImpl in project karaf by apache.

the class Subsystem method cloneResource.

ResourceImpl cloneResource(Resource resource) {
    ResourceImpl res = new ResourceImpl();
    for (Capability cap : resource.getCapabilities(null)) {
        res.addCapability(new CapabilityImpl(res, cap.getNamespace(), new HashMap<>(cap.getDirectives()), new HashMap<>(cap.getAttributes())));
    }
    for (Requirement req : resource.getRequirements(null)) {
        SimpleFilter sf;
        if (req instanceof RequirementImpl) {
            sf = ((RequirementImpl) req).getFilter();
        } else if (req.getDirectives().containsKey(REQUIREMENT_FILTER_DIRECTIVE)) {
            sf = SimpleFilter.parse(req.getDirectives().get(REQUIREMENT_FILTER_DIRECTIVE));
        } else {
            sf = SimpleFilter.convert(req.getAttributes());
        }
        res.addRequirement(new RequirementImpl(res, req.getNamespace(), new HashMap<>(req.getDirectives()), new HashMap<>(req.getAttributes()), sf));
    }
    return res;
}
Also used : ResourceUtils.addIdentityRequirement(org.apache.karaf.features.internal.resolver.ResourceUtils.addIdentityRequirement) ResourceUtils.toFeatureRequirement(org.apache.karaf.features.internal.resolver.ResourceUtils.toFeatureRequirement) Requirement(org.osgi.resource.Requirement) ResourceImpl(org.apache.karaf.features.internal.resolver.ResourceImpl) CapabilityImpl(org.apache.karaf.features.internal.resolver.CapabilityImpl) RequirementImpl(org.apache.karaf.features.internal.resolver.RequirementImpl) Capability(org.osgi.resource.Capability) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) SimpleFilter(org.apache.karaf.features.internal.resolver.SimpleFilter)

Aggregations

CapabilityImpl (org.apache.karaf.features.internal.resolver.CapabilityImpl)3 ResourceImpl (org.apache.karaf.features.internal.resolver.ResourceImpl)3 Capability (org.osgi.resource.Capability)2 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Location (javax.xml.stream.Location)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 DictionaryAsMap (org.apache.felix.utils.collections.DictionaryAsMap)1 CapabilitySet (org.apache.karaf.features.internal.resolver.CapabilitySet)1 RequirementImpl (org.apache.karaf.features.internal.resolver.RequirementImpl)1 ResourceUtils.addIdentityRequirement (org.apache.karaf.features.internal.resolver.ResourceUtils.addIdentityRequirement)1 ResourceUtils.toFeatureRequirement (org.apache.karaf.features.internal.resolver.ResourceUtils.toFeatureRequirement)1 SimpleFilter (org.apache.karaf.features.internal.resolver.SimpleFilter)1 BundleRevision (org.osgi.framework.wiring.BundleRevision)1 Requirement (org.osgi.resource.Requirement)1 Resource (org.osgi.resource.Resource)1