Class ThreadContextClassLoaderBuilder


  • public final class ThreadContextClassLoaderBuilder
    extends java.lang.Object

    Utility class which assists in synthesizing a URLClassLoader for use as a ThreadLocal ClassLoader. Typical use:

         
             // Create and set the ThreadContext ClassLoader
             ThreadContextClassLoaderHolder holder = null;
    
             try {
    
              holder = ThreadContextClassLoaderBuilder.createFor(getClass())
                  .addPath("some/path")
                  .addURL(someURL)
                  .addPaths(aPathList)
                  .buildAndSet();
    
              // ... perform operations using the newly set ThreadContext ClassLoader...
    
             } finally {
              // Restore the original ClassLoader
              holder.restoreClassLoaderAndReleaseThread();
             }
         
     
    Since:
    2.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.String encoding  
      private org.apache.maven.plugin.logging.Log log  
      private java.lang.ClassLoader originalClassLoader  
      private java.util.List<java.net.URL> urlList  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ThreadContextClassLoaderBuilder​(java.lang.ClassLoader classLoader, org.apache.maven.plugin.logging.Log aLog, java.lang.String encoding)  
    • Field Detail

      • originalClassLoader

        private java.lang.ClassLoader originalClassLoader
      • urlList

        private java.util.List<java.net.URL> urlList
      • log

        private org.apache.maven.plugin.logging.Log log
      • encoding

        private java.lang.String encoding
    • Constructor Detail

      • ThreadContextClassLoaderBuilder

        private ThreadContextClassLoaderBuilder​(java.lang.ClassLoader classLoader,
                                                org.apache.maven.plugin.logging.Log aLog,
                                                java.lang.String encoding)
    • Method Detail

      • addURL

        public ThreadContextClassLoaderBuilder addURL​(java.net.URL anURL)
        Adds the supplied anURL to the list of internal URLs which should be used to build an URLClassLoader. Will only add an URL once, and warns about trying to re-add an URL.
        Parameters:
        anURL - The URL to add.
        Returns:
        This ThreadContextClassLoaderBuilder, for builder pattern chaining.
      • addPath

        public ThreadContextClassLoaderBuilder addPath​(java.lang.String path)
        Converts the supplied path to an URL and adds it to this ThreadContextClassLoaderBuilder.
        Parameters:
        path - A path to convert to an URL and add.
        Returns:
        This ThreadContextClassLoaderBuilder, for builder pattern chaining.
        See Also:
        addURL(java.net.URL)
      • addPaths

        public ThreadContextClassLoaderBuilder addPaths​(java.util.List<java.lang.String> paths)
        Converts the supplied path to an URL and adds it to this ThreadContextClassLoaderBuilder.
        Parameters:
        paths - A List of path to convert to URLs and add.
        Returns:
        This ThreadContextClassLoaderBuilder, for builder pattern chaining.
        See Also:
        addPath(String)
      • buildAndSet

        public ThreadContextClassLoaderHolder buildAndSet()

        This method performs 2 things in order:

        1. Builds a ThreadContext ClassLoader from the URLs supplied to this Builder, and assigns the newly built ClassLoader to the current Thread.
        2. Stores the ThreadContextClassLoaderHolder for later restoration.
        References to the original ThreadContextClassLoader and the currentThread are stored within the returned ThreadContextClassLoaderHolder, and can be restored by a call to ThreadContextClassLoaderHolder.restoreClassLoaderAndReleaseThread().
        Returns:
        A fully set up ThreadContextClassLoaderHolder which is used to set the
      • createFor

        public static ThreadContextClassLoaderBuilder createFor​(java.lang.ClassLoader classLoader,
                                                                org.apache.maven.plugin.logging.Log log,
                                                                java.lang.String encoding)
        Creates a new ThreadContextClassLoaderBuilder using the supplied original classLoader, as well as the supplied Maven Log.
        Parameters:
        classLoader - The original ClassLoader which should be used as the parent for the ThreadContext ClassLoader produced by the ThreadContextClassLoaderBuilder generated by this builder method. Cannot be null.
        log - The active Maven Log. Cannot be null.
        encoding - The encoding used by Maven. Cannot be null.
        Returns:
        A ThreadContextClassLoaderBuilder wrapping the supplied members.
      • createFor

        public static ThreadContextClassLoaderBuilder createFor​(java.lang.Class<?> aClass,
                                                                org.apache.maven.plugin.logging.Log log,
                                                                java.lang.String encoding)
        Creates a new ThreadContextClassLoaderBuilder using the original ClassLoader from the supplied Class, as well as the given Maven Log.
        Parameters:
        aClass - A non-null class from which to extract the original ClassLoader.
        log - The active Maven Log. Cannot be null.
        encoding - The encoding used by Maven. Cannot be null.
        Returns:
        A ThreadContextClassLoaderBuilder wrapping the supplied members.
      • getClassPathElement

        public static java.lang.String getClassPathElement​(java.net.URL anURL,
                                                           java.lang.String encoding)
                                                    throws java.lang.IllegalArgumentException
        Converts the supplied URL to a class path element.
        Parameters:
        anURL - The non-null URL for which to acquire a classPath element.
        encoding - The encoding used by Maven.
        Returns:
        The full (i.e. non-chopped) classpath element corresponding to the supplied URL.
        Throws:
        java.lang.IllegalArgumentException - if the supplied URL had an unknown protocol.
      • addSlashToDirectoryUrlIfRequired

        private java.net.URL addSlashToDirectoryUrlIfRequired​(java.net.URL anURL)