public abstract class WebappClassLoaderBase extends java.net.URLClassLoader implements Lifecycle, InstrumentableClassLoader
This class loader is a full reimplementation of the
URLClassLoader
from the JDK. It is designed to be fully
compatible with a normal URLClassLoader
, although its internal
behavior may be completely different.
IMPLEMENTATION NOTE - By default, this class loader follows
the delegation model required by the specification. The system class
loader will be queried first, then the local repositories, and only then
delegation to the parent class loader will occur. This allows the web
application to override any shared class except the classes from J2SE.
Special handling is provided from the JAXP XML parser interfaces, the JNDI
interfaces, and the classes from the servlet API, which are never loaded
from the webapp repositories. The delegate
property
allows an application to modify this behavior to move the parent class loader
ahead of the local repositories.
IMPLEMENTATION NOTE - Due to limitations in Jasper compilation technology, any repository which contains classes from the servlet API will be ignored by the class loader.
IMPLEMENTATION NOTE - The class loader generates source URLs which include the full JAR URL when a class is loaded from a JAR file, which allows setting security permission at the class level, even when a class is contained inside a JAR.
IMPLEMENTATION NOTE - Local repositories are searched in
the order they are added via the initial constructor and/or any subsequent
calls to addRepository()
or addJar()
.
IMPLEMENTATION NOTE - No check for sealing violations or security is made unless a security manager is present.
TODO: Is there any requirement to provide a proper Lifecycle implementation
rather than the current stubbed implementation?
IMPLEMENTATION NOTE - As of 7.0.64/8.0, this class
loader implements InstrumentableClassLoader
, permitting web
application classes to instrument other classes in the same web
application. It does not permit instrumentation of system or container
classes or classes in other web apps.
Modifier and Type | Class and Description |
---|---|
protected class |
WebappClassLoaderBase.PrivilegedFindResourceByName |
protected static class |
WebappClassLoaderBase.PrivilegedGetClassLoader |
Modifier and Type | Field and Description |
---|---|
protected java.security.Permission |
allPermission
All permission.
|
protected java.lang.String |
canonicalLoaderDir |
protected boolean |
delegate
Should this class loader delegate to the parent class loader
before searching its own repositories (i.e. the
usual Java2 delegation model)?
|
protected java.io.File[] |
files
Repositories translated as path in the work directory (for Jasper
originally), but which is used to generate fake URLs should getURLs be
called.
|
protected boolean |
hasExternalRepositories
Has external repositories.
|
protected java.lang.ClassLoader |
j2seClassLoader
The bootstrap class loader used to load the JavaSE classes.
|
protected java.util.jar.JarFile[] |
jarFiles
The list of JARs, in the order they should be searched
for locally loaded classes or resources.
|
protected java.lang.String[] |
jarNames
The list of JARs, in the order they should be searched
for locally loaded classes or resources.
|
protected java.lang.String |
jarPath
The path which will be monitored for added Jar files.
|
protected java.io.File[] |
jarRealFiles
The list of JARs, in the order they should be searched
for locally loaded classes or resources.
|
protected long |
lastJarAccessed
Last time a JAR was accessed.
|
protected long[] |
lastModifiedDates
The list of JARs last modified dates, in the order they should be
searched for locally loaded classes or resources.
|
protected java.io.File |
loaderDir
Path where resources loaded from JARs will be extracted.
|
protected java.util.HashMap<java.lang.String,java.security.PermissionCollection> |
loaderPC
The PermissionCollection for each CodeSource for a web
application context.
|
protected boolean |
needConvert
need conversion for properties files
|
protected java.util.HashMap<java.lang.String,java.lang.String> |
notFoundResources
The list of not found resources.
|
protected static java.lang.String[] |
packageTriggers
Set of package names which are not allowed to be loaded from a webapp
class loader without delegating first.
|
protected java.lang.ClassLoader |
parent
The parent class loader.
|
protected java.lang.String[] |
paths
The list of resources which should be checked when checking for
modifications.
|
protected java.util.ArrayList<java.security.Permission> |
permissionList
A list of read File and Jndi Permission's required if this loader
is for a web application context.
|
protected java.lang.String[] |
repositories
The list of local repositories, in the order they should be searched
for locally loaded classes or resources.
|
protected java.net.URL[] |
repositoryURLs
Repositories URLs, used to cache the result of getURLs.
|
protected java.util.HashMap<java.lang.String,ResourceEntry> |
resourceEntries
The cache of ResourceEntry for classes and resources we have loaded,
keyed by resource name.
|
protected javax.naming.directory.DirContext |
resources
Associated directory context giving access to the resources in this
webapp.
|
protected boolean |
searchExternalFirst
Search external repositories first
|
protected java.lang.SecurityManager |
securityManager
Instance of the SecurityManager installed.
|
protected static StringManager |
sm |
protected boolean |
started
Has this component been started?
|
protected java.lang.ClassLoader |
system
Deprecated.
Unused. Always null. Will be removed in 8.0.x.
|
protected static java.lang.String[] |
triggers
The set of trigger classes that will cause a proposed repository not
to be added if this class is visible to the class loader that loaded
this factory class.
|
AFTER_DESTROY_EVENT, AFTER_INIT_EVENT, AFTER_START_EVENT, AFTER_STOP_EVENT, BEFORE_DESTROY_EVENT, BEFORE_INIT_EVENT, BEFORE_START_EVENT, BEFORE_STOP_EVENT, CONFIGURE_START_EVENT, CONFIGURE_STOP_EVENT, PERIODIC_EVENT, START_EVENT, STOP_EVENT
Constructor and Description |
---|
WebappClassLoaderBase()
Construct a new ClassLoader with no defined repositories and no
parent ClassLoader.
|
WebappClassLoaderBase(java.lang.ClassLoader parent)
Construct a new ClassLoader with no defined repositories and the given
parent ClassLoader.
|
Modifier and Type | Method and Description |
---|---|
void |
addLifecycleListener(LifecycleListener listener)
Add a lifecycle event listener to this component.
|
void |
addPermission(java.security.Permission permission)
If there is a Java SecurityManager create a Permission.
|
void |
addPermission(java.lang.String filepath)
If there is a Java SecurityManager create a read FilePermission
or JndiPermission for the file directory path.
|
void |
addPermission(java.net.URL url)
If there is a Java SecurityManager create a read FilePermission
or JndiPermission for URL.
|
void |
addRepository(java.lang.String repository)
Add a new repository to the set of places this ClassLoader can look for
classes to be loaded.
|
void |
addTransformer(java.lang.instrument.ClassFileTransformer transformer)
Adds the specified class file transformer to this class loader.
|
protected void |
addURL(java.net.URL url)
Add the specified URL to the classloader.
|
protected void |
clearReferences()
Clear references.
|
void |
closeJARs(boolean force)
Used to periodically signal to the classloader to release
JAR resources.
|
protected void |
copyStateWithoutTransformers(WebappClassLoaderBase base) |
protected static void |
deleteDir(java.io.File dir)
Delete the specified directory, including all of its contents and
subdirectories recursively.
|
void |
destroy()
Prepare to discard the object.
|
protected java.lang.Class<?> |
doDefineClass(java.lang.String name,
byte[] b,
int off,
int len,
java.security.ProtectionDomain protectionDomain)
Expose this method for use by the unit tests.
|
protected boolean |
filter(java.lang.String name)
Filter classes.
|
java.lang.Class<?> |
findClass(java.lang.String name)
Find the specified class in our local repositories, if possible.
|
protected java.lang.Class<?> |
findClassInternal(java.lang.String name)
Find specified class in local repositories.
|
LifecycleListener[] |
findLifecycleListeners()
Get the lifecycle listeners associated with this lifecycle.
|
protected java.lang.Class<?> |
findLoadedClass0(java.lang.String name)
Finds the class with the given name if it has previously been
loaded and cached by this class loader, and return the Class object.
|
protected java.io.InputStream |
findLoadedResource(java.lang.String name)
Finds the resource with the given name if it has previously been
loaded and cached by this class loader, and return an input stream
to the resource data.
|
java.lang.String[] |
findRepositories()
Return a String array of the current repositories for this class
loader.
|
java.net.URL |
findResource(java.lang.String name)
Find the specified resource in our local repository, and return a
URL referring to it, or null if this resource
cannot be found. |
protected ResourceEntry |
findResourceInternal(java.io.File file,
java.lang.String path)
Find specified resource in local repositories.
|
protected ResourceEntry |
findResourceInternal(java.lang.String name,
java.lang.String path,
boolean manifestRequired)
Find specified resource in local repositories.
|
java.util.Enumeration<java.net.URL> |
findResources(java.lang.String name)
Return an enumeration of
URLs representing all of the
resources with the given name. |
boolean |
getAntiJARLocking() |
boolean |
getClearReferencesHttpClientKeepAliveThread()
Return the clearReferencesHttpClientKeepAliveThread flag for this
Context.
|
boolean |
getClearReferencesLogFactoryRelease()
Return the clearReferencesLogFactoryRelease flag for this Context.
|
boolean |
getClearReferencesStatic()
Return the clearReferencesStatic flag for this Context.
|
boolean |
getClearReferencesStopThreads()
Return the clearReferencesStopThreads flag for this Context.
|
boolean |
getClearReferencesStopTimerThreads()
Return the clearReferencesStopTimerThreads flag for this Context.
|
java.lang.String |
getContextName()
Return the context name for this class loader.
|
boolean |
getDelegate()
Return the "delegate first" flag for this class loader.
|
java.lang.String |
getJarPath()
Return the JAR path.
|
protected java.lang.ClassLoader |
getJavaseClassLoader() |
protected java.security.PermissionCollection |
getPermissions(java.security.CodeSource codeSource)
Get the Permissions for a CodeSource.
|
java.net.URL |
getResource(java.lang.String name)
Find the resource with the given name.
|
java.io.InputStream |
getResourceAsStream(java.lang.String name)
Find the resource with the given name, and return an input stream
that can be used for reading it.
|
javax.naming.directory.DirContext |
getResources()
Get associated resources.
|
boolean |
getSearchExternalFirst() |
LifecycleState |
getState()
Obtain the current state of the source component.
|
java.lang.String |
getStateName()
Obtain a textual representation of the current component state.
|
protected java.net.URL |
getURI(java.io.File file)
Get the URI for the given file.
|
protected java.net.URL |
getURL(java.io.File file,
boolean encoded)
Deprecated.
Use
getURI(File) instead |
java.net.URL[] |
getURLs()
Returns the search path of URLs for loading classes and resources.
|
void |
init()
Prepare the component for starting.
|
protected boolean |
isPackageSealed(java.lang.String name,
java.util.jar.Manifest man)
Returns true if the specified package name is sealed according to the
given manifest.
|
boolean |
isStarted() |
java.lang.Class<?> |
loadClass(java.lang.String name)
Load the class with the specified name.
|
java.lang.Class<?> |
loadClass(java.lang.String name,
boolean resolve)
Load the class with the specified name, searching using the following
algorithm until it finds and returns the class.
|
boolean |
modified()
Have one or more classes or resources been modified so that a reload
is appropriate?
|
protected boolean |
openJARs()
Used to periodically signal to the classloader to release JAR resources.
|
protected void |
refreshPolicy()
Refresh the system policy file, to pick up eventual changes.
|
void |
removeLifecycleListener(LifecycleListener listener)
Remove a lifecycle event listener from this component.
|
void |
removeTransformer(java.lang.instrument.ClassFileTransformer transformer)
Removes the specified class file transformer from this class loader.
|
void |
setAntiJARLocking(boolean antiJARLocking) |
void |
setClearReferencesHttpClientKeepAliveThread(boolean clearReferencesHttpClientKeepAliveThread)
Set the clearReferencesHttpClientKeepAliveThread feature for this
Context.
|
void |
setClearReferencesLogFactoryRelease(boolean clearReferencesLogFactoryRelease)
Set the clearReferencesLogFactoryRelease feature for this Context.
|
void |
setClearReferencesStatic(boolean clearReferencesStatic)
Set the clearReferencesStatic feature for this Context.
|
void |
setClearReferencesStopThreads(boolean clearReferencesStopThreads)
Set the clearReferencesStopThreads feature for this Context.
|
void |
setClearReferencesStopTimerThreads(boolean clearReferencesStopTimerThreads)
Set the clearReferencesStopTimerThreads feature for this Context.
|
void |
setDelegate(boolean delegate)
Set the "delegate first" flag for this class loader.
|
void |
setJarPath(java.lang.String jarPath)
Change the Jar path.
|
protected void |
setJavaseClassLoader(java.lang.ClassLoader classLoader) |
protected void |
setParentClassLoader(java.lang.ClassLoader pcl)
Deprecated.
Will be removed in 8.0.x onwards.
|
void |
setResources(javax.naming.directory.DirContext resources)
Set associated resources.
|
void |
setSearchExternalFirst(boolean searchExternalFirst) |
void |
setWorkDir(java.io.File workDir)
Change the work directory.
|
void |
start()
Start the class loader.
|
void |
stop()
Stop the class loader.
|
java.lang.String |
toString()
Render a String representation of this object.
|
protected boolean |
validate(java.lang.String name)
Validate a classname.
|
protected boolean |
validateJarFile(java.io.File file)
Check the specified JAR file, and return
true if it does
not contain any of the trigger classes. |
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findLibrary, findLoadedClass, findSystemClass, getClassLoadingLock, getPackage, getPackages, getParent, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, registerAsParallelCapable, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
copyWithoutTransformers
protected static final StringManager sm
protected static final java.lang.String[] triggers
protected static final java.lang.String[] packageTriggers
protected javax.naming.directory.DirContext resources
protected java.util.HashMap<java.lang.String,ResourceEntry> resourceEntries
protected java.util.HashMap<java.lang.String,java.lang.String> notFoundResources
protected boolean delegate
false
,
this class loader will search its own repositories first, and
delegate to the parent only if the class or resource is not
found locally. Note that the default, false
, is
the behavior called for by the servlet specification.protected long lastJarAccessed
protected java.lang.String[] repositories
protected java.net.URL[] repositoryURLs
protected java.io.File[] files
protected java.util.jar.JarFile[] jarFiles
protected java.io.File[] jarRealFiles
protected java.lang.String jarPath
protected java.lang.String[] jarNames
protected long[] lastModifiedDates
protected java.lang.String[] paths
protected java.util.ArrayList<java.security.Permission> permissionList
protected java.io.File loaderDir
protected java.lang.String canonicalLoaderDir
protected java.util.HashMap<java.lang.String,java.security.PermissionCollection> loaderPC
protected java.lang.SecurityManager securityManager
protected java.lang.ClassLoader parent
@Deprecated protected java.lang.ClassLoader system
protected java.lang.ClassLoader j2seClassLoader
null and in
those cases ClassLoader.getParent()
will be called recursively on
the system class loader and the last non-null result used.
protected boolean started
protected boolean hasExternalRepositories
protected boolean searchExternalFirst
protected boolean needConvert
protected java.security.Permission allPermission
public WebappClassLoaderBase()
public WebappClassLoaderBase(java.lang.ClassLoader parent)
Method is used via reflection -
see WebappLoader.createClassLoader()
parent
- Our parent class loaderpublic javax.naming.directory.DirContext getResources()
public void setResources(javax.naming.directory.DirContext resources)
public java.lang.String getContextName()
public boolean getDelegate()
public void setDelegate(boolean delegate)
false
(the default),
this class loader will search its own repositories first, and
delegate to the parent only if the class or resource is not
found locally, as per the servlet specification.delegate
- The new "delegate first" flagpublic boolean getAntiJARLocking()
public void setAntiJARLocking(boolean antiJARLocking)
antiJARLocking
- The antiJARLocking to set.public boolean getSearchExternalFirst()
public void setSearchExternalFirst(boolean searchExternalFirst)
searchExternalFirst
- Whether external repositories should be searched firstpublic void addPermission(java.lang.String filepath)
filepath
- file directory pathpublic void addPermission(java.net.URL url)
url
- URL for a file or directory on local systempublic void addPermission(java.security.Permission permission)
permission
- The permissionpublic java.lang.String getJarPath()
public void setJarPath(java.lang.String jarPath)
public void setWorkDir(java.io.File workDir)
@Deprecated protected void setParentClassLoader(java.lang.ClassLoader pcl)
public boolean getClearReferencesStatic()
public void setClearReferencesStatic(boolean clearReferencesStatic)
clearReferencesStatic
- The new flag valuepublic boolean getClearReferencesStopThreads()
public void setClearReferencesStopThreads(boolean clearReferencesStopThreads)
clearReferencesStopThreads
- The new flag valuepublic boolean getClearReferencesStopTimerThreads()
public void setClearReferencesStopTimerThreads(boolean clearReferencesStopTimerThreads)
clearReferencesStopTimerThreads
- The new flag valuepublic boolean getClearReferencesLogFactoryRelease()
public void setClearReferencesLogFactoryRelease(boolean clearReferencesLogFactoryRelease)
clearReferencesLogFactoryRelease
- The new flag valuepublic boolean getClearReferencesHttpClientKeepAliveThread()
public void setClearReferencesHttpClientKeepAliveThread(boolean clearReferencesHttpClientKeepAliveThread)
clearReferencesHttpClientKeepAliveThread
- The new flag valuepublic void addTransformer(java.lang.instrument.ClassFileTransformer transformer)
addTransformer
in interface InstrumentableClassLoader
transformer
- The transformer to add to the class loaderpublic void removeTransformer(java.lang.instrument.ClassFileTransformer transformer)
removeTransformer
in interface InstrumentableClassLoader
transformer
- The transformer to removeprotected void copyStateWithoutTransformers(WebappClassLoaderBase base)
public void addRepository(java.lang.String repository)
repository
- Name of a source of classes to be loaded, such as a
directory pathname, a JAR file pathname, or a ZIP file pathnamejava.lang.IllegalArgumentException
- if the specified repository is
invalid or does not existpublic java.lang.String[] findRepositories()
public boolean modified()
public java.lang.String toString()
toString
in class java.lang.Object
protected void addURL(java.net.URL url)
addURL
in class java.net.URLClassLoader
protected final java.lang.Class<?> doDefineClass(java.lang.String name, byte[] b, int off, int len, java.security.ProtectionDomain protectionDomain)
public java.lang.Class<?> findClass(java.lang.String name) throws java.lang.ClassNotFoundException
ClassNotFoundException
.findClass
in class java.net.URLClassLoader
name
- Name of the class to be loadedjava.lang.ClassNotFoundException
- if the class was not foundpublic java.net.URL findResource(java.lang.String name)
URL
referring to it, or null
if this resource
cannot be found.findResource
in class java.net.URLClassLoader
name
- Name of the resource to be foundpublic java.util.Enumeration<java.net.URL> findResources(java.lang.String name) throws java.io.IOException
URLs
representing all of the
resources with the given name. If no resources with this name are
found, return an empty enumeration.findResources
in class java.net.URLClassLoader
name
- Name of the resources to be foundjava.io.IOException
- if an input/output error occurspublic java.net.URL getResource(java.lang.String name)
null
.
This method searches according to the following algorithm, returning
as soon as it finds the appropriate URL. If the resource cannot be
found, returns null
.
delegate
property is set to true
,
call the getResource()
method of the parent class
loader, if any.findResource()
to find this resource in our
locally defined repositories.getResource()
method of the parent class
loader, if any.getResource
in class java.lang.ClassLoader
name
- Name of the resource to return a URL forpublic java.io.InputStream getResourceAsStream(java.lang.String name)
getResource()
, after checking to see if the resource
data has been previously cached. If the resource cannot be found,
return null
.getResourceAsStream
in class java.net.URLClassLoader
name
- Name of the resource to return an input stream forpublic java.lang.Class<?> loadClass(java.lang.String name) throws java.lang.ClassNotFoundException
loadClass(String, boolean)
with false
as the second argument.loadClass
in class java.lang.ClassLoader
name
- Name of the class to be loadedjava.lang.ClassNotFoundException
- if the class was not foundpublic java.lang.Class<?> loadClass(java.lang.String name, boolean resolve) throws java.lang.ClassNotFoundException
ClassNotFoundException
.
findLoadedClass(String)
to check if the
class has already been loaded. If it has, the same
Class
object is returned.delegate
property is set to true
,
call the loadClass()
method of the parent class
loader, if any.findClass()
to find this class in our locally
defined repositories.loadClass()
method of our parent
class loader, if any.resolve
flag is true
, this method will then
call resolveClass(Class)
on the resulting Class object.loadClass
in class java.lang.ClassLoader
name
- Name of the class to be loadedresolve
- If true
then resolve the classjava.lang.ClassNotFoundException
- if the class was not foundprotected java.security.PermissionCollection getPermissions(java.security.CodeSource codeSource)
getPermissions
in class java.net.URLClassLoader
codeSource
- where the code was loaded frompublic java.net.URL[] getURLs()
getURLs
in class java.net.URLClassLoader
public void addLifecycleListener(LifecycleListener listener)
addLifecycleListener
in interface Lifecycle
listener
- The listener to addpublic LifecycleListener[] findLifecycleListeners()
findLifecycleListeners
in interface Lifecycle
public void removeLifecycleListener(LifecycleListener listener)
removeLifecycleListener
in interface Lifecycle
listener
- The listener to removepublic LifecycleState getState()
public java.lang.String getStateName()
getStateName
in interface Lifecycle
public void init()
Lifecycle
LifecycleEvent
s will be fired in the following order:
public void start() throws LifecycleException
start
in interface Lifecycle
LifecycleException
- if a lifecycle error occurspublic boolean isStarted()
public void stop() throws LifecycleException
stop
in interface Lifecycle
LifecycleException
- if a lifecycle error occurspublic void destroy()
Lifecycle
LifecycleEvent
s will
be fired in the following order:
public void closeJARs(boolean force)
protected java.lang.ClassLoader getJavaseClassLoader()
protected void setJavaseClassLoader(java.lang.ClassLoader classLoader)
protected void clearReferences()
protected boolean openJARs()
protected java.lang.Class<?> findClassInternal(java.lang.String name) throws java.lang.ClassNotFoundException
java.lang.ClassNotFoundException
protected ResourceEntry findResourceInternal(java.io.File file, java.lang.String path)
protected ResourceEntry findResourceInternal(java.lang.String name, java.lang.String path, boolean manifestRequired)
protected boolean isPackageSealed(java.lang.String name, java.util.jar.Manifest man)
protected java.io.InputStream findLoadedResource(java.lang.String name)
null
.name
- Name of the resource to returnprotected java.lang.Class<?> findLoadedClass0(java.lang.String name)
null
.name
- Name of the resource to returnprotected void refreshPolicy()
protected boolean filter(java.lang.String name)
name
- class nameprotected boolean validate(java.lang.String name)
name
- class nameprotected boolean validateJarFile(java.io.File file) throws java.io.IOException
true
if it does
not contain any of the trigger classes.file
- The JAR file to be checkedjava.io.IOException
- if an input/output error occurs@Deprecated protected java.net.URL getURL(java.io.File file, boolean encoded) throws java.net.MalformedURLException
getURI(File)
insteadjava.net.MalformedURLException
protected java.net.URL getURI(java.io.File file) throws java.net.MalformedURLException
java.net.MalformedURLException
protected static void deleteDir(java.io.File dir)
dir
- File object representing the directory to be deletedCopyright © 2000-2015 Apache Software Foundation. All Rights Reserved.