001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017package org.apache.logging.log4j.core.osgi; 018 019import java.lang.ref.WeakReference; 020import java.net.URI; 021import java.util.Objects; 022import java.util.concurrent.TimeUnit; 023import java.util.concurrent.atomic.AtomicReference; 024 025import org.apache.logging.log4j.core.LoggerContext; 026import org.apache.logging.log4j.core.impl.ContextAnchor; 027import org.apache.logging.log4j.core.selector.ClassLoaderContextSelector; 028import org.apache.logging.log4j.util.StackLocatorUtil; 029import org.osgi.framework.Bundle; 030import org.osgi.framework.BundleReference; 031import org.osgi.framework.FrameworkUtil; 032 033/** 034 * ContextSelector for OSGi bundles. This ContextSelector works rather similarly to the 035 * {@link ClassLoaderContextSelector}, but instead of each ClassLoader having its own LoggerContext (like in a 036 * servlet container), each OSGi bundle has its own LoggerContext. 037 * 038 * @since 2.1 039 */ 040public class BundleContextSelector extends ClassLoaderContextSelector { 041 042 @Override 043 public void shutdown(final String fqcn, final ClassLoader loader, final boolean currentContext, 044 final boolean allContexts) { 045 LoggerContext ctx = null; 046 Bundle bundle = null; 047 if (currentContext) { 048 ctx = ContextAnchor.THREAD_CONTEXT.get(); 049 ContextAnchor.THREAD_CONTEXT.remove(); 050 } 051 if (ctx == null && loader instanceof BundleReference) { 052 bundle = ((BundleReference) loader).getBundle(); 053 ctx = getLoggerContext(bundle); 054 removeLoggerContext(ctx); 055 } 056 if (ctx == null) { 057 final Class<?> callerClass = StackLocatorUtil.getCallerClass(fqcn); 058 if (callerClass != null) { 059 bundle = FrameworkUtil.getBundle(callerClass); 060 ctx = getLoggerContext(FrameworkUtil.getBundle(callerClass)); 061 removeLoggerContext(ctx); 062 } 063 } 064 if (ctx == null) { 065 ctx = ContextAnchor.THREAD_CONTEXT.get(); 066 ContextAnchor.THREAD_CONTEXT.remove(); 067 } 068 if (ctx != null) { 069 ctx.stop(DEFAULT_STOP_TIMEOUT, TimeUnit.MILLISECONDS); 070 } 071 if (bundle != null && allContexts) { 072 final Bundle[] bundles = bundle.getBundleContext().getBundles(); 073 for (final Bundle bdl : bundles) { 074 ctx = getLoggerContext(bdl); 075 if (ctx != null) { 076 ctx.stop(DEFAULT_STOP_TIMEOUT, TimeUnit.MILLISECONDS); 077 } 078 } 079 } 080 } 081 private LoggerContext getLoggerContext(final Bundle bundle) { 082 final String name = Objects.requireNonNull(bundle, "No Bundle provided").getSymbolicName(); 083 final AtomicReference<WeakReference<LoggerContext>> ref = CONTEXT_MAP.get(name); 084 if (ref != null && ref.get() != null) { 085 return ref.get().get(); 086 } 087 return null; 088 } 089 090 private void removeLoggerContext(LoggerContext context) { 091 CONTEXT_MAP.remove(context.getName()); 092 } 093 094 @Override 095 public boolean hasContext(final String fqcn, final ClassLoader loader, final boolean currentContext) { 096 if (currentContext && ContextAnchor.THREAD_CONTEXT.get() != null) { 097 return ContextAnchor.THREAD_CONTEXT.get().isStarted(); 098 } 099 if (loader instanceof BundleReference) { 100 return hasContext(((BundleReference) loader).getBundle()); 101 } 102 final Class<?> callerClass = StackLocatorUtil.getCallerClass(fqcn); 103 if (callerClass != null) { 104 return hasContext(FrameworkUtil.getBundle(callerClass)); 105 } 106 return ContextAnchor.THREAD_CONTEXT.get() != null && ContextAnchor.THREAD_CONTEXT.get().isStarted(); 107 } 108 @Override 109 public LoggerContext getContext(final String fqcn, final ClassLoader loader, final boolean currentContext, 110 final URI configLocation) { 111 if (currentContext) { 112 final LoggerContext ctx = ContextAnchor.THREAD_CONTEXT.get(); 113 if (ctx != null) { 114 return ctx; 115 } 116 return getDefault(); 117 } 118 // it's quite possible that the provided ClassLoader may implement BundleReference which gives us a nice shortcut 119 if (loader instanceof BundleReference) { 120 return locateContext(((BundleReference) loader).getBundle(), configLocation); 121 } 122 final Class<?> callerClass = StackLocatorUtil.getCallerClass(fqcn); 123 if (callerClass != null) { 124 return locateContext(FrameworkUtil.getBundle(callerClass), configLocation); 125 } 126 final LoggerContext lc = ContextAnchor.THREAD_CONTEXT.get(); 127 return lc == null ? getDefault() : lc; 128 } 129 130 private static boolean hasContext(final Bundle bundle) { 131 final String name = Objects.requireNonNull(bundle, "No Bundle provided").getSymbolicName(); 132 final AtomicReference<WeakReference<LoggerContext>> ref = CONTEXT_MAP.get(name); 133 return ref != null && ref.get() != null && ref.get().get() != null && ref.get().get().isStarted(); 134 } 135 136 private static LoggerContext locateContext(final Bundle bundle, final URI configLocation) { 137 final String name = Objects.requireNonNull(bundle, "No Bundle provided").getSymbolicName(); 138 final AtomicReference<WeakReference<LoggerContext>> ref = CONTEXT_MAP.get(name); 139 if (ref == null) { 140 final LoggerContext context = new LoggerContext(name, bundle, configLocation); 141 CONTEXT_MAP.putIfAbsent(name, 142 new AtomicReference<>(new WeakReference<>(context))); 143 return CONTEXT_MAP.get(name).get().get(); 144 } 145 final WeakReference<LoggerContext> r = ref.get(); 146 final LoggerContext ctx = r.get(); 147 if (ctx == null) { 148 final LoggerContext context = new LoggerContext(name, bundle, configLocation); 149 ref.compareAndSet(r, new WeakReference<>(context)); 150 return ref.get().get(); 151 } 152 final URI oldConfigLocation = ctx.getConfigLocation(); 153 if (oldConfigLocation == null && configLocation != null) { 154 LOGGER.debug("Setting bundle ({}) configuration to {}", name, configLocation); 155 ctx.setConfigLocation(configLocation); 156 } else if (oldConfigLocation != null && configLocation != null && !configLocation.equals(oldConfigLocation)) { 157 LOGGER.warn("locateContext called with URI [{}], but existing LoggerContext has URI [{}]", 158 configLocation, oldConfigLocation); 159 } 160 return ctx; 161 } 162}