Search in sources :

Example 6 with BundleRequirementImpl

use of org.apache.felix.framework.wiring.BundleRequirementImpl in project felix by apache.

the class ManifestParser method convertImports.

private static List<BundleRequirement> convertImports(List<ParsedHeaderClause> clauses, BundleRevision owner) {
    // Now convert generic header clauses into requirements.
    List<BundleRequirement> reqList = new ArrayList<BundleRequirement>();
    for (ParsedHeaderClause clause : clauses) {
        for (String path : clause.m_paths) {
            // Prepend the package name to the array of attributes.
            Map<String, Object> attrs = clause.m_attrs;
            // Note that we use a linked hash map here to ensure the
            // package attribute is first, which will make indexing
            // more efficient.
            // TODO: OSGi R4.3 - This is ordering is kind of hacky.
            // Prepend the package name to the array of attributes.
            Map<String, Object> newAttrs = new LinkedHashMap<String, Object>(attrs.size() + 1);
            // We want this first from an indexing perspective.
            newAttrs.put(BundleRevision.PACKAGE_NAMESPACE, path);
            newAttrs.putAll(attrs);
            // But we need to put it again to make sure it wasn't overwritten.
            newAttrs.put(BundleRevision.PACKAGE_NAMESPACE, path);
            // Create filter now so we can inject filter directive.
            SimpleFilter sf = SimpleFilter.convert(newAttrs);
            // Inject filter directive.
            // TODO: OSGi R4.3 - Can we insert this on demand somehow?
            Map<String, String> dirs = clause.m_dirs;
            Map<String, String> newDirs = new HashMap<String, String>(dirs.size() + 1);
            newDirs.putAll(dirs);
            newDirs.put(Constants.FILTER_DIRECTIVE, sf.toString());
            // Create package requirement and add to requirement list.
            reqList.add(new BundleRequirementImpl(owner, BundleRevision.PACKAGE_NAMESPACE, newDirs, Collections.EMPTY_MAP, sf));
        }
    }
    return reqList;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SimpleFilter(org.apache.felix.framework.capabilityset.SimpleFilter) ArrayList(java.util.ArrayList) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) BundleRequirementImpl(org.apache.felix.framework.wiring.BundleRequirementImpl) LinkedHashMap(java.util.LinkedHashMap)

Example 7 with BundleRequirementImpl

use of org.apache.felix.framework.wiring.BundleRequirementImpl in project felix by apache.

the class ManifestParser method convertRequireCapabilities.

private static List<BundleRequirement> convertRequireCapabilities(List<ParsedHeaderClause> clauses, BundleRevision owner) throws BundleException {
    // Now convert generic header clauses into requirements.
    List<BundleRequirement> reqList = new ArrayList<BundleRequirement>();
    for (ParsedHeaderClause clause : clauses) {
        try {
            String filterStr = clause.m_dirs.get(Constants.FILTER_DIRECTIVE);
            SimpleFilter sf = (filterStr != null) ? SimpleFilter.parse(filterStr) : new SimpleFilter(null, null, SimpleFilter.MATCH_ALL);
            for (String path : clause.m_paths) {
                if (path.startsWith("osgi.wiring.")) {
                    throw new BundleException("Manifest cannot use Require-Capability for '" + path + "' namespace.");
                }
                // Create requirement and add to requirement list.
                reqList.add(new BundleRequirementImpl(owner, path, clause.m_dirs, clause.m_attrs, sf));
            }
        } catch (Exception ex) {
            throw new BundleException("Error creating requirement: " + ex);
        }
    }
    return reqList;
}
Also used : SimpleFilter(org.apache.felix.framework.capabilityset.SimpleFilter) ArrayList(java.util.ArrayList) BundleException(org.osgi.framework.BundleException) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) BundleRequirementImpl(org.apache.felix.framework.wiring.BundleRequirementImpl) BundleException(org.osgi.framework.BundleException)

Example 8 with BundleRequirementImpl

use of org.apache.felix.framework.wiring.BundleRequirementImpl in project felix by apache.

the class StatefulResolver method isAllowedDynamicImport.

