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.atomic.AtomicReference;
023
024import org.apache.logging.log4j.core.LoggerContext;
025import org.apache.logging.log4j.core.impl.ContextAnchor;
026import org.apache.logging.log4j.core.selector.ClassLoaderContextSelector;
027import org.apache.logging.log4j.util.StackLocatorUtil;
028import org.osgi.framework.Bundle;
029import org.osgi.framework.BundleReference;
030import org.osgi.framework.FrameworkUtil;
031
032/**
033 * ContextSelector for OSGi bundles. This ContextSelector works rather similarly to the
034 * {@link ClassLoaderContextSelector}, but instead of each ClassLoader having its own LoggerContext (like in a
035 * servlet container), each OSGi bundle has its own LoggerContext.
036 *
037 * @since 2.1
038 */
039public class BundleContextSelector extends ClassLoaderContextSelector {
040
041    @Override
042    public LoggerContext getContext(final String fqcn, final ClassLoader loader, final boolean currentContext,
043                                    final URI configLocation) {
044        if (currentContext) {
045            final LoggerContext ctx = ContextAnchor.THREAD_CONTEXT.get();
046            if (ctx != null) {
047                return ctx;
048            }
049            return getDefault();
050        }
051        // it's quite possible that the provided ClassLoader may implement BundleReference which gives us a nice shortcut
052        if (loader instanceof BundleReference) {
053            return locateContext(((BundleReference) loader).getBundle(), configLocation);
054        }
055        final Class<?> callerClass = StackLocatorUtil.getCallerClass(fqcn);
056        if (callerClass != null) {
057            return locateContext(FrameworkUtil.getBundle(callerClass), configLocation);
058        }
059        final LoggerContext lc = ContextAnchor.THREAD_CONTEXT.get();
060        return lc == null ? getDefault() : lc;
061    }
062
063    private static LoggerContext locateContext(final Bundle bundle, final URI configLocation) {
064        final String name = Objects.requireNonNull(bundle, "No Bundle provided").getSymbolicName();
065        final AtomicReference<WeakReference<LoggerContext>> ref = CONTEXT_MAP.get(name);
066        if (ref == null) {
067            final LoggerContext context = new LoggerContext(name, bundle, configLocation);
068            CONTEXT_MAP.putIfAbsent(name,
069                new AtomicReference<>(new WeakReference<>(context)));
070            return CONTEXT_MAP.get(name).get().get();
071        }
072        final WeakReference<LoggerContext> r = ref.get();
073        final LoggerContext ctx = r.get();
074        if (ctx == null) {
075            final LoggerContext context = new LoggerContext(name, bundle, configLocation);
076            ref.compareAndSet(r, new WeakReference<>(context));
077            return ref.get().get();
078        }
079        final URI oldConfigLocation = ctx.getConfigLocation();
080        if (oldConfigLocation == null && configLocation != null) {
081            LOGGER.debug("Setting bundle ({}) configuration to {}", name, configLocation);
082            ctx.setConfigLocation(configLocation);
083        } else if (oldConfigLocation != null && configLocation != null && !configLocation.equals(oldConfigLocation)) {
084            LOGGER.warn("locateContext called with URI [{}], but existing LoggerContext has URI [{}]",
085                configLocation, oldConfigLocation);
086        }
087        return ctx;
088    }
089}