View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.logging.log4j.core.config.plugins.osgi;
19  
20  import org.apache.logging.log4j.LogManager;
21  import org.apache.logging.log4j.Logger;
22  import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
23  import org.apache.logging.log4j.core.impl.Log4jContextFactory;
24  import org.apache.logging.log4j.core.util.BundleResourceLoader;
25  import org.apache.logging.log4j.simple.SimpleLoggerContextFactory;
26  import org.apache.logging.log4j.spi.LoggerContextFactory;
27  import org.apache.logging.log4j.status.StatusLogger;
28  import org.osgi.framework.Bundle;
29  import org.osgi.framework.BundleContext;
30  import org.osgi.framework.BundleEvent;
31  import org.osgi.framework.BundleListener;
32  
33  /**
34   * OSGi BundleActivator.
35   */
36  public final class Activator implements org.osgi.framework.BundleActivator {
37  
38      private static final Logger LOGGER = StatusLogger.getLogger();
39  
40      @Override
41      public void start(final BundleContext context) throws Exception {
42          registerLoggerContextFactory();
43          context.addBundleListener(new Listener());
44          // done after the BundleListener as to not miss any new bundle installs in the interim
45          scanInstalledBundlesForPlugins(context);
46      }
47  
48      private void registerLoggerContextFactory() {
49          final LoggerContextFactory current = LogManager.getFactory();
50          if (!(current instanceof Log4jContextFactory)) {
51              LOGGER.debug("Replacing LogManager LoggerContextFactory.");
52              LogManager.setFactory(new Log4jContextFactory());
53          }
54      }
55  
56      private void scanInstalledBundlesForPlugins(final BundleContext context) {
57          final Bundle[] bundles = context.getBundles();
58          for (final Bundle bundle : bundles) {
59              if (bundle.getState() == Bundle.ACTIVE) {
60                  // TODO: bundle state can change during this
61                  scanBundleForPlugins(bundle);
62              }
63          }
64      }
65  
66      private static void scanBundleForPlugins(final Bundle bundle) {
67          LOGGER.debug("Scanning bundle [{}] for plugins.", bundle.getSymbolicName());
68          PluginManager.loadPlugins(new BundleResourceLoader(bundle));
69      }
70  
71      @Override
72      public void stop(final BundleContext context) throws Exception {
73          unregisterLoggerContextFactory();
74      }
75  
76      private void unregisterLoggerContextFactory() {
77          LogManager.setFactory(new SimpleLoggerContextFactory());
78      }
79  
80      private static class Listener implements BundleListener {
81  
82          @Override
83          public void bundleChanged(final BundleEvent event) {
84              switch (event.getType()) {
85                  case BundleEvent.STARTED:
86                      scanBundleForPlugins(event.getBundle());
87                      break;
88  
89                  default:
90                      break;
91              }
92          }
93      }
94  }