// This method duplicates a lot of logic from:
// ResolverImpl.getDynamicImportCandidates()
// This is only a rough check since it doesn't include resolver hooks.
boolean isAllowedDynamicImport(BundleRevision revision, String pkgName) {
    // package be dynamically imported.
    if ((revision.getWiring() == null) || pkgName.length() == 0) {
        return false;
    }
    // If the revision doesn't have dynamic imports, then just return
    // immediately.
    List<BundleRequirement> dynamics = Util.getDynamicRequirements(revision.getWiring().getRequirements(null));
    if ((dynamics == null) || dynamics.isEmpty()) {
        return false;
    }
    // attempt to dynamically import it.
    for (BundleCapability cap : revision.getWiring().getCapabilities(null)) {
        if (cap.getNamespace().equals(BundleRevision.PACKAGE_NAMESPACE) && cap.getAttributes().get(BundleRevision.PACKAGE_NAMESPACE).equals(pkgName)) {
            return false;
        }
    }
    // we cannot dynamically import it.
    if (((BundleWiringImpl) revision.getWiring()).hasPackageSource(pkgName)) {
        return false;
    }
    // Loop through the importer's dynamic requirements to determine if
    // there is a matching one for the package from which we want to
    // load a class.
    Map<String, Object> attrs = Collections.singletonMap(BundleRevision.PACKAGE_NAMESPACE, (Object) pkgName);
    BundleRequirementImpl req = new BundleRequirementImpl(revision, BundleRevision.PACKAGE_NAMESPACE, Collections.EMPTY_MAP, attrs);
    List<BundleCapability> candidates = findProviders(req, false);
    // Try to find a dynamic requirement that matches the capabilities.
    BundleRequirementImpl dynReq = null;
    for (int dynIdx = 0; (candidates.size() > 0) && (dynReq == null) && (dynIdx < dynamics.size()); dynIdx++) {
        for (Iterator<BundleCapability> itCand = candidates.iterator(); (dynReq == null) && itCand.hasNext(); ) {
            Capability cap = itCand.next();
            if (CapabilitySet.matches(cap, ((BundleRequirementImpl) dynamics.get(dynIdx)).getFilter())) {
                dynReq = (BundleRequirementImpl) dynamics.get(dynIdx);
            }
        }
    }
    // any candidates that do not match it.
    if (dynReq != null) {
        for (Iterator<BundleCapability> itCand = candidates.iterator(); itCand.hasNext(); ) {
            Capability cap = itCand.next();
            if (!CapabilitySet.matches(cap, dynReq.getFilter())) {
                itCand.remove();
            }
        }
    } else {
        candidates.clear();
    }
    return !candidates.isEmpty();
}
Also used : BundleCapability(org.osgi.framework.wiring.BundleCapability) Capability(org.osgi.resource.Capability) BundleCapability(org.osgi.framework.wiring.BundleCapability) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) BundleRequirementImpl(org.apache.felix.framework.wiring.BundleRequirementImpl)

Example 9 with BundleRequirementImpl

use of org.apache.felix.framework.wiring.BundleRequirementImpl in project felix by apache.

the class ManifestParser method parseFragmentHost.

private static List<BundleRequirementImpl> parseFragmentHost(Logger logger, BundleRevision owner, Map<String, Object> headerMap) throws BundleException {
    List<BundleRequirementImpl> reqs = new ArrayList<BundleRequirementImpl>();
    String mv = getManifestVersion(headerMap);
    if ((mv != null) && mv.equals("2")) {
        List<ParsedHeaderClause> clauses = parseStandardHeader((String) headerMap.get(Constants.FRAGMENT_HOST));
        if (clauses.size() > 0) {
            // Make sure that only one fragment host symbolic name is specified.
            if (clauses.size() > 1) {
                throw new BundleException("Fragments cannot have multiple hosts: " + headerMap.get(Constants.FRAGMENT_HOST));
            } else if (clauses.get(0).m_paths.size() > 1) {
                throw new BundleException("Fragments cannot have multiple hosts: " + headerMap.get(Constants.FRAGMENT_HOST));
            }
            // If the bundle-version attribute is specified, then convert
            // it to the proper type.
            Object value = clauses.get(0).m_attrs.get(Constants.BUNDLE_VERSION_ATTRIBUTE);
            value = (value == null) ? "0.0.0" : value;
            if (value != null) {
                clauses.get(0).m_attrs.put(Constants.BUNDLE_VERSION_ATTRIBUTE, VersionRange.parse(value.toString()));
            }
            // Note that we use a linked hash map here to ensure the
            // host symbolic name is first, which will make indexing
            // more efficient.
            // TODO: OSGi R4.3 - This is ordering is kind of hacky.
            // Prepend the host symbolic name to the map of attributes.
            Map<String, Object> attrs = clauses.get(0).m_attrs;
            Map<String, Object> newAttrs = new LinkedHashMap<String, Object>(attrs.size() + 1);
            // We want this first from an indexing perspective.
            newAttrs.put(BundleRevision.HOST_NAMESPACE, clauses.get(0).m_paths.get(0));
            newAttrs.putAll(attrs);
            // But we need to put it again to make sure it wasn't overwritten.
            newAttrs.put(BundleRevision.HOST_NAMESPACE, clauses.get(0).m_paths.get(0));
            // Create filter now so we can inject filter directive.
            SimpleFilter sf = SimpleFilter.convert(newAttrs);
            // Inject filter directive.
            // TODO: OSGi R4.3 - Can we insert this on demand somehow?
            Map<String, String> dirs = clauses.get(0).m_dirs;
            Map<String, String> newDirs = new HashMap<String, String>(dirs.size() + 1);
            newDirs.putAll(dirs);
            newDirs.put(Constants.FILTER_DIRECTIVE, sf.toString());
            reqs.add(new BundleRequirementImpl(owner, BundleRevision.HOST_NAMESPACE, newDirs, newAttrs));
        }
    } else if (headerMap.get(Constants.FRAGMENT_HOST) != null) {
        String s = (String) headerMap.get(Constants.BUNDLE_SYMBOLICNAME);
        s = (s == null) ? (String) headerMap.get(Constants.BUNDLE_NAME) : s;
        s = (s == null) ? headerMap.toString() : s;
        logger.log(Logger.LOG_WARNING, "Only R4 bundles can be fragments: " + s);
    }
    return reqs;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) BundleRequirementImpl(org.apache.felix.framework.wiring.BundleRequirementImpl) LinkedHashMap(java.util.LinkedHashMap) SimpleFilter(org.apache.felix.framework.capabilityset.SimpleFilter) BundleException(org.osgi.framework.BundleException)

