Class WebAppClassLoader
- All Implemented Interfaces:
Closeable
,AutoCloseable
,ClassVisibilityChecker
- Direct Known Subclasses:
CachingWebAppClassLoader
,OSGiWebappClassLoader
,WebAppClassLoader
,WebAppClassLoader
,WebAppClassLoader
Specializes URLClassLoader with some utility and file mapping methods.
This loader implements the Servlet specification behavior that may invert the normal Java classloader parent priority
behaviour. The ClassVisibilityChecker
API of the WebAppClassLoader.Context
implementation is
used to determine which classes from the parent classloader are hidden from the context, and which are protected
from being overridden by the context.
Java compliant loading, where the parent loader always has priority, can be selected with the
WebAppClassLoader.Context.isParentLoaderPriority()
method.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
The Context in which the classloader operates. -
Constructor Summary
ConstructorsConstructorDescriptionWebAppClassLoader
(ClassLoader parent, WebAppClassLoader.Context context) Constructor.Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addClassPath
(String classPathList) void
addClassPath
(Resource resource) void
Add elements to the class path for the context from the jar and zip files found in the specified resource.void
addTransformer
(ClassFileTransformer transformer) void
close()
protected Class
<?> protected Class
<?> foundClass
(String name, URL url) getName()
Get the name of the classloader.getResource
(String name) Get a resource from the classloadergetResources
(String name) boolean
isHiddenClass
(Class<?> clazz) Is the class a Hidden (Server) Class.boolean
isProtectedClass
(Class<?> clazz) Is the class a Protected (System) Class.protected Class
<?> loadAsResource
(String name, boolean checkSystemResource) Look for the classname as a resource to avoid loading a class that is potentially a system resource.protected Class
<?> boolean
removeTransformer
(ClassFileTransformer transformer) static <T> T
Run an action with access to Hidden Classesvoid
Set the name of the classloader.toString()
Methods inherited from class java.net.URLClassLoader
addURL, definePackage, findResource, findResources, getResourceAsStream, getURLs, newInstance, newInstance
Methods inherited from class java.security.SecureClassLoader
defineClass, defineClass
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
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.eclipse.jetty.util.ClassVisibilityChecker
isServerClass, isSystemClass
-
Constructor Details
-
WebAppClassLoader
Constructor.The
Thread.currentThread()
Thread.getContextClassLoader()
will be used as the parent loader, unlessnull
, in which case the classloader that loaded this class is used as the parent.- Parameters:
context
- the context for this classloader
-
WebAppClassLoader
Constructor.- Parameters:
parent
- the parent classloader; ifnull
then theThread.currentThread()
Thread.getContextClassLoader()
will be used as the parent loader, unless alsonull
, in which case the classloader that loaded this class is used as the parent.context
- the context for this classloader
-
-
Method Details
-
runWithHiddenClassAccess
Run an action with access to Hidden ClassesRun the passed
PrivilegedExceptionAction
with the classloader configured so as to allow server classes to be visible- Type Parameters:
T
- the type of PrivilegedExceptionAction and the type returned by the action- Parameters:
action
- The action to run- Returns:
- The return from the action
- Throws:
Exception
- if thrown by the action
-
getName
Get the name of the classloader.- Overrides:
getName
in classClassLoader
- Returns:
- the name of the classloader
-
setName
Set the name of the classloader.- Parameters:
name
- the name of the classloader
-
getContext
-
addClassPath
- Parameters:
resource
- The resources to add to the classpath
-
addClassPath
- Parameters:
classPathList
- Comma or semicolon separated path of filenames or URLs pointing to directories or jar files. Directories should end with '/'.- Throws:
IOException
- if unable to add classpath
-
addJars
Add elements to the class path for the context from the jar and zip files found in the specified resource.- Parameters:
libs
- the directory resource that contains the jar and/or zip files.
-
getPermissions
- Overrides:
getPermissions
in classURLClassLoader
-
getResources
- Overrides:
getResources
in classClassLoader
- Throws:
IOException
-
getResource
Get a resource from the classloaderNOTE: 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 classClassLoader
-
loadClass
- Overrides:
loadClass
in classClassLoader
- Throws:
ClassNotFoundException
-
addTransformer
-
removeTransformer
-
loadAsResource
protected Class<?> loadAsResource(String name, boolean checkSystemResource) throws 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:
ClassNotFoundException
- if the class cannot be found
-
findClass
- Overrides:
findClass
in classURLClassLoader
- Throws:
ClassNotFoundException
-
foundClass
- Throws:
ClassNotFoundException
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classURLClassLoader
- Throws:
IOException
-
toString
-
isProtectedClass
Description copied from interface:ClassVisibilityChecker
Is the class a Protected (System) Class. A System class is a class that is visible to a web application, but that cannot be overridden by the contents of WEB-INF/lib or WEB-INF/classes- Specified by:
isProtectedClass
in interfaceClassVisibilityChecker
- Parameters:
clazz
- The fully qualified name of the class.- Returns:
- True if the class is a system class.
-
isHiddenClass
Description copied from interface:ClassVisibilityChecker
Is the class a Hidden (Server) Class. A Server class is a class that is part of the implementation of the server and is NOT visible to a web application. The web application may provide its own implementation of the class, to be loaded from WEB-INF/lib or WEB-INF/classes- Specified by:
isHiddenClass
in interfaceClassVisibilityChecker
- Parameters:
clazz
- The fully qualified name of the class.- Returns:
- True if the class is a server class.
-