edu.emory.mathcs.util.classloader
Class GenericClassLoader

java.lang.Object
  extended byjava.lang.ClassLoader
      extended byjava.security.SecureClassLoader
          extended byedu.emory.mathcs.util.classloader.GenericClassLoader

public class GenericClassLoader
extends java.security.SecureClassLoader

This class loader can be used to find class, resource and library handles as well as load classes, resources and libraries using abstract ResourceFinder entity encapsulating the searching approach. Resource handles allow accessing meta-information (like Attributes, Certificates etc.) related to classes, resources and libraries prior to loading them.

GenericClassLoader is intended to be used as a base for custom class loaders. In most applications, GenericClassLoader can be used directly -- the application-specific functionality of resource searching can often be completely delegated to the resource finder. See URIClassLoader for a concrete implementation using a simple resource finder.

Version:
1.0
Author:
Dawid Kurzyniec
See Also:
ResourceFinder, ResourceLoader, ResourceHandle

Field Summary
protected  ResourceFinder finder
           
 
Constructor Summary
GenericClassLoader(ResourceFinder finder)
          Creates new GenericClassLoader instance using specified ResourceFinder to find resources and with default parent class loader.
GenericClassLoader(ResourceFinder finder, java.lang.ClassLoader parent)
          Creates new GenericClassLoader instance using specified ResourceFinder to find resources and having specified parent class loader.
 
Method Summary
protected  java.lang.Class defineClass(java.lang.String name, ResourceHandle h)
           
protected  java.lang.Package definePackage(java.lang.String name, java.util.jar.Manifest man, java.net.URL url)
          Defines a new package by name in this ClassLoader.
protected  java.lang.Class findClass(java.lang.String name)
          Finds and loads the class with the specified name.
protected  java.lang.String findLibrary(java.lang.String libname)
          Returns the absolute path name of a native library.
protected  java.net.URL findResource(java.lang.String name)
          Finds the resource with the specified name.
protected  java.util.Enumeration findResources(java.lang.String name)
          Returns an Enumeration of URLs representing all of the resources having the specified name.
protected  ResourceHandle getClassHandle(java.lang.String name)
          Finds the ResourceHandle object for the class with the specified name.
static java.net.URLStreamHandler getDefaultURLStreamHandler(java.lang.String protocol)
           
protected  ResourceHandle getLibraryHandle(java.lang.String name)
          Finds the ResourceHandle object for the native library with the specified name.
protected  ResourceHandle getResourceHandle(java.lang.String name)
          Finds the ResourceHandle object for the resource with the specified name.
protected  java.util.Enumeration getResourceHandles(java.lang.String name)
          Returns an Enumeration of ResourceHandle objects representing all of the resources having the specified name.
 
Methods inherited from class java.security.SecureClassLoader
defineClass, getPermissions
 
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, definePackage, 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
 

Field Detail

finder

protected ResourceFinder finder
Constructor Detail

GenericClassLoader

public GenericClassLoader(ResourceFinder finder,
                          java.lang.ClassLoader parent)
Creates new GenericClassLoader instance using specified ResourceFinder to find resources and having specified parent class loader.


GenericClassLoader

public GenericClassLoader(ResourceFinder finder)
Creates new GenericClassLoader instance using specified ResourceFinder to find resources and with default parent class loader.

Method Detail

findClass

protected java.lang.Class findClass(java.lang.String name)
                             throws java.lang.ClassNotFoundException
Finds and loads the class with the specified name.

Parameters:
name - the name of the class
Returns:
the resulting class
Throws:
java.lang.ClassNotFoundException - if the class could not be found

defineClass

protected java.lang.Class defineClass(java.lang.String name,
                                      ResourceHandle h)
                               throws java.io.IOException
Throws:
java.io.IOException

findResource

protected java.net.URL findResource(java.lang.String name)
Finds the resource with the specified name.

Parameters:
name - the name of the resource
Returns:
a URL for the resource, or null if the resource could not be found.

findResources

protected java.util.Enumeration findResources(java.lang.String name)
                                       throws java.io.IOException
Returns an Enumeration of URLs representing all of the resources having the specified name.

Parameters:
name - the resource name
Returns:
an Enumeration of URLs
Throws:
java.io.IOException - if an I/O exception occurs

findLibrary

protected java.lang.String findLibrary(java.lang.String libname)
Returns the absolute path name of a native library. The VM invokes this method to locate the native libraries that belong to classes loaded with this class loader. If this method returns null, the VM searches the library along the path specified as the java.library.path property. This method invoke getLibraryHandle(java.lang.String) method to find handle of this library. If the handle is found and its URL protocol is "file", the system-dependent absolute library file path is returned. Otherwise this method returns null.

Subclasses can override this method to provide specific approaches in library searching.

Parameters:
libname - the library name
Returns:
the absolute path of the native library
See Also:
System.loadLibrary(java.lang.String), System.mapLibraryName(java.lang.String)

getClassHandle

protected ResourceHandle getClassHandle(java.lang.String name)
Finds the ResourceHandle object for the class with the specified name. Unlike findClass(), this method does not load the class.

Parameters:
name - the name of the class
Returns:
the ResourceHandle of the class

getResourceHandle

protected ResourceHandle getResourceHandle(java.lang.String name)
Finds the ResourceHandle object for the resource with the specified name.

Parameters:
name - the name of the resource
Returns:
the ResourceHandle of the resource

getLibraryHandle

protected ResourceHandle getLibraryHandle(java.lang.String name)
Finds the ResourceHandle object for the native library with the specified name. The library name must be '/'-separated path. The last part of this path is substituted by its system-dependent mapping (using System.mapLibraryName(String) method). Next, the ResourceFinder is used to look for the library as it was ordinary resource.

Subclasses can override this method to provide specific approaches in library searching.

Parameters:
name - the name of the library
Returns:
the ResourceHandle of the library

getResourceHandles

protected java.util.Enumeration getResourceHandles(java.lang.String name)
Returns an Enumeration of ResourceHandle objects representing all of the resources having the specified name.

Parameters:
name - the name of the resource
Returns:
the ResourceHandle of the resource

definePackage

protected java.lang.Package definePackage(java.lang.String name,
                                          java.util.jar.Manifest man,
                                          java.net.URL url)
                                   throws java.lang.IllegalArgumentException
Defines a new package by name in this ClassLoader. The attributes contained in the specified Manifest will be used to obtain package version and sealing information. For sealed packages, the additional URL specifies the code source URL from which the package was loaded.

Parameters:
name - the package name
man - the Manifest containing package version and sealing information
url - the code source url for the package, or null if none
Returns:
the newly defined Package object
Throws:
java.lang.IllegalArgumentException - if the package name duplicates an existing package either in this class loader or one of its ancestors

getDefaultURLStreamHandler

public static java.net.URLStreamHandler getDefaultURLStreamHandler(java.lang.String protocol)