Top Description Inners Fields Constructors Methods
org.eclipse.jetty.ee10.webapp

public Class WebAppClassLoader

extends URLClassLoader
implements ClassVisibilityChecker
Class Inheritance
All Implemented Interfaces
org.eclipse.jetty.util.ClassVisibilityChecker
Known Direct Subclasses
org.eclipse.jetty.ee10.webapp.CachingWebAppClassLoader
Imports
java.io.IOException, .InputStream, java.lang.instrument.ClassFileTransformer, .IllegalClassFormatException, java.net.MalformedURLException, .URL, .URLClassLoader, java.nio.file.Files, .Path, java.security.CodeSource, .PermissionCollection, .PrivilegedExceptionAction, java.util.ArrayList, .Collections, .Comparator, .Enumeration, .HashSet, .List, .Set, .StringTokenizer, java.util.concurrent.CopyOnWriteArrayList, java.util.stream.Stream, org.eclipse.jetty.util.ClassVisibilityChecker, .FileID, .IO, .StringUtil, .TypeUtil, .URIUtil, org.eclipse.jetty.util.resource.Resource, .ResourceFactory, .Resources, org.slf4j.Logger, .LoggerFactory

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 with WebAppContext#isServerClass(Class) and WebAppContext#isSystemClass(Class).

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 and Inner Type Summary

Modifier and TypeClass and Description
public static interface
WebAppClassLoader.Context

The Context in which the classloader operates.

Field Summary

Modifier and TypeField and Description
private static final ThreadLocal<Boolean>
private final WebAppClassLoader.Context
private final Set<String>
private String
private final ClassLoader
private final ResourceFactory.Closeable
private final List<ClassFileTransformer>
private static final Logger

Constructor Summary

AccessConstructor and Description
public
WebAppClassLoader(WebAppClassLoader.Context
the context for this classloader
context
)

Constructor.

public
WebAppClassLoader(ClassLoader
the parent classloader
parent
,
WebAppClassLoader.Context
the context for this classloader
context
)

Constructor.

Method Summary

Modifier and TypeMethod and Description
public void
addClassPath(Resource
The resources to add to the classpath
resource
)

public void
addClassPath(String
Comma or semicolon separated path of filenames or URLs pointing to directories or jar files. Directories should end with '/'.
classPathList
)

public void
addJars(Resource
the directory resource that contains the jar and/or zip files.
libs
)

Add elements to the class path for the context from the jar and zip files found in the specified resource.

public void
public void
close()

Overrides java.net.URLClassLoader.close.

Implements java.io.Closeable.close.

Closes this URLClassLoader, so that it can no longer be used to load new classes or resources that are defined by this loader.
protected Class<?>
findClass(final String
the name of the class
name
)

Overrides java.net.URLClassLoader.findClass.

Finds and loads the class with the specified name from the URL search path.
protected Class<?>
foundClass(final String name, URL url)

public WebAppClassLoader.Context
public String

Returns:

the name of the classloader
getName
()

Overrides java.lang.ClassLoader.getName.

Get the name of the classloader.
public PermissionCollection
getPermissions(CodeSource
the codesource
cs
)

Overrides java.net.URLClassLoader.getPermissions.

Returns the permissions for the given codesource object.
public URL
getResource(String
The resource name
name
)

Overrides java.lang.ClassLoader.getResource.

Get a resource from the classloader NOTE: this method provides a convenience of hacking off a leading / should one be present.
public Enumeration<URL>
getResources(String
The resource name
name
)

Overrides java.lang.ClassLoader.getResources.

Finds all the resources with the given name.
private boolean
isFileSupported(String
Checks if this file type can be added to the classpath.
file
)

private boolean
public boolean
isServerClass(Class<?>
The fully qualified name of the class.
clazz
)

Implements org.eclipse.jetty.util.ClassVisibilityChecker.isServerClass.

Is the class a Server Class.
public boolean
isSystemClass(Class<?>
The fully qualified name of the class.
clazz
)

