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 org.slf4j.test_osgi;
15  
16  import java.util.ArrayList;
17  import java.util.List;
18  import java.util.Map;
19  import java.util.Properties;
20  
21  import org.apache.felix.framework.Felix;
22  import org.apache.felix.framework.util.FelixConstants;
23  import org.apache.felix.framework.util.StringMap;
24  import org.apache.felix.main.AutoProcessor;
25  import org.osgi.framework.Bundle;
26  import org.osgi.framework.BundleContext;
27  import org.osgi.framework.BundleException;
28  import org.osgi.framework.Constants;
29  
30  /**
31   * Runs a hosted version of Felix for testing purposes. Any bundle errors are
32   * reported via the FrameworkListener passed to the constructor.
33   * 
34   * @author Ceki Gülcü
35   */
36  public class FelixHost {
37  
38      private Felix felix = null;
39  
40      Properties otherProps = new Properties();
41  
42      final FrameworkErrorListener frameworkErrorListener;
43      final CheckingBundleListener myBundleListener;
44  
45      public FelixHost(FrameworkErrorListener frameworkErrorListener, CheckingBundleListener myBundleListener) {
46          this.frameworkErrorListener = frameworkErrorListener;
47          this.myBundleListener = myBundleListener;
48      }
49  
50      public void doLaunch() {
51          // Create a case-insensitive configuration property map.
52          Map<String, Object> configMap = new StringMap();
53          // Configure the Felix instance to be embedded.
54          // configMap.put(FelixConstants.EMBEDDED_EXECUTION_PROP, "true");
55          // Add core OSGi packages to be exported from the class path
56          // via the system bundle.
57          configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES,
58                  "org.osgi.framework; version=1.3.0," + "org.osgi.service.packageadmin; version=1.2.0,"
59                          + "org.osgi.service.startlevel; version=1.0.0," + "org.osgi.service.url; version=1.0.0");
60  
61          configMap.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
62  
63          // Explicitly specify the directory to use for caching bundles.
64          // configMap.put(BundleCache.CACHE_PROFILE_DIR_PROP, "cache");
65  
66          try {
67              // Create host activator;
68  
69              List<Object> list = new ArrayList<Object>();
70  
71              // list.add(new HostActivator());
72              configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA,
73                      "org.xml.sax, org.xml.sax.helpers, javax.xml.parsers, javax.naming");
74              configMap.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);
75              configMap.put("felix.log.level", "4");
76  
77              // Now create an instance of the framework with
78              // our configuration properties and activator.
79              felix = new Felix(configMap);
80              felix.init();
81  
82              // otherProps.put(Constants.FRAMEWORK_STORAGE, "bundles");
83  
84              otherProps.put(AutoProcessor.AUTO_DEPLOY_DIR_PROPERTY, AutoProcessor.AUTO_DEPLOY_DIR_VALUE);
85              otherProps.put(AutoProcessor.AUTO_DEPLOY_ACTION_PROPERTY,
86                      AutoProcessor.AUTO_DEPLOY_START_VALUE + "," + AutoProcessor.AUTO_DEPLOY_INSTALL_VALUE);
87  
88              BundleContext felixBudleContext = felix.getBundleContext();
89  
90              AutoProcessor.process(otherProps, felixBudleContext);
91              // listen to errors
92              felixBudleContext.addFrameworkListener(frameworkErrorListener);
93              felixBudleContext.addBundleListener(myBundleListener);
94              // Now start Felix instance.
95              felix.start();
96              System.out.println("felix started");
97  
98          } catch (Exception ex) {
99              ex.printStackTrace();
100         }
101     }
102 
103     public void stop() throws BundleException {
104         felix.stop();
105     }
106 
107     public Bundle[] getInstalledBundles() {
108         // Use the system bundle activator to gain external
109         // access to the set of installed bundles.
110         return null;// m_activator.getBundles();
111     }
112 }