com.caucho.loader
Class DynamicClassLoader

java.lang.Object
  extended by java.lang.ClassLoader
      extended by java.security.SecureClassLoader
          extended by java.net.URLClassLoader
              extended by com.caucho.loader.DynamicClassLoader
All Implemented Interfaces:
Make, DynamicClassLoaderMXBean, Dependency
Direct Known Subclasses:
EnvironmentClassLoader, NonScanDynamicClassLoader, ProxyClassLoader, TempDynamicClassLoader

public class DynamicClassLoader
extends java.net.URLClassLoader
implements Dependency, Make, DynamicClassLoaderMXBean

Class loader which checks for changes in class files and automatically picks up new jars.

DynamicClassLoaders can be chained creating one virtual class loader. From the perspective of the JDK, it's all one classloader. Internally, the class loader chain searches like a classpath.


Constructor Summary
DynamicClassLoader(java.lang.ClassLoader parent)
          Create a new class loader.
DynamicClassLoader(java.lang.ClassLoader parent, boolean enableDependencyCheck)
          Create a new class loader.
 
Method Summary
protected  void addCodeBasePath(java.lang.String path)
           
 void addDependency(Dependency dependency)
          Adds a dependency.
 void addJar(Path jar)
          Adds a jar loader.
 void addJarManifestClassPath(Path path)
          Adds jars based on a manifest classpath.
 void addListener(ClassLoaderListener listener)
          Adds a listener to detect class loader changes.
 void addLoader(Loader loader)
          Adds a resource loader to the end of the list.
 void addLoader(Loader loader, int offset)
          Adds a resource loader.
 void addManifestClassPath(java.lang.String classPath, Path pwd)
          Adds jars based on a manifest classpath.
 void addNative(Path path)
          Adds a native path.
 void addParentPriorityPackage(java.lang.String pkg)
          Add to the list of packages that don't use the setServletHack(boolean).
 void addParentPriorityPackages(java.lang.String[] pkg)
          Add to the list of packages that don't use the hack.
 void addPathClass(java.lang.String className, Path path)
          Adds a jar loader.
 void addPermission(java.security.Permission permission)
          Adds a permission to the loader.
 void addPermission(java.lang.String path, java.lang.String actions)
           
 void addPermissions(java.util.ArrayList<java.security.Permission> perms)
           
 void addPriorityPackage(java.lang.String pkg)
          Add to the list of packages that take priority over the parent
 void addRoot(Path root)
          Adds a root loader.
 void addScanRoot()
           
 void addTransformer(java.lang.instrument.ClassFileTransformer transformer)
          Sets any enhancer.
 void addURL(int index, java.net.URL url)
          Adds the URL to the URLClassLoader.
 boolean addURL(Path path)
          Adds the URL to the URLClassLoader.
 void addURL(java.net.URL url)
          Adds the URL to the URLClassLoader.
 void appendToClassPathForInstrumentation(java.lang.String path)
          Adds a class loader for instrumentation (jdk 1.6).
 void buildClassPath(java.util.ArrayList<java.lang.String> cp)
          Fill data for the class path.
protected  void buildImportClassPath(java.util.ArrayList<java.lang.String> sb)
           
protected  void buildResourcePathSpecificFirst(java.util.ArrayList<java.lang.String> pathList)
          Returns the resource path with most specific first.
protected  void buildSourcePath(java.util.ArrayList<java.lang.String> cp)
          Fill data for the class path.
 void clearModified()
          Returns true if any of the classes have been modified.
protected  void configureEnhancerEvent()
          Sends an event to notify than an event has changed.
protected  void configurePostEnhancerEvent()
          Sends an event to notify than an event has changed.
 boolean containsURL(Path path)
          Adds the URL to the URLClassLoader.
protected  boolean containsURL(java.net.URL url)
          Returns true if the loader contains the url.
protected  java.lang.Package definePackage(java.lang.String name, java.lang.String a1, java.lang.String a2, java.lang.String a3, java.lang.String b1, java.lang.String b2, java.lang.String b3, java.net.URL url)
          Defines a new package.
 void destroy()
          Destroys the class loader.
protected  java.lang.Class<?> findClass(java.lang.String name)
          Load a class using this class loader
 java.lang.Class<?> findClassImpl(java.lang.String name)
          Load a class using this class loader