Implements org.eclipse.jetty.util.ClassVisibilityChecker.isSystemClass.

Is the class a System Class.
protected Class<?>

Returns:

the loaded class
loadAsResource
(final String
the name of the class to load
name
,
boolean
if true and the class isn't a system class we return it
checkSystemResource
)

Look for the classname as a resource to avoid loading a class that is potentially a system resource.

protected Class<?>
loadClass(String
The binary name of the class
name
,
boolean
If true then resolve the class
resolve
)

Overrides java.lang.ClassLoader.loadClass.

Loads the class with the specified binary name.
public boolean
public static <
The type returned by the action
T
>
T

Returns:

The return from the action
runWithServerClassAccess
(PrivilegedExceptionAction<T>
The action to run
action
)

Run an action with access to ServerClasses

Run the passed PrivilegedExceptionAction with the classloader configured so as to allow server classes to be visible

public void
setName(String
the name of the classloader
name
)

Set the name of the classloader.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.net.URLClassLoader:
addURLdefinePackagefindResourcefindResourcesgetResourceAsStreamgetURLsnewInstancenewInstance

Field Detail

__loadServerClassesback to summary
private static final ThreadLocal<Boolean> __loadServerClasses
_contextback to summary
private final WebAppClassLoader.Context _context
_extensionsback to summary
private final Set<String> _extensions
_nameback to summary
private String _name
_parentback to summary
private final ClassLoader _parent
_resourceFactoryback to summary
private final ResourceFactory.Closeable _resourceFactory
_transformersback to summary
private final List<ClassFileTransformer> _transformers
LOGback to summary
private static final Logger LOG

Constructor Detail

WebAppClassLoaderback to summary
public WebAppClassLoader(WebAppClassLoader.Context context)

Constructor.

Parameters
context:WebAppClassLoader.Context

the context for this classloader

WebAppClassLoaderback to summary
public WebAppClassLoader(ClassLoader parent, WebAppClassLoader.Context context)

Constructor.

Parameters
parent:ClassLoader

the parent classloader

context:WebAppClassLoader.Context

the context for this classloader

Method Detail

addClassPathback to summary
public void addClassPath(Resource resource)
Parameters
resource:Resource

The resources to add to the classpath

addClassPathback to summary
public void addClassPath(String classPathList) throws IOException
Parameters
classPathList:String

Comma or semicolon separated path of filenames or URLs pointing to directories or jar files. Directories should end with '/'.

Exceptions
IOException:
if unable to add classpath
addJarsback to summary
public void addJars(Resource libs)

Add elements to the class path for the context from the jar and zip files found in the specified resource.

Parameters
libs:Resource

the directory resource that contains the jar and/or zip files.

addTransformerback to summary
public void addTransformer(ClassFileTransformer transformer)
closeback to summary
public void close() throws IOException

Overrides java.net.URLClassLoader.close.

Implements java.io.Closeable.close.

Doc from java.net.URLClassLoader.close.

Closes this URLClassLoader, so that it can no longer be used to load new classes or resources that are defined by this loader. Classes and resources defined by any of this loader's parents in the delegation hierarchy are still accessible. Also, any classes or resources that are already loaded, are still accessible.

In the case of jar: and file: URLs, it also closes any files that were opened by it. If another thread is loading a class when the close method is invoked, then the result of that load is undefined.

The method makes a best effort attempt to close all opened files, by catching IOExceptions internally. Unchecked exceptions and errors are not caught. Calling close on an already closed loader has no effect.

Annotations
@Override
Exceptions
IOException:
if closing any file opened by this class loader resulted in an IOException. Any such exceptions are caught internally. If only one is caught, then it is re-thrown. If more than one exception is caught, then the second and following exceptions are added as suppressed exceptions of the first one caught, which is then re-thrown.
findClassback to summary
protected Class<?> findClass(final String name) throws ClassNotFoundException

Overrides java.net.URLClassLoader.findClass.

Doc from java.net.URLClassLoader.findClass.

