public class ConcurrentServiceReferenceMap<K,V>
extends java.lang.Object
Usage (following OSGi DS naming conventions/patterns):
private final ConcurrentServiceReferenceMap<K,V> serviceMap = new ConcurrentServiceReferenceMap<K,V>("referenceName"); protected void activate(ComponentContext ctx) { serviceMap.activate(ctx); } protected void deactivate(ComponentContext ctx) { serviceMap.deactivate(ctx); } protected void setReferenceName(ServiceReference<V> ref) { K key; serviceMap.addReference(key, ref); } protected void unsetReferenceName(ServiceReference<V> ref) { K key; serviceMap.removeReference(key, ref); } public ServiceReference<V> getReferenceName(K key) { return serviceMap.getServices(key); }
Constructor and Description |
---|
ConcurrentServiceReferenceMap(java.lang.String name)
Create a new ConcurrentServiceReferenceMap for the named service.
|
Modifier and Type | Method and Description |
---|---|
void |
activate(org.osgi.service.component.ComponentContext context) |
void |
deactivate(org.osgi.service.component.ComponentContext context)
Deactivates the map.
|
org.osgi.framework.ServiceReference<V> |
getReference(K key)
Returns the ServiceReference associated with key
|
V |
getService(K key)
Retrieve the service associated with key.
|
java.util.Iterator<V> |
getServices()
Iterate over all services in the map in no specific order.
|
V |
getServiceWithException(K key) |
boolean |
isEmpty()
Check if there are any registered/added service references: this will return
true if the set is empty (none available).
|
java.util.Set<K> |
keySet()
Answers a Set of the keys contained in this Map in no specific order.
|
boolean |
putReference(K key,
org.osgi.framework.ServiceReference<V> reference)
Associates the reference with the key.
|
org.osgi.framework.ServiceReference<V> |
putReferenceIfAbsent(K key,
org.osgi.framework.ServiceReference<V> reference)
Associates the reference with the key but only if there is not an
existing reference associated with that key.
|
java.lang.Iterable<org.osgi.framework.ServiceReference<V>> |
references() |
boolean |
removeReference(K key,
org.osgi.framework.ServiceReference<V> reference)
Removes the reference associated with the key.
|
int |
size()
Answers the number of elements in this Map.
|
java.lang.String |
toString() |
public ConcurrentServiceReferenceMap(java.lang.String name)
name
- Name of DS referencepublic void activate(org.osgi.service.component.ComponentContext context)
public void deactivate(org.osgi.service.component.ComponentContext context)
public java.lang.String toString()
toString
in class java.lang.Object
public boolean putReference(K key, org.osgi.framework.ServiceReference<V> reference)
key
- Key associated with this referencereference
- ServiceReference for the target servicepublic org.osgi.framework.ServiceReference<V> putReferenceIfAbsent(K key, org.osgi.framework.ServiceReference<V> reference)
key
is not null
.key
- Key associated with this referencereference
- ServiceReference for the target servicenull
otherwiseConcurrentMap.putIfAbsent(Object, Object)
public boolean removeReference(K key, org.osgi.framework.ServiceReference<V> reference)
key
- Key associated with this referencereference
- ServiceReference associated with service to be unset.public boolean isEmpty()
public int size()
public java.util.Set<K> keySet()
public java.lang.Iterable<org.osgi.framework.ServiceReference<V>> references()
public V getService(K key)
key
- The key associated with the requested servicepublic V getServiceWithException(K key)
java.lang.IllegalStateException
- if the internal state is such that
locating the service is not possible or if the service
is not retrievablepublic org.osgi.framework.ServiceReference<V> getReference(K key)
key
- The key associated with the servicepublic java.util.Iterator<V> getServices()