org.pfsw.plugin
Class PluginRegistry

java.lang.Object
  extended by org.pfsw.plugin.PluginRegistry

public class PluginRegistry
extends java.lang.Object

A special registry that is capable to hold any number of plug-ins of any type.


Field Summary
protected static boolean DEBUG
           
protected static java.lang.String DEBUG_PREFIX
           
protected static java.lang.String OPTIONS_END
           
protected static java.lang.String OPTIONS_START
           
protected static java.lang.String PLUGIN_DEF_SEPARATOR
           
protected static org.pfsw.text.StringPattern PLUGIN_OPTIONS_PATTERN
           
 
Constructor Summary
PluginRegistry()
          Initialize the new instance with default values (i.e. an empty registry).
 
Method Summary
protected  int addDefinitionsToRegistry(org.pfsw.util.OrderedProperties<?> definitions)
           
 void clear()
          Remove all registered plugins.
protected  org.pfsw.util.CollectionUtil coll()
           
protected  void debug(java.lang.String text)
           
protected  org.pfsw.file.FileUtil fileUtil()
           
protected  java.lang.reflect.Method find_getPluginId_Method(java.lang.Object object)
           
protected  java.lang.String getIdOfPlugin(java.lang.Object plugin, java.lang.String defaultId)
           
 java.lang.Object getPlugin(java.lang.String pluginId)
          Returns the plug-in with the specified ID or null if no such plug-in can be found in the registry.
<T> T
getPlugin(java.lang.String pluginId, java.lang.Class<T> pluginType)
          Returns the plug-in with the specified ID and the given type or null if no such plug-in can be found in the registry.
protected  org.pfsw.file.Classpath getPluginClasspath()
          Returns the classpath that is used to lookup any plug-in information files.
 java.lang.String[] getPluginIDsOfType(java.lang.Class<?> pluginType)
          Returns the IDs of all plug-ins that are of the specified type.
<T> T[]
getPluginsOfType(java.lang.Class<T> pluginType)
          Returns all plug-ins that are of the specified type.
protected  org.pfsw.util.NamedValueList<org.pfsw.plugin.PluginHolder> getRegistry()
           
<T> int
loadPluginsByServiceLoader(java.lang.Class<T> pluginType, java.lang.ClassLoader... classLoaders)
          Utilizes the Java ServiceLoader mechanism to load all plugins implementing the given type into this registry.
 int loadPluginsFrom(java.lang.String filename)
          Load all plug-ins that are defined in any library in the specified file.
protected  boolean parseAndAddToRegistry(java.lang.String id, java.lang.String pluginDefinition)
           
protected  PluginUtil plugUtil()
           
protected  int readPlugins(java.lang.String filename, org.pfsw.file.Classpath classpath)
          Read all classes specified in the given file, if it exists in the specified classpath element.
protected  int readPluginsFrom(org.pfsw.file.ClasspathElement element, java.lang.String filename)
           
protected
<T> void
registerPlugin(java.lang.Class<T> pluginType, T plugin)
           
protected  boolean registerPlugin(org.pfsw.plugin.PluginHolder pluginHolder)
           
protected  void setRegistry(org.pfsw.util.NamedValueList<org.pfsw.plugin.PluginHolder> newValue)
           
protected  org.pfsw.text.StringUtil str()
           
protected  boolean validateClass(org.pfsw.reflect.ClassInfo<?> classInfo)
           
protected  boolean validateClassFound(org.pfsw.reflect.ClassInfo<?> classInfo)
           
protected  boolean validateInstanceCreation(org.pfsw.reflect.ClassInfo<?> classInfo)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEBUG

protected static final boolean DEBUG

DEBUG_PREFIX

protected static final java.lang.String DEBUG_PREFIX
See Also:
Constant Field Values

PLUGIN_DEF_SEPARATOR

protected static final java.lang.String PLUGIN_DEF_SEPARATOR
See Also:
Constant Field Values

OPTIONS_START

protected static final java.lang.String OPTIONS_START
See Also:
Constant Field Values

OPTIONS_END

protected static final java.lang.String OPTIONS_END
See Also:
Constant Field Values

PLUGIN_OPTIONS_PATTERN

protected static final org.pfsw.text.StringPattern PLUGIN_OPTIONS_PATTERN
Constructor Detail

PluginRegistry

public PluginRegistry()
Initialize the new instance with default values (i.e. an empty registry).

Method Detail

loadPluginsFrom

public int loadPluginsFrom(java.lang.String filename)
Load all plug-ins that are defined in any library in the specified file.

