org.openbp.common.classloader
Class XClassLoaderBase

java.lang.Object
  extended by java.lang.ClassLoader
      extended by java.security.SecureClassLoader
          extended by java.net.URLClassLoader
              extended by org.openbp.common.classloader.XClassLoaderBase
Direct Known Subclasses:
XClassLoader, XClassLoaderSystemErr

public abstract class XClassLoaderBase
extends java.net.URLClassLoader

This class implements a sophisticated classloader that supports loading from classes from various repositories (which might be \.jar, \.zip-files or directories) and a caching mechanism for loaded classes. To avoid multi-threading problems, this class loader is immutable after having been constructed. This means, all configuration data necessary is passed as argument to the constructor, and changing the configuration object (see XClassLoaderConfiguration) after being passed to the constructor doesn't influence the class loader any more (which is achieved by cloning the configuration). Consequentially, the configuration can and should be reused. In order to support a broad field of applications for this class, it is kept free of specific logging code, instead, own logging levels together with an abstract logging method are used to allow easy integration of the appropriate logging mechanism. Implementation note: Actually, the class loader wouldn't have to extend URLClassLoader. This is done only to make the Tomcat Jasper JSP engine happy, which hard-wiredly expects an URLClassLoader.

Author:
Heiko Erhardt

Field Summary
protected  XClassLoaderConfiguration configuration
          A clone of the configuration passed during construction of the class loader.
static boolean DEBUG_LOAD_TIME
          Class loading time debug flag
 
Constructor Summary
protected XClassLoaderBase()
          Protected constructor.
  XClassLoaderBase(XClassLoaderConfiguration configuration)
          Constructor.
 
Method Summary
protected  void addClassToCache(java.lang.String className, java.lang.Class cls)
          Adds a class to the class cache.
 void addResourceEntry(java.lang.String key, ResourceEntry entry)
          Adds a resource entry to the class loader.
protected  void addResourceEntryForClass(java.lang.String resourceName, ResourceEntry entry)
          Adds the given resource entry as class mapping of the entry specifies a class file.
 java.lang.Class createClass(java.lang.String className, byte[] data, boolean resolve)
          Creates a class object from its binary data.
 XClassLoaderConfiguration getConfiguration()
          Gets the a clone of the configuration passed during construction of the class loader..
 java.lang.ClassLoader getParentClassLoader()
          This returns the parent class loader of this class loader
 java.net.URL getResource(java.lang.String name)
          Determines the URL of a resource with a given name.
 java.io.InputStream getResourceAsStream(java.lang.String name)
          Finds a resource with a given name.
 ResourceEntry getResourceEntry(java.lang.String key)
          Gets a resource entry by its key.
protected  java.net.URL getResourceFromPath(java.lang.String resourceName)
          Loads a resource from the class path.
protected  void init(XClassLoaderConfiguration configuration)
          This method initializes the class loader with the configuration passed.
protected abstract  boolean isLogEnabled(java.lang.String logLevel)
          Checks if the specified log level is enabled.
 java.lang.Class loadClass(java.lang.String className, boolean resolve)
          Overrides the loadClass method from the standard java class loader.
protected  java.lang.Class loadClassFromCache(java.lang.String className)
          Tries to load the class from the cache.
protected  java.lang.Class loadClassFromPath(java.lang.String className, boolean resolve)
          Loads a class from a directory or zip file in the class path of the custom class loader.
protected  java.io.InputStream loadResourceFromPath(java.lang.String resourceName)
          Loads a resource from the class path.
protected  void log(java.lang.String logLevel, java.lang.String msg)
          A convenience method that allows logging without an exception.
protected  void log(java.lang.String logLevel, java.lang.String msg, java.lang.Exception e)
          Logs a message.
protected  void log(java.lang.String logLevel, java.lang.String msg, java.lang.String arg)
          Logs a message.
protected  void log(java.lang.String logLevel, java.lang.String msg, java.lang.String arg, java.lang.Exception e)
          Logs a message.
protected  void log(java.lang.String logLevel, java.lang.String msg, java.lang.String arg, java.lang.String arg2)
          Logs a message.
static byte[] readContent(java.io.File file)
          Reads the resource from the repository.
 void rescan()
          Clears all information about classes in the path and rescans the specified class path.
