T
- Service type@ProviderType
public final class RankedServices<T>
extends java.lang.Object
implements java.lang.Iterable<T>
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.
Modifier and Type | Class and Description |
---|---|
static interface |
RankedServices.ChangeListener
Notification for changes on services list.
|
Constructor and Description |
---|
RankedServices()
Deprecated.
Use
#RankedService(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.
|
Modifier and Type | Method and 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.
Use
getList() instead |
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).
|
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.
|
@Deprecated public RankedServices()
#RankedService(Order)
) to explicitly give the order.@Deprecated public RankedServices(RankedServices.ChangeListener changeListener)
RankedServices(Order order, ChangeListener changeListener)
insteadchangeListener
- Change listenerpublic RankedServices(Order order)
order
- the order in which the services should be returned in iterator()
and get()
.
Either Order.ASCENDING
or Order.DESCENDING
.
Use Order.DESCENDING
if you want to have the service with the highest ranking returned first
(this is the service which would also be chosen by BundleContext.getServiceReference(String)
).public RankedServices(Order order, RankedServices.ChangeListener changeListener)
order
- the order in which the services should be returned in iterator()
and get()
.
Either Order.ASCENDING
or Order.DESCENDING
.
Use Order.DESCENDING
if you want to have the service with the highest ranking returned first
(this is the service which would also be chosen by BundleContext.getServiceReference(String)
).changeListener
- Change listenerpublic void bind(T service, java.util.Map<java.lang.String,java.lang.Object> props)
service
- Service instanceprops
- Service reference propertiespublic void unbind(T service, java.util.Map<java.lang.String,java.lang.Object> props)
service
- Service instanceprops
- Service reference propertiespublic java.util.Collection<T> get()
getList()
insteadpublic java.util.List<T> getList()
Copyright © 2010 - 2020 Adobe. All Rights Reserved