Use most derived available classloader when loading plugins so that we can

load application classes when the application uses a derived loader.



git-svn-id: https://svn.apache.org/repos/asf/incubator/openjpa/trunk@486424 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
A. Abram White 2006-12-13 00:16:33 +00:00
parent 0ffb985316
commit 74589c8ae1
1 changed files with 35 additions and 3 deletions

View File

@ -127,12 +127,10 @@ public class Configurations {
ClassLoader loader, boolean fatal) { ClassLoader loader, boolean fatal) {
if (StringUtils.isEmpty(clsName)) if (StringUtils.isEmpty(clsName))
return null; return null;
if (loader == null && conf != null)
loader = conf.getClass().getClassLoader();
Class cls = null; Class cls = null;
try { try {
cls = Strings.toClass(clsName, loader); cls = Strings.toClass(clsName, findDerivedLoader(conf, loader));
} catch (RuntimeException re) { } catch (RuntimeException re) {
if (val != null) if (val != null)
re = getCreateException(clsName, val, re); re = getCreateException(clsName, val, re);
@ -158,6 +156,40 @@ public class Configurations {
} }
} }
/**
* Attempt to find a derived loader that delegates to our target loader.
* This allows application loaders that delegate appropriately for known
* classes first crack at class names.
*/
private static ClassLoader findDerivedLoader(Configuration conf,
ClassLoader loader) {
// we always prefer the thread loader, because it's the only thing we
// can access that isn't bound to the OpenJPA classloader, unless
// the conf object is of a custom class
ClassLoader ctxLoader = Thread.currentThread().getContextClassLoader();
if (loader == null) {
if (ctxLoader != null)
return ctxLoader;
if (conf != null)
return conf.getClass().getClassLoader();
return Configurations.class.getClassLoader();
}
for (ClassLoader parent = ctxLoader; parent != null;
parent = parent.getParent()) {
if (parent == loader)
return ctxLoader;
}
if (conf != null) {
for (ClassLoader parent = conf.getClass().getClassLoader();
parent != null; parent = parent.getParent()) {
if (parent == loader)
return conf.getClass().getClassLoader();
}
}
return loader;
}
/** /**
* Set the given {@link Configuration} instance from the command line * Set the given {@link Configuration} instance from the command line
* options provided. All property names of the given configuration are * options provided. All property names of the given configuration are