use of org.apache.felix.connect.felix.framework.capabilityset.SimpleFilter in project felix by apache.
the class PojoSRBundleContext 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 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 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<ServiceReference<?>> refList = m_reg.getServiceReferences(className, filter);
// Filter on assignable references
if (checkAssignable) {
for (Iterator<ServiceReference<?>> it = refList.iterator(); it.hasNext(); ) {
// Get the current service reference.
ServiceReference ref = it.next();
// Now check for castability.
if (!Util.isServiceAssignable(m_bundle, ref)) {
it.remove();
}
}
}
// activate findhooks
Set<ServiceReference<FindHook>> findHooks = m_reg.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 = m_reg.getService(getBundle(0), sr);
if (fh != null) {
try {
fh.find(this, className, expr, !checkAssignable, new ShrinkableCollection<ServiceReference<?>>(refList));
} catch (Throwable th) {
System.err.println("Problem invoking service registry hook");
th.printStackTrace();
} finally {
m_reg.ungetService(getBundle(0), sr);
}
}
}
if (refList.size() > 0) {
return refList.toArray(new ServiceReference[refList.size()]);
}
return null;
}
use of org.apache.felix.connect.felix.framework.capabilityset.SimpleFilter in project felix by apache.
the class ServiceRegistry method getServiceReferences.
public synchronized Collection<ServiceReference<?>> getServiceReferences(String className, SimpleFilter filter) {
if ((className == null) && (filter == null)) {
// Return all services.
filter = new SimpleFilter(Constants.OBJECTCLASS, "*", SimpleFilter.PRESENT);
} 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.
List<Object> filters = new ArrayList<Object>(2);
filters.add(new SimpleFilter(Constants.OBJECTCLASS, className, SimpleFilter.EQ));
filters.add(filter);
filter = new SimpleFilter(null, filters, SimpleFilter.AND);
}
// else just use the specified filter.
Set<ServiceRegistrationImpl.ServiceReferenceImpl> matches = m_regCapSet.match(filter, false);
return (Collection) matches;
}
Aggregations