org.allcolor.yahp.converter
Class CClassLoader

java.lang.Object
  extended by java.lang.ClassLoader
      extended by java.security.SecureClassLoader
          extended by java.net.URLClassLoader
              extended by org.allcolor.yahp.converter.CClassLoader

public final class CClassLoader
extends java.net.URLClassLoader

This is a custom tree classloader

Version:
1.0
Author:
Quentin Anciaux

Field Summary
private  boolean booAlone
          is alone ?
private  boolean booDoNotForwardToParent
          is not forwarding to parent ?
private  boolean booInit
          is initializing ?
private  boolean booMandatory
          is mandatory ?
private  boolean booResourceOnly
          is resource only ?
private  java.util.Map cacheMap
           
static java.lang.String CCLASSLOADER_NAMESPACE
          classloader namespace
private  java.util.Map childrenMap
          contains reference to children loaders
private  java.util.Map classesMap
          known classes by this loader
private  CClassLoaderConfig config
          reference to the loader config object
private static java.lang.ThreadLocal contextLoader
           
private static int DEBUG
          DEBUG log level
private  java.util.Map dllMap
          Contains a name-URL of dll/.so mapping for this loader
private static int FATAL
          FATAL log level
private  java.lang.String finalizepath
           
private static int INFO
          INFO log level
private static java.util.logging.Logger log
          use for logging
private static java.util.Map mandatoryLoadersMap
          contains all mandatory loaders
private  java.lang.String name
          loader name
private  java.lang.String path
          loader path
private  java.util.Map resourcesMap
          known resources by this loader
static java.lang.String ROOT_LOADER
          name of the rootloader
private static CClassLoader rootLoader
          handle to the root loader
private static java.net.URLClassLoader urlLoader
          Ser the URLClassLoader that will return to calls to getURLs()
 
Constructor Summary
private CClassLoader(java.lang.ClassLoader parent, java.lang.String name)
          Create a new loader
 
Method Summary
private  void _destroy(java.lang.reflect.Method logFactoryRelease)
          Destroy instance variables.
 void addClass(java.lang.String className, java.net.URL urlToClass)
          add a class to known class
 void addResource(java.lang.String resouceName, java.net.URL urlToResource)
          add a resource
private static void clearArray(java.lang.Object[] array)
           
private static void clearList(java.util.List list)
           
private static void clearMap(java.util.Map map)
           
private static void clearSet(java.util.Set list)
           
private static CClassLoader createLoader(java.lang.ClassLoader parent, java.lang.String name)
          Create a new loader
static java.net.URL createMemoryURL(java.lang.String entryName, byte[] entry)
          Creates and allocates a memory URL
static void destroy()
          destroy the loader tree
protected  void finalize()
           
protected  java.lang.Class findClass(java.lang.String name)
           
protected  java.lang.String findLibrary(java.lang.String libname)
           
 java.net.URL findResource(java.lang.String fname)
           
 java.util.Enumeration findResources(java.lang.String name)
           
 java.util.Iterator getChildLoader()
          return an iterator on children loaders
private static java.util.List getClasses(java.io.File current, java.util.List list)
          Return a list of classes and jar files
 java.util.Map getClassesMap()
          return a copy of the known classes
static java.lang.ClassLoader getContextLoader()
           
static CClassLoader getLoader(java.lang.String fpath)
          return the loader with the given path
private  CClassLoader getLoaderByName(java.lang.String name)
          Return the child loader with the given name
 java.lang.String getName()
          Returns the name.
 java.lang.String getPath()
          Returns the path.
private  java.util.List getPrivateResource(java.lang.String name)
          get the resource with the given name
 java.net.URL getResource(java.lang.String name)
           
 java.io.InputStream getResourceAsStream(java.lang.String name)
           
 java.util.Map getResourcesMap()
          return a copy of the known resources
static CClassLoader getRootLoader()
          return the rootloader
private static void getThreadGroups(java.lang.ThreadGroup tg, java.util.List ltg)
           
 java.net.URL[] getURLs()
           
