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