org.homeunix.thecave.moss.classpath
Class MutableClassLoader

java.lang.Object
  extended by java.lang.ClassLoader
      extended by java.security.SecureClassLoader
          extended by java.net.URLClassLoader
              extended by org.homeunix.thecave.moss.classpath.MutableClassLoader

public class MutableClassLoader
extends java.net.URLClassLoader

Mutable class loader, which allows you to load additional jar files at runtime, via the addURL() method. Most people would want to run this initially from their main method, and use it to load other jars as needed. This class loader also provides other useful meathods, such as the ability to return a list of all classes which implement / extend a given interface / class. This can, for instance, be useful for automatic discovery of plugins at runtime.

Author:
wyatt

Nested Class Summary
static class MutableClassLoader.MutableClassLoaderException
          An exception which is thrown by MutableClassLoader.
 
Constructor Summary
MutableClassLoader()
          Creates a new instance of ExtensibleClassLoader, with a parent delegate of the system class loader.
MutableClassLoader(java.net.URL[] urls)
          Creates a new instance of ExtensibleClassLoader, with a parent delegate of the system class loader, and load all specified URLs.
 
Method Summary
 void addURL(java.net.URL url)
          Adds the URL to the current classpath, if it has not already been added.
 java.net.URL[] getAddedURLs()
          Returns all the URLs which have been added via the addURL() method.
 java.util.List<java.lang.Class<?>> getImplementingClasses(java.lang.Class<?> ancestor, boolean searchAllClasspath)
          Returns all classes in the current class path (system class path plus any added URLs) which are descendents of or implement the given class / interface.
 java.util.List<?> getImplementingObjects(java.lang.Class<?> c, java.lang.Object... constructorAguments)
          Returns a list of instantiated obejcts, all of which are instances of the given class.
 java.net.URL[] getURLs()
          Returns all the URLs which were initially in the classpath, and which have been added via the addURL() method.
 java.lang.Object newInstance(java.lang.Class<?> c, java.lang.Object... arguments)
          Instantiates a new object of the specified class, using the constructor which matches the given arguments.
 java.lang.Object newInstance(java.lang.String className, java.lang.Object... arguments)
          Instantiates a new object of the specified class name, using the constructor which matches the given arguments.
 
Methods inherited from class java.net.URLClassLoader
definePackage, findClass, findResource, findResources, getPermissions, newInstance, newInstance
 
Methods inherited from class java.security.SecureClassLoader
defineClass, defineClass
 
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findLibrary, findLoadedClass, findSystemClass, getPackage, getPackages, getParent, getResource, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, loadClass, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MutableClassLoader

public MutableClassLoader()
Creates a new instance of ExtensibleClassLoader, with a parent delegate of the system class loader.


MutableClassLoader

public MutableClassLoader(java.net.URL[] urls)
Creates a new instance of ExtensibleClassLoader, with a parent delegate of the system class loader, and load all specified URLs.

Parameters:
urls - URLs of resources to load initially. Equivalent to calling the default constructor, and calling addURL() for each of the URLs.
Method Detail

addURL

public void addURL(java.net.URL url)
Adds the URL to the current classpath, if it has not already been added.

Overrides:
addURL in class java.net.URLClassLoader

newInstance

public java.lang.Object newInstance(java.lang.Class<?> c,
                                    java.lang.Object... arguments)
                             throws MutableClassLoader.MutableClassLoaderException
Instantiates a new object of the specified class, using the constructor which matches the given arguments. If successful, we pass back the newly instantiated object; if not successful, we throw an exception with details of the problem.

Parameters:
c - The class to load
arguments - An array of the arguments to the desired constructor
Returns:
Throws:
MutableClassLoader.MutableClassLoaderException

newInstance

public java.lang.Object newInstance(java.lang.String className,
                                    java.lang.Object... arguments)
                             throws MutableClassLoader.MutableClassLoaderException
Instantiates a new object of the specified class name, using the constructor which matches the given arguments. If successful, we pass back the newly instantiated object; if not successful, we throw an exception with details of the problem.

Parameters:
className - The class name to load
arguments - An array of the arguments to the desired constructor
Returns:
Throws:
MutableClassLoader.MutableClassLoaderException

getImplementingClasses

public java.util.List<java.lang.Class<?>> getImplementingClasses(java.lang.Class<?> ancestor,
                                                                 boolean searchAllClasspath)
                                                          throws MutableClassLoader.MutableClassLoaderException
Returns all classes in the current class path (system class path plus any added URLs) which are descendents of or implement the given class / interface.

Parameters:
ancestor - The class or interface to check against.
Returns:
Throws:
MutableClassLoader.MutableClassLoaderException

getImplementingObjects

public java.util.List<?> getImplementingObjects(java.lang.Class<?> c,
                                                java.lang.Object... constructorAguments)
Returns a list of instantiated obejcts, all of which are instances of the given class. The arguments for the constructor used to instantiate the objects are given by constructorArguments. This method is meant as a convenience method, if all the object instances share the same contructor signature.

Parameters:
c -
constructorAguments -
Returns:

getURLs

public java.net.URL[] getURLs()
Returns all the URLs which were initially in the classpath, and which have been added via the addURL() method. To get only the URLs which have been loaded via addURL(), use the getAddedURLs() method instead.

Overrides:
getURLs in class java.net.URLClassLoader
Returns:
All URLs in the classpath, including both those on the system classpath and those which have been added via the addURL() method.

getAddedURLs

public java.net.URL[] getAddedURLs()
Returns all the URLs which have been added via the addURL() method. To get all URLs in the classpath, use the getURLs() method instead.

Returns:
All URLs which have been added via the addURL() method.