public final class HippoServiceRegistry extends Object
The HippoServiceRegistry itself is a static singleton created in the common/shared classloader, and services can only be looked up through the service interface under which they are registered. This also implies that services registered with an interface which is not provided through the shared classloader will only be visible to their own web application, which can be considered a feature!
A registered service will be wrapped in a dynamic proxy which only exposes the service interface, and optionally
extra implemented interfaces as specified during the registration. Furthermore, all method invocations on the
service proxy will temporarily set the current content classloader
to the context classloader at the time of the service registration, for cross-context/cross-classloader
usages.
While registration of services by a non-shared interface is supported, more practical usage use a shared interface
for the registration but to also specify non-shared extra implemented interfaces. This allows using web application
internal interfaces to be used to access additional methods not to be shared across web applications. The
getService(Class, Class)
method can be used to automatically return a specialized service type for that
purpose.
An additional feature is using a addTracker(ProxiedServiceTracker, Class)
as a callback on
(un)registration of a specific service by its interface. This can be used to wait for and 'chain' specific
processes which depend on a service to be(come) available, or when it is removed. Note that a ProxiedServiceTracker
only can be used for monitoring (un)registration of a service by its singleton interface, not any of its extra
interfaces or a subtype. The serviceRegistered
and serviceUnregistered
callback methods
provided by the service tracker will be invoked using the context classloader of the service tracker.
For registration and tracking of non-singleton services by a common interface and automatic proxy wrapping, extend
or use concrete implementations of a WhiteboardProxiedServiceRegistry
base class instead. Or for more basic
non-singleton service objects registration and tracking the WhiteboardServiceRegistry
base class.
Both these base classes support using the
Whiteboard Patternfor decoupled lookup/wiring of multiple service objects by a common interface or common base
type (class or interface) respectively. Example implementation and usages of these are the
PersistedHippoEventListenerRegistry
from the hippo-repository-api module and the HippoEventListenerRegistry
Modifier and Type | Method and Description |
---|---|
static <T> void |
addTracker(ProxiedServiceTracker<T> tracker,
Class<T> serviceInterface)
Add a
service tracker for tracking a service (to be) registered with a specific service
interface. |
static <T> T |
getService(Class<?> serviceInterface,
Class<T> extraInterface)
Lookup a service (proxy) by its (main) service interface and cast it to one of the extra interfaces exposed
by the proxy.
|
static <T> T |
getService(Class<T> serviceInterface)
Lookup a service (proxy) by its (main) service interface.
|
static <T> void |
register(T serviceObject,
Class<T> serviceInterface,
Class<?>... extraInterfaces)
Register a service object as a singleton service of a certain interface type.
|
static <T> void |
registerService(T serviceObject,
Class<T> serviceInterface)
Deprecated.
since v13.0. Use
register(Object, Class, Class[]) instead. |
static <T> boolean |
removeTracker(ProxiedServiceTracker<T> tracker,
Class<T> serviceInterface)
Remove a previously added
ProxiedServiceTracker<T> . |
static <T> boolean |
unregister(T serviceObject,
Class<T> serviceInterface)
Unregister a previously registered service object
|
static <T> void |
unregisterService(T serviceObject,
Class<T> serviceInterface)
Deprecated.
since v13.0. Use
unregister(Object, Class) instead. |
public static <T> void register(T serviceObject, Class<T> serviceInterface, Class<?>... extraInterfaces)
The service object will be proxied to expose only the service interface, and optionally extra interfaces, and to enforce setting the Thread ContextClassLoader to its registration classloader during invocation of its service (interface) methods.
Additional interfaces can be specified by the extraInterfaces parameter to be also exposed through the proxy. See the class level documentation for usages and common use-cases.
serviceObject
- service object to registerserviceInterface
- the service interface to implement and lookup the serviceextraInterfaces
- optional extra interfaces to proxy for the service,HippoServiceException
- when the service object was already registered@Deprecated public static <T> void registerService(T serviceObject, Class<T> serviceInterface)
register(Object, Class, Class[])
instead.public static <T> boolean unregister(T serviceObject, Class<T> serviceInterface)
serviceObject
- the service interface under which the service was registered@Deprecated public static <T> void unregisterService(T serviceObject, Class<T> serviceInterface)
unregister(Object, Class)
instead.public static <T> T getService(Class<T> serviceInterface)
serviceInterface
- the service interface under which the service was registeredpublic static <T> T getService(Class<?> serviceInterface, Class<T> extraInterface)
serviceInterface
- the service interface under which the service was registeredextraInterface
- the extra interface exposed by the proxy to be used as return typepublic static <T> void addTracker(ProxiedServiceTracker<T> tracker, Class<T> serviceInterface)
service tracker
for tracking a service (to be) registered with a specific service
interface. Multiple trackers may be added to track (un)registration of the same service interface.tracker
- the service trackerHippoServiceException
- when the provided tracker instance already was added before for tracking the
specific service interfacepublic static <T> boolean removeTracker(ProxiedServiceTracker<T> tracker, Class<T> serviceInterface)
ProxiedServiceTracker<T>
.tracker
- the service trackerCopyright © 2012–2019 Hippo B.V. (http://www.onehippo.com). All rights reserved.