Finds and loads the class with the specified name from the URL search path. Any URLs referring to JAR files are loaded and opened as needed until the class is found.

Parameters
name:String

the name of the class

Returns:Class<?>

the resulting class

Annotations
@Override
Exceptions
ClassNotFoundException:
if the class could not be found, or if the loader is closed.
foundClassback to summary
protected Class<?> foundClass(final String name, URL url) throws ClassNotFoundException
getContextback to summary
public WebAppClassLoader.Context getContext()
getNameback to summary
public String getName()

Overrides java.lang.ClassLoader.getName.

Get the name of the classloader.

Returns:String

the name of the classloader

getPermissionsback to summary
public PermissionCollection getPermissions(CodeSource cs)

Overrides java.net.URLClassLoader.getPermissions.

Doc from java.net.URLClassLoader.getPermissions.

Returns the permissions for the given codesource object. The implementation of this method first calls super.getPermissions and then adds permissions based on the URL of the codesource.

If the protocol of this URL is "jar", then the permission granted is based on the permission that is required by the URL of the Jar file.

If the protocol is "file" and there is an authority component, then permission to connect to and accept connections from that authority may be granted. If the protocol is "file" and the path specifies a file, then permission to read that file is granted. If protocol is "file" and the path is a directory, permission is granted to read all files and (recursively) all files and subdirectories contained in that directory.

If the protocol is not "file", then permission to connect to and accept connections from the URL's host is granted.

Parameters
cs:CodeSource

the codesource

Returns:PermissionCollection

the permissions granted to the codesource

Annotations
@Override
getResourceback to summary
public URL getResource(String name)

Overrides java.lang.ClassLoader.getResource.

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

Parameters
name:String

Doc from java.lang.ClassLoader.getResource.

The resource name

Returns:URL

Doc from java.lang.ClassLoader.getResource.

URL object for reading the resource; null if the resource could not be found, a URL could not be constructed to locate the resource, the resource is in a package that is not opened unconditionally, or access to the resource is denied by the security manager.

Annotations
@Override
getResourcesback to summary
public Enumeration<URL> getResources(String name) throws IOException

Overrides java.lang.ClassLoader.getResources.

Doc from java.lang.ClassLoader.getResources.

Finds all the resources with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code.

The name of a resource is a /-separated path name that identifies the resource.

Resources in named modules are subject to the encapsulation rules specified by Module.getResourceAsStream. Additionally, and except for the special case where the resource has a name ending with ".class", this method will only find resources in packages of named modules when the package is opened unconditionally (even if the caller of this method is in the same module as the resource).

Parameters
name:String

The resource name

Returns:Enumeration<URL>

An enumeration of URL objects for the resource. If no resources could be found, the enumeration will be empty. Resources for which a URL cannot be constructed, are in a package that is not opened unconditionally, or access to the resource is denied by the security manager, are not returned in the enumeration.

Annotations
@Override
Exceptions
IOException:
If I/O errors occur
isFileSupportedback to summary
private boolean isFileSupported(String file)
Parameters
file:String

Checks if this file type can be added to the classpath.

isFileSupportedback to summary
private boolean isFileSupported(Path path)
isServerClassback to summary
public boolean isServerClass(Class<?> clazz)

Implements org.eclipse.jetty.util.ClassVisibilityChecker.isServerClass.

Doc from org.eclipse.jetty.util.ClassVisibilityChecker.isServerClass.

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

Parameters
clazz:Class<?>

The fully qualified name of the class.

Returns:boolean

True if the class is a server class.

Annotations
@Override
isSystemClassback to summary
public boolean isSystemClass(Class<?> clazz)

Implements org.eclipse.jetty.util.ClassVisibilityChecker.isSystemClass.

Doc from org.eclipse.jetty.util.ClassVisibilityChecker.isSystemClass.

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

Parameters
clazz:Class<?>

The fully qualified name of the class.

Returns:boolean

True if the class is a system class.

