Class ServiceFactory
- java.lang.Object
-
- ej.service.ServiceFactory
-
public class ServiceFactory extends Object
A simple dependency injection facility.Gets a service loader that is able to retrieve services implementation.
The service loader implementation is retrieved from system properties (see
SystemPropertiesServiceLoader), if none is found, aSystemPropertiesServiceLoaderis used. The implementation may be aCompositeServiceLoaderthat searches to resolve the services in a list of service loaders.Usage:
MyInterface instance = ServiceFactory.getService(MyInterface.class);
The service registry implementation is retrieved from system properties (see
SystemPropertiesServiceLoader), if none is found, aSimpleServiceRegistryis used.Usage:
ServiceFactory.register(MyInterface.class, myInstance);
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> TgetRequiredService(Class<T> service)Gets the instance of the given service.static <T> TgetService(Class<T> service)Gets the instance of the given service.static <T> TgetService(Class<T> service, Class<? extends T> defaultImplementation)Gets the instance of the given service in the service loader then in the service registry (seegetService(Class).static ServiceLoadergetServiceLoader()Gets a service loader unique instance.static ServiceRegistrygetServiceRegistry()Gets a service registry unique instance.static <T> voidregister(Class<T> service, T instance)Registers a service instance.static <T> voidunregister(Class<T> service, T instance)Unregisters a service instance.
-
-
-
Method Detail
-
getServiceLoader
public static ServiceLoader getServiceLoader()
Gets a service loader unique instance.- Returns:
- a service loader.
-
getServiceRegistry
public static ServiceRegistry getServiceRegistry()
Gets a service registry unique instance.- Returns:
- a service registry.
-
getService
@Nullable public static <T> T getService(Class<T> service)
Gets the instance of the given service.It first searches in the service loader, then in the service registry.
May return
nullif the service is not defined.- Type Parameters:
T- the service type.- Parameters:
service- the service.- Returns:
- the instance of the given service.
- Throws:
ServiceLoadingException- if an error occurs while instantiating the service instance.SecurityException- if a security manager exists and does not allow the caller to retrieve the given service instance.- See Also:
getServiceLoader(),getServiceRegistry()
-
register
public static <T> void register(Class<T> service, T instance)
Registers a service instance.The given instance is returned each time the service is requested by
getService(Class).If the service already has a registered instance, the previous one is replaced.
- Type Parameters:
T- the service type.- Parameters:
service- the implemented service.instance- the service instance.- Throws:
SecurityException- if a security manager exists and does not allow the caller to register the given service instance.
-
unregister
public static <T> void unregister(Class<T> service, T instance)
Unregisters a service instance.If the given instance is not the one currently registered for the given service, nothing is done.
- Type Parameters:
T- the service type.- Parameters:
service- the implemented service.instance- the service instance.- Throws:
SecurityException- if a security manager exists and does not allow the caller to unregister the given service instance.
-
getService
public static <T> T getService(Class<T> service, Class<? extends T> defaultImplementation)
Gets the instance of the given service in the service loader then in the service registry (seegetService(Class).If there is no registered implementation, an instance of the default implementation is created, registered in the service registry and returned.
- Type Parameters:
T- the service type.- Parameters:
service- the service.defaultImplementation- the default implementation to use.- Returns:
- the instance of the given service.
- Throws:
ServiceLoadingException- if an error occurs while instantiating the service instance.SecurityException- if a security manager exists and does not allow the caller to retrieve the given service instance.- See Also:
getServiceLoader(),getServiceRegistry()
-
getRequiredService
public static <T> T getRequiredService(Class<T> service)
Gets the instance of the given service.If there is already an instance in cache, it is returned otherwise a new one is created.
May throw a
MissingServiceExceptionif the service is not defined.- Type Parameters:
T- the service type.- Parameters:
service- the service.- Returns:
- the instance of the given service.
- Throws:
ServiceLoadingException- if an error occurs while instantiating the service instance.SecurityException- if a security manager exists and does not allow the caller to retrieve the given service instance.MissingServiceException- if the service has no registered implementation.- See Also:
ServiceLoader.getService(Class)
-
-