Class ExtensionFactoryRegistry<T extends ExtensionFactory<?,S>,S extends AlluxioConfiguration>
- java.lang.Object
-
- alluxio.extensions.ExtensionFactoryRegistry<T,S>
-
- Type Parameters:
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 java.lang.Object
An extension registry that uses the
ServiceLoader
mechanism to automatically discover available factories and provides a central place for obtaining actual extension instances.Registering New Factories
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 enableDEBUG
level logging for this class to see the order in which factories are discovered and which is selected when obtaining aExtensionFactoryRegistry
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.Automatic Discovery
To use the ServiceLoader based mechanism you need to have a file with the same name as the factory class
T
placed in theMETA-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 (seeServiceLoader
for more detail on this). You can enableDEBUG
level logging to see factories as they are discovered if you wish to check that your implementation gets discovered.Manual Registration
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.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
UNKNOWN_VERSION
-
Constructor Summary
Constructors Constructor Description ExtensionFactoryRegistry(java.lang.Class<T> factoryClass, java.lang.String extensionPattern)
Constructs a registry for loading extension of a particular type.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.List<T>
findAllWithRecorder(java.lang.String path, S conf, Recorder recorder)
Finds all the factories that support the given path and record the detailed execution process.java.util.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 statejava.util.List<T>
scanRegistered(java.lang.String path, S conf)
Finds factories from registered collection.void
unregister(T factory)
Unregisters an existing factory.
-
-
-
Field Detail
-
UNKNOWN_VERSION
public static final java.lang.String UNKNOWN_VERSION
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ExtensionFactoryRegistry
public ExtensionFactoryRegistry(java.lang.Class<T> factoryClass, java.lang.String extensionPattern)
Constructs a registry for loading extension of a particular type.- Parameters:
factoryClass
- the type of the extension factoryextensionPattern
- the pattern used to select libraries to be loaded
-
-
Method Detail
-
getAvailable
public java.util.List<T> getAvailable()
Returns a read-only view of the available base factories.- Returns:
- Read-only view of the available base factories
-
findAllWithRecorder
public java.util.List<T> findAllWithRecorder(java.lang.String path, S conf, Recorder recorder)
Finds all the factories that support the given path and record the detailed execution process.- Parameters:
path
- pathconf
- configuration of the extensionrecorder
- recorder used to record the detailed execution process- Returns:
- list of factories that support the given path which may be an empty list
-
scanRegistered
public java.util.List<T> scanRegistered(java.lang.String path, S conf)
Finds factories from registered collection.- Parameters:
path
- pathconf
- configuration of the extension- Returns:
- list of factories that support the given path which may be an empty list
-
register
public void register(T factory)
Registers a new 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.- Parameters:
factory
- factory to register
-
reset
public void reset()
Resets the registry to its default stateThis clears the registry as it stands and rediscovers the available factories.
-
unregister
public void unregister(T factory)
Unregisters an existing factory.- Parameters:
factory
- factory to unregister
-
-