DUE TO SPAM, SIGN-UP IS DISABLED. Goto Selfserve wiki signup and request an account.
...
| Code Block |
|---|
package example; import java.util.Map; import java.util.HashMapio.*; import org.osgi.framework.launch.Framework*; import org.apache.felix.main.AutoProcessor; public class Main { private static Framework m_fwk = null; public static void main(String[] argv) throws Exception { // Print welcome banner. System.out.println("\nWelcome to Felix."); System.out.println("=================\n"); try { Map configMap = new HashMap(); m_fwk = getFrameworkFactory().newInstance(configMapnull); m_fwk.init() AutoProcessor.process(configMapnull, m_fwk.getBundleContext()); m_fwk.start(); m_fwk.waitForStop(); System.exit(0); } catch (Exception ex) { System.err.println("Could not create framework: " + ex); ex.printStackTrace(); System.exit(-1); } } private static FrameworkFactory getFrameworkFactory() throws Exception { URL url = Main.class.getClassLoader().getResource( "META-INF/services/org.osgi.framework.launch.FrameworkFactory"); if (url != null) { BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream())); try { for (String s = br.readLine(); s != null; s = br.readLine()) { s = s.trim(); // Try to load first non-empty, non-commented line. if ((s.length() > 0) && (s.charAt(0) != '#')) { return (FrameworkFactory) Class.forName(s).newInstance(); } } } finally { if (br != null) br.close(); } } throw new Exception("Could not find framework factory."); } } |
This launcher relies on the default behavior of AutoProcessor for auto- to automatically deploy of the shell bundles. This simple, generic launcher provides a good starting point if the features of the default Felix launcher are is not necessarysufficient. Since very few configuration properties are specified, the default values are used. For the bundle auto-deploy directory, "bundle" in the current directory is used, while for the framework bundle cache, "felix-cache" in the current directory is used.
By breaking down the above source code into small chunks, it is quite easy to see what is going on.
| Code Block |
|---|
Map configMap m_fwk = new HashMap(getFrameworkFactory().newInstance(null);
|
This simply creates a map to hold configuration properties.
| Code Block |
|---|
configMap.put(AutoActivator.AUTO_START_PROP + ".1", "file:bundle/org.apache.felix.shell-1.0.2.jar " + "file:bundle/org.apache.felix.shell.tui-1.0.2.jar"); |
This sets the AutoActivator.AUTO_START_PROP configuration property (string value "felix.auto.start"), which is a space-delimited list of bundle URLs that the framework will automatically install and start when the framework starts. However, this property key cannot be used as is; it must be appended with a "." and then a number, where the number represents the start level for the bundle when it is installed. In this particular example, ".1" is appended to the property name, thus the two bundles will be installed into start level one. This example uses relative file: URLs, which will load the bundles from the bundle/ directory assuming that the launcher is started from the root directory of the launcher project. It is also possible to specify absolute URLs or remote URLs.
m_fwk.init()
|
These steps get a the framework factory service and use it to create a framework instance with a default configuration. Once the framework instance is created, it is initialized with init().
| Code Block |
|---|
|
| Code Block |
List list = new ArrayList(); list.add(new AutoActivator(configMap)); configMapAutoProcessor.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list); process(null, m_fwk.getBundleContext()); |
The AutorProcessor will automatically deploy bundles in the auto-deploy directory and any referenced from the auto-install/start properties. Since we are using an empty configuration, the auto-deploy directory is the bundle directory in the current directory and there are no auto properties. Therefore, in this case, the two shell bundles will be installedThis above creates a list to hold custom framework activators and adds an instance of org.apache.felix.main.AutoActivator to it, which will process the auto-install and auto-start configuration properties during framework startup. The list of activators is then added to the configuration map.
| Code Block |
|---|
m_felix = new Felix(configMap);
m_felixfwk.start();
|
These steps create the framework instance and start it. The configuration property map is passed into the Felix constructor.
| Code Block |
|---|
m_felixfwk.waitForStop(); System.exit(0); |
These final steps start the framework and cause the launching application thread to wait for the framework to stop and when it does the launching thread calls System.exit() to make sure the VM actually exits.
...
| No Format |
|---|
javac -d . -classpath lib/org.apache.felix.main-12.40.0.jar src/example/Main.java |
...
| No Format |
|---|
java -cp .:lib/org.apache.felix.main-12.40.0.jar example.Main |
After executing this command, a "felix-cache/" directory is created that contains the installed cached bundles, which were installed from the bundle/ directory.
...