Search in sources :

Example 1 with StringArrayMap

use of org.apache.karaf.features.internal.util.StringArrayMap in project karaf by apache.

the class ResourceUtils method toFeatureRequirement.

/**
 * Changes feature identifier (<code>name[/version]</code>) into a requirement specification.
 * The OSGi manifest header for a feature will be: <code>osgi.identity;osgi.identity=feature-name;type=karaf.feature[;version=feature-version];filter:=filter-from-attrs</code>.
 *
 * @param feature The feature name.
 * @return The feature requirement.
 */
public static String toFeatureRequirement(String feature) {
    String[] parts = feature.split("/");
    Map<String, Object> attrs = new StringArrayMap<>(parts.length > 1 ? 3 : 2);
    attrs.put(IDENTITY_NAMESPACE, parts[0]);
    attrs.put(CAPABILITY_TYPE_ATTRIBUTE, TYPE_FEATURE);
    if (parts.length > 1) {
        attrs.put(CAPABILITY_VERSION_ATTRIBUTE, new VersionRange(parts[1]));
    }
    Map<String, String> dirs = Collections.singletonMap(Constants.FILTER_DIRECTIVE, SimpleFilter.convert(attrs).toString());
    return new RequirementImpl(null, IDENTITY_NAMESPACE, dirs, attrs).toString();
}
Also used : StringArrayMap(org.apache.karaf.features.internal.util.StringArrayMap) VersionRange(org.apache.felix.utils.version.VersionRange)

Example 2 with StringArrayMap

