Search in sources :

Example 1 with SimpleFilter

use of org.apache.felix.framework.capabilityset.SimpleFilter in project felix by apache.

the class StatefulResolver method findProvidersInternal.

synchronized List<BundleCapability> findProvidersInternal(final ResolverHookRecord record, final Requirement req, final boolean obeyMandatory, final boolean invokeHooksAndSecurity) {
    List<BundleCapability> result = new ArrayList<BundleCapability>();
    CapabilitySet capSet = m_capSets.get(req.getNamespace());
    if (capSet != null) {
        // Get the requirement's filter; if this is our own impl we
        // have a shortcut to get the already parsed filter, otherwise
        // we must parse it from the directive.
        SimpleFilter sf;
        if (req instanceof BundleRequirementImpl) {
            sf = ((BundleRequirementImpl) req).getFilter();
        } else {
            String filter = req.getDirectives().get(Constants.FILTER_DIRECTIVE);
            if (filter == null) {
                sf = new SimpleFilter(null, null, SimpleFilter.MATCH_ALL);
            } else {
                sf = SimpleFilter.parse(filter);
            }
        }
        // Find the matching candidates.
        Set<Capability> matches = capSet.match(sf, obeyMandatory);
        // Filter matching candidates.
        for (Capability cap : matches) {
            if (!(cap instanceof BundleCapability))
                continue;
            BundleCapability bcap = (BundleCapability) cap;
            // Filter according to security.
            if (invokeHooksAndSecurity && filteredBySecurity((BundleRequirement) req, bcap)) {
                continue;
            }
            // dynamic attachment of fragments.
            if (req.getNamespace().equals(BundleRevision.HOST_NAMESPACE) && (bcap.getRevision().getWiring() != null)) {
                continue;
            }
            result.add(bcap);
        }
    }
    if (invokeHooksAndSecurity) {
        // based on a whitelist and/or fine-grained candidate filtering.
        if (!result.isEmpty() && !record.getResolverHookRefs().isEmpty()) {
            // from disallowed revisions.
            if (record.getBundleRevisionWhitelist() != null) {
                for (Iterator<BundleCapability> it = result.iterator(); it.hasNext(); ) {
                    if (!record.getBundleRevisionWhitelist().contains(it.next().getRevision())) {
                        it.remove();
                    }
                }
            }
            // Now give the hooks a chance to do fine-grained filtering.
            ShrinkableCollection<BundleCapability> shrinkable = new ShrinkableCollection<BundleCapability>(result);
            for (ResolverHook hook : record.getResolverHooks()) {
                try {
                    Felix.m_secureAction.invokeResolverHookMatches(hook, (BundleRequirement) req, shrinkable);
                } catch (Throwable th) {
                    m_logger.log(Logger.LOG_WARNING, "Resolver hook exception.", th);
                }
            }
        }
    }
    Collections.sort(result, new CandidateComparator());
    return result;
}
Also used : BundleCapability(org.osgi.framework.wiring.BundleCapability) Capability(org.osgi.resource.Capability) ResolverHook(org.osgi.framework.hooks.resolver.ResolverHook) ArrayList(java.util.ArrayList) BundleRequirementImpl(org.apache.felix.framework.wiring.BundleRequirementImpl) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) CandidateComparator(org.apache.felix.framework.resolver.CandidateComparator) SimpleFilter(org.apache.felix.framework.capabilityset.SimpleFilter) CapabilitySet(org.apache.felix.framework.capabilityset.CapabilitySet) ShrinkableCollection(org.apache.felix.framework.util.ShrinkableCollection) BundleCapability(org.osgi.framework.wiring.BundleCapability)

Example 2 with SimpleFilter

use of org.apache.felix.framework.capabilityset.SimpleFilter in project felix by apache.

the class ManifestParser method parseBreeHeader.

private static List<BundleRequirement> parseBreeHeader(String header, BundleRevision owner) {
    List<String> filters = new ArrayList<String>();
    for (String entry : parseDelimitedString(header, ",")) {
        List<String> names = parseDelimitedString(entry, "/");
        List<String> left = parseDelimitedString(names.get(0), "-");
        String lName = left.get(0);
        Version lVer;
        try {
            lVer = Version.parseVersion(left.get(1));
        } catch (Exception ex) {
            // Version doesn't parse. Make it part of the name.
            lName = names.get(0);
            lVer = null;
        }
        String rName = null;
        Version rVer = null;
        if (names.size() > 1) {
            List<String> right = parseDelimitedString(names.get(1), "-");
            rName = right.get(0);
            try {
                rVer = Version.parseVersion(right.get(1));
            } catch (Exception ex) {
                rName = names.get(1);
                rVer = null;
            }
        }
        String versionClause;
        if (lVer != null) {
            if ((rVer != null) && (!rVer.equals(lVer))) {
                // Both versions are defined, but different. Make each of them part of the name
                lName = names.get(0);
                rName = names.get(1);
                versionClause = null;
            } else {
                versionClause = getBreeVersionClause(lVer);
            }
        } else {
            versionClause = getBreeVersionClause(rVer);
        }
        if ("J2SE".equals(lName)) {
            // J2SE is not used in the Capability variant of BREE, use JavaSE here
            // This can only happen with the lName part...
            lName = "JavaSE";
        }
        String nameClause;
        if (rName != null)
            nameClause = "(" + ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE + "=" + lName + "/" + rName + ")";
        else
            nameClause = "(" + ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE + "=" + lName + ")";
        String filter;
        if (versionClause != null)
            filter = "(&" + nameClause + versionClause + ")";
        else
            filter = nameClause;
        filters.add(filter);
    }
    if (filters.size() == 0) {
        return Collections.emptyList();
    } else {
        String reqFilter;
        if (filters.size() == 1) {
            reqFilter = filters.get(0);
        } else {
            // If there are more BREE filters, we need to or them together
            StringBuilder sb = new StringBuilder("(|");
            for (String f : filters) {
                sb.append(f);
            }
            sb.append(")");
            reqFilter = sb.toString();
        }
        SimpleFilter sf = SimpleFilter.parse(reqFilter);
        return Collections.<BundleRequirement>singletonList(new BundleRequirementImpl(owner, ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE, Collections.singletonMap(ExecutionEnvironmentNamespace.REQUIREMENT_FILTER_DIRECTIVE, reqFilter), Collections.<String, Object>emptyMap(), sf));
    }
}
Also used : Version(org.osgi.framework.Version) 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)

