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