Search in sources :

Example 6 with SimpleFilter

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

the class Felix method getServiceReferences.

/**
 * Retrieves an array of {@link ServiceReference} objects based on calling bundle,
 * service class name, and filter expression.  Optionally checks for isAssignable to
 * make sure that the service can be cast to the
 * @param bundle Calling Bundle
 * @param className Service Classname or <code>null</code> for all
 * @param expr Filter Criteria or <code>null</code>
 * @return Array of ServiceReference objects that meet the criteria
 * @throws InvalidSyntaxException
 */
ServiceReference[] getServiceReferences(final BundleImpl bundle, final String className, final String expr, final boolean checkAssignable) throws InvalidSyntaxException {
    // Define filter if expression is not null.
    SimpleFilter filter = null;
    if (expr != null) {
        try {
            filter = SimpleFilter.parse(expr);
        } catch (Exception ex) {
            throw new InvalidSyntaxException(ex.getMessage(), expr);
        }
    }
    // Ask the service registry for all matching service references.
    final Collection refList = m_registry.getServiceReferences(className, filter);
    // Filter on assignable references
    if (checkAssignable) {
        for (Iterator refIter = refList.iterator(); refIter.hasNext(); ) {
            // Get the current service reference.
            ServiceReference ref = (ServiceReference) refIter.next();
            // Now check for castability.
            if (!Util.isServiceAssignable(bundle, ref)) {
                refIter.remove();
            }
        }
    }
    // If the requesting bundle is the system bundle, ignore the effects of the findhooks
    Collection resRefList = (bundle == this ? new ArrayList(refList) : refList);
    // activate findhooks
    Set<ServiceReference<org.osgi.framework.hooks.service.FindHook>> findHooks = getHookRegistry().getHooks(org.osgi.framework.hooks.service.FindHook.class);
    for (ServiceReference<org.osgi.framework.hooks.service.FindHook> sr : findHooks) {
        org.osgi.framework.hooks.service.FindHook fh = getService(this, sr, false);
        if (fh != null) {
            try {
                m_secureAction.invokeServiceFindHook(fh, bundle._getBundleContext(), className, expr, !checkAssignable, new ShrinkableCollection(refList));
            } catch (Throwable th) {
                m_logger.log(sr, Logger.LOG_WARNING, "Problem invoking service registry hook", th);
            } finally {
                m_registry.ungetService(this, sr, null);
            }
        }
    }
    // the requestor, resRefList is a copy of the original list before the hooks were invoked.
    if (resRefList.size() > 0) {
        return (ServiceReference[]) resRefList.toArray(new ServiceReference[resRefList.size()]);
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) ServiceException(org.osgi.framework.ServiceException) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) FileNotFoundException(java.io.FileNotFoundException) AccessControlException(java.security.AccessControlException) ResolutionException(org.osgi.service.resolver.ResolutionException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ServiceReference(org.osgi.framework.ServiceReference) SimpleFilter(org.apache.felix.framework.capabilityset.SimpleFilter) Iterator(java.util.Iterator) ShrinkableCollection(org.apache.felix.framework.util.ShrinkableCollection) Collection(java.util.Collection) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ShrinkableCollection(org.apache.felix.framework.util.ShrinkableCollection)

Example 7 with SimpleFilter

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

the class ServiceRegistry method getServiceReferences.

public Collection<Capability> getServiceReferences(final String className, SimpleFilter filter) {
    if ((className == null) && (filter == null)) {
        // Return all services.
        filter = new SimpleFilter(null, null, SimpleFilter.MATCH_ALL);
    } else if ((className != null) && (filter == null)) {
        // Return services matching the class name.
        filter = new SimpleFilter(Constants.OBJECTCLASS, className, SimpleFilter.EQ);
    } else if ((className != null) && (filter != null)) {
        // Return services matching the class name and filter.
        final List<SimpleFilter> filters = new ArrayList<SimpleFilter>(2);
        filters.add(new SimpleFilter(Constants.OBJECTCLASS, className, SimpleFilter.EQ));
        filters.add(filter);
        filter = new SimpleFilter(null, filters, SimpleFilter.AND);
    }
    return m_regCapSet.match(filter, false);
}
Also used : SimpleFilter(org.apache.felix.framework.capabilityset.SimpleFilter) ArrayList(java.util.ArrayList)

Example 8 with SimpleFilter

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

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

the class ManifestParser method buildFilterFromArray.

private static SimpleFilter buildFilterFromArray(String attributeName, String[] stringArray, int operation) {
    SimpleFilter result = null;
    List<SimpleFilter> filterSet = new ArrayList<SimpleFilter>();
    if (stringArray != null) {
        for (String currentValue : stringArray) {
            filterSet.add(new SimpleFilter(attributeName, currentValue.toLowerCase(), operation));
        }
        if (filterSet.size() == 1) {
            result = filterSet.get(0);
        } else {
            result = new SimpleFilter(null, filterSet, SimpleFilter.OR);
        }
    }
    return result;
}
Also used : SimpleFilter(org.apache.felix.framework.capabilityset.SimpleFilter) ArrayList(java.util.ArrayList)

Example 10 with SimpleFilter

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

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