protected  java.lang.Class<?> findImportClass(java.lang.String name)
          Returns any import class, e.g.
 java.lang.String findLibrary(java.lang.String name)
          Returns the full library path for the name.
 Path findPath(java.lang.String name)
          Returns the matching single-level path.
 java.util.Enumeration<java.net.URL> findResources(java.lang.String name)
          Returns an enumeration of matching resources.
protected  ClassEntry getClassEntry(java.lang.String name)
          Returns the matching class entry.
 java.lang.String getClassPath()
          Fill data for the class path.
 long getDependencyCheckInterval()
          Gets the dependency check interval.
static long getGlobalDependencyCheckInterval()
          Returns the global dependency check interval.
 java.lang.String getHash()
           
static java.lang.String getHash(java.lang.ClassLoader loader)
           
 long getHashCrc()
           
 java.lang.String getId()
          Gets the name.
protected  java.net.URL getImportResource(java.lang.String name)
          Get resource from OSGi
 java.lang.ClassLoader getInstrumentableClassLoader()
           
protected  java.util.ArrayList<ClassLoaderListener> getListeners()
          Returns the listeners.
 java.util.ArrayList<Loader> getLoaders()
           
 java.lang.String getLocalClassPath()
          Fill data for the class path.
 java.lang.ClassLoader getNewTempClassLoader()
           
 java.util.ArrayList<java.security.Permission> getPermissions()
           
protected  java.security.PermissionCollection getPermissions(java.security.CodeSource codeSource)
          Returns the permission collection for the given code source.
 java.net.URL getResource(java.lang.String name)
          Gets the named resource
protected  java.lang.String getResourceAlias(java.lang.String name)
           
 java.io.InputStream getResourceAsStream(java.lang.String name)
          Opens a stream to a resource somewhere in the classpath
 java.lang.String getResourcePathSpecificFirst()
          Returns the resource path with most specific first.
 java.util.Enumeration<java.net.URL> getResources(java.lang.String name)
           
 java.lang.String getSourcePath()
          Returns the source path.
 java.lang.ClassLoader getThrowawayClassLoader()
           
protected  java.util.ArrayList<java.lang.instrument.ClassFileTransformer> getTransformerList()
           
 java.net.URL[] getURLs()
          Returns the URLs.
 void init()
          Initialize the class loader.
 boolean isDestroyed()
          Returns true if the class loader is closed.
 boolean isDirectoryLoader()
          Returns true for a class-loader that contains a WEB-INF/classes style directory.
 boolean isJarCacheEnabled()
          Returns true if jar entries should be cached.
static boolean isJarCacheEnabledDefault()
          Returns true if jar entries should be cached.
 boolean isModified()
          Returns true if any of the classes have been modified.
 boolean isModified(boolean enable)
          Returns true if any of the classes have been modified.
static boolean isModified(java.lang.ClassLoader loader)
          Returns true if any of the classes have been modified.
 boolean isModifiedNow()
          Returns true if any of the classes have been modified, forcing a check.
 java.lang.Class<?> loadClass(java.lang.String name)
           
protected  java.lang.Class<?> loadClass(java.lang.String name, boolean resolve)
          Load a class using this class loader
 java.lang.Class<?> loadClass(java.lang.String className, byte[] bytecode)
           
protected  java.lang.Class<?> loadClassEntry(ClassEntry entry)
          Loads the class from the loader.
 java.lang.Class<?> loadClassImpl(java.lang.String name, boolean resolve)
          Load a class using this class loader
 boolean logModified(java.util.logging.Logger log)
          Logs the reason for modification.
 void make()
          Makes any changed classes for the virtual class loader.
 void removeListener(ClassLoaderListener listener)
          Adds a listener to detect class loader changes.
 void removeLoader(Loader loader)
          Removes a loader (this should only be used by generating code, for example EJB to deal with package-private).
protected  void replace(DynamicClassLoader source)
          Copies the loader.
 void resetDependencyCheckInterval()
          Returns true if any of the classes have been modified.
 void scan()
           
protected  void sendAddLoaderEvent()
          Adds a listener to detect class loader changes.
 void setDependencyCheckInterval(long interval)
          Sets the dependency check interval.
 void setEnableDependencyCheck(boolean enable)
          Enables the dependency checking.
static void setGlobalDependencyCheckInterval(long interval)
          Sets the global dependency check interval.
 void setId(java.lang.String id)
          Sets the name.
static void setJarCacheEnabled(boolean isEnabled)
          Returns true if jar entries should be cached.