static java.net.URL[] getURLs(java.lang.String path)
          Return a list of jar and classes located in path
static void init(CClassLoaderConfig config)
          initialize the loaders hierarchy
private static void installURLStreamHandlerFactory()
          Install a custom URLStreamHandlerFactory which handle nested jar loading, and memory url.
 boolean isAlone()
          Returns the booAlone.
 boolean isForwardingToParent()
          return booDoNotForwardToParent
 boolean isInit()
          Returns the booInit.
 boolean isMandatory()
          Returns the booMandatory.
 boolean isResourceOnly()
          Returns the booResourceOnly.
static byte[] loadByteArray(java.io.InputStream in)
          load the given inputstream in a byte array
static byte[] loadByteArray(java.net.URL urlToResource)
          load the given url in a byte array
protected  java.lang.Class loadClass(java.lang.String name, boolean resolve)
           
private static void loadResources(CClassLoader loader, java.util.List URLList, java.lang.String resourceName)
          load multiple resources of same name
private static void log(java.lang.String Message, int level)
          log the message
private  java.lang.String nGetLoaderPath()
          calculate the loader path
private  void readDirectories(java.net.URL jarFile)
          analyse the content of the given jar file
static void releaseMemoryURL(java.net.URL u)
          Release a previously allocated memory URL
 void reload()
          reload this loader
 void reload(CClassLoaderConfig config)
          reload this loader
 CClassLoader removeLoader(java.lang.String loaderToRemove)
          remove the given loader from this loader
 void removeResource(java.lang.String resourceName)
          remove the given resource name from known resource
static void setContextLoader(java.lang.ClassLoader contextLoader)
           
private static void setInit(CClassLoader loader)
          set the init flag of all loaders
static void setURLClassLoader(java.net.URLClassLoader urlLoader)
          Set the urlloader used to resolved getURLs
private static boolean sl(int level)
          test if the logging level is enabled
 java.lang.String toString()
           
 
Methods inherited from class java.net.URLClassLoader
addURL, definePackage, 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, 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, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

CCLASSLOADER_NAMESPACE

public static final java.lang.String CCLASSLOADER_NAMESPACE
classloader namespace

See Also:
Constant Field Values

contextLoader

private static java.lang.ThreadLocal contextLoader

DEBUG

private static final int DEBUG
DEBUG log level

See Also:
Constant Field Values

FATAL

private static final int FATAL
FATAL log level

See Also:
Constant Field Values

INFO

private static final int INFO
INFO log level

See Also:
Constant Field Values

log

private static final java.util.logging.Logger log
use for logging


mandatoryLoadersMap

private static final java.util.Map mandatoryLoadersMap
contains all mandatory loaders


ROOT_LOADER

public static final java.lang.String ROOT_LOADER
name of the rootloader

See Also:
Constant Field Values

rootLoader

private static CClassLoader rootLoader
handle to the root loader


urlLoader

private static volatile java.net.URLClassLoader urlLoader
Ser the URLClassLoader that will return to calls to getURLs()


booAlone

private boolean booAlone
is alone ?


booDoNotForwardToParent

private boolean booDoNotForwardToParent
is not forwarding to parent ?


booInit

private boolean booInit
is initializing ?


booMandatory

private boolean booMandatory
is mandatory ?


booResourceOnly

private boolean booResourceOnly
is resource only ?


cacheMap

private final java.util.Map cacheMap

childrenMap

private final java.util.Map childrenMap
contains reference to children loaders


classesMap

private final java.util.Map classesMap
known classes by this loader


config

private CClassLoaderConfig config
reference to the loader config object


dllMap

private final java.util.Map dllMap
Contains a name-URL of dll/.so mapping for this loader


name

private java.lang.String name
loader name


path

private java.lang.String path
loader path


finalizepath

private java.lang.String finalizepath

resourcesMap

private final java.util.Map resourcesMap
known resources by this loader

Constructor Detail

CClassLoader

