Class ThreadContextClassLoaderBuilder
java.lang.Object
org.codehaus.mojo.jaxb2.shared.environment.classloading.ThreadContextClassLoaderBuilder
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) class
Default implementation of the ThreadContextClassLoaderCleaner specification, with added finalizer to ensure we release the Thread reference no matter what happens with any DefaultCleaner objects.(package private) static enum
Simple enumeration of supported classpath URL protocols. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate String
private org.apache.maven.plugin.logging.Log
private ClassLoader
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
ThreadContextClassLoaderBuilder
(ClassLoader classLoader, org.apache.maven.plugin.logging.Log aLog, String encoding) -
Method Summary
Modifier and TypeMethodDescriptionConverts the supplied path to an URL and adds it to this ThreadContextClassLoaderBuilder.Converts the supplied path to an URL and adds it to this ThreadContextClassLoaderBuilder.private URL
Adds the supplied anURL to the list of internal URLs which should be used to build an URLClassLoader.This method performs 2 things in order:Creates a new ThreadContextClassLoaderBuilder using the original ClassLoader from the supplied Class, as well as the given Maven Log.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.static String
getClassPathElement
(URL anURL, String encoding) Converts the supplied URL to a class path element.
-
Field Details
-
originalClassLoader
-
urlList
-
log
private org.apache.maven.plugin.logging.Log log -
encoding
-
-
Constructor Details
-
ThreadContextClassLoaderBuilder
private ThreadContextClassLoaderBuilder(ClassLoader classLoader, org.apache.maven.plugin.logging.Log aLog, String encoding)
-
-
Method Details
-
addURL
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
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:
-
addPaths
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:
-
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.
- Stores the ThreadContextClassLoaderHolder for later restoration.
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.
-
addSlashToDirectoryUrlIfRequired
-