Class ModuleLoader
java.lang.Object
com.github.cafapi.common.util.moduleloader.ModuleLoader
Utility class that provides methods for finding and returning components at runtime using the Java ServiceLoader.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
getService
(Class<T> intf) Determine the first advertised service implementation for the specified interface.static <T> T
getService
(Class<T> intf, Class<? extends T> defaultImpl) Determine the first advertised service implementation for the specified interface.static <T> T
getServiceOrElse
(Class<T> intf, T defaultObj) Determine the first advertised service implementation for the specified interface.static <T> List<T>
getServices
(Class<T> intf) Get all advertised service implementations of the specified interface.
-
Method Details
-
getService
Determine the first advertised service implementation for the specified interface. The implementations are advertised via the Java "ServiceLoader" mechanism.- Type Parameters:
T
- the interface- Parameters:
intf
- the interface to find an advertised service implementation for- Returns:
- an advertised implementation of intf of type T
- Throws:
ModuleLoaderException
- if there is no advertised implementation available
-
getService
public static <T> T getService(Class<T> intf, Class<? extends T> defaultImpl) throws ModuleLoaderException Determine the first advertised service implementation for the specified interface. The implementations are advertised via the Java "ServiceLoader" mechanism.- Type Parameters:
T
- the interface- Parameters:
intf
- the interface to find an advertised service implementation fordefaultImpl
- the default implementation class if an advertised one is not found, may be null- Returns:
- an advertised implementation of intf of type T
- Throws:
ModuleLoaderException
- if the implementation is missing and no defaultImpl is specified
-
getServiceOrElse
Determine the first advertised service implementation for the specified interface. The implementations are advertised via the Java "ServiceLoader" mechanism.- Type Parameters:
T
- the interface- Parameters:
intf
- the interface to find an advertised service implementation fordefaultObj
- the default object to return if an advertised one is not found, may be null- Returns:
- an advertised implementation of intf of type T, or the default object if one is not found
-
getServices
Get all advertised service implementations of the specified interface.- Type Parameters:
T
- the interface- Parameters:
intf
- the interface to find advertised service implementations of- Returns:
- a collection of implementations of the specified interface
-