View Javadoc
1   /**
2    * Copyright (c) 2004-2011 QOS.ch
3    * All rights reserved.
4    *
5    * Permission is hereby granted, free  of charge, to any person obtaining
6    * a  copy  of this  software  and  associated  documentation files  (the
7    * "Software"), to  deal in  the Software without  restriction, including
8    * without limitation  the rights to  use, copy, modify,  merge, publish,
9    * distribute,  sublicense, and/or sell  copies of  the Software,  and to
10   * permit persons to whom the Software  is furnished to do so, subject to
11   * the following conditions:
12   *
13   * The  above  copyright  notice  and  this permission  notice  shall  be
14   * included in all copies or substantial portions of the Software.
15   *
16   * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
17   * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
18   * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
19   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20   * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21   * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
22   * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23   *
24   */
25  package org.slf4j.test_osgi;
26  
27  import java.util.ArrayList;
28  import java.util.List;
29  import java.util.Properties;
30  
31  import org.apache.felix.framework.Felix;
32  import org.apache.felix.framework.util.FelixConstants;
33  import org.apache.felix.framework.util.StringMap;
34  import org.apache.felix.main.AutoProcessor;
35  import org.osgi.framework.Bundle;
36  import org.osgi.framework.BundleContext;
37  import org.osgi.framework.BundleException;
38  import org.osgi.framework.Constants;
39  
40  /**
41   * Runs a hosted version of Felix for testing purposes. Any bundle errors are
42   * reported via the FrameworkListener passed to the constructor.
43   * 
44   * @author Ceki Gülcü
45   */
46  public class FelixHost {
47  
48      private Felix felix = null;
49  
50      Properties otherProps = new Properties();
51  
52      final FrameworkErrorListener frameworkErrorListener;
53      final CheckingBundleListener myBundleListener;
54  
55      public FelixHost(FrameworkErrorListener frameworkErrorListener, CheckingBundleListener myBundleListener) {
56          this.frameworkErrorListener = frameworkErrorListener;
57          this.myBundleListener = myBundleListener;
58      }
59  
60      public void doLaunch() {
61          // Create a case-insensitive configuration property map.
62          StringMap configMap = new StringMap();
63          // Configure the Felix instance to be embedded.
64          // configMap.put(FelixConstants.EMBEDDED_EXECUTION_PROP, "true");
65          // Add core OSGi packages to be exported from the class path
66          // via the system bundle.
67          configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES, "org.osgi.framework; version=1.3.0," + "org.osgi.service.packageadmin; version=1.2.0,"
68                          + "org.osgi.service.startlevel; version=1.0.0," + "org.osgi.service.url; version=1.0.0");
69  
70          configMap.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
71  
72          // Explicitly specify the directory to use for caching bundles.
73          // configMap.put(BundleCache.CACHE_PROFILE_DIR_PROP, "cache");
74  
75          try {
76              // Create host activator;
77  
78              List<Object> list = new ArrayList<>();
79  
80              // list.add(new HostActivator());
81              configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "org.xml.sax, org.xml.sax.helpers, javax.xml.parsers, javax.naming");
82              configMap.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);
83              configMap.put("felix.log.level", "4");
84  
85              // Now create an instance of the framework with
86              // our configuration properties and activator.
87              felix = new Felix(configMap);
88              felix.init();
89  
90              // otherProps.put(Constants.FRAMEWORK_STORAGE, "bundles");
91  
92              otherProps.put(AutoProcessor.AUTO_DEPLOY_DIR_PROPERTY, AutoProcessor.AUTO_DEPLOY_DIR_VALUE);
93              otherProps.put(AutoProcessor.AUTO_DEPLOY_ACTION_PROPERTY, AutoProcessor.AUTO_DEPLOY_START_VALUE + "," + AutoProcessor.AUTO_DEPLOY_INSTALL_VALUE);
94  
95              BundleContext felixBudleContext = felix.getBundleContext();
96  
97              AutoProcessor.process(otherProps, felixBudleContext);
98              // listen to errors
99              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 }