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;
}
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);
}
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;
}
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;
}
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;
}
Aggregations