private CClassLoader(java.lang.ClassLoader parent,
                     java.lang.String name)
              throws java.lang.Exception
Create a new loader

Parameters:
parent - a reference to the parent loader
name - loader name
Throws:
java.lang.Exception - should not happen
Method Detail

finalize

protected void finalize()
                 throws java.lang.Throwable
Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable

createLoader

private static final CClassLoader createLoader(java.lang.ClassLoader parent,
                                               java.lang.String name)
Create a new loader

Parameters:
parent - a reference to the parent loader
name - loader name
Returns:
a new loader

createMemoryURL

public static java.net.URL createMemoryURL(java.lang.String entryName,
                                           byte[] entry)
Creates and allocates a memory URL

Parameters:
entryName - name of the entry
entry - byte array of the entry
Returns:
the created URL or null if an error occurs.

clearMap

private static final void clearMap(java.util.Map map)

clearArray

private static final void clearArray(java.lang.Object[] array)

clearList

private static final void clearList(java.util.List list)

clearSet

private static final void clearSet(java.util.Set list)

destroy

public static final void destroy()
destroy the loader tree


getClasses

private static java.util.List getClasses(java.io.File current,
                                         java.util.List list)
Return a list of classes and jar files

Parameters:
current - currently inspected file
list - list of url to classes and jar files
Returns:
list of url to classes and jar files

getContextLoader

public static final java.lang.ClassLoader getContextLoader()

getLoader

public static final CClassLoader getLoader(java.lang.String fpath)
return the loader with the given path

Parameters:
fpath - the path to lookup
Returns:
the loader with the given path

getRootLoader

public static final CClassLoader getRootLoader()
return the rootloader

Returns:
the rootloader

getThreadGroups

private static void getThreadGroups(java.lang.ThreadGroup tg,
                                    java.util.List ltg)

getURLs

public static java.net.URL[] getURLs(java.lang.String path)
Return a list of jar and classes located in path

Parameters:
path - the path in which to search
Returns:
a list of jar and classes located in path

init

public static final void init(CClassLoaderConfig config)
initialize the loaders hierarchy

Parameters:
config - the loaders config object

installURLStreamHandlerFactory

private static void installURLStreamHandlerFactory()
Install a custom URLStreamHandlerFactory which handle nested jar loading, and memory url. The installation is done only if necessary.


loadByteArray

public static final byte[] loadByteArray(java.io.InputStream in)
load the given inputstream in a byte array

Parameters:
in - the stream to load
Returns:
a byte array

loadByteArray

public static final byte[] loadByteArray(java.net.URL urlToResource)
load the given url in a byte array

Parameters:
urlToResource - url to load
Returns:
a byte array

loadResources

private static final void loadResources(CClassLoader loader,
                                        java.util.List URLList,
                                        java.lang.String resourceName)
load multiple resources of same name

Parameters:
loader - current lookup loader
URLList - list of found resources
resourceName - name to match

log

private static final void log(java.lang.String Message,
                              int level)
log the message

Parameters:
Message - message to log
level - log level (INFO,DEBUG,FATAL)

releaseMemoryURL

public static void releaseMemoryURL(java.net.URL u)
Release a previously allocated memory URL

Parameters:
u - the URL to release memory

setContextLoader

public static final void setContextLoader(java.lang.ClassLoader contextLoader)

setInit

private static final void setInit(CClassLoader loader)
set the init flag of all loaders

Parameters:
loader - the current loader

setURLClassLoader

public static void setURLClassLoader(java.net.URLClassLoader urlLoader)
Set the urlloader used to resolved getURLs

Parameters:
urlLoader - the urlloader used to resolved getURLs

sl

private static final boolean sl(int level)
test if the logging level is enabled

Parameters:
level - the logging level to test
Returns:
true if enabled

_destroy

private final void _destroy(java.lang.reflect.Method logFactoryRelease)
Destroy instance variables.

Parameters:
logFactoryRelease - method to release commons logging

addClass

public final void addClass(java.lang.String className,
                           java.net.URL urlToClass)
