public class ClassLoaderLeakPreventor extends Object
| Modifier and Type | Field and Description |
|---|---|
static int |
THREAD_WAIT_MS_DEFAULT
Default no of milliseconds to wait for threads to finish execution
|
| Constructor and Description |
|---|
ClassLoaderLeakPreventor(ClassLoader leakSafeClassLoader,
ClassLoader classLoader,
Logger logger,
Collection<PreClassLoaderInitiator> preClassLoaderInitiators,
Collection<ClassLoaderPreMortemCleanUp> cleanUps) |
| Modifier and Type | Method and Description |
|---|---|
AccessControlContext |
createAccessControlContext()
Create
AccessControlContext that is used in doInLeakSafeClassLoader(Runnable). |
void |
debug(String msg) |
protected void |
doInLeakSafeClassLoader(Runnable runnable)
Perform action in the provided ClassLoader (normally system ClassLoader, that may retain references to the
Thread.contextClassLoader. |
void |
error(String msg) |
void |
error(Throwable t) |
Class<?> |
findClass(String className) |
Class<?> |
findClass(String className,
boolean trySystemCL) |
Field |
findField(Class<?> clazz,
String fieldName) |
Field |
findFieldOfClass(String className,
String fieldName) |
Field |
findFieldOfClass(String className,
String fieldName,
boolean trySystemCL) |
Method |
findMethod(Class<?> clazz,
String methodName,
Class... parameterTypes) |
Method |
findMethod(String className,
String methodName,
Class... parameterTypes) |
static void |
gc()
Unlike
this method guarantees that garbage collection has been performed before
returning. |
Collection<Thread> |
getAllThreads()
Get a Collection with all Threads.
|
ClassLoader |
getClassLoader() |
<T> T |
getFieldValue(Field field,
Object obj) |
<T> T |
getFieldValue(Object obj,
String fieldName) |
ClassLoader |
getLeakSafeClassLoader()
Get
ClassLoader to be used when invoking the PreClassLoaderInitiators. |
String |
getStackTrace(Thread thread)
Get current stack trace or provided thread as string.
|
<E> E |
getStaticFieldValue(Class<?> clazz,
String fieldName) |
<T> T |
getStaticFieldValue(Field field) |
<E> E |
getStaticFieldValue(String className,
String fieldName) |
<E> E |
getStaticFieldValue(String className,
String fieldName,
boolean trySystemCL) |
void |
info(String msg) |
boolean |
isClassLoaderOrChild(ClassLoader cl)
Test if provided ClassLoader is the
classLoader, or a child thereof |
boolean |
isJBoss()
Override this method if you want to customize how we determine if we're running in
JBoss WildFly (a.k.a JBoss AS).
|
boolean |
isJvmShuttingDown()
Is the JVM currently shutting down?
|
boolean |
isLoadedByClassLoader(Class<?> clazz)
Test if provided class is loaded wby
classLoader |
boolean |
isLoadedInClassLoader(Object o)
Test if provided object is loaded by
classLoader |
boolean |
isOracleJRE()
Are we running in the Oracle/Sun Java Runtime Environment?
|
boolean |
isThreadInClassLoader(Thread thread)
Is the
Thread ties do the protected classloader, either by being a custom Thread class, having a
custom ThreadGroup or having the protected classloader as its Thread.contextClassLoader? |
void |
removeDomainCombiner(String owner,
AccessControlContext accessControlContext)
Recursively unset our custom
DomainCombiner (loaded in the web app) from the AccessControlContext
and any parents or privilegedContext thereof. |
void |
removeDomainCombiner(Thread thread,
AccessControlContext accessControlContext)
Deprecated.
|
void |
runCleanUps()
Invoke all the registered
ClassLoaderPreMortemCleanUps |
void |
runPreClassLoaderInitiators()
Invoke all the registered
PreClassLoaderInitiators in the leakSafeClassLoader |
void |
setFinalStaticField(Field field,
Object newValue) |
void |
waitForThread(Thread thread,
long waitMs,
boolean interrupt)
Make the provided Thread stop sleep(), wait() or join() and then give it the provided no of milliseconds to finish
executing.
|
void |
warn(String msg) |
void |
warn(Throwable t) |
public static final int THREAD_WAIT_MS_DEFAULT
public ClassLoaderLeakPreventor(ClassLoader leakSafeClassLoader, ClassLoader classLoader, Logger logger, Collection<PreClassLoaderInitiator> preClassLoaderInitiators, Collection<ClassLoaderPreMortemCleanUp> cleanUps)
public void runPreClassLoaderInitiators()
PreClassLoaderInitiators in the leakSafeClassLoaderprotected void doInLeakSafeClassLoader(Runnable runnable)
Thread.contextClassLoader.
The motive for the custom AccessControlContext is to avoid spawned threads from inheriting all the
ProtectionDomains of the running code, since that may include the classloader we want to
avoid leaking. This however means the AccessControlContext will have a DomainCombiner referencing the
classloader, which will be taken care of in runCleanUps().public AccessControlContext createAccessControlContext()
AccessControlContext that is used in doInLeakSafeClassLoader(Runnable).
The motive is to avoid spawned threads from inheriting all the ProtectionDomains of the
running code, since that will include the web app classloader.@Deprecated public void removeDomainCombiner(Thread thread, AccessControlContext accessControlContext)
DomainCombiner (loaded in the web app) from the AccessControlContext
and any parents or privilegedContext thereof.public void removeDomainCombiner(String owner, AccessControlContext accessControlContext)
DomainCombiner (loaded in the web app) from the AccessControlContext
and any parents or privilegedContext thereof.public void runCleanUps()
ClassLoaderPreMortemCleanUpspublic ClassLoader getClassLoader()
public ClassLoader getLeakSafeClassLoader()
ClassLoader to be used when invoking the PreClassLoaderInitiators.
This will normally be the ClassLoader.getSystemClassLoader(), but could be any other framework or
app server classloader. Normally, but not necessarily, a parent of classLoader.public boolean isLoadedInClassLoader(Object o)
classLoaderpublic boolean isLoadedByClassLoader(Class<?> clazz)
classLoaderpublic boolean isClassLoaderOrChild(ClassLoader cl)
classLoader, or a child thereofpublic boolean isThreadInClassLoader(Thread thread)
Thread ties do the protected classloader, either by being a custom Thread class, having a
custom ThreadGroup or having the protected classloader as its Thread.contextClassLoader?public void waitForThread(Thread thread, long waitMs, boolean interrupt)
thread - The thread to wake up and wait forwaitMs - The no of milliseconds to wait. If <= 0 this method does nothing.interrupt - Should Thread.interrupt() be called first, to make thread stop sleep(), wait() or join()?public String getStackTrace(Thread thread)
"unavailable" if stack trace could not be acquired.public <E> E getStaticFieldValue(String className, String fieldName, boolean trySystemCL)
public Field findFieldOfClass(String className, String fieldName, boolean trySystemCL)
public <T> T getStaticFieldValue(Field field)
public Collection<Thread> getAllThreads()
public boolean isJBoss()
public boolean isOracleJRE()
public static void gc()
System.gc() this method guarantees that garbage collection has been performed before
returning.public boolean isJvmShuttingDown()
public void debug(String msg)
public void warn(Throwable t)
public void error(Throwable t)
public void warn(String msg)
public void error(String msg)
public void info(String msg)
Copyright © 2019. All rights reserved.