Class WebAppClassLoader

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable
    Direct Known Subclasses:
    CachingWebAppClassLoader

    public class WebAppClassLoader
    extends java.net.URLClassLoader
    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(String) and WebAppContext.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.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      void addClassFileTransformer​(java.lang.instrument.ClassFileTransformer transformer)
      void addClassPath​(java.lang.String classPath)  
      void addClassPath​(org.eclipse.jetty.util.resource.Resource resource)  
      void addJars​(org.eclipse.jetty.util.resource.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)  
      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)
      boolean removeTransformer​(java.lang.instrument.ClassFileTransformer transformer)  
      static <T> T runWithServerClassAccess​(java.security.PrivilegedExceptionAction<T> action)
      Run an action with access to ServerClasses
      void 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.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
    • 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 classloader
        context - 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 ServerClasses

        Run 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 class java.lang.ClassLoader
        Returns:
        the name of the classloader
      • setName

        public void setName​(java.lang.String name)
        Parameters:
        name - the name of the classloader
      • addClassPath

        public void addClassPath​(org.eclipse.jetty.util.resource.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​(org.eclipse.jetty.util.resource.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 class java.net.URLClassLoader
      • getResources

        public java.util.Enumeration<java.net.URL> getResources​(java.lang.String name)
                                                         throws java.io.IOException
        Overrides:
        getResources in class java.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 class java.lang.ClassLoader
      • loadClass

        protected java.lang.Class<?> loadClass​(java.lang.String name,
                                               boolean resolve)
                                        throws java.lang.ClassNotFoundException
        Overrides:
        loadClass in class java.lang.ClassLoader
        Throws:
        java.lang.ClassNotFoundException
      • addClassFileTransformer

        @Deprecated
        public void addClassFileTransformer​(java.lang.instrument.ClassFileTransformer transformer)
        Parameters:
        transformer - the transformer to add
      • removeClassFileTransformer

        @Deprecated
        public boolean removeClassFileTransformer​(java.lang.instrument.ClassFileTransformer transformer)
        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 load
        checkSystemResource - if true and the class isn't a system class we return it
        Returns:
        the loaded class
        Throws:
        java.lang.ClassNotFoundException
      • findClass

        protected java.lang.Class<?> findClass​(java.lang.String name)
                                        throws java.lang.ClassNotFoundException
        Overrides:
        findClass in class java.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 interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.net.URLClassLoader
        Throws:
        java.io.IOException
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object