se.hirt.plugin
Class PluginLoader

java.lang.Object
  extended by se.hirt.plugin.PluginLoader

public class PluginLoader
extends java.lang.Object

Utility class for finding and instantiating implementors and/or subclasses of a class in a set of specified directories and/or JAR files. Since we may want to separate the search URL's for different plugins and minimize interference between sets of plugins, the factory methods is used to create an new plugin loader.

Author:
Marcus Hirt

Field Summary
static java.lang.String CLASS_SUFFIX
          The suffix for classes.
static java.lang.String JAR_SUFFIX
          The suffix for JAR files.
 
Method Summary
<T> java.util.Set<T>
createPlugins(java.lang.Class<T> clazz)
          Convenience function using the default constructor, to create instances of either: 1.
 java.util.Set<java.lang.Class> getImplementorsOf(java.lang.Class<?> myInterface)
           
 java.util.Set<java.lang.Class> getSubclassesOf(java.lang.Class<?> myClass)
           
 java.net.URL[] getURLs()
           
 void rescan()
          Rescans all files and directories and resets all caches.
 void setURLs(java.net.URL[] urls)
          Sets the URL's to search for classes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CLASS_SUFFIX

public static final java.lang.String CLASS_SUFFIX
The suffix for classes.

See Also:
Constant Field Values

JAR_SUFFIX

public static final java.lang.String JAR_SUFFIX
The suffix for JAR files.

See Also:
Constant Field Values
Method Detail

setURLs

public void setURLs(java.net.URL[] urls)
Sets the URL's to search for classes. If a URL is a directory, it will be scanned for jar files and classes. If a URL is a jar file, it will be scanned for classes.

Parameters:
urls - the URLS to search for classes.

getURLs

public java.net.URL[] getURLs()
Returns:
the URLs searched for classes.

getSubclassesOf

public java.util.Set<java.lang.Class> getSubclassesOf(java.lang.Class<?> myClass)
Parameters:
myClass - the class object of the class to retrieve the subclasses for. May not be an interface.
Returns:
all instantiable subclasses of the class myClass.

getImplementorsOf

public java.util.Set<java.lang.Class> getImplementorsOf(java.lang.Class<?> myInterface)
Parameters:
myInterface - the class of the interface to retrieve the implementors for. This class must be a valid interface!
Returns:
all the implementors of the provided interface, that can be instantiated (that have public contructors and that aren't abstract or interfaces themselves)

rescan

public void rescan()
Rescans all files and directories and resets all caches.


createPlugins

public <T> java.util.Set<T> createPlugins(java.lang.Class<T> clazz)
                               throws java.lang.InstantiationException,
                                      java.lang.IllegalAccessException
Convenience function using the default constructor, to create instances of either: 1. If clazz is a class, it's instantiable subclasses will be found and one instance per subclass will be created. 2. If clazz is an interface, all its instantiable implementors will be found, and one instace per implementor will be created.

Type Parameters:
T -
Parameters:
clazz - the class for which to find classes to instantiate.
Returns:
the empty set if no suitable classes were found, or if no instance could be created, or the set of freshly baked plugins as described.
Throws:
java.lang.IllegalAccessException
java.lang.InstantiationException


Copyright © 2005 Marcus Hirt