public class WebappClassLoader extends URLClassLoader implements Reloader, InstrumentableClassLoader, org.glassfish.hk2.api.PreDestroy, DDPermissionsLoader, JarFileResourcesProvider
This class loader is a full reimplementation of the
URLClassLoader
from the JDK. It is desinged to be fully
compatible with a normal URLClassLoader
, although its internal
behavior may be completely different.
IMPLEMENTATION NOTE - This class loader faithfully follows the delegation model recommended in 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 repository.
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.
Modifier and Type | Class and Description |
---|---|
protected class |
WebappClassLoader.PrivilegedFindResource |
protected static class |
WebappClassLoader.PrivilegedGetClassLoader |
Modifier and Type | Field and Description |
---|---|
protected String |
canonicalLoaderDir |
protected int |
debug
The debugging detail level of this component.
|
protected boolean |
delegate
Should this class loader delegate to the parent class loader
before searching its own repositories (i.e.
|
protected 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 JarFile[] |
jarFiles
The list of JARs, in the order they should be searched
for locally loaded classes or resources.
|
protected Object |
jarFilesLock
Lock to synchronize closing and opening of jar
|
protected List<String> |
jarNames
The list of JARs, in the order they should be searched
for locally loaded classes or resources.
|
protected String |
jarPath
The path which will be monitored for added Jar files.
|
protected 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 File |
loaderDir
Path where resources loaded from JARs will be extracted.
|
protected ConcurrentHashMap<String,String> |
notFoundResources
The list of not found resources.
|
protected String[] |
paths
The list of resources which should be checked when checking for
modifications.
|
protected String[] |
repositories
The list of local repositories, in the order they should be searched
for locally loaded classes or resources.
|
protected URL[] |
repositoryURLs
Repositories URLs, used to cache the result of getURLs.
|
protected ConcurrentHashMap<String,ResourceEntry> |
resourceEntries
The cache of ResourceEntry for classes and resources we have loaded,
keyed by resource name.
|
protected DirContext |
resources
Associated directory context giving access to the resources in this
webapp.
|
protected boolean |
started
Has this component been started?
|
SET_EE_POLICY
Constructor and Description |
---|
WebappClassLoader(Application application)
Construct a new ClassLoader with no defined repositories and no
parent ClassLoader.
|
WebappClassLoader(ClassLoader parent)
for use in OSGi loader
|
WebappClassLoader(ClassLoader parent,
Application application)
Construct a new ClassLoader with the given parent ClassLoader,
but no defined repositories.
|
WebappClassLoader(URL[] urls,
ClassLoader parent,
Application application)
Construct a new ClassLoader with the given parent ClassLoader
and defined repositories.
|
Modifier and Type | Method and Description |
---|---|
void |
addByteCodePreprocessor(BytecodePreprocessor preprocessor) |
void |
addDeclaredPermissions(PermissionCollection declaredPc)
Pass the declared permission collection from the module handler to the classloader
|
void |
addEEPermissions(PermissionCollection eePc)
Pass the EE permission to the classloader
|
void |
addJar(String jar,
JarFile jarFile,
File file) |
void |
addOverridablePackage(String packageName)
Adds the given package name to the list of packages that may always be
overriden, regardless of whether they belong to a protected namespace
|
void |
addPermission(Permission permission)
If there is a Java SecurityManager create a Permission.
|
void |
addPermission(String path)
If there is a Java SecurityManager create a read FilePermission
or JndiPermission for the file directory path.
|
void |
addPermission(URL url)
If there is a Java SecurityManager create a read FilePermission
or JndiPermission for URL.
|
void |
addRepository(String repository)
Add a new repository to the set of places this ClassLoader can look for
classes to be loaded.
|
void |
addRepository(String repository,
File file)
Add a new repository to the set of places this ClassLoader can look for
classes to be loaded.
|
void |
addRepository(URL url) |
void |
addTransformer(ClassFileTransformer transformer)
Add a new ClassFileTransformer to this class loader.
|
protected void |
clearReferences()
Clear references.
|
void |
closeJARs(boolean force)
Used to periodically signal to the classloader to release
JAR resources.
|
ClassLoader |
copy()
Create and return a temporary loader with the same visibility
as this loader.
|
protected static void |
deleteDir(File dir)
Delete the specified directory, including all of its contents and
subdirectories recursively.
|
protected boolean |
filter(String name)
Filter classes.
|
protected Class<?> |
findClass(String name)
Find the specified class in our local repositories, if possible.
|
protected ResourceEntry |
findClassInternal(String name)
Find specified class in local repositories.
|
protected Class<?> |
findLoadedClass0(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 InputStream |
findLoadedResource(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.
|
URL |
findResource(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(String name,
String path)
Attempts to find the specified resource in local repositories.
|
Enumeration<URL> |
findResources(String name)
Return an enumeration of
URLs representing all of the
resources with the given name. |
boolean |
getAntiJARLocking() |
boolean |
getClearReferencesStatic()
Return the clearReferencesStatic flag for this Context.
|
String |
getContextName()
Return the context name for this class loader.
|
int |
getDebug()
Return the debugging detail level for this component.
|
boolean |
getDelegate()
Return the "delegate first" flag for this class loader.
|
File |
getExtractedResourcePath(String path) |
JarFile[] |
getJarFiles() |
String |
getJarPath()
Return the JAR path.
|
protected PermissionCollection |
getPermissions(CodeSource codeSource)
Get the Permissions for a CodeSource.
|
URL |
getResource(String name)
Find the resource with the given name.
|
InputStream |
getResourceAsStream(String name)
Find the resource with the given name, and return an input stream
that can be used for reading it.
|
ConcurrentHashMap<String,ResourceEntry> |
getResourceEntries() |
DirContext |
getResources()
Get associated resources.
|
Enumeration<URL> |
getResources(String name)
Finds all the resources with the given name.
|
protected URL |
getURI(File file)
Get URL.
|
protected URL |
getURL(File file)
Get URL.
|
URL[] |
getURLs()
Returns the search path of URLs for loading classes and resources.
|
protected boolean |
isPackageSealed(String name,
Manifest man)
Returns true if the specified package name is sealed according to the
given manifest.
|
boolean |
isStarted() |
Class<?> |
loadClass(String name)
Load the class with the specified name.
|
protected Class<?> |
loadClass(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 void |
nullInstance(Object instance) |
protected boolean |
openJARs()
Used to periodically signal to the classloader to release JAR resources.
|
void |
preDestroy() |
protected void |
refreshPolicy()
Refresh the system policy file, to pick up eventual changes.
|
void |
setAntiJARLocking(boolean antiJARLocking) |
void |
setClearReferencesStatic(boolean clearReferencesStatic)
Set the clearReferencesStatic feature for this Context.
|
void |
setDebug(int debug)
Set the debugging detail level for this component.
|
void |
setDelegate(boolean delegate)
Set the "delegate first" flag for this class loader.
|
void |
setJarPath(String jarPath)
Change the Jar path.
|
void |
setResources(DirContext resources)
Set associated resources.
|
void |
setUseMyFaces(boolean useMyFaces) |
void |
setWorkDir(File workDir)
Change the work directory.
|
void |
start()
Start the class loader.
|
void |
stop()
Stop the class loader.
|
String |
toString()
Render a String representation of this object.
|
protected boolean |
validate(String name)
Validate a classname.
|
addURL, close, definePackage, newInstance, newInstance
defineClass, defineClass
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findLibrary, findLoadedClass, findSystemClass, getClassLoadingLock, getPackage, getPackages, getParent, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, registerAsParallelCapable, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
protected DirContext resources
protected ConcurrentHashMap<String,ResourceEntry> resourceEntries
protected ConcurrentHashMap<String,String> notFoundResources
protected int debug
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.protected long lastJarAccessed
protected String[] repositories
protected URL[] repositoryURLs
protected File[] files
protected JarFile[] jarFiles
protected final Object jarFilesLock
protected File[] jarRealFiles
protected String jarPath
protected List<String> jarNames
protected long[] lastModifiedDates
protected String[] paths
protected File loaderDir
protected String canonicalLoaderDir
protected boolean started
protected boolean hasExternalRepositories
public WebappClassLoader(Application application)
application
- public WebappClassLoader(ClassLoader parent, Application application)
parent
- application
- public WebappClassLoader(ClassLoader parent)
parent
- public WebappClassLoader(URL[] urls, ClassLoader parent, Application application)
urls
- parent
- application
- public void addOverridablePackage(String packageName)
public DirContext getResources()
public void setResources(DirContext resources)
public String getContextName()
public ConcurrentHashMap<String,ResourceEntry> getResourceEntries()
public int getDebug()
public void setDebug(int debug)
debug
- The new debugging detail levelpublic boolean getDelegate()
public void setDelegate(boolean delegate)
delegate
- The new "delegate first" flagpublic boolean getAntiJARLocking()
public void setAntiJARLocking(boolean antiJARLocking)
antiJARLocking
- The antiJARLocking to set.public JarFile[] getJarFiles()
getJarFiles
in interface JarFileResourcesProvider
public void addPermission(String path)
path
- file directory pathpublic void addPermission(URL url)
url
- URL for a file or directory on local systempublic void addPermission(Permission permission)
permission
- permission to addpublic void addDeclaredPermissions(PermissionCollection declaredPc) throws SecurityException
DDPermissionsLoader
addDeclaredPermissions
in interface DDPermissionsLoader
declaredPc
- the declared permission collection obtained from permissions.xml file
throws AccessControlException if caller has no privilegeSecurityException
public void addEEPermissions(PermissionCollection eePc) throws SecurityException
DDPermissionsLoader
addEEPermissions
in interface DDPermissionsLoader
eePc
- EE permissions
throws AccessControlException if caller has no privilegeSecurityException
public String getJarPath()
public void setJarPath(String jarPath)
public void setWorkDir(File workDir)
public void setUseMyFaces(boolean useMyFaces)
public boolean getClearReferencesStatic()
public void setClearReferencesStatic(boolean clearReferencesStatic)
clearReferencesStatic
- The new flag valuepublic void addRepository(String repository)
addRepository
in interface Reloader
repository
- Name of a source of classes to be loaded, such as a
directory pathname, a JAR file pathname, or a ZIP file pathnameIllegalArgumentException
- if the specified repository is
invalid or does not existpublic void addRepository(URL url)
public void addRepository(String repository, File file)
repository
- Name of a source of classes to be loaded, such as a
directory pathname, a JAR file pathname, or a ZIP file pathnameIllegalArgumentException
- if the specified repository is
invalid or does not existpublic void addJar(String jar, JarFile jarFile, File file) throws IOException
IOException
public boolean modified()
public String toString()
protected Class<?> findClass(String name) throws ClassNotFoundException
ClassNotFoundException
.findClass
in class URLClassLoader
name
- Name of the class to be loadedClassNotFoundException
- if the class was not foundpublic URL findResource(String name)
URL
referring to it, or null
if this resource
cannot be found.findResource
in class URLClassLoader
name
- Name of the resource to be foundpublic Enumeration<URL> findResources(String name) throws 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 URLClassLoader
name
- Name of the resources to be foundIOException
- if an input/output error occurspublic URL getResource(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 ClassLoader
name
- Name of the resource to return a URL forpublic InputStream getResourceAsStream(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 URLClassLoader
name
- Name of the resource to return an input stream forpublic Enumeration<URL> getResources(String name) throws IOException
getResources
in class ClassLoader
IOException
public Class<?> loadClass(String name) throws ClassNotFoundException
loadClass(String, boolean)
with false
as the second argument.loadClass
in class ClassLoader
name
- Name of the class to be loadedClassNotFoundException
- if the class was not foundprotected Class<?> loadClass(String name, boolean resolve) throws 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 ClassLoader
name
- Name of the class to be loadedresolve
- If true
then resolve the classClassNotFoundException
- if the class was not foundprotected PermissionCollection getPermissions(CodeSource codeSource)
getPermissions
in class URLClassLoader
codeSource
- where the code was loaded frompublic URL[] getURLs()
getURLs
in class URLClassLoader
public void start()
public boolean isStarted()
public void preDestroy()
preDestroy
in interface org.glassfish.hk2.api.PreDestroy
public void stop() throws Exception
LifecycleException
- if a lifecycle error occursException
public void closeJARs(boolean force)
protected void clearReferences()
protected void nullInstance(Object instance)
protected boolean openJARs()
protected ResourceEntry findClassInternal(String name) throws ClassNotFoundException
ClassNotFoundException
protected ResourceEntry findResourceInternal(String name, String path)
protected boolean isPackageSealed(String name, Manifest man)
protected InputStream findLoadedResource(String name)
null
.name
- Name of the resource to returnprotected Class<?> findLoadedClass0(String name)
null
.name
- Name of the resource to returnprotected void refreshPolicy()
protected boolean filter(String name)
name
- class nameprotected boolean validate(String name)
name
- class nameprotected URL getURL(File file) throws MalformedURLException
MalformedURLException
protected URL getURI(File file) throws MalformedURLException
MalformedURLException
protected static void deleteDir(File dir)
dir
- File object representing the directory to be deletedpublic void addByteCodePreprocessor(BytecodePreprocessor preprocessor)
public ClassLoader copy()
copy
in interface InstrumentableClassLoader
public void addTransformer(ClassFileTransformer transformer)
addTransformer
in interface InstrumentableClassLoader
transformer
- new class file transformer to do byte code enhancement.Copyright © 2018. All rights reserved.