Class Services


  • public final class Services
    extends java.lang.Object
    Helper class for loading services from the classpath, using a ServiceLoader. Decouples loading logic for better separation of concerns and testability.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> java.util.List<T> loadAll​(java.lang.Class<T> spi)
      Loads and instantiates all service implementation of the given SPI class and returns them as a List.
      static <T> T loadFirst​(java.lang.Class<T> spi)
      Loads the first available implementation the given SPI class from the classpath.
      static <T> T loadFirst​(java.lang.Class<T> spi, T defaultValue)
      Loads the first available implementation the given SPI class from the classpath.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • loadAll

        public static <T> java.util.List<T> loadAll​(java.lang.Class<T> spi)
        Loads and instantiates all service implementation of the given SPI class and returns them as a List.
        Type Parameters:
        T - The type of the SPI
        Parameters:
        spi - The class of the Service Provider Interface
        Returns:
        An unmodifiable list with an instance of all available implementations of the SPI. No guarantee is given on the order of implementations, if more than one.
      • loadFirst

        public static <T> T loadFirst​(java.lang.Class<T> spi)
        Loads the first available implementation the given SPI class from the classpath. Uses the ServiceLoader to find implementations. When multiple implementations are available it will return the first one that it encounters. There is no guarantee with regard to ordering.
        Type Parameters:
        T - The type of the SPI
        Parameters:
        spi - The class of the Service Provider Interface
        Returns:
        A new instance of the service.
        Throws:
        dev.paseto.jpaseto.lang.UnavailableImplementationException - When no implementation the SPI is available on the classpath.
      • loadFirst

        public static <T> T loadFirst​(java.lang.Class<T> spi,
                                      T defaultValue)
        Loads the first available implementation the given SPI class from the classpath. Uses the ServiceLoader to find implementations. When multiple implementations are available it will return the first one that it encounters. There is no guarantee with regard to ordering.
        Type Parameters:
        T - The type of the SPI
        Parameters:
        spi - The class of the Service Provider Interface
        defaultValue - the default implementation to load if service loader returns empty
        Returns:
        A new instance of the service.
        Throws:
        dev.paseto.jpaseto.lang.UnavailableImplementationException - When no implementation the SPI is available on the classpath.