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 */
017
018package org.apache.logging.log4j.core.osgi;
019
020import java.util.Hashtable;
021import java.util.concurrent.atomic.AtomicReference;
022
023import org.apache.logging.log4j.LogManager;
024import org.apache.logging.log4j.Logger;
025import org.apache.logging.log4j.core.config.plugins.util.PluginRegistry;
026import org.apache.logging.log4j.core.impl.Log4jProvider;
027import org.apache.logging.log4j.core.util.Constants;
028import org.apache.logging.log4j.spi.Provider;
029import org.apache.logging.log4j.status.StatusLogger;
030import org.apache.logging.log4j.util.PropertiesUtil;
031import org.osgi.framework.Bundle;
032import org.osgi.framework.BundleActivator;
033import org.osgi.framework.BundleContext;
034import org.osgi.framework.BundleEvent;
035import org.osgi.framework.ServiceRegistration;
036import org.osgi.framework.SynchronousBundleListener;
037import org.osgi.framework.wiring.BundleWiring;
038
039/**
040 * OSGi BundleActivator.
041 */
042public final class Activator implements BundleActivator, SynchronousBundleListener {
043
044    private static final Logger LOGGER = StatusLogger.getLogger();
045
046    private final AtomicReference<BundleContext> contextRef = new AtomicReference<>();
047
048    ServiceRegistration provideRegistration = null;
049
050    @Override
051    public void start(final BundleContext context) throws Exception {
052        final Provider provider = new Log4jProvider();
053        final Hashtable<String, String> props = new Hashtable<>();
054        props.put("APIVersion", "2.60");
055        provideRegistration = context.registerService(Provider.class.getName(), provider, props);
056        // allow the user to override the default ContextSelector (e.g., by using BasicContextSelector for a global cfg)
057        if (PropertiesUtil.getProperties().getStringProperty(Constants.LOG4J_CONTEXT_SELECTOR) == null) {
058            System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR, BundleContextSelector.class.getName());
059        }
060        if (this.contextRef.compareAndSet(null, context)) {
061            context.addBundleListener(this);
062            // done after the BundleListener as to not miss any new bundle installs in the interim
063            scanInstalledBundlesForPlugins(context);
064        }
065    }
066
067    private static void scanInstalledBundlesForPlugins(final BundleContext context) {
068        final Bundle[] bundles = context.getBundles();
069        for (final Bundle bundle : bundles) {
070            // TODO: bundle state can change during this
071            scanBundleForPlugins(bundle);
072        }
073    }
074
075    private static void scanBundleForPlugins(final Bundle bundle) {
076        final long bundleId = bundle.getBundleId();
077        // LOG4J2-920: don't scan system bundle for plugins
078        if (bundle.getState() == Bundle.ACTIVE && bundleId != 0) {
079            LOGGER.trace("Scanning bundle [{}, id=%d] for plugins.", bundle.getSymbolicName(), bundleId);
080            PluginRegistry.getInstance().loadFromBundle(bundleId,
081                    bundle.adapt(BundleWiring.class).getClassLoader());
082        }
083    }
084
085    private static void stopBundlePlugins(final Bundle bundle) {
086        LOGGER.trace("Stopping bundle [{}] plugins.", bundle.getSymbolicName());
087        // TODO: plugin lifecycle code
088        PluginRegistry.getInstance().clearBundlePlugins(bundle.getBundleId());
089    }
090
091    @Override
092    public void stop(final BundleContext context) throws Exception {
093        provideRegistration.unregister();
094        this.contextRef.compareAndSet(context, null);
095        LogManager.shutdown();
096    }
097
098    @Override
099    public void bundleChanged(final BundleEvent event) {
100        switch (event.getType()) {
101            // FIXME: STARTING instead of STARTED?
102            case BundleEvent.STARTED:
103                scanBundleForPlugins(event.getBundle());
104                break;
105
106            case BundleEvent.STOPPING:
107                stopBundlePlugins(event.getBundle());
108                break;
109
110            default:
111                break;
112        }
113    }
114}