Search in sources :

Example 1 with InvalidServiceFilterSyntaxException

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);
    }
}
Also used : ArrayList(java.util.ArrayList) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) InvalidServiceFilterSyntaxException(org.apache.sling.api.scripting.InvalidServiceFilterSyntaxException) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

ArrayList (java.util.ArrayList)1 InvalidServiceFilterSyntaxException (org.apache.sling.api.scripting.InvalidServiceFilterSyntaxException)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1 ServiceReference (org.osgi.framework.ServiceReference)1