Class ServiceUtil


  • @Beta
    @PublicAPI
    public final class ServiceUtil
    extends Object
    Utility class to ease service location.

    Please note that in the future different DI solutions might be used instead of ServiceLoader.

    Please note that this class is annotated with the Beta annotation, so

    • Method Detail

      • loadService

        public static <S> S loadService​(Class<S> service,
                                        boolean ensureSingle)
                                 throws ServiceConfigurationError
        Load and instantiate an implementation for a given service interface.
        Type Parameters:
        S - Interface type to instantiate
        Parameters:
        service - Service interface class
        ensureSingle - Ensure that only one implementation exists. If set and multiple implementations found then a ServiceConfigurationError is thrown.
        Returns:
        One instantiated implementation for the given interface
        Throws:
        ServiceConfigurationError - When something goes wrong (in the underlying ServiceLoader, incase no implementation found, or ensureSingle is true and multiple implementations found
      • isServiceAvailable

        public static <S> boolean isServiceAvailable​(Class<S> service)
                                              throws ServiceConfigurationError
        Check if a specific service is defined and implementation can be loaded.

        Note that there is no guarantee regarding the successfull instantiation of the service.

        Type Parameters:
        S - Interface type
        Parameters:
        service - Service interface class
        Returns:
        true when at least one implementation of the given service is available
        Throws:
        ServiceConfigurationError - can be thrown by the underlying ServiceLoader.