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