Source code

    001/**
    002 * Copyright (c) 2004-2011 QOS.ch
    003 * All rights reserved.
    004 *
    005 * Permission is hereby granted, free  of charge, to any person obtaining
    006 * a  copy  of this  software  and  associated  documentation files  (the
    007 * "Software"), to  deal in  the Software without  restriction, including
    008 * without limitation  the rights to  use, copy, modify,  merge, publish,
    009 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
    010 * permit persons to whom the Software  is furnished to do so, subject to
    011 * the following conditions:
    012 *
    013 * The  above  copyright  notice  and  this permission  notice  shall  be
    014 * included in all copies or substantial portions of the Software.
    015 *
    016 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
    017 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
    018 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
    019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    021 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
    022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    023 *
    024 */
    025package org.slf4j.test_osgi;
    026
    027import java.util.ArrayList;
    028import java.util.List;
    029import java.util.Properties;
    030
    031import org.apache.felix.framework.Felix;
    032import org.apache.felix.framework.util.FelixConstants;
    033import org.apache.felix.framework.util.StringMap;
    034import org.apache.felix.main.AutoProcessor;
    035import org.osgi.framework.Bundle;
    036import org.osgi.framework.BundleContext;
    037import org.osgi.framework.BundleException;
    038import org.osgi.framework.Constants;
    039
    040/**
    041 * Runs a hosted version of Felix for testing purposes. Any bundle errors are
    042 * reported via the FrameworkListener passed to the constructor.
    043 *
    044 * @author Ceki G&uuml;lc&uuml;
    045 */
    046public class FelixHost {
    047
    048    private Felix felix = null;
    049
    050    Properties otherProps = new Properties();
    051
    052    final FrameworkErrorListener frameworkErrorListener;
    053    final CheckingBundleListener myBundleListener;
    054
    055    public FelixHost(FrameworkErrorListener frameworkErrorListener, CheckingBundleListener myBundleListener) {
    056        this.frameworkErrorListener = frameworkErrorListener;
    057        this.myBundleListener = myBundleListener;
    058    }
    059
    060    public void doLaunch() {
    061        // Create a case-insensitive configuration property map.
    062        StringMap configMap = new StringMap();
    063        // Configure the Felix instance to be embedded.
    064        // configMap.put(FelixConstants.EMBEDDED_EXECUTION_PROP, "true");
    065        // Add core OSGi packages to be exported from the class path
    066        // via the system bundle.
    067        configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES, "org.osgi.framework; version=1.3.0," + "org.osgi.service.packageadmin; version=1.2.0,"
    068                        + "org.osgi.service.startlevel; version=1.0.0," + "org.osgi.service.url; version=1.0.0");
    069
    070        configMap.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
    071
    072        // Explicitly specify the directory to use for caching bundles.
    073        // configMap.put(BundleCache.CACHE_PROFILE_DIR_PROP, "cache");
    074
    075        try {
    076            // Create host activator;
    077
    078            List<Object> list = new ArrayList<Object>();
    079
    080            // list.add(new HostActivator());
    081            configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "org.xml.sax, org.xml.sax.helpers, javax.xml.parsers, javax.naming");
    082            configMap.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);
    083            configMap.put("felix.log.level", "4");
    084
    085            // Now create an instance of the framework with
    086            // our configuration properties and activator.
    087            felix = new Felix(configMap);
    088            felix.init();
    089
    090            // otherProps.put(Constants.FRAMEWORK_STORAGE, "bundles");
    091
    092            otherProps.put(AutoProcessor.AUTO_DEPLOY_DIR_PROPERTY, AutoProcessor.AUTO_DEPLOY_DIR_VALUE);
    093            otherProps.put(AutoProcessor.AUTO_DEPLOY_ACTION_PROPERTY, AutoProcessor.AUTO_DEPLOY_START_VALUE + "," + AutoProcessor.AUTO_DEPLOY_INSTALL_VALUE);
    094
    095            BundleContext felixBudleContext = felix.getBundleContext();
    096
    097            AutoProcessor.process(otherProps, felixBudleContext);
    098            // listen to errors
    099            felixBudleContext.addFrameworkListener(frameworkErrorListener);
    100            felixBudleContext.addBundleListener(myBundleListener);
    101            // Now start Felix instance.
    102            felix.start();
    103            System.out.println("felix started");
    104
    105        } catch (Exception ex) {
    106            ex.printStackTrace();
    107        }
    108    }
    109
    110    public void stop() throws BundleException {
    111        felix.stop();
    112    }
    113
    114    public Bundle[] getInstalledBundles() {
    115        // Use the system bundle activator to gain external
    116        // access to the set of installed bundles.
    117        return null;// m_activator.getBundles();
    118    }
    119}