001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package integrator;
015
016import org.osgi.framework.Bundle;
017import org.osgi.framework.BundleActivator;
018import org.osgi.framework.BundleContext;
019import org.slf4j.Logger;
020import org.slf4j.LoggerFactory;
021
022import ch.qos.logback.classic.LoggerContext;
023import ch.qos.logback.classic.joran.JoranConfigurator;
024import ch.qos.logback.core.joran.spi.JoranException;
025import ch.qos.logback.core.util.StatusPrinter;
026
027/**
028 * A BundleActivator which invokes slf4j loggers
029 * @author Ceki Gülcü
030 *
031 */
032public class Activator implements BundleActivator {
033
034    private BundleContext m_context = null;
035
036    public void start(BundleContext context) {
037        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
038
039        try {
040            JoranConfigurator configurator = new JoranConfigurator();
041            configurator.setContext(lc);
042            // the context was probably already configured by default configuration
043            // rules
044            lc.reset();
045            configurator.doConfigure("src/test/input/osgi/simple.xml");
046        } catch (JoranException je) {
047            je.printStackTrace();
048        }
049        StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
050
051        Logger logger = LoggerFactory.getLogger(this.getClass());
052        logger.info("Activator.start()");
053        m_context = context;
054    }
055
056    public void stop(BundleContext context) {
057        m_context = null;
058        Logger logger = LoggerFactory.getLogger(this.getClass());
059        logger.info("Activator.stop");
060    }
061
062    public Bundle[] getBundles() {
063        if (m_context != null) {
064            return m_context.getBundles();
065        }
066        return null;
067    }
068}