Class ThreadContextClassLoaderBuilder


  • public final class ThreadContextClassLoaderBuilder
    extends 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
    Author:
    Lennart Jörelid, jGuru Europe AB
    • Method Detail

      • addURL

        public ThreadContextClassLoaderBuilder addURL​(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​(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​(List<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​(ClassLoader classLoader,
                                                                org.apache.maven.plugin.logging.Log log,
                                                                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​(Class<?> aClass,
                                                                org.apache.maven.plugin.logging.Log log,
                                                                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 String getClassPathElement​(URL anURL,
                                                 String encoding)
                                          throws 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:
        IllegalArgumentException - if the supplied URL had an unknown protocol.