static void setOldLoader(java.lang.Thread thread, java.lang.ClassLoader oldLoader)
          Sets the old loader if not destroyed.
 void setServletHack(boolean servletHack)
          Set true if the loader should use the servlet spec's hack.
 void stop()
          stops the class loader.
protected static java.lang.String toClassPath(java.util.ArrayList<java.lang.String> list)
           
 java.lang.String toString()
           
 void validate()
          Validates the class loader.
 
Methods inherited from class java.net.URLClassLoader
definePackage, findResource, newInstance, newInstance
 
Methods inherited from class java.security.SecureClassLoader
defineClass, defineClass
 
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, findLoadedClass, findSystemClass, getPackage, getPackages, getParent, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DynamicClassLoader

public DynamicClassLoader(java.lang.ClassLoader parent)
Create a new class loader.

Parameters:
parent - parent class loader

DynamicClassLoader

public DynamicClassLoader(java.lang.ClassLoader parent,
                          boolean enableDependencyCheck)
Create a new class loader.

Parameters:
parent - parent class loader
Method Detail

isJarCacheEnabledDefault

public static boolean isJarCacheEnabledDefault()
Returns true if jar entries should be cached.


isJarCacheEnabled

public boolean isJarCacheEnabled()
Returns true if jar entries should be cached.


setJarCacheEnabled

public static void setJarCacheEnabled(boolean isEnabled)
Returns true if jar entries should be cached.


getGlobalDependencyCheckInterval

public static long getGlobalDependencyCheckInterval()
Returns the global dependency check interval.


setGlobalDependencyCheckInterval

public static void setGlobalDependencyCheckInterval(long interval)
Sets the global dependency check interval.


setId

public void setId(java.lang.String id)
Sets the name.


getId

public java.lang.String getId()
Gets the name.


isDestroyed

public boolean isDestroyed()
Returns true if the class loader is closed.


isDirectoryLoader

public boolean isDirectoryLoader()
Returns true for a class-loader that contains a WEB-INF/classes style directory.


addLoader

public void addLoader(Loader loader)
Adds a resource loader to the end of the list.


removeLoader

public void removeLoader(Loader loader)
Removes a loader (this should only be used by generating code, for example EJB to deal with package-private).


addLoader

public void addLoader(Loader loader,
                      int offset)
Adds a resource loader.


getLoaders

public java.util.ArrayList<Loader> getLoaders()

addJarManifestClassPath

public void addJarManifestClassPath(Path path)
                             throws java.io.IOException
Adds jars based on a manifest classpath.

Throws:
java.io.IOException

addManifestClassPath

public void addManifestClassPath(java.lang.String classPath,
                                 Path pwd)
Adds jars based on a manifest classpath.


addNative

public void addNative(Path path)
Adds a native path.


addJar

public void addJar(Path jar)
Adds a jar loader.


addRoot

public void addRoot(Path root)
Adds a root loader.


addPathClass

public void addPathClass(java.lang.String className,
                         Path path)
Adds a jar loader.


addURL

public boolean addURL(Path path)
Adds the URL to the URLClassLoader.


containsURL

public boolean containsURL(Path path)
Adds the URL to the URLClassLoader.


addURL

public void addURL(java.net.URL url)
Adds the URL to the URLClassLoader.

Overrides:
addURL in class java.net.URLClassLoader

addURL

public void addURL(int index,
                   java.net.URL url)
Adds the URL to the URLClassLoader.


appendToClassPathForInstrumentation

public void appendToClassPathForInstrumentation(java.lang.String path)
Adds a class loader for instrumentation (jdk 1.6).


getURLs

public java.net.URL[] getURLs()
Returns the URLs.

Overrides:
getURLs in class java.net.URLClassLoader

containsURL

protected boolean containsURL(java.net.URL url)
Returns true if the loader contains the url.


setDependencyCheckInterval

public void setDependencyCheckInterval(long interval)
Sets the dependency check interval.


getDependencyCheckInterval

public long getDependencyCheckInterval()
Gets the dependency check interval.


setEnableDependencyCheck

public void setEnableDependencyCheck(boolean enable)
Enables the dependency checking.


addDependency

public void addDependency(Dependency dependency)
Adds a dependency.


addPermission

public void addPermission(java.lang.String path,
                          java.lang.String actions)

addPermission

public void addPermission(java.security.Permission permission)
Adds a permission to the loader.


getPermissions

public java.util.ArrayList<java.security.Permission> getPermissions()

addPermissions

public void addPermissions(java.util.ArrayList<java.security.Permission> perms)

