Package ej.service

Class 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, a SystemPropertiesServiceLoader is used. The implementation may be a CompositeServiceLoader that 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, a SimpleServiceRegistry is used.

    Usage:

     ServiceFactory.register(MyInterface.class, myInstance);
     
    • 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 null if 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 (see getService(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 MissingServiceException if 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)