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