diff --git a/src/main/java/org/jboss/logmanager/CallerClassLoaderLogContextSelector.java b/src/main/java/org/jboss/logmanager/CallerClassLoaderLogContextSelector.java new file mode 100644 index 00000000..ad9e4a2d --- /dev/null +++ b/src/main/java/org/jboss/logmanager/CallerClassLoaderLogContextSelector.java @@ -0,0 +1,216 @@ +/* + * JBoss, Home of Professional Open Source. + * + * Copyright 2017 Red Hat, Inc., and individual contributors + * as indicated by the @author tags. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jboss.logmanager; + +import java.security.AccessController; +import java.security.Permission; +import java.security.PrivilegedAction; +import java.util.Collections; +import java.util.Set; +import java.util.concurrent.ConcurrentMap; + +/** + * A log context selector which chooses a log context based on the caller's classloader. The first caller that is not + * a {@linkplain #addLogApiClassLoader(ClassLoader) log API} will be the class loader used. Also note that if the + * callers classloader is {@code null} it will not be considered the first caller. + */ +@SuppressWarnings({"WeakerAccess", "unused"}) +public final class CallerClassLoaderLogContextSelector implements LogContextSelector { + + private static final Permission REGISTER_LOG_CONTEXT_PERMISSION = new RuntimePermission("registerLogContext", null); + private static final Permission UNREGISTER_LOG_CONTEXT_PERMISSION = new RuntimePermission("unregisterLogContext", null); + private static final Permission LOG_API_PERMISSION = new RuntimePermission("logApiPermission", null); + + /** + * Construct a new instance. If no matching log context is found, the provided default selector is consulted. + * + * @param defaultSelector the selector to consult if no matching log context is found + */ + public CallerClassLoaderLogContextSelector(final LogContextSelector defaultSelector) { + this(defaultSelector, false); + } + + /** + * Construct a new instance. If no matching log context is found, the provided default selector is consulted. + *
+ * If the {@code checkParentClassLoaders} is set to {@code true} this selector recursively searches the class loader + * parents until a match is found or a {@code null} parent is found. + *
+ * + * @param defaultSelector the selector to consult if no matching log context is found + * @param checkParentClassLoaders {@code true} if the {@link LogContext log context} could not + * found for the class loader and the {@link ClassLoader#getParent() parent class + * loader} should be checked + */ + public CallerClassLoaderLogContextSelector(final LogContextSelector defaultSelector, final boolean checkParentClassLoaders) { + this.defaultSelector = defaultSelector == null ? LogContext.DEFAULT_LOG_CONTEXT_SELECTOR : defaultSelector; + this.checkParentClassLoaders = checkParentClassLoaders; + } + + /** + * Construct a new instance. If no matching log context is found, the system context is used. + */ + public CallerClassLoaderLogContextSelector() { + this(false); + } + + /** + * Construct a new instance. If no matching log context is found, the system context is used. + *+ * If the {@code checkParentClassLoaders} is set to {@code true} this selector recursively searches the class loader + * parents until a match is found or a {@code null} parent is found. + *
+ * + * @param checkParentClassLoaders {@code true} if the {@link LogContext log context} could not + * found for the class loader and the {@link ClassLoader#getParent() parent class + * loader} should be checked + */ + public CallerClassLoaderLogContextSelector(final boolean checkParentClassLoaders) { + this(LogContext.DEFAULT_LOG_CONTEXT_SELECTOR, checkParentClassLoaders); + } + + private static final class Gateway extends SecurityManager { + protected Class[] getClassContext() { + return super.getClassContext(); + } + } + + private static final Gateway GATEWAY; + + static { + GATEWAY = AccessController.doPrivileged(new PrivilegedAction