com.ibm.wsspi.kernel.service.utils

Class AtomicServiceReference<T>

  • java.lang.Object
    • com.ibm.wsspi.kernel.service.utils.AtomicServiceReference<T>


  • public class AtomicServiceReference<T>
    extends java.lang.Object
    Small class performing atomic operations to find/retrieve/cache the instance of a DeclarativeServices component reference from service registry via the service reference.

    Use this to maintain a lazy-resolved reference of cardinality one (single service reference associated with a single, lazily-resolved service instance).

    Usage (following OSGi DS naming conventions/patterns):

     private final AtomicServiceReference<T> serviceRef = new AtomicServiceReference<T>("referenceName");
     
     protected void activate(ComponentContext ctx) {
      serviceRef.activate(ctx);
     }
     
     protected void deactivate(ComponentContext ctx) {
      serviceRef.deactivate(ctx);
     }
     
     protected void setReferenceName(ServiceReference<T> ref) {
      serviceRef.setReference(ref);
     }
     
     protected void unsetReferenceName(ServiceReference<T> ref) {
      serviceRef.unsetReference(ref);
     }
     
     private T getReferenceName() {
      return serviceRef.getService();
     }
     
    • Constructor Summary

      Constructors 
      Constructor and Description
      AtomicServiceReference(java.lang.String name)
      Create a new AtomicServiceReference for the named service.
    • Method Summary

      Methods 
      Modifier and Type Method and Description
      void activate(org.osgi.service.component.ComponentContext context) 
      void deactivate(org.osgi.service.component.ComponentContext context) 
      org.osgi.framework.ServiceReference<T> getReference() 
      T getService() 
      T getServiceWithException() 
      boolean setReference(org.osgi.framework.ServiceReference<T> reference)
      Update service reference associated with this service.
      java.lang.String toString() 
      boolean unsetReference(org.osgi.framework.ServiceReference<T> reference)
      Clear the service reference associated with this service.
      • Methods inherited from class java.lang.Object

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

      • AtomicServiceReference

        public AtomicServiceReference(java.lang.String name)
        Create a new AtomicServiceReference for the named service. e.g. from bnd.bnd: referenceName=.... or from component.xml:
        Parameters:
        name - Name of DS reference
    • Method Detail

      • activate

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

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

        public boolean setReference(org.osgi.framework.ServiceReference<T> reference)
        Update service reference associated with this service.
        Parameters:
        referenceName - ServiceReference for the target service. Service references are equal if they point to the same service registration, and are ordered by increasing service.ranking and decreasing service.id. ServiceReferences hold no service properties: requests/queries for properties are forwarded onto the backing service registration.
        Returns:
        true if this is replacing a previous (non-null) service reference
      • unsetReference

        public boolean unsetReference(org.osgi.framework.ServiceReference<T> reference)
        Clear the service reference associated with this service. This first checks to see whether or not the reference being unset matches the current reference. For Declarative Services dynamic components: if a replacement is available for a dynamic reference, DS will call set with the new reference before calling unset to clear the old one.
        Parameters:
        reference - ServiceReference associated with service to be unset.
        Returns:
        true if a non-null value was replaced
      • getReference

        public org.osgi.framework.ServiceReference<T> getReference()
        Returns:
        ServiceReference for the target service. Service references are equal if they point to the same service registration, and are ordered by increasing service.ranking and decreasing service.id. ServiceReferences hold no service properties: requests/queries for properties are forwarded onto the backing service registration.
      • getService

        public T getService()
        Returns:
        T or null if unavailable
      • getServiceWithException

        public T getServiceWithException()
        Returns:
        T
        Throws:
        java.lang.IllegalStateException - if the internal state is such that locating the service is not possible or if the service is not retrievable
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object