add a class to known class

Parameters:
className - class name
urlToClass - url to class file

addResource

public final void addResource(java.lang.String resouceName,
                              java.net.URL urlToResource)
add a resource

Parameters:
resouceName - name of the resource to add
urlToResource - url to the resource

findClass

protected final java.lang.Class findClass(java.lang.String name)
                                   throws java.lang.ClassNotFoundException
Overrides:
findClass in class java.net.URLClassLoader
Throws:
java.lang.ClassNotFoundException

findLibrary

protected java.lang.String findLibrary(java.lang.String libname)
Overrides:
findLibrary in class java.lang.ClassLoader

findResource

public final java.net.URL findResource(java.lang.String fname)
Overrides:
findResource in class java.net.URLClassLoader

findResources

public final java.util.Enumeration findResources(java.lang.String name)
                                          throws java.io.IOException
Overrides:
findResources in class java.net.URLClassLoader
Throws:
java.io.IOException

getChildLoader

public final java.util.Iterator getChildLoader()
return an iterator on children loaders

Returns:
an iterator on children loaders

getClassesMap

public final java.util.Map getClassesMap()
return a copy of the known classes

Returns:
a copy of the known classes

getLoaderByName

private final CClassLoader getLoaderByName(java.lang.String name)
Return the child loader with the given name

Parameters:
name - name of the child to get
Returns:
the child loader with the given name

getName

public final java.lang.String getName()
Returns the name.

Returns:
Returns the name.

getPath

public final java.lang.String getPath()
Returns the path.

Returns:
Returns the path.

getPrivateResource

private final java.util.List getPrivateResource(java.lang.String name)
get the resource with the given name

Parameters:
name - name of the resource to get
Returns:
the resource with the given name

getResource

public final java.net.URL getResource(java.lang.String name)
Overrides:
getResource in class java.lang.ClassLoader

getResourceAsStream

public final java.io.InputStream getResourceAsStream(java.lang.String name)
Overrides:
getResourceAsStream in class java.lang.ClassLoader

getResourcesMap

public final java.util.Map getResourcesMap()
return a copy of the known resources

Returns:
a copy of the known resources

getURLs

public java.net.URL[] getURLs()
Overrides:
getURLs in class java.net.URLClassLoader

isAlone

public final boolean isAlone()
Returns the booAlone.

Returns:
Returns the booAlone.

isForwardingToParent

public final boolean isForwardingToParent()
return booDoNotForwardToParent

Returns:
booDoNotForwardToParent

isInit

public final boolean isInit()
Returns the booInit.

Returns:
Returns the booInit.

isMandatory

public final boolean isMandatory()
Returns the booMandatory.

Returns:
Returns the booMandatory.

isResourceOnly

public final boolean isResourceOnly()
Returns the booResourceOnly.

Returns:
Returns the booResourceOnly.

loadClass

protected final java.lang.Class loadClass(java.lang.String name,
                                          boolean resolve)
                                   throws java.lang.ClassNotFoundException
Overrides:
loadClass in class java.lang.ClassLoader
Throws:
java.lang.ClassNotFoundException

nGetLoaderPath

private final java.lang.String nGetLoaderPath()
calculate the loader path

Returns:
the calculated path

readDirectories

private final void readDirectories(java.net.URL jarFile)
analyse the content of the given jar file

Parameters:
jarFile - the jar to analise

reload

public final void reload()
reload this loader


reload

public final void reload(CClassLoaderConfig config)
reload this loader

Parameters:
config - a loader config object

removeLoader

public final CClassLoader removeLoader(java.lang.String loaderToRemove)
remove the given loader from this loader

Parameters:
loaderToRemove - name of the loader to remove. NOT NULL.
Returns:
the removed loader or null.
Throws:
java.lang.NullPointerException - if loaderToRemove is null or a zero length/blank string

removeResource

public final void removeResource(java.lang.String resourceName)
remove the given resource name from known resource

Parameters:
resourceName - name of the resource to remove

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object