下面列出了org.apache.logging.log4j.util.StackLocatorUtil#getCallerClass ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public LoggerContext getContext(final String fqcn, final ClassLoader loader, final boolean currentContext,
final URI configLocation) {
if (currentContext) {
final LoggerContext ctx = ContextAnchor.THREAD_CONTEXT.get();
if (ctx != null) {
return ctx;
}
return getDefault();
}
// it's quite possible that the provided ClassLoader may implement BundleReference which gives us a nice shortcut
if (loader instanceof BundleReference) {
return locateContext(((BundleReference) loader).getBundle(), configLocation);
}
final Class<?> callerClass = StackLocatorUtil.getCallerClass(fqcn);
if (callerClass != null) {
return locateContext(FrameworkUtil.getBundle(callerClass), configLocation);
}
final LoggerContext lc = ContextAnchor.THREAD_CONTEXT.get();
return lc == null ? getDefault() : lc;
}
@Override
public void shutdown(final String fqcn, final ClassLoader loader, final boolean currentContext,
final boolean allContexts) {
LoggerContext ctx = null;
if (currentContext) {
ctx = ContextAnchor.THREAD_CONTEXT.get();
} else if (loader != null) {
ctx = findContext(loader);
} else {
final Class<?> clazz = StackLocatorUtil.getCallerClass(fqcn);
if (clazz != null) {
ctx = findContext(clazz.getClassLoader());
}
if (ctx == null) {
ctx = ContextAnchor.THREAD_CONTEXT.get();
}
}
if (ctx != null) {
ctx.stop(DEFAULT_STOP_TIMEOUT, TimeUnit.MILLISECONDS);
}
}
@Override
public boolean hasContext(final String fqcn, final ClassLoader loader, final boolean currentContext) {
LoggerContext ctx;
if (currentContext) {
ctx = ContextAnchor.THREAD_CONTEXT.get();
} else if (loader != null) {
ctx = findContext(loader);
} else {
final Class<?> clazz = StackLocatorUtil.getCallerClass(fqcn);
if (clazz != null) {
ctx = findContext(clazz.getClassLoader());
} else {
ctx = ContextAnchor.THREAD_CONTEXT.get();
}
}
return ctx != null && ctx.isStarted();
}
@Override
public LoggerContext getContext(final String fqcn, final ClassLoader loader, final boolean currentContext,
final 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 {
final Class<?> clazz = StackLocatorUtil.getCallerClass(fqcn);
if (clazz != null) {
return locateContext(clazz.getClassLoader(), configLocation);
}
final LoggerContext lc = ContextAnchor.THREAD_CONTEXT.get();
if (lc != null) {
return lc;
}
return getDefault();
}
}
private static Class<?> callerClass(final Class<?> clazz) {
if (clazz != null) {
return clazz;
}
final Class<?> candidate = StackLocatorUtil.getCallerClass(3);
if (candidate == null) {
throw new UnsupportedOperationException("No class provided, and an appropriate one cannot be found.");
}
return candidate;
}
public static String[] getCallStack() {
int size = 100;
int adviceStackEntryCount = 2;
String[] callStack = new String[size];
for (int i = 0; i < size; i++) {
Class c = StackLocatorUtil.getCallerClass(i + adviceStackEntryCount);
if (c == null) {
return Arrays.copyOfRange(callStack, 0, i);
}
callStack[i] = c.getName();
}
return callStack;
}
@Benchmark
public Class<?>[] test11_getClassContextViaCallerClass() {
// let's not benchmark LinkedList or anything here
final Class<?>[] classes = new Class<?>[100];
Class<?> clazz;
for (int i = 0; null != (clazz = StackLocatorUtil.getCallerClass(i)); i++) {
classes[i] = clazz;
}
return classes;
}
private static Class<?> callerClass(final Class<?> clazz) {
if (clazz != null) {
return clazz;
}
final Class<?> candidate = StackLocatorUtil.getCallerClass(3);
if (candidate == null) {
throw new UnsupportedOperationException("No class provided, and an appropriate one cannot be found.");
}
return candidate;
}
@Override
public void shutdown(final String fqcn, final ClassLoader loader, final boolean currentContext,
final boolean allContexts) {
LoggerContext ctx = null;
Bundle bundle = null;
if (currentContext) {
ctx = ContextAnchor.THREAD_CONTEXT.get();
ContextAnchor.THREAD_CONTEXT.remove();
}
if (ctx == null && loader instanceof BundleReference) {
bundle = ((BundleReference) loader).getBundle();
ctx = getLoggerContext(bundle);
removeLoggerContext(ctx);
}
if (ctx == null) {
final Class<?> callerClass = StackLocatorUtil.getCallerClass(fqcn);
if (callerClass != null) {
bundle = FrameworkUtil.getBundle(callerClass);
ctx = getLoggerContext(FrameworkUtil.getBundle(callerClass));
removeLoggerContext(ctx);
}
}
if (ctx == null) {
ctx = ContextAnchor.THREAD_CONTEXT.get();
ContextAnchor.THREAD_CONTEXT.remove();
}
if (ctx != null) {
ctx.stop(DEFAULT_STOP_TIMEOUT, TimeUnit.MILLISECONDS);
}
if (bundle != null && allContexts) {
final Bundle[] bundles = bundle.getBundleContext().getBundles();
for (final Bundle bdl : bundles) {
ctx = getLoggerContext(bdl);
if (ctx != null) {
ctx.stop(DEFAULT_STOP_TIMEOUT, TimeUnit.MILLISECONDS);
}
}
}
}
@Override
public boolean hasContext(final String fqcn, final ClassLoader loader, final boolean currentContext) {
if (currentContext && ContextAnchor.THREAD_CONTEXT.get() != null) {
return ContextAnchor.THREAD_CONTEXT.get().isStarted();
}
if (loader instanceof BundleReference) {
return hasContext(((BundleReference) loader).getBundle());
}
final Class<?> callerClass = StackLocatorUtil.getCallerClass(fqcn);
if (callerClass != null) {
return hasContext(FrameworkUtil.getBundle(callerClass));
}
return ContextAnchor.THREAD_CONTEXT.get() != null && ContextAnchor.THREAD_CONTEXT.get().isStarted();
}
@Benchmark
public Class<?> test07_getReflectiveCallerClassUtility() {
return StackLocatorUtil.getCallerClass(3);
}
private Class<?> locateCaller() {
return StackLocatorUtil.getCallerClass(ClassLocator.class.getName());
}
@Override
protected LoggerContext getContext() {
final Class<?> anchor = StackLocatorUtil.getCallerClass(FQCN, PACKAGE);
return anchor == null ? LogManager.getContext() : getContext(StackLocatorUtil.getCallerClass(anchor));
}
@Override
protected LoggerContext getContext() {
final Class<?> anchor = StackLocatorUtil.getCallerClass(FQCN, PACKAGE);
return anchor == null ? LogManager.getContext() : getContext(StackLocatorUtil.getCallerClass(anchor));
}