Annotations
@Override
loadAsResourceback to summary
protected Class<?> loadAsResource(final 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:String

the name of the class to load

checkSystemResource:boolean

if true and the class isn't a system class we return it

Returns:Class<?>

the loaded class

Exceptions
ClassNotFoundException:
if the class cannot be found
loadClassback to summary
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException

Overrides java.lang.ClassLoader.loadClass.

Doc from java.lang.ClassLoader.loadClass.

Loads the class with the specified binary name. The default implementation of this method searches for classes in the following order:

  1. Invoke findLoadedClass(String) to check if the class has already been loaded.

  2. Invoke the loadClass method on the parent class loader. If the parent is null the class loader built into the virtual machine is used, instead.

  3. Invoke the findClass(String) method to find the class.

If the class was found using the above steps, and the resolve flag is true, this method will then invoke the resolveClass(Class) method on the resulting Class object.

Subclasses of ClassLoader are encouraged to override findClass(String), rather than this method.

Unless overridden, this method synchronizes on the result of getClassLoadingLock method during the entire class loading process.

Parameters
name:String

The binary name of the class

resolve:boolean

If true then resolve the class

Returns:Class<?>

The resulting Class object

Annotations
@Override
Exceptions
ClassNotFoundException:
If the class could not be found
removeTransformerback to summary
public boolean removeTransformer(ClassFileTransformer transformer)
runWithServerClassAccessback to summary
public static <T> T runWithServerClassAccess(PrivilegedExceptionAction<T> action) throws Exception

Run an action with access to ServerClasses

Run the passed PrivilegedExceptionAction with the classloader configured so as to allow server classes to be visible

Parameters
<T>
  • The type returned by the action
  • the type of PrivilegedExceptionAction
action:PrivilegedExceptionAction<T>

The action to run

Returns:T

The return from the action

Exceptions
Exception:
if thrown by the action
setNameback to summary
public void setName(String name)

Set the name of the classloader.

Parameters
name:String

the name of the classloader

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
org.eclipse.jetty.ee10.webapp back to summary

public Interface WebAppClassLoader.Context

extends ClassVisibilityChecker
Known Direct Implementers
org.eclipse.jetty.ee10.webapp.WebAppContext

The Context in which the classloader operates.

Method Summary

Modifier and TypeMethod and Description
public List<Resource>
public PermissionCollection

Returns:

Returns the permissions.
getPermissions
()

public boolean

Returns:

True if the classloader should delegate first to the parent classloader (standard java behaviour) or false if the classloader should first try to load from WEB-INF/lib or WEB-INF/classes (servlet spec recommendation).
isParentLoaderPriority
()

public boolean
isServerResource(String name, URL parentUrl)

public boolean
isSystemResource(String name, URL webappUrl)

public Resource

Returns:

The Resource for the URL/path
newResource
(String
The URL or path to convert
urlOrPath
)

Convert a URL or path to a Resource.

Inherited from org.eclipse.jetty.util.ClassVisibilityChecker:
isServerClassisSystemClass

Method Detail

getExtraClasspathback to summary
public List<Resource> getExtraClasspath()
getPermissionsback to summary
public PermissionCollection getPermissions()
Returns:PermissionCollection

Returns the permissions.

isParentLoaderPriorityback to summary
public boolean isParentLoaderPriority()
Returns:boolean

True if the classloader should delegate first to the parent classloader (standard java behaviour) or false if the classloader should first try to load from WEB-INF/lib or WEB-INF/classes (servlet spec recommendation).

isServerResourceback to summary
public boolean isServerResource(String name, URL parentUrl)
isSystemResourceback to summary
public boolean isSystemResource(String name, URL webappUrl)
newResourceback to summary
public Resource newResource(String urlOrPath) throws IOException

Convert a URL or path to a Resource. The default implementation is a wrapper for ResourceFactory#newResource(String).

Parameters
urlOrPath:String

The URL or path to convert

Returns:Resource

The Resource for the URL/path

Exceptions
IOException:
The Resource could not be created.