protected  void scanRepositories()
          This method scans all repositories in the configuration for classes.
protected abstract  void setupLogger()
          Initializes the logger.
 java.lang.String toString()
          Overridden toString method.
protected abstract  void writeLog(java.lang.String logLevel, java.lang.String msg, java.lang.Exception e)
          Writes a message to the log.
 
Methods inherited from class java.net.URLClassLoader
addURL, definePackage, findClass, findResource, findResources, getPermissions, getURLs, 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, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEBUG_LOAD_TIME

public static boolean DEBUG_LOAD_TIME
Class loading time debug flag


configuration

protected XClassLoaderConfiguration configuration
A clone of the configuration passed during construction of the class loader.

Constructor Detail

XClassLoaderBase

public XClassLoaderBase(XClassLoaderConfiguration configuration)
                 throws java.lang.Exception
Constructor.

Parameters:
configuration - The configuration to be used for this class loader. Must not be null.
Throws:
java.lang.Exception - On any error that occurs while scanning the repositories specified in the class loader configuration

XClassLoaderBase

protected XClassLoaderBase()
Protected constructor. Should only be used by subclasses that need to take essential actions before starting with the configuration processing. If this constructor was used, it is absolute necessary to call init(org.openbp.common.classloader.XClassLoaderConfiguration)!

Method Detail

init

protected void init(XClassLoaderConfiguration configuration)
             throws java.lang.Exception
This method initializes the class loader with the configuration passed.

Parameters:
configuration - The configuration to be used for this class loader. Must not be null.
Throws:
java.lang.Exception - On any error that occurs while scanning the repositories specified in the class loader configuration

rescan

public void rescan()
            throws java.lang.Exception
Clears all information about classes in the path and rescans the specified class path. This method should be called if a class file was added to a directory of the class path programatically (e. g. by an invoked compilation or byte code construction).

Throws:
java.lang.Exception - On any error that occurs while scanning the repositories specified in the class loader configuration

getParentClassLoader

public java.lang.ClassLoader getParentClassLoader()
This returns the parent class loader of this class loader

Returns:
The parent class loader or null if no explicit parent class loader has been specified when creating this class loader

getConfiguration

public XClassLoaderConfiguration getConfiguration()
Gets the a clone of the configuration passed during construction of the class loader..


getResourceEntry

public ResourceEntry getResourceEntry(java.lang.String key)
Gets a resource entry by its key.

Parameters:
key - Key
Returns:
The entry or null if no such entry exists

addResourceEntry

public void addResourceEntry(java.lang.String key,
                             ResourceEntry entry)
Adds a resource entry to the class loader.

Parameters:
key - Key of the entry (e. g. a fully qualified class name)
entry - Entry to add

addResourceEntryForClass

protected void addResourceEntryForClass(java.lang.String resourceName,
                                        ResourceEntry entry)
Adds the given resource entry as class mapping of the entry specifies a class file. Does nothing if the entry does not denote a class file.

Parameters:
resourceName - Resource name (relative path name or zip entry name)
entry - Entry to add

loadClass

public java.lang.Class loadClass(java.lang.String className,
                                 boolean resolve)
                          throws java.lang.ClassNotFoundException
Overrides the loadClass method from the standard java class loader.

Overrides:
loadClass in class java.lang.ClassLoader
Parameters:
className - Name of the class to be loaded
resolve - If true, all dependents are loaded too
Returns:
The class object
Throws:
java.lang.ClassNotFoundException - If the class was not found

getResource

public java.net.URL getResource(java.lang.String name)
Determines the URL of a resource with a given name. This method returns null if no resource with this name is found.

Overrides:
getResource in class java.lang.ClassLoader
Parameters:
name - Name of the desired resource
Returns:
The URL or null

getResourceAsStream

public java.io.InputStream getResourceAsStream(java.lang.String name)
Finds a resource with a given name. This method returns null if no resource with this name is found.

Overrides:
getResourceAsStream in class java.lang.ClassLoader
Parameters:
name - Name of the desired resource
Returns:
An input stream to the resource or null

loadClassFromPath

protected java.lang.Class loadClassFromPath(java.lang.String className,
                                            boolean resolve)
