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   * 
30   * @author Ceki Gülcü
31   *
32   */
33  public class Activator implements BundleActivator {
34  
35      private BundleContext m_context = null;
36  
37      public void start(BundleContext context) {
38          LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
39  
40          try {
41              JoranConfigurator configurator = new JoranConfigurator();
42              configurator.setContext(lc);
43              // the context was probably already configured by default configuration
44              // rules
45              lc.reset();
46              configurator.doConfigure("src/test/input/osgi/simple.xml");
47          } catch (JoranException je) {
48              je.printStackTrace();
49          }
50          StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
51  
52          Logger logger = LoggerFactory.getLogger(this.getClass());
53          logger.info("Activator.start()");
54          m_context = context;
55      }
56  
57      public void stop(BundleContext context) {
58          m_context = null;
59          Logger logger = LoggerFactory.getLogger(this.getClass());
60          logger.info("Activator.stop");
61      }
62  
63      public Bundle[] getBundles() {
64          if (m_context != null) {
65              return m_context.getBundles();
66          }
67          return null;
68      }
69  }