use of org.apache.sling.api.scripting.InvalidServiceFilterSyntaxException in project sling by apache.
the class ScriptHelper method getServices.
/**
* @see org.apache.sling.api.scripting.SlingScriptHelper#getServices(java.lang.Class, java.lang.String)
*/
@SuppressWarnings("unchecked")
public <ServiceType> ServiceType[] getServices(Class<ServiceType> serviceType, String filter) throws InvalidServiceFilterSyntaxException {
try {
final ServiceReference[] refs = this.bundleContext.getServiceReferences(serviceType.getName(), filter);
ServiceType[] result = null;
if (refs != null) {
// sort by service ranking (lowest first) (see ServiceReference#compareTo(Object))
List<ServiceReference> references = Arrays.asList(refs);
Collections.sort(references);
// get the highest ranking first
Collections.reverse(references);
final List<ServiceType> objects = new ArrayList<ServiceType>();
for (ServiceReference reference : references) {
final ServiceType service = (ServiceType) this.bundleContext.getService(reference);
if (service != null) {
if (this.references == null) {
this.references = new ArrayList<ServiceReference>();
}
this.references.add(reference);
objects.add(service);
}
}
if (objects.size() > 0) {
ServiceType[] srv = (ServiceType[]) Array.newInstance(serviceType, objects.size());
result = objects.toArray(srv);
}
}
return result;
} catch (InvalidSyntaxException ise) {
throw new InvalidServiceFilterSyntaxException(filter, "Invalid filter syntax", ise);
}
}
Aggregations