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
ProxiedServiceTracker
s to retrieve those currently registered as well as be notified on future (un)registrations. Also currently registered service entries can be retrieved throughgetEntries()
.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 theProxiedServiceTracker
(s), storing the service object itself together with the context classloader used to register the service object. Furthermore it provides access to a dynamically createdservice 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, theserviceRegistered
andserviceUnregistered
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 Summary
Constructors Constructor Description 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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addTracker(ProxiedServiceTracker<T> tracker)
Add aProxiedServiceTracker<T>
for tracking service objects of common interface <T>registered
andunregistered
in this registry.Stream<ProxiedServiceHolder<T>>
getEntries()
protected List<ProxiedServiceHolder<T>>
getEntriesList()
Stream<T>
getServices()
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 theservice proxy
created for this service object.boolean
removeTracker(ProxiedServiceTracker<T> tracker)
Remove a previously addedProxiedServiceTracker<T>
.int
size()
boolean
unregister(T serviceObject)
Unregister a previously registered service object
-
-
-
Method Detail
-
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 theservice proxy
created for this service object.- Parameters:
serviceObject
- the service objectextraInterfaces
- 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 aProxiedServiceTracker<T>
for tracking service objects of common interface <T>registered
andunregistered
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 addedProxiedServiceTracker<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
-
size
public int size()
- Returns:
- the number of registered services
-
-