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, "org.osgi.framework; version=1.3.0," + "org.osgi.service.packageadmin; version=1.2.0,"
58                          + "org.osgi.service.startlevel; version=1.0.0," + "org.osgi.service.url; version=1.0.0");
59  
60          configMap.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
61  
62          // Explicitly specify the directory to use for caching bundles.
63          // configMap.put(BundleCache.CACHE_PROFILE_DIR_PROP, "cache");
64  
65          try {
66              // Create host activator;
67  
68              List<Object> list = new ArrayList<Object>();
69  
70              // list.add(new HostActivator());
71              configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "org.xml.sax, org.xml.sax.helpers, javax.xml.parsers, javax.naming");
72              configMap.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);
73              configMap.put("felix.log.level", "4");
74  
75              // Now create an instance of the framework with
76              // our configuration properties and activator.
77              felix = new Felix(configMap);
78              felix.init();
79  
80              // otherProps.put(Constants.FRAMEWORK_STORAGE, "bundles");
81  
82              otherProps.put(AutoProcessor.AUTO_DEPLOY_DIR_PROPERTY, AutoProcessor.AUTO_DEPLOY_DIR_VALUE);
83              otherProps.put(AutoProcessor.AUTO_DEPLOY_ACTION_PROPERTY, AutoProcessor.AUTO_DEPLOY_START_VALUE + "," + AutoProcessor.AUTO_DEPLOY_INSTALL_VALUE);
84  
85              BundleContext felixBudleContext = felix.getBundleContext();
86  
87              AutoProcessor.process(otherProps, felixBudleContext);
88              // listen to errors
89              felixBudleContext.addFrameworkListener(frameworkErrorListener);
90              felixBudleContext.addBundleListener(myBundleListener);
91              // Now start Felix instance.
92              felix.start();
93              System.out.println("felix started");
94  
95          } catch (Exception ex) {
96              ex.printStackTrace();
97          }
98      }
99  
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 }