Interface ThreadContextClassLoaderHolder


  • public interface ThreadContextClassLoaderHolder

    Specification for how to restore the original ThreadContext ClassLoader to a Thread. When we support JDK 1.7, this should really be an extension of AutoCloseable instead, to support the try-with-resources pattern. 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

      • restoreClassLoaderAndReleaseThread

        void restoreClassLoaderAndReleaseThread()
        Restores the original ThreadContext ClassLoader, and nullifies any references to the Thread which had its ThreadContext ClassLoader altered.
      • getClassPathAsArgument

        String getClassPathAsArgument()
        Retrieves the ClassPath held by this ThreadContextClassLoaderHolder as a File.pathSeparatorChar-separated string. This is directly usable as a String argument by any external process.
        Returns:
        the ClassPath as an argument to external processes such as XJC.