Class ConcurrentServiceReferenceMap<K,V>

java.lang.Object
com.ibm.wsspi.kernel.service.utils.ConcurrentServiceReferenceMap<K,V>

public class ConcurrentServiceReferenceMap<K,V> extends Object
This provides a simple map implementation for lazy-resolution of services. Use this class when you have frequent retrieval with infrequent add/removal. Entries are not stored in any particular order.

Usage (following OSGi DS naming conventions/patterns):

 private final ConcurrentServiceReferenceMap&ltK,V&gt serviceMap = new ConcurrentServiceReferenceMap&ltK,V&gt("referenceName");
 
 protected void activate(ComponentContext ctx) {
  serviceMap.activate(ctx);
 }
 
 protected void deactivate(ComponentContext ctx) {
  serviceMap.deactivate(ctx);
 }
 
 protected void setReferenceName(ServiceReference&ltV&gt ref) {
  K key;
  serviceMap.addReference(key, ref);
 }
 
 protected void unsetReferenceName(ServiceReference&ltV&gt ref) {
  K key;
  serviceMap.removeReference(key, ref);
 }
 
 public ServiceReference&ltV&gt getReferenceName(K key) {
  return serviceMap.getServices(key);
 }
 
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a new ConcurrentServiceReferenceMap for the named service.
  • Method Summary

    Modifier and Type
    Method
    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>
    Returns the ServiceReference associated with key
    Retrieve the service associated with key.
    Iterate over all services in the map in no specific order.
     
    boolean
    Check if there are any registered/added service references: this will return true if the set is empty (none available).
    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.
    Iterable<org.osgi.framework.ServiceReference<V>>
     
    boolean
    removeReference(K key, org.osgi.framework.ServiceReference<V> reference)
    Removes the reference associated with the key.
    int
    Answers the number of elements in this Map.
     

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • ConcurrentServiceReferenceMap

      public ConcurrentServiceReferenceMap(String name)
      Create a new ConcurrentServiceReferenceMap for the named service. e.g. from bnd.bnd: referenceName=.... or from component.xml: <reference name="referenceName".... >
      Parameters:
      name - Name of DS reference
  • Method Details

    • activate

      public void activate(org.osgi.service.component.ComponentContext context)
    • deactivate

      public void deactivate(org.osgi.service.component.ComponentContext context)
      Deactivates the map.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • putReference

      public boolean putReference(K key, org.osgi.framework.ServiceReference<V> reference)
      Associates the reference with the key.
      Parameters:
      key - Key associated with this reference
      reference - ServiceReference for the target service
      Returns:
      true if this is replacing a previous (non-null) service reference
    • putReferenceIfAbsent

      public 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. It will only attempt to add the reference to the map if key is not null.
      Parameters:
      key - Key associated with this reference
      reference - ServiceReference for the target service
      Returns:
      The service reference that was previously associated with the key or null otherwise
      See Also:
    • removeReference

      public boolean removeReference(K key, org.osgi.framework.ServiceReference<V> reference)
      Removes the reference associated with the key.
      Parameters:
      key - Key associated with this reference
      reference - ServiceReference associated with service to be unset.
      Returns:
      true if reference was unset (not previously replaced)
    • isEmpty

      public boolean isEmpty()
      Check if there are any registered/added service references: this will return true if the set is empty (none available). If the set is not empty, the services will only be resolvable if there is a viable component context.
      Returns:
      true if the list of registered service references is empty.
    • size

      public int size()
      Answers the number of elements in this Map.
      Returns:
      the number of elements in this Map
    • keySet

      public Set<K> keySet()
      Answers a Set of the keys contained in this Map in no specific order. The set is backed by this Map so changes to one are reflected by the other. The set does not support adding.
      Returns:
      a Set of the keys
    • references

      public Iterable<org.osgi.framework.ServiceReference<V>> references()
    • getService

      public V getService(K key)
      Retrieve the service associated with key.
      Parameters:
      key - The key associated with the requested service
      Returns:
      The service if available, null otherwise.
    • getServiceWithException

      public V getServiceWithException(K key)
      Returns:
      T
      Throws:
      IllegalStateException - if the internal state is such that locating the service is not possible or if the service is not retrievable
    • getReference

      public org.osgi.framework.ServiceReference<V> getReference(K key)
      Returns the ServiceReference associated with key
      Parameters:
      key - The key associated with the service
      Returns:
      ServiceRerefence associated with key, or null
    • getServices

      public Iterator<V> getServices()
      Iterate over all services in the map in no specific order. The iterator will return the service associated with each ServiceReference as it progresses. Creation of the iterator does not eagerly resolve services: resolution is done only once per service reference, and only when "next" would retrieve that service.