Class ServiceLoader<T>
- java.lang.Object
-
- com.saperion.common.lang.classloading.ServiceLoader<T>
-
- Type Parameters:
T- type of the service interface to be loaded by thisServiceLoader
- All Implemented Interfaces:
java.lang.Iterable<T>
public final class ServiceLoader<T> extends java.lang.Object implements java.lang.Iterable<T>A simple extension of aServiceLoader. This class provides the same methods and some additional functionality.- Author:
- agz
- See Also:
ServiceLoader
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.Collection<T>getAllProviders()Loads all providers the same way as a call toiterator()would do.TgetFirstProvider()Loads and returns the first provider the same way as a call toiterator()would do.TgetProvider(java.lang.String className)Returns the provider with the specified class name.java.lang.Class<T>getService()Returns the service interface of this loader.java.util.Iterator<T>iterator()SeeServiceLoader.iterator()static <T> ServiceLoader<T>load(java.lang.Class<T> service)Creates a newServiceLoaderfor the specified service interface, using the current thread's context class loader.static <T> ServiceLoader<T>load(java.lang.Class<T> service, java.lang.ClassLoader loader)Creates a newServiceLoaderfor the specified service interface and class loader.static <T> ServiceLoader<T>loadInstalled(java.lang.Class<T> service)Creates a newServiceLoaderfor the specified service type, using the extension class loader.voidreload()SeeServiceLoader.reload().java.lang.StringtoString()
-
-
-
Method Detail
-
load
public static <T> ServiceLoader<T> load(java.lang.Class<T> service, java.lang.ClassLoader loader)
Creates a newServiceLoaderfor the specified service interface and class loader.- Type Parameters:
T- type of the service interface to be loaded by thisServiceLoader- Parameters:
service- the interface or abstract class representing the serviceloader- the class loader to be used to load provider-configuration files and provider classes, ornullif the system class loader (or, failing that, the bootstrap class loader) is to be used- Returns:
- a new
ServiceLoader - See Also:
ServiceLoader.load(Class, ClassLoader)
-
load
public static <T> ServiceLoader<T> load(java.lang.Class<T> service)
Creates a newServiceLoaderfor the specified service interface, using the current thread's context class loader.- Type Parameters:
T- type of the service interface to be loaded by thisServiceLoader- Parameters:
service- the interface or abstract class representing the service- Returns:
- a new
ServiceLoader - See Also:
ServiceLoader.load(Class)
-
loadInstalled
public static <T> ServiceLoader<T> loadInstalled(java.lang.Class<T> service)
Creates a newServiceLoaderfor the specified service type, using the extension class loader.If the extension class loader cannot be found then the system class loader is used; if there is no system class loader then the bootstrap class loader is used.
This method is intended for use when only installed providers are desired. The resulting service will only find and load providers that have been installed into the current Java virtual machine; providers on the application's class path will be ignored.
- Type Parameters:
T- type of the service interface to be loaded by thisServiceLoader- Parameters:
service- the interface or abstract class representing the service- Returns:
- a new
ServiceLoader - See Also:
ServiceLoader.loadInstalled(Class)
-
getService
public java.lang.Class<T> getService()
Returns the service interface of this loader.- Returns:
- the service interface of this loader
-
reload
public void reload()
SeeServiceLoader.reload().
-
iterator
public java.util.Iterator<T> iterator()
SeeServiceLoader.iterator()- Specified by:
iteratorin interfacejava.lang.Iterable<T>
-
getFirstProvider
public T getFirstProvider()
Loads and returns the first provider the same way as a call toiterator()would do.- Returns:
- the first provider
- Throws:
java.util.ServiceConfigurationError- on errors getting the first provider
-
getProvider
public T getProvider(java.lang.String className)
Returns the provider with the specified class name. Throws anServiceConfigurationErrorif no such provider is found. Loads providers the same way as a call toiterator()would do until it finds the provider with the specified name or until all providers are iterated through.- Parameters:
className- class name of the provider to find- Returns:
- the provider with the specified class name
- Throws:
java.util.ServiceConfigurationError- on errors getting the provider with the specified class name
-
getAllProviders
public java.util.Collection<T> getAllProviders()
Loads all providers the same way as a call toiterator()would do. Returns aCollectionof these providers. Returns an emptyCollectionif no provider is configured.- Returns:
Collectionof all providers- Throws:
java.util.ServiceConfigurationError- on errors getting the providers
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-