Java Code Examples for org.apache.logging.log4j.util.StackLocatorUtil#getCallerClass()

The following examples show how to use org.apache.logging.log4j.util.StackLocatorUtil#getCallerClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BundleContextSelector.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 2
Source File: ClassLoaderContextSelector.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example 3
Source File: ClassLoaderContextSelector.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example 4
Source File: ClassLoaderContextSelector.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@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();
    }
}
 
Example 5
Source File: AbstractADTest.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 6
Source File: ContextAwareAdvice.java    From james with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 7
Source File: ReflectionBenchmark.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 8
Source File: LogManager.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 9
Source File: BundleContextSelector.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@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);
            }
        }
    }
}
 
Example 10
Source File: BundleContextSelector.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 11
Source File: ReflectionBenchmark.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Benchmark
public Class<?> test07_getReflectiveCallerClassUtility() {
    return StackLocatorUtil.getCallerClass(3);
}
 
Example 12
Source File: ReflectionBenchmark.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
private Class<?> locateCaller() {
    return StackLocatorUtil.getCallerClass(ClassLocator.class.getName());
}
 
Example 13
Source File: Log4jLoggerFactory.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
protected LoggerContext getContext() {
    final Class<?> anchor = StackLocatorUtil.getCallerClass(FQCN, PACKAGE);
    return anchor == null ? LogManager.getContext() : getContext(StackLocatorUtil.getCallerClass(anchor));
}
 
Example 14
Source File: Log4jLoggerFactory.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
protected LoggerContext getContext() {
    final Class<?> anchor = StackLocatorUtil.getCallerClass(FQCN, PACKAGE);
    return anchor == null ? LogManager.getContext() : getContext(StackLocatorUtil.getCallerClass(anchor));
}