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.concurrent.atomic.AtomicReference;
022
023import org.apache.logging.log4j.core.LoggerContext;
024import org.apache.logging.log4j.core.impl.ContextAnchor;
025import org.apache.logging.log4j.core.selector.ClassLoaderContextSelector;
026import org.apache.logging.log4j.core.selector.ContextSelector;
027import org.apache.logging.log4j.core.util.Assert;
028import org.apache.logging.log4j.util.ReflectionUtil;
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 implements ContextSelector {
041
042    @Override
043    public LoggerContext getContext(final String fqcn, final ClassLoader loader, final boolean currentContext,
044                                    final URI configLocation) {
045        if (currentContext) {
046            final LoggerContext ctx = ContextAnchor.THREAD_CONTEXT.get();
047            if (ctx != null) {
048                return ctx;
049            }
050            return getDefault();
051        }
052        // it's quite possible that the provided ClassLoader may implement BundleReference which gives us a nice shortcut
053        if (loader instanceof BundleReference) {
054            return locateContext(((BundleReference) loader).getBundle(), configLocation);
055        }
056        final Class<?> callerClass = ReflectionUtil.getCallerClass(fqcn);
057        if (callerClass != null) {
058            return locateContext(FrameworkUtil.getBundle(callerClass), configLocation);
059        }
060        final LoggerContext lc = ContextAnchor.THREAD_CONTEXT.get();
061        return lc == null ? getDefault() : lc;
062    }
063
064    private static LoggerContext locateContext(final Bundle bundle, final URI configLocation) {
065        final String name = Assert.requireNonNull(bundle, "No Bundle provided").getSymbolicName();
066        final AtomicReference<WeakReference<LoggerContext>> ref = CONTEXT_MAP.get(name);
067        if (ref == null) {
068            final LoggerContext context = new LoggerContext(name, bundle, configLocation);
069            CONTEXT_MAP.putIfAbsent(name,
070                new AtomicReference<WeakReference<LoggerContext>>(new WeakReference<LoggerContext>(context)));
071            return CONTEXT_MAP.get(name).get().get();
072        }
073        final WeakReference<LoggerContext> r = ref.get();
074        final LoggerContext ctx = r.get();
075        if (ctx == null) {
076            final LoggerContext context = new LoggerContext(name, bundle, configLocation);
077            ref.compareAndSet(r, new WeakReference<LoggerContext>(context));
078            return ref.get().get();
079        }
080        final URI oldConfigLocation = ctx.getConfigLocation();
081        if (oldConfigLocation == null && configLocation != null) {
082            LOGGER.debug("Setting bundle ({}) configuration to {}", name, configLocation);
083            ctx.setConfigLocation(configLocation);
084        } else if (oldConfigLocation != null && configLocation != null && !configLocation.equals(oldConfigLocation)) {
085            LOGGER.warn("locateContext called with URI [{}], but existing LoggerContext has URI [{}]",
086                configLocation, oldConfigLocation);
087        }
088        return ctx;
089    }
090}