net.jinx.plugin
Class PluginLoader

java.lang.Object
  extended by net.jinx.plugin.PluginLoader

public class PluginLoader
extends java.lang.Object


Constructor Summary
PluginLoader(java.lang.Class tag, java.io.File dir, boolean deep)
          Constructs a PluginLoader.
PluginLoader(java.io.File dir)
          Constructs a PluginLoader.
PluginLoader(java.io.File dir, boolean deep)
          Constructs a PluginLoader.
 
Method Summary
protected  void foundTaggedClass(java.lang.Class c)
          Adds a class to the tagged class cache.
protected  java.lang.ClassLoader getClassLoader(java.io.File jar)
          Provides a class loader for the given jar file.
 java.lang.Class[] getTagged()
          Returns an array containing all of the found tagged classes.
 java.lang.Class[] getTaggedExtending(java.lang.Class sc)
          Returns an array of all of the Classes that extend sc.
 java.lang.Class[] getTaggedImplementingAll(java.lang.Class[] interfaces)
          Returns an array of all of the Classes that implement all of interfaces.
 java.lang.Class[] getTaggedImplementingAny(java.lang.Class[] interfaces)
          Returns an array of all of the Classes that implement any of interfaces.
protected  boolean hasScanned(java.io.File file)
          Provides a way to check if a given file has already been marked scanned.
protected  void markScanned(java.io.File file)
          Marks the given File object as having been scanned.
protected  void rescan()
          Resets this PluginLoader, discarding all cached tagged classes.
 void scan()
          Scans for new jar files and adds their classes to the tagged class collection if they extend/implement tag (from the constructor).
protected  void scan(java.io.File dir, boolean deep)
          Scans a directory for new jar files.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PluginLoader

public PluginLoader(java.io.File dir)
Constructs a PluginLoader. The tag is assumed to be Plugin. This constructor forces a shallow scan.

Parameters:
dir - the directory to load plugins from.
Throws:
java.lang.NullPointerException - if dir is null.
java.lang.IllegalArgumentException - if dir is not a directory or doesn't exist.
java.lang.SecurityException - if dir is not accessible.

PluginLoader

public PluginLoader(java.io.File dir,
                    boolean deep)
Constructs a PluginLoader. The tag is assumed to be Plugin.

Parameters:
dir - the directory to load plugins from.
deep - indicates if dir should be deep scanned. If true, then all sub-directories of dir will be searched as well.
Throws:
java.lang.NullPointerException - if dir is null.
java.lang.IllegalArgumentException - if dir is not a directory or doesn't exist.
java.lang.SecurityException - if dir is not accessible.

PluginLoader

public PluginLoader(java.lang.Class tag,
                    java.io.File dir,
                    boolean deep)
Constructs a PluginLoader.

Parameters:
tag - the tagging class/interface to scan for.
dir - the directory to load plugins from.
deep - indicates if dir should be deep scanned. If true, then all sub-directories of dir will be searched as well.
Throws:
java.lang.NullPointerException - if dir or tag are null.
java.lang.IllegalArgumentException - if dir is not a directory or doesn't exist.
java.lang.SecurityException - if dir is not accessible.
Method Detail

scan

public void scan()
Scans for new jar files and adds their classes to the tagged class collection if they extend/implement tag (from the constructor).


scan

protected void scan(java.io.File dir,
                    boolean deep)
Scans a directory for new jar files.

Parameters:
dir - the directory to search.
deep - should dir be recursively searched?

getClassLoader

protected java.lang.ClassLoader getClassLoader(java.io.File jar)
                                        throws java.io.IOException
Provides a class loader for the given jar file. This can be overriden if you want to change how tagged classes are loaded.

Parameters:
jar - the File object for the jar file.
Throws:
java.io.IOException - if an I/O error occurs while setting up the class loader.

rescan

protected void rescan()
Resets this PluginLoader, discarding all cached tagged classes. After that is done, scan() is called to rebuild the cache.


hasScanned

protected boolean hasScanned(java.io.File file)
Provides a way to check if a given file has already been marked scanned.

Parameters:
file - the File to check.
Returns:
true if the file has been marked scanned.

markScanned

protected void markScanned(java.io.File file)
Marks the given File object as having been scanned.

Parameters:
file - the File that's been scanned.

foundTaggedClass

protected void foundTaggedClass(java.lang.Class c)
Adds a class to the tagged class cache.

Parameters:
c - the Class.

getTagged

public java.lang.Class[] getTagged()
Returns an array containing all of the found tagged classes.

Returns:
the array.

getTaggedExtending

public java.lang.Class[] getTaggedExtending(java.lang.Class sc)
Returns an array of all of the Classes that extend sc.

Parameters:
sc - the class to search for extensions of.
Returns:
the array.

getTaggedImplementingAll

public java.lang.Class[] getTaggedImplementingAll(java.lang.Class[] interfaces)
Returns an array of all of the Classes that implement all of interfaces.

Parameters:
interfaces - the interfaces that must be implemented.
Returns:
the array.

getTaggedImplementingAny

public java.lang.Class[] getTaggedImplementingAny(java.lang.Class[] interfaces)
Returns an array of all of the Classes that implement any of interfaces.

Parameters:
interfaces - the interfaces that must be implemented.
Returns:
the array.