Class WebAppClassLoader

All Implemented Interfaces:
Closeable, AutoCloseable, ClassVisibilityChecker
Direct Known Subclasses:
CachingWebAppClassLoader, OSGiWebappClassLoader

public class WebAppClassLoader extends 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 with WebAppContext.isHiddenClass(Class) and WebAppContext.isProtectedClass(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.

  • Constructor Details

    • WebAppClassLoader

      public WebAppClassLoader(WebAppClassLoader.Context context)
      Constructor.
      Parameters:
      context - the context for this classloader
    • WebAppClassLoader

      public WebAppClassLoader(ClassLoader parent, WebAppClassLoader.Context context)
      Constructor.
      Parameters:
      parent - the parent classloader
      context - the context for this classloader
  • Method Details

    • runWithServerClassAccess

      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

      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

      public String getName()
      Get the name of the classloader.
      Overrides:
      getName in class ClassLoader
      Returns:
      the name of the classloader
    • setName

      public void setName(String name)
      Set the name of the classloader.
      Parameters:
      name - the name of the classloader
    • getContext

      public WebAppClassLoader.Context getContext()
    • addClassPath

      public void addClassPath(Resource resource)
      Parameters:
      resource - The resources to add to the classpath
    • addClassPath

      public void addClassPath(String classPathList) throws IOException
      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

      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 - the directory resource that contains the jar and/or zip files.
    • getPermissions

      public PermissionCollection getPermissions(CodeSource cs)
      Overrides:
      getPermissions in class URLClassLoader
    • getResources

      public Enumeration<URL> getResources(String name) throws IOException
      Overrides:
      getResources in class ClassLoader
      Throws:
      IOException
    • getResource

      public URL getResource(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 class ClassLoader
    • loadClass

      protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException
      Overrides:
      loadClass in class ClassLoader
      Throws:
      ClassNotFoundException
    • addTransformer

      public void addTransformer(ClassFileTransformer transformer)
    • removeTransformer

      public boolean removeTransformer(ClassFileTransformer transformer)
    • 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 load
      checkSystemResource - 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

      protected Class<?> findClass(String name) throws ClassNotFoundException
      Overrides:
      findClass in class URLClassLoader
      Throws:
      ClassNotFoundException
    • foundClass

      protected Class<?> foundClass(String name, URL url) throws ClassNotFoundException
      Throws:
      ClassNotFoundException
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class URLClassLoader
      Throws:
      IOException
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • isProtectedClass

      public boolean isProtectedClass(Class<?> clazz)
      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 interface ClassVisibilityChecker
      Parameters:
      clazz - The fully qualified name of the class.
      Returns:
      True if the class is a system class.
    • isHiddenClass

      public boolean isHiddenClass(Class<?> clazz)
      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 interface ClassVisibilityChecker
      Parameters:
      clazz - The fully qualified name of the class.
      Returns:
      True if the class is a server class.