Loads a class from a directory or zip file in the class path of the custom class loader.

Parameters:
className - Fully qualified name of the class to be loaded
resolve - If true, classes referenced by the class should be loaded, too
Returns:
The loaded class object or null if the class could not be loaded

createClass

public java.lang.Class createClass(java.lang.String className,
                                   byte[] data,
                                   boolean resolve)
Creates a class object from its binary data.

Parameters:
className - Name of the class
data - Class data (from file)
resolve - If true, all dependents are loaded too
Returns:
The class object

loadResourceFromPath

protected java.io.InputStream loadResourceFromPath(java.lang.String resourceName)
Loads a resource from the class path.

Parameters:
resourceName - Name of the resource file
Returns:
An input stream to the resource or null if the resource was not found

getResourceFromPath

protected java.net.URL getResourceFromPath(java.lang.String resourceName)
Loads a resource from the class path.

Parameters:
resourceName - Name of the resource file
Returns:
A URL of the resource or null if the resource could not be found

addClassToCache

protected void addClassToCache(java.lang.String className,
                               java.lang.Class cls)
Adds a class to the class cache.

Parameters:
className - Fully qualified class name
cls - Class object

loadClassFromCache

protected java.lang.Class loadClassFromCache(java.lang.String className)
Tries to load the class from the cache.

Parameters:
className - Fully qualified name of the class
Returns:
The class if it is in the cache or null

scanRepositories

protected void scanRepositories()
                         throws java.lang.Exception
This method scans all repositories in the configuration for classes.

Throws:
java.lang.Exception - On any error that occurs while scanning the repositories specified in the class loader configuration

readContent

public static byte[] readContent(java.io.File file)
                          throws java.lang.Exception
Reads the resource from the repository.

Parameters:
file - File to read
Returns:
Bytes that define the content of the entry (i. e. the class code)
Throws:
java.lang.Exception - On any error that occurs while scanning the repositories specified in the class loader configuration

log

protected void log(java.lang.String logLevel,
                   java.lang.String msg,
                   java.lang.Exception e)
Logs a message.

Parameters:
logLevel - Log level as defined by LogLevel
msg - The message to be logged
e - An exception that should be logged together with the message (might be null)

log

protected void log(java.lang.String logLevel,
                   java.lang.String msg,
                   java.lang.String arg,
                   java.lang.Exception e)
Logs a message.

Parameters:
logLevel - Log level as defined by LogLevel
msg - The message to be logged
arg - message argument
e - An exception that should be logged together with the message (might be null)

log

protected void log(java.lang.String logLevel,
                   java.lang.String msg,
                   java.lang.String arg)
Logs a message.

Parameters:
logLevel - Log level as defined by LogLevel
msg - The message to be logged
arg - message argument

log

protected void log(java.lang.String logLevel,
                   java.lang.String msg,
                   java.lang.String arg,
                   java.lang.String arg2)
Logs a message.

Parameters:
logLevel - Log level as defined by LogLevel
msg - The message to be logged
arg - message argument
arg2 - message argument

log

protected void log(java.lang.String logLevel,
                   java.lang.String msg)
A convenience method that allows logging without an exception.

Parameters:
logLevel - Log level as defined by LogLevel
msg - The message to be logged

setupLogger

protected abstract void setupLogger()
Initializes the logger. This will be called before any log output is being written.


writeLog

protected abstract void writeLog(java.lang.String logLevel,
                                 java.lang.String msg,
                                 java.lang.Exception e)
Writes a message to the log. This abstract method has to be implemented by sub classes of this class to implement support of a custom log mechanism.

Parameters:
logLevel - Log level as defined by LogLevel
msg - Message to be logged
e - An exception that should be logged together with the message (might be null)

isLogEnabled

protected abstract boolean isLogEnabled(java.lang.String logLevel)
Checks if the specified log level is enabled. This abstract method has to be implemented by sub classes of this class to implement support of a custom log mechanism.

Parameters:
logLevel - Log level as defined by LogLevel
Returns:
true The log level is enabled. false The log level is disabled.

toString

public java.lang.String toString()
Overridden toString method.

Overrides:
toString in class java.lang.Object


Copyright © 2011. All Rights Reserved.