use of org.apache.karaf.features.internal.util.StringArrayMap 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 StringArrayMap<>(cap.getDirectives()), new StringArrayMap<>(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 StringArrayMap<>(req.getDirectives()), new StringArrayMap<>(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) StringArrayMap(org.apache.karaf.features.internal.util.StringArrayMap) SimpleFilter(org.apache.karaf.features.internal.resolver.SimpleFilter)

Example 3 with StringArrayMap

use of org.apache.karaf.features.internal.util.StringArrayMap in project karaf by apache.

the class ResourceBuilder method doBuild.

private static ResourceImpl doBuild(ResourceImpl resource, String uri, Map<String, String> headerMap, boolean removeServiceRequirements) throws BundleException {
    // Verify that only manifest version 2 is specified.
    String manifestVersion = getManifestVersion(headerMap);
    if (manifestVersion == null || !manifestVersion.equals("2")) {
        throw new BundleException("Unsupported 'Bundle-ManifestVersion' value: " + manifestVersion);
    }
    // 
    // Parse bundle version.
    // 
    Version bundleVersion = Version.emptyVersion;
    if (headerMap.get(Constants.BUNDLE_VERSION) != null) {
        bundleVersion = VersionTable.getVersion(headerMap.get(Constants.BUNDLE_VERSION));
    }
    // 
    // Parse bundle symbolic name.
    // 
    String bundleSymbolicName;
    ParsedHeaderClause bundleCap = parseBundleSymbolicName(headerMap);
    if (bundleCap == null) {
        throw new BundleException("Bundle manifest must include bundle symbolic name");
    }
    bundleSymbolicName = (String) bundleCap.attrs.get(BundleRevision.BUNDLE_NAMESPACE);
    // Now that we have symbolic name and version, create the resource
    String type = headerMap.get(Constants.FRAGMENT_HOST) == null ? IdentityNamespace.TYPE_BUNDLE : IdentityNamespace.TYPE_FRAGMENT;
    {
        Map<String, Object> attrs = new StringArrayMap<>(3);
        attrs.put(IdentityNamespace.IDENTITY_NAMESPACE, bundleSymbolicName);
        attrs.put(IdentityNamespace.CAPABILITY_TYPE_ATTRIBUTE, type);
        attrs.put(IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE, bundleVersion);
        CapabilityImpl identity = new CapabilityImpl(resource, IdentityNamespace.IDENTITY_NAMESPACE, Collections.emptyMap(), attrs);
        resource.addCapability(identity);
    }
    if (uri != null) {
        resource.addCapability(new CapabilityImpl(resource, ContentNamespace.CONTENT_NAMESPACE, Collections.emptyMap(), Collections.singletonMap(ContentNamespace.CAPABILITY_URL_ATTRIBUTE, uri)));
    }
    // dependencies.
    if (headerMap.get(Constants.FRAGMENT_HOST) == null) {
        // All non-fragment bundles have bundle capability.
        resource.addCapability(new CapabilityImpl(resource, BundleRevision.BUNDLE_NAMESPACE, bundleCap.dirs, bundleCap.attrs));
        // A non-fragment bundle can choose to not have a host capability.
        String attachment = bundleCap.dirs.get(Constants.FRAGMENT_ATTACHMENT_DIRECTIVE);
        attachment = (attachment == null) ? Constants.FRAGMENT_ATTACHMENT_RESOLVETIME : attachment;
        if (!attachment.equalsIgnoreCase(Constants.FRAGMENT_ATTACHMENT_NEVER)) {
            Map<String, Object> hostAttrs = new StringArrayMap<>(bundleCap.attrs.size());
            for (Map.Entry<String, Object> e : bundleCap.attrs.entrySet()) {
                String k = e.getKey();
                if (BundleRevision.BUNDLE_NAMESPACE.equals(k)) {
                    k = BundleRevision.HOST_NAMESPACE;
                }
                hostAttrs.put(k, e.getValue());
            }
            resource.addCapability(new CapabilityImpl(resource, BundleRevision.HOST_NAMESPACE, bundleCap.dirs, hostAttrs));
        }
    }
    // 
    // Parse Fragment-Host.
    // 
    List<RequirementImpl> hostReqs = parseFragmentHost(resource, headerMap);
    // 
    // Parse Require-Bundle
    // 
    List<ParsedHeaderClause> rbClauses = parseStandardHeader(headerMap.get(Constants.REQUIRE_BUNDLE));
    rbClauses = normalizeRequireClauses(rbClauses);
    List<Requirement> rbReqs = convertRequires(rbClauses, resource);
    // 
    // Parse Import-Package.
    // 
    List<ParsedHeaderClause> importClauses = parseStandardHeader(headerMap.get(Constants.IMPORT_PACKAGE));
    importClauses = normalizeImportClauses(importClauses);
    List<Requirement> importReqs = convertImports(importClauses, resource);
    // 
    // Parse DynamicImport-Package.
    // 
    List<ParsedHeaderClause> dynamicClauses = parseStandardHeader(headerMap.get(Constants.DYNAMICIMPORT_PACKAGE));
    dynamicClauses = normalizeDynamicImportClauses(dynamicClauses);
    List<Requirement> dynamicReqs = convertImports(dynamicClauses, resource);
    // 
    // Parse Require-Capability.
    // 
    List<ParsedHeaderClause> requireClauses = parseStandardHeader(headerMap.get(Constants.REQUIRE_CAPABILITY));
    requireClauses = normalizeRequireCapabilityClauses(requireClauses);
    List<Requirement> requireReqs = convertRequireCapabilities(requireClauses, resource);
    // 
    // Parse Bundle-RequiredExecutionEnvironment.
    // 
    List<Requirement> breeReqs = parseBreeHeader(headerMap.get(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT), resource);
    // 
    // Parse Export-Package.
    // 
    List<ParsedHeaderClause> exportClauses = parseStandardHeader(headerMap.get(Constants.EXPORT_PACKAGE));
    exportClauses = normalizeExportClauses(exportClauses, bundleSymbolicName, bundleVersion);
    List<Capability> exportCaps = convertExports(exportClauses, resource);
    // 
    // Parse Provide-Capability.
    // 
    List<ParsedHeaderClause> provideClauses = parseStandardHeader(headerMap.get(Constants.PROVIDE_CAPABILITY));
    provideClauses = normalizeProvideCapabilityClauses(provideClauses);
    List<Capability> provideCaps = convertProvideCapabilities(provideClauses, resource);
    // 
    // Parse Import-Service and Export-Service
    // if Require-Capability and Provide-Capability are not set for services
    // 
    boolean hasServiceReferenceCapability = false;
    for (Capability cap : exportCaps) {
        hasServiceReferenceCapability |= ServiceNamespace.SERVICE_NAMESPACE.equals(cap.getNamespace());
    }
    if (!hasServiceReferenceCapability) {
        List<ParsedHeaderClause> exportServices = parseStandardHeader(headerMap.get(Constants.EXPORT_SERVICE));
        List<Capability> caps = convertExportService(exportServices, resource);
        provideCaps.addAll(caps);
    }
    boolean hasServiceReferenceRequirement = false;
    for (Requirement req : requireReqs) {
        hasServiceReferenceRequirement |= ServiceNamespace.SERVICE_NAMESPACE.equals(req.getNamespace());
    }
    if (!hasServiceReferenceRequirement) {
        List<ParsedHeaderClause> importServices = parseStandardHeader(headerMap.get(Constants.IMPORT_SERVICE));
        List<Requirement> reqs = convertImportService(importServices, resource);
        if (!reqs.isEmpty()) {
            requireReqs.addAll(reqs);
            hasServiceReferenceRequirement = true;
        }
    }
    if (hasServiceReferenceRequirement && removeServiceRequirements) {
        for (Iterator<Requirement> iterator = requireReqs.iterator(); iterator.hasNext(); ) {
            Requirement req = iterator.next();
            if (ServiceNamespace.SERVICE_NAMESPACE.equals(req.getNamespace())) {
                iterator.remove();
            }
        }
    }
    // Combine all capabilities.
    resource.addCapabilities(exportCaps);
    resource.addCapabilities(provideCaps);
    // Combine all requirements.
    resource.addRequirements(hostReqs);
    resource.addRequirements(importReqs);
    resource.addRequirements(rbReqs);
    resource.addRequirements(requireReqs);
    resource.addRequirements(dynamicReqs);
    return resource;
}
Also used : Capability(org.osgi.resource.Capability) StringArrayMap(org.apache.karaf.features.internal.util.StringArrayMap) Requirement(org.osgi.resource.Requirement) Version(org.osgi.framework.Version) BundleException(org.osgi.framework.BundleException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) StringArrayMap(org.apache.karaf.features.internal.util.StringArrayMap)

Example 4 with StringArrayMap

use of org.apache.karaf.features.internal.util.StringArrayMap in project karaf by apache.

the class ResourceUtils method addIdentityRequirement.

public static void addIdentityRequirement(ResourceImpl resource, Resource required, boolean mandatory) {
    for (Capability cap : required.getCapabilities(null)) {
        if (cap.getNamespace().equals(IDENTITY_NAMESPACE)) {
            Map<String, Object> attributes = cap.getAttributes();
            Map<String, String> dirs = new StringArrayMap<>(1);
            dirs.put(REQUIREMENT_RESOLUTION_DIRECTIVE, mandatory ? RESOLUTION_MANDATORY : RESOLUTION_OPTIONAL);
            Version version = (Version) attributes.get(CAPABILITY_VERSION_ATTRIBUTE);
            Map<String, Object> attrs = new StringArrayMap<>(version != null ? 3 : 2);
            attrs.put(IDENTITY_NAMESPACE, attributes.get(IDENTITY_NAMESPACE));
            attrs.put(CAPABILITY_TYPE_ATTRIBUTE, attributes.get(CAPABILITY_TYPE_ATTRIBUTE));
            if (version != null) {
                attrs.put(CAPABILITY_VERSION_ATTRIBUTE, new VersionRange(version, true));
            }
            resource.addRequirement(new RequirementImpl(resource, IDENTITY_NAMESPACE, dirs, attrs));
        }
    }
}
Also used : Capability(org.osgi.resource.Capability) StringArrayMap(org.apache.karaf.features.internal.util.StringArrayMap) Version(org.osgi.framework.Version) VersionRange(org.apache.felix.utils.version.VersionRange)

Aggregations

StringArrayMap (org.apache.karaf.features.internal.util.StringArrayMap)4 Capability (org.osgi.resource.Capability)3 VersionRange (org.apache.felix.utils.version.VersionRange)2 Version (org.osgi.framework.Version)2 Requirement (org.osgi.resource.Requirement)2 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 CapabilityImpl (org.apache.karaf.features.internal.resolver.CapabilityImpl)1 RequirementImpl (org.apache.karaf.features.internal.resolver.RequirementImpl)1 ResourceImpl (org.apache.karaf.features.internal.resolver.ResourceImpl)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 BundleException (org.osgi.framework.BundleException)1