Parameters:
filename - The name of the file where the plug-in definition is looked-up.
Returns:
The number of plug-ins that have been loaded to the registry

loadPluginsByServiceLoader

public <T> int loadPluginsByServiceLoader(java.lang.Class<T> pluginType,
                                          java.lang.ClassLoader... classLoaders)
Utilizes the Java ServiceLoader mechanism to load all plugins implementing the given type into this registry.

Parameters:
pluginType - The type (i.e. interface) of the plug-in (must not be null).
classLoaders - An optional array of class loaders. The first non-null classloader will be used for the lookup. If none is provided the thread context classloader and the class loader of this class will be tried.
Returns:
The number of plug-ins that have been loaded to the registry

getPlugin

public java.lang.Object getPlugin(java.lang.String pluginId)
Returns the plug-in with the specified ID or null if no such plug-in can be found in the registry. If more than one plug-in is registered under the same ID the result is the first one found.

Parameters:
pluginId - The identifier of the plug-in (must not be null)
Returns:
An instance of the found plug-in or null

getPlugin

public <T> T getPlugin(java.lang.String pluginId,
                       java.lang.Class<T> pluginType)
Returns the plug-in with the specified ID and the given type or null if no such plug-in can be found in the registry.

Parameters:
pluginId - The identifier of the plug-in (must not be null).
pluginType - The type of the plug-in (must not be null).
Returns:
An instance of the found plug-in or null.

getPluginsOfType

public <T> T[] getPluginsOfType(java.lang.Class<T> pluginType)
Returns all plug-ins that are of the specified type. That also implies plug-ins that are subclasses or implementors of the given type.

Parameters:
pluginType - The type the plug-ins to look for must comply with.
Returns:
An array of found plug-in instances (never null).

getPluginIDsOfType

public java.lang.String[] getPluginIDsOfType(java.lang.Class<?> pluginType)
Returns the IDs of all plug-ins that are of the specified type. That also implies plug-ins that are subclasses or implementors of the given type.

Parameters:
pluginType - The type the plug-ins to look for must comply with.
Returns:
An array of IDs (never null).

clear

public void clear()
Remove all registered plugins.


registerPlugin

protected boolean registerPlugin(org.pfsw.plugin.PluginHolder pluginHolder)

registerPlugin

protected <T> void registerPlugin(java.lang.Class<T> pluginType,
                                  T plugin)

getIdOfPlugin

protected java.lang.String getIdOfPlugin(java.lang.Object plugin,
                                         java.lang.String defaultId)

readPlugins

protected int readPlugins(java.lang.String filename,
                          org.pfsw.file.Classpath classpath)
Read all classes specified in the given file, if it exists in the specified classpath element. Add those classes to the registry if they can be instantiated and are of the correct type.

Parameters:
filename - The name of the properties file that contains the class definitions
classpath - The classpath the specified filename to be looked up in
Returns:
The number of plug-ins that have been added to the registry

readPluginsFrom

protected int readPluginsFrom(org.pfsw.file.ClasspathElement element,
                              java.lang.String filename)

addDefinitionsToRegistry

protected int addDefinitionsToRegistry(org.pfsw.util.OrderedProperties<?> definitions)

parseAndAddToRegistry

protected boolean parseAndAddToRegistry(java.lang.String id,
                                        java.lang.String pluginDefinition)

validateClass

protected boolean validateClass(org.pfsw.reflect.ClassInfo<?> classInfo)

validateClassFound

protected boolean validateClassFound(org.pfsw.reflect.ClassInfo<?> classInfo)

validateInstanceCreation

protected boolean validateInstanceCreation(org.pfsw.reflect.ClassInfo<?> classInfo)

find_getPluginId_Method

protected java.lang.reflect.Method find_getPluginId_Method(java.lang.Object object)

getPluginClasspath

protected org.pfsw.file.Classpath getPluginClasspath()
Returns the classpath that is used to lookup any plug-in information files.


getRegistry

protected org.pfsw.util.NamedValueList<org.pfsw.plugin.PluginHolder> getRegistry()

setRegistry

protected void setRegistry(org.pfsw.util.NamedValueList<org.pfsw.plugin.PluginHolder> newValue)

fileUtil

protected org.pfsw.file.FileUtil fileUtil()

str

protected org.pfsw.text.StringUtil str()

coll

protected org.pfsw.util.CollectionUtil coll()

plugUtil

protected PluginUtil plugUtil()

debug

protected void debug(java.lang.String text)