Class WhiteboardProxiedServiceRegistry<T>

java.lang.Object
org.onehippo.cms7.services.WhiteboardProxiedServiceRegistry<T>
Type Parameters:
T - service interface type

public abstract class WhiteboardProxiedServiceRegistry<T> extends Object
WhiteboardProxiedServiceRegistry<T> is an abstract base class for implementing and using the Whiteboard Patternfor decoupled lookup/wiring of multiple service objects by a common (main) interface.

This WhiteboardProxiedServiceRegistry base class supports (un)registering service objects, and ProxiedServiceTrackers to retrieve those currently registered as well as be notified on future (un)registrations. Also currently registered service entries can be retrieved through getEntries().

All service objects registered through a WhiteboardProxiedServiceRegistry implementation must implement a common interface of generic type T.

For example with the following (complete) org.onehippo.repository.events.PersistedHippoEventListenerRegistry implementation from the hippo-repository-api module using interface type org.onehippo.repository.events.PersistedHippoEventListener, service objects implementing that interface can be registered:


     public final class PersistedHippoEventListenerRegistry extends WhiteboardProxiedServiceRegistry<PersistedHippoEventListener> {

         private static final PersistedHippoEventListenerRegistry INSTANCE = new PersistedHippoEventListenerRegistry();

         private HippoEventListenerRegistry() {
             super(PersistedHippoEventListener.class);
         }

         public static PersistedHippoEventListenerRegistry get() { return INSTANCE; }
     }

For a typical usage of a WhiteboardProxiedServiceRegistry see the javadoc for the org.onehippo.repository.events.PersistedHippoEventListenerRegistry and org.onehippo.repository.events.PersistedHippoEventListener service interface in the hippo-repository-api module.

For each registered service object a ProxiedServiceHolder is created and passed on to the ProxiedServiceTracker(s), storing the service object itself together with the context classloader used to register the service object. Furthermore it provides access to a dynamically created service proxy for the service exposing only the service interface and optionally extra interfaces. When using the service proxy all method invocation will be executed with the context classloader set to its registration classloader. Also, the serviceRegistered and serviceUnregistered callback methods provided by the service tracker will be invoked using the context classloader of the service tracker.

When a service object should be used as a singleton then use HippoServiceRegistry instead.

When only the registration and tracking of one or more service objects of a certain (base) type (class or interface) is needed nor a service proxy with automatic enforcing of its context classloader during method invocation, use the WhiteboardServiceRegistry<T> base class instead, Note that it then requires manual enforcement of the current context classloader in case of cross-context usage of the service objects.

  • Constructor Details

    • WhiteboardProxiedServiceRegistry

      public WhiteboardProxiedServiceRegistry(Class<T> serviceInterface)
      Constructor needed for storing the required common interface of generic type T which must be implemented by all service objects registered.
      Parameters:
      serviceInterface - common interface
  • Method Details

    • getEntriesList

      protected List<ProxiedServiceHolder<T>> getEntriesList()
      Returns:
      a new copied List of current service entries to guard against potential concurrent service (un)registrations
    • register

      public void register(T serviceObject, Class<?>... extraInterfaces)
      Register a service object implementing interface type <T> and optionally some additional interfaces which all will be exposed by the service proxy created for this service object.
      Parameters:
      serviceObject - the service object
      extraInterfaces - additional interfaces implemented by the service object to be exposed by the service proxy
      Throws:
      HippoServiceException - when the service object was already registered
    • unregister

      public boolean unregister(T serviceObject)
      Unregister a previously registered service object
      Parameters:
      serviceObject - the service object
      Returns:
      true if the service object was registered before and now removed, false otherwise
    • addTracker

      public void addTracker(ProxiedServiceTracker<T> tracker)
      Add a ProxiedServiceTracker<T> for tracking service objects of common interface <T> registered and unregistered in this registry.
      Parameters:
      tracker - the service tracker
      Throws:
      HippoServiceException - when the provided tracker instance already was added before
    • removeTracker

      public boolean removeTracker(ProxiedServiceTracker<T> tracker)
      Remove a previously added ProxiedServiceTracker<T>.
      Parameters:
      tracker - the service tracker
      Returns:
      true if the service tracker was added before and now removed, false otherwise
    • getEntries

      public Stream<ProxiedServiceHolder<T>> getEntries()
      Returns:
      the registered service entries
    • getServices

      public Stream<T> getServices()
      Returns:
      the registered services (proxied)
    • size

      public int size()
      Returns:
      the number of registered services