Example 3 with SimpleFilter

use of org.apache.felix.framework.capabilityset.SimpleFilter in project felix by apache.

the class ManifestParser method convertNativeCode.

static List<BundleRequirement> convertNativeCode(BundleRevision owner, List<NativeLibraryClause> nativeLibraryClauses, boolean hasOptionalLibraryDirective) {
    List<BundleRequirement> result = new ArrayList<BundleRequirement>();
    List<SimpleFilter> nativeFilterClauseList = new ArrayList<SimpleFilter>();
    if (nativeLibraryClauses != null && !nativeLibraryClauses.isEmpty()) {
        for (NativeLibraryClause clause : nativeLibraryClauses) {
            String[] osNameArray = clause.getOSNames();
            String[] osVersionArray = clause.getOSVersions();
            String[] processorArray = clause.getProcessors();
            String[] languageArray = clause.getLanguages();
            String currentSelectionFilter = clause.getSelectionFilter();
            List<SimpleFilter> nativeFilterList = new ArrayList<SimpleFilter>();
            if (osNameArray != null && osNameArray.length > 0) {
                nativeFilterList.add(buildFilterFromArray(NativeNamespace.CAPABILITY_OSNAME_ATTRIBUTE, osNameArray, SimpleFilter.APPROX));
            }
            if (osVersionArray != null && osVersionArray.length > 0) {
                nativeFilterList.add(buildFilterFromArray(NativeNamespace.CAPABILITY_OSVERSION_ATTRIBUTE, osVersionArray, SimpleFilter.EQ));
            }
            if (processorArray != null && processorArray.length > 0) {
                nativeFilterList.add(buildFilterFromArray(NativeNamespace.CAPABILITY_PROCESSOR_ATTRIBUTE, processorArray, SimpleFilter.APPROX));
            }
            if (languageArray != null && languageArray.length > 0) {
                nativeFilterList.add(buildFilterFromArray(NativeNamespace.CAPABILITY_LANGUAGE_ATTRIBUTE, languageArray, SimpleFilter.APPROX));
            }
            if (currentSelectionFilter != null) {
                nativeFilterList.add(SimpleFilter.parse(currentSelectionFilter));
            }
            if (!nativeFilterList.isEmpty()) {
                SimpleFilter nativeClauseFilter = new SimpleFilter(null, nativeFilterList, SimpleFilter.AND);
                nativeFilterClauseList.add(nativeClauseFilter);
            }
        }
        Map<String, String> requirementDirectives = new HashMap<String, String>();
        SimpleFilter consolidatedNativeFilter = null;
        if (hasOptionalLibraryDirective) {
            requirementDirectives.put(NativeNamespace.REQUIREMENT_RESOLUTION_DIRECTIVE, NativeNamespace.RESOLUTION_OPTIONAL);
        }
        if (nativeFilterClauseList.size() > 1) {
            consolidatedNativeFilter = new SimpleFilter(null, nativeFilterClauseList, SimpleFilter.OR);
            requirementDirectives.put(NativeNamespace.REQUIREMENT_FILTER_DIRECTIVE, consolidatedNativeFilter.toString());
        } else if (nativeFilterClauseList.size() == 1) {
            consolidatedNativeFilter = nativeFilterClauseList.get(0);
            requirementDirectives.put(NativeNamespace.REQUIREMENT_FILTER_DIRECTIVE, consolidatedNativeFilter.toString());
        }
        if (requirementDirectives.size() > 0) {
            result.add(new BundleRequirementImpl(owner, NativeNamespace.NATIVE_NAMESPACE, requirementDirectives, Collections.<String, Object>emptyMap(), consolidatedNativeFilter));
        }
    }
    return result;
}
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)

Example 4 with SimpleFilter

use of org.apache.felix.framework.capabilityset.SimpleFilter 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 5 with SimpleFilter

use of org.apache.felix.framework.capabilityset.SimpleFilter 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)

Aggregations

ArrayList (java.util.ArrayList)10 SimpleFilter (org.apache.felix.framework.capabilityset.SimpleFilter)10 BundleRequirementImpl (org.apache.felix.framework.wiring.BundleRequirementImpl)7 BundleRequirement (org.osgi.framework.wiring.BundleRequirement)5 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 BundleException (org.osgi.framework.BundleException)4 ShrinkableCollection (org.apache.felix.framework.util.ShrinkableCollection)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 AccessControlException (java.security.AccessControlException)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 CapabilitySet (org.apache.felix.framework.capabilityset.CapabilitySet)1 CandidateComparator (org.apache.felix.framework.resolver.CandidateComparator)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1 ServiceException (org.osgi.framework.ServiceException)1 ServiceReference (org.osgi.framework.ServiceReference)1 Version (org.osgi.framework.Version)1