Package dev.paseto.jpaseto.lang
Class Services
- java.lang.Object
-
- dev.paseto.jpaseto.lang.Services
-
public final class Services extends java.lang.ObjectHelper class for loading services from the classpath, using aServiceLoader. 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> TloadFirst(java.lang.Class<T> spi)Loads the first available implementation the given SPI class from the classpath.static <T> TloadFirst(java.lang.Class<T> spi, T defaultValue)Loads the first available implementation the given SPI class from the classpath.
-
-
-
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 theServiceLoaderto 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 theServiceLoaderto 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 InterfacedefaultValue- 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.
-
-