Class WebAppClassLoader
- java.lang.Object
-
- java.lang.ClassLoader
-
- java.security.SecureClassLoader
-
- java.net.URLClassLoader
-
- org.eclipse.jetty.webapp.WebAppClassLoader
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,ClassVisibilityChecker
- Direct Known Subclasses:
CachingWebAppClassLoader
,OSGiWebappClassLoader
public class WebAppClassLoader extends java.net.URLClassLoader implements ClassVisibilityChecker
ClassLoader for HttpContext.Specializes URLClassLoader with some utility and file mapping methods.
This loader defaults to the 2.3 servlet spec behavior where non system classes are loaded from the classpath in preference to the parent loader. Java2 compliant loading, where the parent loader always has priority, can be selected with the
WebAppContext.setParentLoaderPriority(boolean)
method and influenced withWebAppContext.isServerClass(String)
andWebAppContext.isSystemClass(String)
.If no parent class loader is provided, then the current thread context classloader will be used. If that is null then the classloader that loaded this class is used as the parent.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
WebAppClassLoader.Context
The Context in which the classloader operates.
-
Constructor Summary
Constructors Constructor Description WebAppClassLoader(java.lang.ClassLoader parent, WebAppClassLoader.Context context)
Constructor.WebAppClassLoader(WebAppClassLoader.Context context)
Constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addClassFileTransformer(java.lang.instrument.ClassFileTransformer transformer)
Deprecated.void
addClassPath(java.lang.String classPath)
void
addClassPath(Resource resource)
void
addJars(Resource lib)
Add elements to the class path for the context from the jar and zip files found in the specified resource.void
addTransformer(java.lang.instrument.ClassFileTransformer transformer)
void
close()
protected java.lang.Class<?>
findClass(java.lang.String name)
protected java.lang.Class<?>
foundClass(java.lang.String name, java.net.URL url)
WebAppClassLoader.Context
getContext()
java.lang.String
getName()
java.security.PermissionCollection
getPermissions(java.security.CodeSource cs)
java.net.URL
getResource(java.lang.String name)
Get a resource from the classloader NOTE: this method provides a convenience of hacking off a leading / should one be present.java.util.Enumeration<java.net.URL>
getResources(java.lang.String name)
boolean
isServerClass(java.lang.Class<?> clazz)
Is the class a Server Class.boolean
isSystemClass(java.lang.Class<?> clazz)
Is the class a System Class.protected java.lang.Class<?>
loadAsResource(java.lang.String name, boolean checkSystemResource)
Look for the classname as a resource to avoid loading a class that is potentially a system resource.protected java.lang.Class<?>
loadClass(java.lang.String name, boolean resolve)
boolean
removeClassFileTransformer(java.lang.instrument.ClassFileTransformer transformer)
Deprecated.useremoveTransformer(ClassFileTransformer)
insteadboolean
removeTransformer(java.lang.instrument.ClassFileTransformer transformer)
static <T> T
runWithServerClassAccess(java.security.PrivilegedExceptionAction<T> action)
Run an action with access to ServerClassesvoid
setName(java.lang.String name)
java.lang.String
toString()
-
Methods inherited from class java.net.URLClassLoader
addURL, definePackage, findResource, findResources, getResourceAsStream, getURLs, newInstance, newInstance
-
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findClass, findLibrary, findLoadedClass, findResource, findSystemClass, getClassLoadingLock, getDefinedPackage, getDefinedPackages, getPackage, getPackages, getParent, getPlatformClassLoader, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, getUnnamedModule, isRegisteredAsParallelCapable, loadClass, registerAsParallelCapable, resolveClass, resources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
-
-
-
-
Constructor Detail
-
WebAppClassLoader
public WebAppClassLoader(WebAppClassLoader.Context context) throws java.io.IOException
Constructor.- Parameters:
context
- the context for this classloader- Throws:
java.io.IOException
- if unable to initialize from context
-
WebAppClassLoader
public WebAppClassLoader(java.lang.ClassLoader parent, WebAppClassLoader.Context context) throws java.io.IOException
Constructor.- Parameters:
parent
- the parent classloadercontext
- the context for this classloader- Throws:
java.io.IOException
- if unable to initialize classloader
-
-
Method Detail
-
runWithServerClassAccess
public static <T> T runWithServerClassAccess(java.security.PrivilegedExceptionAction<T> action) throws java.lang.Exception
Run an action with access to ServerClassesRun the passed
PrivilegedExceptionAction
with the classloader configured so as to allow server classes to be visible- Type Parameters:
T
- The type returned by the action- Parameters:
action
- The action to run- Returns:
- The return from the action
- Throws:
java.lang.Exception
- if thrown by the action
-
getName
public java.lang.String getName()
- Overrides:
getName
in classjava.lang.ClassLoader
- Returns:
- the name of the classloader
-
setName
public void setName(java.lang.String name)
- Parameters:
name
- the name of the classloader
-
getContext
public WebAppClassLoader.Context getContext()
-
addClassPath
public void addClassPath(Resource resource) throws java.io.IOException
- Parameters:
resource
- Comma or semicolon separated path of filenames or URLs pointing to directories or jar files. Directories should end with '/'.- Throws:
java.io.IOException
- if unable to add classpath from resource
-
addClassPath
public void addClassPath(java.lang.String classPath) throws java.io.IOException
- Parameters:
classPath
- Comma or semicolon separated path of filenames or URLs pointing to directories or jar files. Directories should end with '/'.- Throws:
java.io.IOException
- if unable to add classpath
-
addJars
public void addJars(Resource lib)
Add elements to the class path for the context from the jar and zip files found in the specified resource.- Parameters:
lib
- the resource that contains the jar and/or zip files.
-
getPermissions
public java.security.PermissionCollection getPermissions(java.security.CodeSource cs)
- Overrides:
getPermissions
in classjava.net.URLClassLoader
-
getResources
public java.util.Enumeration<java.net.URL> getResources(java.lang.String name) throws java.io.IOException
- Overrides:
getResources
in classjava.lang.ClassLoader
- Throws:
java.io.IOException
-
getResource
public java.net.URL getResource(java.lang.String name)
Get a resource from the classloader NOTE: this method provides a convenience of hacking off a leading / should one be present. This is non-standard and it is recommended to not rely on this behavior- Overrides:
getResource
in classjava.lang.ClassLoader
-
loadClass
protected java.lang.Class<?> loadClass(java.lang.String name, boolean resolve) throws java.lang.ClassNotFoundException
- Overrides:
loadClass
in classjava.lang.ClassLoader
- Throws:
java.lang.ClassNotFoundException
-
addClassFileTransformer
@Deprecated public void addClassFileTransformer(java.lang.instrument.ClassFileTransformer transformer)
Deprecated.- Parameters:
transformer
- the transformer to add
-
removeClassFileTransformer
@Deprecated public boolean removeClassFileTransformer(java.lang.instrument.ClassFileTransformer transformer)
Deprecated.useremoveTransformer(ClassFileTransformer)
instead- Parameters:
transformer
- the transformer to remove- Returns:
- true if transformer was removed
-
addTransformer
public void addTransformer(java.lang.instrument.ClassFileTransformer transformer)
-
removeTransformer
public boolean removeTransformer(java.lang.instrument.ClassFileTransformer transformer)
-
loadAsResource
protected java.lang.Class<?> loadAsResource(java.lang.String name, boolean checkSystemResource) throws java.lang.ClassNotFoundException
Look for the classname as a resource to avoid loading a class that is potentially a system resource.- Parameters:
name
- the name of the class to loadcheckSystemResource
- if true and the class isn't a system class we return it- Returns:
- the loaded class
- Throws:
java.lang.ClassNotFoundException
- if the class cannot be found
-
findClass
protected java.lang.Class<?> findClass(java.lang.String name) throws java.lang.ClassNotFoundException
- Overrides:
findClass
in classjava.net.URLClassLoader
- Throws:
java.lang.ClassNotFoundException
-
foundClass
protected java.lang.Class<?> foundClass(java.lang.String name, java.net.URL url) throws java.lang.ClassNotFoundException
- Throws:
java.lang.ClassNotFoundException
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.net.URLClassLoader
- Throws:
java.io.IOException
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
isSystemClass
public boolean isSystemClass(java.lang.Class<?> clazz)
Description copied from interface:ClassVisibilityChecker
Is the class a System Class. A System class is a class that is visible to a webapplication, but that cannot be overridden by the contents of WEB-INF/lib or WEB-INF/classes- Specified by:
isSystemClass
in interfaceClassVisibilityChecker
- Parameters:
clazz
- The fully qualified name of the class.- Returns:
- True if the class is a system class.
-
isServerClass
public boolean isServerClass(java.lang.Class<?> clazz)
Description copied from interface:ClassVisibilityChecker
Is the class a Server Class. A Server class is a class that is part of the implementation of the server and is NIT visible to a webapplication. The web application may provide it's own implementation of the class, to be loaded from WEB-INF/lib or WEB-INF/classes- Specified by:
isServerClass
in interfaceClassVisibilityChecker
- Parameters:
clazz
- The fully qualified name of the class.- Returns:
- True if the class is a server class.
-
-