public 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();
}
Modifier and Type | Method and Description |
---|---|
ThreadContextClassLoaderBuilder |
addPath(String path)
Converts the supplied path to an URL and adds it to this ThreadContextClassLoaderBuilder.
|
ThreadContextClassLoaderBuilder |
addPaths(List<String> paths)
Converts the supplied path to an URL and adds it to this ThreadContextClassLoaderBuilder.
|
ThreadContextClassLoaderBuilder |
addURL(URL anURL)
Adds the supplied anURL to the list of internal URLs which should be used to build an URLClassLoader.
|
ThreadContextClassLoaderHolder |
buildAndSet()
This method performs 2 things in order:
Builds a ThreadContext ClassLoader from the URLs supplied to this Builder, and assigns the
newly built ClassLoader to the current Thread.
|
static ThreadContextClassLoaderBuilder |
createFor(Class<?> aClass,
org.apache.maven.plugin.logging.Log log)
Creates a new ThreadContextClassLoaderBuilder using the original ClassLoader from the supplied Class, as well
as the given Maven Log.
|
static ThreadContextClassLoaderBuilder |
createFor(ClassLoader classLoader,
org.apache.maven.plugin.logging.Log log)
Creates a new ThreadContextClassLoaderBuilder using the supplied original classLoader, as well
as the supplied Maven Log.
|
static String |
getClassPathElement(URL anURL)
Converts the supplied URL to a class path element.
|
public ThreadContextClassLoaderBuilder addURL(URL anURL)
anURL
- The URL to add.public ThreadContextClassLoaderBuilder addPath(String path)
path
- A path to convert to an URL and add.addURL(java.net.URL)
public ThreadContextClassLoaderBuilder addPaths(List<String> paths)
paths
- A List of path to convert to URLs and add.addPath(String)
public ThreadContextClassLoaderHolder buildAndSet()
This method performs 2 things in order:
ThreadContextClassLoaderHolder.restoreClassLoaderAndReleaseThread()
.public static ThreadContextClassLoaderBuilder createFor(ClassLoader classLoader, org.apache.maven.plugin.logging.Log log)
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.public static ThreadContextClassLoaderBuilder createFor(Class<?> aClass, org.apache.maven.plugin.logging.Log log)
aClass
- A non-null class from which to extract the original ClassLoader.log
- The active Maven Log. Cannot be null.public static String getClassPathElement(URL anURL) throws IllegalArgumentException
anURL
- The non-null URL for which to acquire a classPath element.IllegalArgumentException
- if the supplied URL had an unknown protocol.Copyright © 2005–2015 MojoHaus. All rights reserved.