Package org.apache.sling.commons.osgi
Class RankedServices<T>
- java.lang.Object
-
- org.apache.sling.commons.osgi.RankedServices<T>
-
- Type Parameters:
T
- Service type
- All Implemented Interfaces:
java.lang.Iterable<T>
@ProviderType public final class RankedServices<T> extends java.lang.Object implements java.lang.Iterable<T>
Helper class that collects all services registered via OSGi bind/unbind methods. The services are ordered by service ranking and can be iterated directly using this object instance. Implementation is thread-safe.
With Declarative Services 1.3 supporting field injection with multiple cardinality (leveraging Collections), this class should only be used if DS 1.3 cannot be used for some reason. DS 1.3 is using the same ordering asServiceReference.compareTo(Object)
.
Usage example:
1. Define a dynamic reference with cardinality OPTIONAL_MULTIPLE in your service:
@Reference(name = "myService", referenceInterface = MyService.class, cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE, policy = ReferencePolicy.DYNAMIC) private final RankedServices<MyService> myServices = new RankedServices<MyService>(Order.DESCENDING);
2. Define bind/unbind methods that delegate to the RankedServices instance:
void bindMyService(MyService service, Map<String, Object> props) { myServices.bind(service, props); } void unbindMyService(MyService service, Map<String, Object> props) { myServices.unbind(service, props); }
To access the list of referenced services you can access them in a thread-safe manner:
for (MyService service : myServices) { // your code... }
Optionally you can pass in a
RankedServices.ChangeListener
instance to get notified when the list of referenced services has changed.- Since:
- 2.3
- See Also:
- "OSGi Compendium 6.0, Declarative Services 1.3, Reference Field Option, ยง112.3.8.1"
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
RankedServices.ChangeListener
Notification for changes on services list.
-
Constructor Summary
Constructors Constructor Description RankedServices()
Deprecated.UseRankedServices(Order)
to explicitly give the order.RankedServices(Order order)
Instantiate without change listener but with a given order.RankedServices(Order order, RankedServices.ChangeListener changeListener)
Instantiate with change listener.RankedServices(RankedServices.ChangeListener changeListener)
Deprecated.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
bind(T service, java.util.Map<java.lang.String,java.lang.Object> props)
Handle bind service event.java.util.Collection<T>
get()
Deprecated.UsegetList()
insteadjava.util.List<T>
getList()
Lists all services registered in OSGi, sorted by service ranking (either ascending or descending depending on the order given in the constructor).java.util.Iterator<T>
iterator()
Iterates all services registered in OSGi, sorted by service ranking (either ascending or descending depending on the order given in the constructor).void
unbind(T service, java.util.Map<java.lang.String,java.lang.Object> props)
Handle unbind service event.
-
-
-
Constructor Detail
-
RankedServices
@Deprecated public RankedServices()
Deprecated.UseRankedServices(Order)
to explicitly give the order.Instantiate without change listener in ascending order (lowest service ranking first).
-
RankedServices
@Deprecated public RankedServices(RankedServices.ChangeListener changeListener)
Deprecated.Instantiate with change listener in ascending order (lowest service ranking first).- Parameters:
changeListener
- Change listener
-
RankedServices
public RankedServices(Order order)
Instantiate without change listener but with a given order.- Parameters:
order
- the order in which the services should be returned initerator()
andget()
. EitherOrder.ASCENDING
orOrder.DESCENDING
. UseOrder.DESCENDING
if you want to have the service with the highest ranking returned first (this is the service which would also be chosen byBundleContext.getServiceReference(String)
).- Since:
- 2.4
-
RankedServices
public RankedServices(Order order, RankedServices.ChangeListener changeListener)
Instantiate with change listener.- Parameters:
order
- the order in which the services should be returned initerator()
andget()
. EitherOrder.ASCENDING
orOrder.DESCENDING
. UseOrder.DESCENDING
if you want to have the service with the highest ranking returned first (this is the service which would also be chosen byBundleContext.getServiceReference(String)
).changeListener
- Change listener- Since:
- 2.4
-
-
Method Detail
-
bind
public void bind(T service, java.util.Map<java.lang.String,java.lang.Object> props)
Handle bind service event.- Parameters:
service
- Service instanceprops
- Service reference properties
-
unbind
public void unbind(T service, java.util.Map<java.lang.String,java.lang.Object> props)
Handle unbind service event.- Parameters:
service
- Service instanceprops
- Service reference properties
-
get
public java.util.Collection<T> get()
Deprecated.UsegetList()
insteadLists all services registered in OSGi, sorted by service ranking (either ascending or descending depending on the order given in the constructor).- Returns:
- Collection of service instances
-
getList
public java.util.List<T> getList()
Lists all services registered in OSGi, sorted by service ranking (either ascending or descending depending on the order given in the constructor).- Returns:
- List of service instances
-
-