public abstract class FactoriesLoader extends Object
Loader for automatically discovered factories. This loader provides a
pluggable mechanism for discovering factories that are in the class-path.
Despite its name, this mechanisms is not limited to factories, but can be
used with any interfaces (or parent class). This class has been inspired by
the SpringFactoriesLoader
from Spring Boot.
This class loads the file specified by FACTORIES_RESOURCE_LOCATION
from the class-path. Multiple instances of this file (originating from
different modules) might exist in the class-path. This is intended and the
entries found in all those files are merged to get a list of all available
implementations in the class-path. The file itself follows the Java
properties syntax, using interface names as keys and implementation names as
values.
Modifier and Type | Field and Description |
---|---|
static String |
FACTORIES_RESOURCE_LOCATION
Location of the factories configuration file in the class-path.
|
Constructor and Description |
---|
FactoriesLoader() |
Modifier and Type | Method and Description |
---|---|
static <FactoryType> |
loadFactories(Class<? extends FactoryType> factoryType,
ClassLoader classLoader)
Loads and instantiates all factories implementing a given base type.
|
static List<String> |
loadFactoryNames(Class<?> factoryType,
ClassLoader classLoader)
Loads the list of class names of implementations for a specific interface
or base class.
|
public static final String FACTORIES_RESOURCE_LOCATION
public static List<String> loadFactoryNames(Class<?> factoryType, ClassLoader classLoader)
FACTORIES_RESOURCE_LOCATION
file and looks up the property whose
key is equal to the fully-qualified class name of
factoryType
. It builds a list containing the values listed
under those keys. A single property file might contain multiple values
for the same key if the values are separated by commas. The values are
not verified in any way (for example, they might not represent the names
of loadable classes) and simply returned.factoryType
- type of the interface or class whose fully qualified name
should be used as the lookup key.classLoader
- class loader to be used when loading the
FACTORIES_RESOURCE_LOCATION
files. If
null
, the class loader that loaded this class is
used.null
.RuntimeException
- if there is an I/O error while trying to load one of the
configuration files from the class-path.public static <FactoryType> List<FactoryType> loadFactories(Class<? extends FactoryType> factoryType, ClassLoader classLoader)
loadFactoryNames(Class, ClassLoader)
to find all implementations
of the specified base type. It then tries to create instances using the
values returned by loadFactoryNames(Class, ClassLoader)
as class
names. This means that each implementation listed for the specified base
type must have a default constructor.FactoryType
- type of the interface or base class for which factories are
created. Typically, this is automatically inferred from
factoryType
.factoryType
- interface or base class that has to be implemented by all
discovered implementations. The fully qualified name of this
type is used when discovering the implementations.classLoader
- class loader used for discovering implementations and loading
their classes. If null
, the class loader that
loaded this class is used.factoryType
. This
list might be empty, but it is never null
.IllegalArgumentException
- if one of the discovered classes cannot be instantiated (e.g.
because it cannot be loaded or it does not have a default
constructor).RuntimeException
- if there is an I/O problem while trying to discover the
implementations.Copyright © 2011–2017 aquenos GmbH. All rights reserved.