Methods Summary |
---|
public static java.lang.ClassLoader | createClassLoader(java.lang.String classpath, java.lang.ClassLoader parent)Creates a new ClassLoader from a classpath specification and a parent
class loader.
The classpath string will be split using the system path seperator
character (e.g. : or ;), just as the java system-wide class path is
processed.
String[] names = StringUtils.split(classpath, System.getProperty("path.separator").charAt(0));
URL[] urls = new URL[names.length];
try {
for(int i = 0; i < urls.length; i++)
urls[i] = new File(names[i]).toURL();
}
catch (MalformedURLException e) {
// I don't think this is possible, so I'm throwing this as an
// un-checked exception
throw (IllegalArgumentException) new IllegalArgumentException(
"Unable to parse classpath: " + classpath);
}
return new URLClassLoader(urls, parent);
|
public static java.lang.Class | forName(java.lang.String className)Use this method instead of Class.forName
return loadClass(className);
|
public static java.lang.Class | forName(java.lang.String _className, boolean init, java.lang.ClassLoader _loader)Use this method instead of Class.forName (String className, boolean init, ClassLoader loader)
// Create final vars for doPrivileged block
final String className = _className;
final ClassLoader loader = _loader;
try {
// Get the class within a doPrivleged block
Object ret =
AccessController.doPrivileged(
new PrivilegedAction() {
public Object run() {
try {
return Class.forName(className, true, loader);
} catch (Throwable e) {
return e;
}
}
});
// If the class was located, return it. Otherwise throw exception
if (ret instanceof Class) {
return (Class) ret;
} else if (ret instanceof ClassNotFoundException) {
throw (ClassNotFoundException) ret;
} else {
throw new ClassNotFoundException(_className);
}
} catch (ClassNotFoundException cnfe) {
return loadClass(className);
}
|
public static java.lang.ClassLoader | getClassLoader(java.lang.String className)Obtain the ClassLoader (if any) associated with the given
className.
if (className == null) {
return null;
}
return (ClassLoader) classloaders.get(className);
|
public static java.lang.ClassLoader | getDefaultClassLoader()
return defaultClassLoader;
|
public static java.io.InputStream | getResourceAsStream(java.lang.Class clazz, java.lang.String resource)Get an input stream from a named resource.
Tries
- the classloader that loaded "clazz" first,
- the system classloader
- the class "clazz" itself
InputStream myInputStream = null;
if(clazz.getClassLoader()!=null) {
// Try the class loader that loaded this class.
myInputStream = clazz.getClassLoader().getResourceAsStream(resource);
} else {
// Try the system class loader.
myInputStream = ClassLoader.getSystemClassLoader().getResourceAsStream(resource);
}
if (myInputStream == null && Thread.currentThread().getContextClassLoader() != null) {
// try the context class loader.
myInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
}
if (myInputStream == null) {
// if not found in classpath fall back to default
myInputStream = clazz.getResourceAsStream(resource);
}
return myInputStream;
|
public static java.io.InputStream | getResourceAsStream(java.lang.Class clazz, java.lang.String resource, boolean checkThreadContextFirst)Get an input stream from a named resource.
Tries
- the classloader that loaded "clazz" first,
- the system classloader
- the class "clazz" itself
InputStream myInputStream = null;
if (checkThreadContextFirst &&
Thread.currentThread().getContextClassLoader() != null) {
// try the context class loader.
myInputStream =
Thread.currentThread().getContextClassLoader()
.getResourceAsStream(resource);
}
if (myInputStream == null) {
// if not found in context class loader fall back to default
myInputStream = getResourceAsStream(clazz, resource);
}
return myInputStream;
|
private static java.lang.Class | loadClass(java.lang.String _className)Loads the class from the context class loader and then falls back to
getDefaultClassLoader().forName
// Create final vars for doPrivileged block
final String className = _className;
// Get the class within a doPrivleged block
Object ret =
AccessController.doPrivileged(
new PrivilegedAction() {
public Object run() {
try {
// Check if the class is a registered class then
// use the classloader for that class.
ClassLoader classLoader = getClassLoader(className);
return Class.forName(className, true, classLoader);
} catch (ClassNotFoundException cnfe) {
} catch (SecurityException cnfe) {
}
try {
// Try the context class loader
ClassLoader classLoader =
Thread.currentThread().getContextClassLoader();
return Class.forName(className, true, classLoader);
} catch (ClassNotFoundException cnfe2) {
try {
// Try the classloader that loaded this class.
ClassLoader classLoader =
ClassUtils.class.getClassLoader();
return Class.forName(className, true, classLoader);
} catch (ClassNotFoundException cnfe3) {
// Try the default class loader.
try {
return defaultClassLoader.loadClass(
className);
} catch (Throwable e) {
// Still not found, return exception
return e;
}
}
}
}
});
// If the class was located, return it. Otherwise throw exception
if (ret instanceof Class) {
return (Class) ret;
} else if (ret instanceof ClassNotFoundException) {
throw (ClassNotFoundException) ret;
} else {
throw new ClassNotFoundException(_className);
}
|
public static void | removeClassLoader(java.lang.String className)Deregister the ClassLoader for a given className.
classloaders.remove(className);
|
public static void | setClassLoader(java.lang.String className, java.lang.ClassLoader loader)Set the ClassLoader associated with the given className. If either the
class name or the loader are null, no action is performed.
if (className != null && loader != null)
classloaders.put(className, loader);
|
public static void | setDefaultClassLoader(java.lang.ClassLoader loader)Set the default ClassLoader. If loader is null, the default loader is
not changed.
if (loader != null)
defaultClassLoader = loader;
|