View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package integrator;
15  
16  import org.osgi.framework.Bundle;
17  import org.osgi.framework.BundleActivator;
18  import org.osgi.framework.BundleContext;
19  import org.slf4j.Logger;
20  import org.slf4j.LoggerFactory;
21  
22  import ch.qos.logback.classic.LoggerContext;
23  import ch.qos.logback.classic.joran.JoranConfigurator;
24  import ch.qos.logback.core.joran.spi.JoranException;
25  import ch.qos.logback.core.util.StatusPrinter;
26  
27  /**
28   * A BundleActivator which invokes slf4j loggers
29   * @author Ceki Gülcü
30   *
31   */
32  public class Activator implements BundleActivator {
33  
34      private BundleContext m_context = null;
35  
36      public void start(BundleContext context) {
37          LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
38  
39          try {
40              JoranConfigurator configurator = new JoranConfigurator();
41              configurator.setContext(lc);
42              // the context was probably already configured by default configuration
43              // rules
44              lc.reset();
45              configurator.doConfigure("src/test/input/osgi/simple.xml");
46          } catch (JoranException je) {
47              je.printStackTrace();
48          }
49          StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
50  
51          Logger logger = LoggerFactory.getLogger(this.getClass());
52          logger.info("Activator.start()");
53          m_context = context;
54      }
55  
56      public void stop(BundleContext context) {
57          m_context = null;
58          Logger logger = LoggerFactory.getLogger(this.getClass());
59          logger.info("Activator.stop");
60      }
61  
62      public Bundle[] getBundles() {
63          if (m_context != null) {
64              return m_context.getBundles();
65          }
66          return null;
67      }
68  }