setServletHack

public void setServletHack(boolean servletHack)
Set true if the loader should use the servlet spec's hack.


addListener

public final void addListener(ClassLoaderListener listener)
Adds a listener to detect class loader changes.


removeListener

public final void removeListener(ClassLoaderListener listener)
Adds a listener to detect class loader changes.


getListeners

protected java.util.ArrayList<ClassLoaderListener> getListeners()
Returns the listeners.


sendAddLoaderEvent

protected final void sendAddLoaderEvent()
Adds a listener to detect class loader changes.


configureEnhancerEvent

protected void configureEnhancerEvent()
Sends an event to notify than an event has changed.


configurePostEnhancerEvent

protected void configurePostEnhancerEvent()
Sends an event to notify than an event has changed.


addParentPriorityPackages

public void addParentPriorityPackages(java.lang.String[] pkg)
Add to the list of packages that don't use the hack.


addParentPriorityPackage

public void addParentPriorityPackage(java.lang.String pkg)
Add to the list of packages that don't use the setServletHack(boolean).


addPriorityPackage

public void addPriorityPackage(java.lang.String pkg)
Add to the list of packages that take priority over the parent


getPermissions

protected java.security.PermissionCollection getPermissions(java.security.CodeSource codeSource)
Returns the permission collection for the given code source.

Overrides:
getPermissions in class java.net.URLClassLoader

addCodeBasePath

protected void addCodeBasePath(java.lang.String path)

addTransformer

public void addTransformer(java.lang.instrument.ClassFileTransformer transformer)
Sets any enhancer.


getTransformerList

protected java.util.ArrayList<java.lang.instrument.ClassFileTransformer> getTransformerList()

getHash

public static final java.lang.String getHash(java.lang.ClassLoader loader)

getHash

public java.lang.String getHash()

getHashCrc

public long getHashCrc()

getClassPath

public final java.lang.String getClassPath()
Fill data for the class path. fillClassPath() will add all .jar and .zip files in the directory list.


buildClassPath

public final void buildClassPath(java.util.ArrayList<java.lang.String> cp)
Fill data for the class path. fillClassPath() will add all .jar and .zip files in the directory list.


buildImportClassPath

protected void buildImportClassPath(java.util.ArrayList<java.lang.String> sb)

getLocalClassPath

public final java.lang.String getLocalClassPath()
Fill data for the class path. fillClassPath() will add all .jar and .zip files in the directory list.


getSourcePath

public final java.lang.String getSourcePath()
Returns the source path. The source path is used for looking up resources.


buildSourcePath

protected final void buildSourcePath(java.util.ArrayList<java.lang.String> cp)
Fill data for the class path. fillSourcePath() will add all .jar and .zip files in the directory list.


getResourcePathSpecificFirst

public final java.lang.String getResourcePathSpecificFirst()
Returns the resource path with most specific first.


buildResourcePathSpecificFirst

protected final void buildResourcePathSpecificFirst(java.util.ArrayList<java.lang.String> pathList)
Returns the resource path with most specific first.


toClassPath

protected static java.lang.String toClassPath(java.util.ArrayList<java.lang.String> list)

isModified

public final boolean isModified()
Returns true if any of the classes have been modified.

Specified by:
isModified in interface Dependency

isModified

public final boolean isModified(boolean enable)
Returns true if any of the classes have been modified.


isModifiedNow

public final boolean isModifiedNow()
Returns true if any of the classes have been modified, forcing a check.


logModified

public final boolean logModified(java.util.logging.Logger log)
Logs the reason for modification.

Specified by:
logModified in interface Dependency

resetDependencyCheckInterval

public final void resetDependencyCheckInterval()
Returns true if any of the classes have been modified.


clearModified

public final void clearModified()
Returns true if any of the classes have been modified.


isModified

public static boolean isModified(java.lang.ClassLoader loader)
Returns true if any of the classes have been modified.


make

public final void make()
                throws java.lang.Exception
Makes any changed classes for the virtual class loader.

Specified by:
make in interface Make
Throws:
java.lang.Exception

definePackage

protected java.lang.Package definePackage(java.lang.String name,
                                          java.lang.String a1,
                                          java.lang.String a2,
                                          java.lang.String a3,
                                          java.lang.String b1,
                                          java.lang.String b2,
                                          java.lang.String b3,
                                          java.net.URL url)
Defines a new package.

Overrides:
definePackage in class java.lang.ClassLoader

init

public void init()
Initialize the class loader.


