public abstract class PluginLoader<TInterface>
extends java.lang.Object
Modifier | Constructor and Description |
---|---|
protected |
PluginLoader(java.lang.Class<TInterface> interfaceClass,
java.lang.String classNameConfigParamName,
java.lang.String aliasConfigParamName) |
Modifier and Type | Method and Description |
---|---|
protected ClassRegistry<TInterface> |
autoLoadPlugins(java.lang.ClassLoader loader) |
protected TInterface |
createInstanceFromServiceDeclaration(java.lang.ClassLoader classLoader) |
protected boolean |
defaultToFirstEntry()
The return value of this method defines if the first entry in the found
plugins should be returned as default if no explicit alias name has been
provided in the configuration settings.
|
protected java.lang.ClassLoader |
determineClassLoader(java.lang.ClassLoader classLoader) |
protected java.lang.String |
getAliasConfigParamName() |
ClassRegistry<TInterface> |
getAllPluginsViaPluginRegistration()
Returns all plugins that can be found via the PluginRegistration mechanism
for the underlying interface definition.
|
ClassRegistry<TInterface> |
getAllPluginsViaPluginRegistration(java.lang.ClassLoader loader)
Returns all plugins that can be found via the PluginRegistration mechanism
for the underlying interface definition using the given class loader as
preferred class loader for the Java ServiceLoader lookup.
|
protected java.lang.String |
getClassNameConfigParamName() |
protected java.lang.Class<TInterface> |
getInterfaceClass() |
protected java.lang.String |
getParameterNames() |
protected TInterface |
loadFirstFoundInstance(java.lang.ClassLoader classLoader)
Returns the and instance of the first found class definition.
|
protected TInterface |
loadInstance(java.lang.ClassLoader classLoader,
java.lang.Class<TInterface> implClass)
Returns the instance for the given class.
|
protected TInterface |
loadInstance(java.lang.ClassLoader classLoader,
java.lang.String className)
Returns the instance for the given className.
|
protected TInterface |
loadInstanceByAlias(java.lang.ClassLoader classLoader,
java.lang.String aliasName)
Returns the instance for the given alias.
|
protected TInterface |
loadInstanceFromService(java.lang.ClassLoader classLoader)
Returns the instance that is specified in the first
META-INF/services/full-qualified-TInterface-name
file on the classpath.
|
TInterface |
loadPlugin(java.lang.ClassLoader classLoader,
IConfigSettings config,
boolean fallbackToServiceLoader)
Returns the instance that is specified by class name configuration parameter
or indirectly by the alias name optionally defined in the alias configuration parameter.
|
TInterface |
loadPlugin(IConfigSettings config)
Returns the instance that is specified by class name configuration parameter
or indirectly by the alias name optionally defined in the alias configuration parameter.
|
protected PluginUtil |
pluginUtil() |
protected PluginLoader(java.lang.Class<TInterface> interfaceClass, java.lang.String classNameConfigParamName, java.lang.String aliasConfigParamName)
public TInterface loadPlugin(IConfigSettings config) throws ConfigurationException
If the class configuration parameter is not specified and the alias names is also not available then the class will be looked-up using the Java service loader mechanism. In that case the first found implementation class for interface TInterface will be taken. Wherever a classloader is needed the current thread's context class loader will be used.
config
- The configuration from which the parameters for class name or alias name will be taken (must not be null).ConfigurationException
- If no implementation class was configured or it cannot be instantiated.public TInterface loadPlugin(java.lang.ClassLoader classLoader, IConfigSettings config, boolean fallbackToServiceLoader) throws ConfigurationException
If the class configuration parameter is not specified and the alias names is also not available then either a ConfigurationException gets thrown (fallbackToServiceLoader=false) or the class will be looked-up using the service loader mechanism (fallbackToServiceLoader=true). In that case the first found implementation class for interface TInterface will be taken.
classLoader
- An optional class loader. If null the current threads context classloader will be used.config
- The configuration from which the parameters for class name or alias name will be taken (must not be null).fallbackToServiceLoader
- Definition whether or not finally the Java ServiceLoader mechanism should be tried.ConfigurationException
- If no implementation class was configured or it cannot be instantiated.public ClassRegistry<TInterface> getAllPluginsViaPluginRegistration()
public ClassRegistry<TInterface> getAllPluginsViaPluginRegistration(java.lang.ClassLoader loader)
loader
- The preferred class loader (may be null).protected TInterface loadInstanceFromService(java.lang.ClassLoader classLoader) throws ConfigurationException
classLoader
- An optional class loader to lookup the service implementors (may be null).ConfigurationException
- If no implementation class was configured or it cannot be instantiated.protected TInterface loadInstance(java.lang.ClassLoader classLoader, java.lang.String className) throws ConfigurationException
classLoader
- The class loader to use for loading the class. If null the class loader
of this class will be used.className
- The name of the class that implements interface TInterface (must not be null).ConfigurationException
- If the class cannot be loaded or instantiated.protected TInterface loadInstanceByAlias(java.lang.ClassLoader classLoader, java.lang.String aliasName)
classLoader
- The class loader to use for loading the class mapping.
If null the context classloader or the class loader of this class will be used.aliasName
- The name of the alias that is associated to a class that implements interface TInterface (must not be null).protected TInterface loadFirstFoundInstance(java.lang.ClassLoader classLoader)
PluginRegistration
mechanism.classLoader
- The class loader to use for loading the class mapping.
If null the context classloader or the class loader of this class will be used.protected TInterface loadInstance(java.lang.ClassLoader classLoader, java.lang.Class<TInterface> implClass) throws ConfigurationException
classLoader
- An optional classloader to be used for looking up the interface implementations.implClass
- The class to instantiate. If null the service loader
mechanism will be used (looking for file META-INF/services/TInterface).ConfigurationException
- If the class cannot be instantiated.protected TInterface createInstanceFromServiceDeclaration(java.lang.ClassLoader classLoader)
protected ClassRegistry<TInterface> autoLoadPlugins(java.lang.ClassLoader loader)
protected java.lang.ClassLoader determineClassLoader(java.lang.ClassLoader classLoader)
protected java.lang.String getParameterNames()
protected boolean defaultToFirstEntry()
Here it returns always false - so no default will be provided.
Subclasses may override this method to alter the behavior.
protected java.lang.Class<TInterface> getInterfaceClass()
protected java.lang.String getClassNameConfigParamName()
protected java.lang.String getAliasConfigParamName()
protected PluginUtil pluginUtil()