Example 10 with BundleRequirementImpl

use of org.apache.felix.framework.wiring.BundleRequirementImpl in project felix by apache.

the class ManifestParser method convertRequires.

private static List<BundleRequirementImpl> convertRequires(List<ParsedHeaderClause> clauses, BundleRevision owner) {
    List<BundleRequirementImpl> reqList = new ArrayList<BundleRequirementImpl>();
    for (ParsedHeaderClause clause : clauses) {
        for (String path : clause.m_paths) {
            // Prepend the bundle symbolic name to the array of attributes.
            Map<String, Object> attrs = clause.m_attrs;
            // Note that we use a linked hash map here to ensure the
            // symbolic name attribute is first, which will make indexing
            // more efficient.
            // TODO: OSGi R4.3 - This is ordering is kind of hacky.
            // Prepend the symbolic name to the array of attributes.
            Map<String, Object> newAttrs = new LinkedHashMap<String, Object>(attrs.size() + 1);
            // We want this first from an indexing perspective.
            newAttrs.put(BundleRevision.BUNDLE_NAMESPACE, path);
            newAttrs.putAll(attrs);
            // But we need to put it again to make sure it wasn't overwritten.
            newAttrs.put(BundleRevision.BUNDLE_NAMESPACE, path);
            // Create filter now so we can inject filter directive.
            SimpleFilter sf = SimpleFilter.convert(newAttrs);
            // Inject filter directive.
            // TODO: OSGi R4.3 - Can we insert this on demand somehow?
            Map<String, String> dirs = clause.m_dirs;
            Map<String, String> newDirs = new HashMap<String, String>(dirs.size() + 1);
            newDirs.putAll(dirs);
            newDirs.put(Constants.FILTER_DIRECTIVE, sf.toString());
            // Create package requirement and add to requirement list.
            reqList.add(new BundleRequirementImpl(owner, BundleRevision.BUNDLE_NAMESPACE, newDirs, newAttrs));
        }
    }
    return reqList;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SimpleFilter(org.apache.felix.framework.capabilityset.SimpleFilter) ArrayList(java.util.ArrayList) BundleRequirementImpl(org.apache.felix.framework.wiring.BundleRequirementImpl) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

BundleRequirementImpl (org.apache.felix.framework.wiring.BundleRequirementImpl)11 ArrayList (java.util.ArrayList)9 BundleRequirement (org.osgi.framework.wiring.BundleRequirement)8 SimpleFilter (org.apache.felix.framework.capabilityset.SimpleFilter)7 BundleCapability (org.osgi.framework.wiring.BundleCapability)5 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 BundleException (org.osgi.framework.BundleException)4 BundleRevision (org.osgi.framework.wiring.BundleRevision)3 BundleWire (org.osgi.framework.wiring.BundleWire)3 Capability (org.osgi.resource.Capability)3 List (java.util.List)2 ResolutionException (org.osgi.service.resolver.ResolutionException)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 PrivilegedActionException (java.security.PrivilegedActionException)1 CapabilitySet (org.apache.felix.framework.capabilityset.CapabilitySet)1 CandidateComparator (org.apache.felix.framework.resolver.CandidateComparator)1 ResolveException (org.apache.felix.framework.resolver.ResolveException)1 ResourceNotFoundException (org.apache.felix.framework.resolver.ResourceNotFoundException)1