validate

public void validate()
              throws ConfigException
Validates the class loader.

Throws:
ConfigException

addScanRoot

public void addScanRoot()

scan

public void scan()

loadClass

public java.lang.Class<?> loadClass(java.lang.String name)
                             throws java.lang.ClassNotFoundException
Overrides:
loadClass in class java.lang.ClassLoader
Throws:
java.lang.ClassNotFoundException

loadClass

protected java.lang.Class<?> loadClass(java.lang.String name,
                                       boolean resolve)
                                throws java.lang.ClassNotFoundException
Load a class using this class loader

Overrides:
loadClass in class java.lang.ClassLoader
Parameters:
name - the classname to load
resolve - if true, resolve the class
Returns:
the loaded classes
Throws:
java.lang.ClassNotFoundException

loadClassImpl

public java.lang.Class<?> loadClassImpl(java.lang.String name,
                                        boolean resolve)
                                 throws java.lang.ClassNotFoundException
Load a class using this class loader

Parameters:
name - the classname to load
resolve - if true, resolve the class
Returns:
the loaded classes
Throws:
java.lang.ClassNotFoundException

findImportClass

protected java.lang.Class<?> findImportClass(java.lang.String name)
Returns any import class, e.g. from an osgi bundle


findClass

protected java.lang.Class<?> findClass(java.lang.String name)
                                throws java.lang.ClassNotFoundException
Load a class using this class loader

Overrides:
findClass in class java.net.URLClassLoader
Parameters:
name - the classname using either '/' or '.'
Returns:
the loaded class
Throws:
java.lang.ClassNotFoundException

findClassImpl

public java.lang.Class<?> findClassImpl(java.lang.String name)
                                 throws java.lang.ClassNotFoundException
Load a class using this class loader

Parameters:
name - the classname using either '/' or '.'
Returns:
the loaded class
Throws:
java.lang.ClassNotFoundException

getClassEntry

protected ClassEntry getClassEntry(java.lang.String name)
                            throws java.lang.ClassNotFoundException
Returns the matching class entry.

Throws:
java.lang.ClassNotFoundException

loadClassEntry

protected java.lang.Class<?> loadClassEntry(ClassEntry entry)
                                     throws java.io.IOException,
                                            java.lang.ClassNotFoundException
Loads the class from the loader. The loadClass must be in the top classLoader because the defineClass must be owned by the top.

Throws:
java.io.IOException
java.lang.ClassNotFoundException

loadClass

public java.lang.Class<?> loadClass(java.lang.String className,
                                    byte[] bytecode)

getResource

public java.net.URL getResource(java.lang.String name)
Gets the named resource

Overrides:
getResource in class java.lang.ClassLoader
Parameters:
name - name of the resource

getResourceAlias

protected java.lang.String getResourceAlias(java.lang.String name)

getImportResource

protected java.net.URL getImportResource(java.lang.String name)
Get resource from OSGi


getResourceAsStream

public java.io.InputStream getResourceAsStream(java.lang.String name)
Opens a stream to a resource somewhere in the classpath

Overrides:
getResourceAsStream in class java.lang.ClassLoader
Parameters:
name - the path to the resource
Returns:
an input stream to the resource

getResources

public java.util.Enumeration<java.net.URL> getResources(java.lang.String name)
                                                 throws java.io.IOException
Overrides:
getResources in class java.lang.ClassLoader
Throws:
java.io.IOException

findResources

public java.util.Enumeration<java.net.URL> findResources(java.lang.String name)
Returns an enumeration of matching resources.

Overrides:
findResources in class java.net.URLClassLoader

findLibrary

public java.lang.String findLibrary(java.lang.String name)
Returns the full library path for the name.

Overrides:
findLibrary in class java.lang.ClassLoader

findPath

public Path findPath(java.lang.String name)
Returns the matching single-level path.


stop

public void stop()
stops the class loader.


destroy

public void destroy()
Destroys the class loader.


setOldLoader

public static void setOldLoader(java.lang.Thread thread,
                                java.lang.ClassLoader oldLoader)
Sets the old loader if not destroyed.


getInstrumentableClassLoader

public java.lang.ClassLoader getInstrumentableClassLoader()

getThrowawayClassLoader

public java.lang.ClassLoader getThrowawayClassLoader()

getNewTempClassLoader

public java.lang.ClassLoader getNewTempClassLoader()

replace

protected void replace(DynamicClassLoader source)
Copies the loader.


toString

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