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