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 org.slf4j.test_osgi;
015
016import java.util.ArrayList;
017import java.util.List;
018import java.util.Map;
019import java.util.Properties;
020
021import org.apache.felix.framework.Felix;
022import org.apache.felix.framework.util.FelixConstants;
023import org.apache.felix.framework.util.StringMap;
024import org.apache.felix.main.AutoProcessor;
025import org.osgi.framework.Bundle;
026import org.osgi.framework.BundleContext;
027import org.osgi.framework.BundleException;
028import org.osgi.framework.Constants;
029
030/**
031 * Runs a hosted version of Felix for testing purposes. Any bundle errors are
032 * reported via the FrameworkListener passed to the constructor.
033 * 
034 * @author Ceki Gülcü
035 */
036public class FelixHost {
037
038    private Felix felix = null;
039
040    Properties otherProps = new Properties();
041
042    final FrameworkErrorListener frameworkErrorListener;
043    final CheckingBundleListener myBundleListener;
044
045    public FelixHost(FrameworkErrorListener frameworkErrorListener, CheckingBundleListener myBundleListener) {
046        this.frameworkErrorListener = frameworkErrorListener;
047        this.myBundleListener = myBundleListener;
048    }
049
050    public void doLaunch() {
051        // Create a case-insensitive configuration property map.
052        Map<String, Object> configMap = new StringMap();
053        // Configure the Felix instance to be embedded.
054        // configMap.put(FelixConstants.EMBEDDED_EXECUTION_PROP, "true");
055        // Add core OSGi packages to be exported from the class path
056        // via the system bundle.
057        configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES, "org.osgi.framework; version=1.3.0," + "org.osgi.service.packageadmin; version=1.2.0,"
058                        + "org.osgi.service.startlevel; version=1.0.0," + "org.osgi.service.url; version=1.0.0");
059
060        configMap.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
061
062        // Explicitly specify the directory to use for caching bundles.
063        // configMap.put(BundleCache.CACHE_PROFILE_DIR_PROP, "cache");
064
065        try {
066            // Create host activator;
067
068            List<Object> list = new ArrayList<Object>();
069
070            // list.add(new HostActivator());
071            configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "org.xml.sax, org.xml.sax.helpers, javax.xml.parsers, javax.naming");
072            configMap.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);
073            configMap.put("felix.log.level", "4");
074
075            // Now create an instance of the framework with
076            // our configuration properties and activator.
077            felix = new Felix(configMap);
078            felix.init();
079
080            // otherProps.put(Constants.FRAMEWORK_STORAGE, "bundles");
081
082            otherProps.put(AutoProcessor.AUTO_DEPLOY_DIR_PROPERTY, AutoProcessor.AUTO_DEPLOY_DIR_VALUE);
083            otherProps.put(AutoProcessor.AUTO_DEPLOY_ACTION_PROPERTY, AutoProcessor.AUTO_DEPLOY_START_VALUE + "," + AutoProcessor.AUTO_DEPLOY_INSTALL_VALUE);
084
085            BundleContext felixBudleContext = felix.getBundleContext();
086
087            AutoProcessor.process(otherProps, felixBudleContext);
088            // listen to errors
089            felixBudleContext.addFrameworkListener(frameworkErrorListener);
090            felixBudleContext.addBundleListener(myBundleListener);
091            // Now start Felix instance.
092            felix.start();
093            System.out.println("felix started");
094
095        } catch (Exception ex) {
096            ex.printStackTrace();
097        }
098    }
099
100    public void stop() throws BundleException {
101        felix.stop();
102    }
103
104    public Bundle[] getInstalledBundles() {
105        // Use the system bundle activator to gain external
106        // access to the set of installed bundles.
107        return null;// m_activator.getBundles();
108    }
109}