org.pfsw.plugin
Class PluginCollector<TType>

java.lang.Object
  extended by org.pfsw.plugin.PluginCollector<TType>

public class PluginCollector<TType>
extends java.lang.Object

The responsibility of this class is to detect all properties files with a specific name on a given classpath and load the defined classes that fit to a given class/interface into a class regitry.

Since version 1.3 it is possible to specify a system property named "org.pf.plugin.path" to define the path on which the plugin collector must search for plug-ins.

Since version 1.4 it is also possible to load all plugin definitions from a single file that is looked-up via the standard classloader mechanism getResourceStream().

Since version 1.5 it is also possible to load plugin definitions from classes that implement the PluginRegistration interface. These classes are found by the Java service loader mechanism on the whole classpath (based on classloader hierarchy).


Field Summary
static boolean DEBUG
           
protected static java.lang.String DEBUG_PREFIX
           
 
Constructor Summary
PluginCollector()
          Initialize the new instance with default values.
PluginCollector(java.lang.Class<TType> pluginType)
          Initialize the new instance the type the plugins to collect must be compliant with.
PluginCollector(ClassRegistry<TType> registry)
          Initialize the new instance with a class registry.
 
Method Summary
protected  void addMappings(java.util.Map<java.lang.String,java.lang.Class<?>> classesToRegister)
           
protected  void addMappings(java.util.Properties properties)
           
protected  void debug(java.lang.String text)
           
protected  org.pfsw.file.FileUtil fileUtil()
           
 ClassRegistry<TType> getClassRegistry()
          Returns the class registry that contains all loaded plugins.
protected  org.pfsw.file.Classpath getPluginClasspath()
          Returns the classpath that is used to lookup any plug-in information files.
 ClassRegistry<TType> loadPlugins(java.lang.String filename)
          Load the class definitions from all files with the given name that can be found in the system classpath.
 ClassRegistry<TType> loadPlugins(java.lang.String filename, org.pfsw.file.Classpath classpath)
          Load the class definitions from all files with the given name that can be found in the specified classpath.
 ClassRegistry<TType> loadPlugins(java.lang.String filename, java.lang.String classpath)
          Load the class definitions from all files with the given name that can be found in the specified classpath.
 ClassRegistry<TType> loadPluginsViaClassLoader(java.lang.String filename)
          Load the class definitions from the file with the given name.
 ClassRegistry<TType> loadPluginsViaPluginRegistration()
          Load the class definitions from all PluginRegistration implementing classes that can be found via Java service loader mechanism using the classloader of this class.
 ClassRegistry<TType> loadPluginsViaPluginRegistration(java.lang.ClassLoader classLoader)
          Load the class definitions from all PluginRegistration implementing classes that can be found via Java service loader mechanism in the given classloader's hierarchy.
protected  PluginUtil pluginUtil()
           
protected  void readMapping(org.pfsw.file.ClasspathElement element, java.lang.String filename)
           
protected  void readMapping(java.io.InputStream stream, java.lang.String filename)
          Reads all plugin definitions from the given stream into the underlying registry.
protected  void 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  void readPlugins(java.lang.String filename, java.lang.String strClasspath)
          Read all classes specified in the given file, if it exists in the specified classpath element.
protected  void setClassRegistry(ClassRegistry<TType> newValue)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEBUG_PREFIX

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

DEBUG

public static boolean DEBUG
Constructor Detail

PluginCollector

public PluginCollector()
Initialize the new instance with default values.


PluginCollector

public PluginCollector(java.lang.Class<TType> pluginType)
Initialize the new instance the type the plugins to collect must be compliant with.


PluginCollector

public PluginCollector(ClassRegistry<TType> registry)
Initialize the new instance with a class registry.

Method Detail

getClassRegistry

public ClassRegistry<TType> getClassRegistry()
Returns the class registry that contains all loaded plugins.


loadPluginsViaClassLoader

public ClassRegistry<TType> loadPluginsViaClassLoader(java.lang.String filename)
Load the class definitions from the file with the given name. The file will be searched for utilizing the classloader of this class.

Parameters:
filename - The name of the properties file that contains the class definitions
Returns:
The class registry that contains the new definitions if the file has been found.

loadPlugins

public ClassRegistry<TType> loadPlugins(java.lang.String filename)
Load the class definitions from all files with the given name that can be found in the system classpath.

Parameters:
filename - The name of the properties file that contains the class definitions
Returns:
The class registry that contains the new definitions from all the files that have been found.

loadPlugins

public ClassRegistry<TType> loadPlugins(java.lang.String filename,
                                        java.lang.String classpath)
Load the class definitions from all files with the given name that can be found in the specified classpath.

Parameters:
filename - The name of the properties file that contains the class definitions
classpath - The classpath the specified filename to be looked up in

loadPlugins

public ClassRegistry<TType> loadPlugins(java.lang.String filename,
                                        org.pfsw.file.Classpath classpath)
Load the class definitions from all files with the given name that can be found in the specified classpath.

Parameters:
filename - The name of the properties file that contains the class definitions
classpath - The classpath on which to lookup the the specified filename

loadPluginsViaPluginRegistration

public ClassRegistry<TType> loadPluginsViaPluginRegistration(java.lang.ClassLoader classLoader)
Load the class definitions from all PluginRegistration implementing classes that can be found via Java service loader mechanism in the given classloader's hierarchy. If the given classcloader is null the first non-null classloader of this list will be used.
  1. The current thread's context class loader
  2. the class loader of this class
  3. the system class loader

Parameters:
classLoader - The class loader from which to start looking for services that implement interface PluginRegistration.

loadPluginsViaPluginRegistration

public ClassRegistry<TType> loadPluginsViaPluginRegistration()
Load the class definitions from all PluginRegistration implementing classes that can be found via Java service loader mechanism using the classloader of this class.


readPlugins

protected void readPlugins(java.lang.String filename,
                           java.lang.String strClasspath)
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
strClasspath - The classpath the specified filename to be looked up in

readPlugins

protected void 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

readMapping

protected void readMapping(org.pfsw.file.ClasspathElement element,
                           java.lang.String filename)

readMapping

protected void readMapping(java.io.InputStream stream,
                           java.lang.String filename)
Reads all plugin definitions from the given stream into the underlying registry. This method definitely closes the given stream (even in case of an exception).

Parameters:
stream - The stream from which to read the definitions.
filename - The filename that corresponds to the stream (just for debugging).

addMappings

protected void addMappings(java.util.Properties properties)

addMappings

protected void addMappings(java.util.Map<java.lang.String,java.lang.Class<?>> classesToRegister)

getPluginClasspath

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


setClassRegistry

protected void setClassRegistry(ClassRegistry<TType> newValue)

fileUtil

protected org.pfsw.file.FileUtil fileUtil()

pluginUtil

protected PluginUtil pluginUtil()

debug

protected void debug(java.lang.String text)