T
- The type of extension factoryS
- the type of configuration to be used when creating the extension@NotThreadSafe public class ExtensionFactoryRegistry<T extends ExtensionFactory<?,S>,S extends AlluxioConfiguration> extends Object
An extension registry that uses the ServiceLoader
mechanism to automatically discover
available factories and provides a central place for obtaining actual extension instances.
New factories can be registered either using the ServiceLoader based automatic
discovery mechanism or manually using the static #register(T)
method. The down-side of the automatic discovery mechanism is that the discovery order is not
controllable. As a result if your implementation is designed as a replacement for one of the
standard implementations, depending on the order in which the JVM discovers the services your own
implementation may not take priority. You can enable DEBUG
level logging for this class
to see the order in which factories are discovered and which is selected when obtaining a
T
instance for a path. If this shows that your implementation is not
getting discovered or used, you may wish to use the manual registration approach.
To use the ServiceLoader based mechanism you need to have a file with the same name
as the factory class T
placed in the META-INF\services
directory
of your project. This file should contain the full name of your factory types (one per line),
your factory types must have a public unparameterised constructor available (see
ServiceLoader
for more detail on this). You can enable DEBUG
level logging to see
factories as they are discovered if you wish to check that your implementation gets discovered.
To manually register a factory, simply pass an instance of your factory to the
#register(T)
method. This can be useful when your factory cannot be
instantiated without arguments or in cases where automatic discovery does not give your factory
priority. Factories registered this way will be registered at the start of the factories list so
will have the first opportunity to indicate whether they support a requested path.
Constructor and Description |
---|
ExtensionFactoryRegistry(Class<T> factoryClass,
String extensionPattern)
Constructs a registry for loading extension of a particular type.
|
Modifier and Type | Method and Description |
---|---|
List<T> |
findAll(String path,
S conf)
Finds all the factories that support the given path.
|
List<T> |
getAvailable()
Returns a read-only view of the available base factories.
|
void |
register(T factory)
Registers a new factory.
|
void |
reset()
Resets the registry to its default state
|
void |
unregister(T factory)
Unregisters an existing factory.
|
public ExtensionFactoryRegistry(Class<T> factoryClass, String extensionPattern)
factoryClass
- the type of the extension factoryextensionPattern
- the pattern used to select libraries to be loadedpublic List<T> getAvailable()
public List<T> findAll(String path, S conf)
path
- pathconf
- configuration of the extensionpublic void register(T factory)
Factories are registered at the start of the factories list so they can override the existing
automatically discovered factories. Generally if you use the ServiceLoader
mechanism
properly it should be unnecessary to call this, however since ServiceLoader discovery order
may be susceptible to class loader behavioral differences there may be rare cases when you
need to manually register the desired factory.
factory
- factory to registerpublic void reset()
This clears the registry as it stands and rediscovers the available factories.
public void unregister(T factory)
factory
- factory to unregisterCopyright © 2023. All Rights Reserved.