Use ReflectionUtil.getCallerClass(String)
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/cbf0f729 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/cbf0f729 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/cbf0f729 Branch: refs/heads/master Commit: cbf0f729d2dfd93220217afa0a5f5c70d013f31f Parents: 066fb89 Author: Matt Sicker <[email protected]> Authored: Fri Sep 5 16:54:06 2014 -0500 Committer: Matt Sicker <[email protected]> Committed: Fri Sep 5 16:54:06 2014 -0500 ---------------------------------------------------------------------- .../selector/ClassLoaderContextSelector.java | 102 +------------------ 1 file changed, 4 insertions(+), 98 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/cbf0f729/log4j-core/src/main/java/org/apache/logging/log4j/core/selector/ClassLoaderContextSelector.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/selector/ClassLoaderContextSelector.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/selector/ClassLoaderContextSelector.java index 2e3d93e..fa9a6e2 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/selector/ClassLoaderContextSelector.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/selector/ClassLoaderContextSelector.java @@ -29,9 +29,8 @@ import java.util.concurrent.atomic.AtomicReference; import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.impl.ContextAnchor; -import org.apache.logging.log4j.core.impl.ReflectiveCallerClassUtility; -import org.apache.logging.log4j.core.util.Loader; import org.apache.logging.log4j.status.StatusLogger; +import org.apache.logging.log4j.util.ReflectionUtil; /** * This ContextSelector chooses a LoggerContext based upon the ClassLoader of the caller. This allows Loggers @@ -48,33 +47,11 @@ public class ClassLoaderContextSelector implements ContextSelector { private static final AtomicReference<LoggerContext> CONTEXT = new AtomicReference<LoggerContext>(); - private static final PrivateSecurityManager SECURITY_MANAGER; - private static final StatusLogger LOGGER = StatusLogger.getLogger(); private static final ConcurrentMap<String, AtomicReference<WeakReference<LoggerContext>>> CONTEXT_MAP = new ConcurrentHashMap<String, AtomicReference<WeakReference<LoggerContext>>>(); - static { - if (ReflectiveCallerClassUtility.isSupported()) { - SECURITY_MANAGER = null; - } else { - PrivateSecurityManager securityManager; - try { - securityManager = new PrivateSecurityManager(); - if (securityManager.getCaller(ClassLoaderContextSelector.class.getName()) == null) { - // This shouldn't happen. - securityManager = null; - LOGGER.error("Unable to obtain call stack from security manager."); - } - } catch (final Exception e) { - securityManager = null; - LOGGER.debug("Unable to install security manager", e); - } - SECURITY_MANAGER = securityManager; - } - } - @Override public LoggerContext getContext(final String fqcn, final ClassLoader loader, final boolean currentContext) { return getContext(fqcn, loader, currentContext, null); @@ -92,59 +69,9 @@ public class ClassLoaderContextSelector implements ContextSelector { } else if (loader != null) { return locateContext(loader, configLocation); } else { - if (ReflectiveCallerClassUtility.isSupported()) { - try { - Class<?> clazz = Class.class; - boolean next = false; - for (int index = 2; clazz != null; ++index) { - clazz = ReflectiveCallerClassUtility.getCaller(index); - 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 (SECURITY_MANAGER != null) { - final Class<?> clazz = SECURITY_MANAGER.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 ignore) { - //this is ok - } + final Class<?> clazz = ReflectionUtil.getCallerClass(fqcn); + if (clazz != null) { + return locateContext(clazz.getClassLoader(), configLocation); } final LoggerContext lc = ContextAnchor.THREAD_CONTEXT.get(); if (lc != null) { @@ -250,25 +177,4 @@ public class ClassLoaderContextSelector implements ContextSelector { return CONTEXT.get(); } - /** - * SecurityManager that will locate the caller of the Log4j 2 API. - */ - private static class PrivateSecurityManager extends SecurityManager { - - public Class<?> getCaller(final String fqcn) { - final Class<?>[] classes = getClassContext(); - boolean next = false; - for (final Class<?> clazz : classes) { - if (clazz.getName().equals(fqcn)) { - next = true; - continue; - } - if (next) { - return clazz; - } - } - return null; - } - } - }
