@Override
public LoggerContext getContext(final String fqcn, final ClassLoader loader, final boolean currentContext,
URI configLocation) {
if (currentContext) {
final LoggerContext ctx = ContextAnchor.THREAD_CONTEXT.get();
if (ctx != null) {
return ctx;
}
return getDefault();
} else if (loader != null) {
return locateContext(loader, configLocation);
} else {
if (getCallerClass != null) {
try {
Class clazz = Class.class;
boolean next = false;
for (int index = 2; clazz != null; ++index) {
final Object[] params = new Object[] {index};
clazz = (Class) getCallerClass.invoke(null, params);
if (clazz == null) {
break;
}
if (clazz.getName().equals(fqcn)) {
next = true;
continue;
}
if (next) {
break;
}
}
if (clazz != null) {
return locateContext(clazz.getClassLoader(), configLocation);
}
} catch (final Exception ex) {
// logger.debug("Unable to determine caller class via Sun Reflection", ex);
}
}
if (securityManager != null) {
final Class clazz = securityManager.getCaller(fqcn);
if (clazz != null) {
final ClassLoader ldr = clazz.getClassLoader() != null ? clazz.getClassLoader() :
ClassLoader.getSystemClassLoader();
return locateContext(ldr, configLocation);
}
}
final Throwable t = new Throwable();
boolean next = false;
String name = null;
for (final StackTraceElement element : t.getStackTrace()) {
if (element.getClassName().equals(fqcn)) {
next = true;
continue;
}
if (next) {
name = element.getClassName();
break;
}
}
if (name != null) {
try {
return locateContext(Loader.loadClass(name).getClassLoader(), configLocation);
} catch (final ClassNotFoundException ex) {
//System.out.println("Could not load class " + name);
}
}
final LoggerContext lc = ContextAnchor.THREAD_CONTEXT.get();
if (lc != null) {
return lc;
}
return getDefault();
}