Karl Pauls | bbc2957 | 2010-12-26 21:07:41 +0000 | [diff] [blame^] | 1 | |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 2 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 3 | <html><head> |
| 4 | |
| 5 | |
| 6 | |
| 7 | <title>Apache Felix - Apache Felix Framework Launching and Embedding</title> |
| 8 | <link rel="stylesheet" href="apache-felix-framework-launching-and-embedding_files/site.css" type="text/css" media="all"> |
| 9 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| 10 | </head><body> |
Karl Pauls | 4994cb0 | 2008-12-20 17:15:21 +0000 | [diff] [blame] | 11 | <div class="title"><div class="logo"><a href="http://felix.apache.org/site/index.html"><img alt="Apache Felix" src="apache-felix-framework-launching-and-embedding_files/logo.png" border="0"></a></div><div class="header"><a href="http://www.apache.org/"><img alt="Apache" src="apache-felix-framework-launching-and-embedding_files/apache.png" border="0"></a></div></div> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 12 | <div class="menu"> |
| 13 | <ul> |
| 14 | <li><a href="http://felix.apache.org/site/news.html" title="news">news</a></li> |
| 15 | <li><a href="http://felix.apache.org/site/license.html" title="license">license</a></li> |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 16 | <li><a href="http://felix.apache.org/site/downloads.cgi" class="external-link" rel="nofollow">downloads</a></li> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 17 | <li><a href="http://felix.apache.org/site/documentation.html" title="documentation">documentation</a></li> |
| 18 | <li><a href="http://felix.apache.org/site/mailinglists.html" title="mailinglists">mailing lists</a></li> |
| 19 | <li><a href="http://felix.apache.org/site/contributing.html" title="Contributing">contributing</a></li> |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 20 | <li><a href="http://www.apache.org/" class="external-link" rel="nofollow">asf</a></li> |
| 21 | <li><a href="http://www.apache.org/foundation/sponsorship.html" class="external-link" rel="nofollow">sponsorship</a></li> |
| 22 | <li><a href="http://www.apache.org/foundation/thanks.html" class="external-link" rel="nofollow">sponsors</a> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 23 | <!-- ApacheCon Ad --> |
| 24 | <iframe src="apache-felix-framework-launching-and-embedding_files/button.html" style="border-width: 0pt; float: left;" frameborder="0" height="135" scrolling="no" width="135"></iframe> |
| 25 | <p style="height: 100px;"> |
| 26 | <!-- ApacheCon Ad --> |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 27 | </p></li></ul> |
| 28 | </div> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 29 | <div class="main"> |
| 30 | <h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-ApacheFelixFrameworkLaunchingandEmbedding"></a>Apache Felix Framework Launching and Embedding</h1> |
| 31 | |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 32 | <p><em>[This document describes framework launching introduced in Felix |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 33 | Framework 2.0.0 and continuing with the latest releases; it is |
| 34 | incompatible with older versions of the Felix framework.]</em></p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 35 | |
| 36 | <ul> |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 37 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-introduction">Introduction</a></li> |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 38 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-overview">OSGi Launching and Embedding API Overview</a> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 39 | <ul> |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 40 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-creatingandconfiguring">Creating and Configuring the Framework Instance</a></li> |
| 41 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-startinginstance">Starting the Framework Instance</a></li> |
| 42 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-stoppinginstance">Stopping the Framework Instance</a></li> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 43 | </ul> |
| 44 | </li> |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 45 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-launching">Launching Felix</a> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 46 | <ul> |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 47 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-standardlauncher">Standard Felix Launcher</a></li> |
| 48 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-customlauncher">Custom Felix Launcher</a></li> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 49 | </ul> |
| 50 | </li> |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 51 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-embedding">Embedding Felix</a> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 52 | <ul> |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 53 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-hostinteraction">Host/Felix Interaction</a></li> |
| 54 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-hostservices">Providing Host Application Services</a></li> |
| 55 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-hostserviceusage">Using Services Provided by Bundles</a> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 56 | <ul> |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 57 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-servicereflection">Using Bundle Services via Reflection</a></li> |
| 58 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-serviceother">Other Approaches</a></li> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 59 | </ul> |
| 60 | </li> |
| 61 | </ul> |
| 62 | </li> |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 63 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-caveat">Caveat</a></li> |
| 64 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-feedback">Feedback</a></li> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 65 | </ul> |
| 66 | |
| 67 | |
| 68 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-introduction"></a></p> |
| 69 | |
| 70 | <h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-Introduction"></a>Introduction</h1> |
| 71 | |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 72 | <p>The Apache Felix Framework is intended to be easily launchable and |
| 73 | embeddable. For example, the Felix framework implementation avoids the |
| 74 | use of system properties for configuration, since these are globals and |
| 75 | can cause interference if multiple framework instances are created in |
| 76 | the same VM. The framework also tries to multiplex singleton |
| 77 | facilities, like the URL stream handler factory. The goal is to make it |
| 78 | possible to use the framework in a variety of scenarios; however, this |
| 79 | is still just a goal. In other words, this is a work in progress and if |
| 80 | any issues arise, it would be greatly appreciated if they are brought |
| 81 | to the attention of the Felix community. The next section provides an |
| 82 | overview of the standard OSGi launching and embedding API for |
| 83 | frameworks, while the remainder of the document is divided into two |
| 84 | sections, one focusing on how to launch Felix and one focusing on how |
| 85 | to embed Felix into a host application.</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 86 | |
| 87 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-overview"></a></p> |
| 88 | |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 89 | <h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-OSGiLaunchingandEmbeddingAPIOverview"></a>OSGi Launching and Embedding API Overview</h1> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 90 | |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 91 | <p>The Felix framework is implemented by the <tt>org.apache.felix.framework.Felix</tt> class or just <tt>Felix</tt> |
| 92 | for short. As part of the R4.2 OSGi specification, the launching and |
| 93 | embedding API of the OSGi framework has been standardized. The approach |
| 94 | is to have the framework implement the <tt>org.osgi.framework.launch.Framework</tt> interface, which extends the <tt>org.osgi.framework.Bundle</tt> interface. These interfaces provide the necessary means to launch and manage framework instances. The <tt>Bundle</tt> interface is defined as:</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 95 | |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 96 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 97 | <pre class="code-java"><span class="code-keyword">public</span> <span class="code-keyword">interface</span> Bundle |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 98 | { |
| 99 | BundleContext getBundleContext(); |
| 100 | <span class="code-object">long</span> getBundleId(); |
| 101 | URL getEntry(<span class="code-object">String</span> name); |
| 102 | Enumeration getEntryPaths(<span class="code-object">String</span> path); |
| 103 | Enumeration findEntries(<span class="code-object">String</span> path, <span class="code-object">String</span> filePattern, <span class="code-object">boolean</span> recurse); |
| 104 | Dictionary getHeaders(); |
| 105 | Dictionary getHeaders(<span class="code-object">String</span> locale); |
| 106 | <span class="code-object">long</span> getLastModified(); |
| 107 | <span class="code-object">String</span> getLocation(); |
| 108 | URL getResource(<span class="code-object">String</span> name); |
| 109 | Enumeration getResources(<span class="code-object">String</span> name) <span class="code-keyword">throws</span> IOException; |
| 110 | ServiceReference[] getRegisteredServices(); |
| 111 | ServiceReference[] getServicesInUse(); |
| 112 | <span class="code-object">int</span> getState(); |
| 113 | <span class="code-object">String</span> getSymbolicName(); |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 114 | Version getVersion(); |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 115 | <span class="code-object">boolean</span> hasPermission(<span class="code-object">Object</span> obj); |
| 116 | <span class="code-object">Class</span> loadClass(<span class="code-object">String</span> name) <span class="code-keyword">throws</span> ClassNotFoundException; |
| 117 | void start() <span class="code-keyword">throws</span> BundleException; |
| 118 | void stop() <span class="code-keyword">throws</span> BundleException; |
| 119 | void uninstall() <span class="code-keyword">throws</span> BundleException; |
| 120 | void update() <span class="code-keyword">throws</span> BundleException; |
| 121 | void update(InputStream is) <span class="code-keyword">throws</span> BundleException; |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 122 | } |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 123 | </pre> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 124 | </div></div> |
| 125 | |
| 126 | <p>The <tt>Framework</tt> interface is defined as:</p> |
| 127 | |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 128 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 129 | <pre class="code-java"><span class="code-keyword">public</span> <span class="code-keyword">interface</span> Framework <span class="code-keyword">extends</span> Bundle |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 130 | { |
| 131 | void init(); |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 132 | FrameworkEvent waitForStop(<span class="code-object">long</span> timeout); |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 133 | } |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 134 | </pre> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 135 | </div></div> |
| 136 | |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 137 | <p>To actually construct a framework instance, the R4.2 specification defines the <tt>FrameworkFactory</tt> interface:</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 138 | |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 139 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 140 | <pre class="code-java"><span class="code-keyword">public</span> <span class="code-keyword">interface</span> FrameworkFactory |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 141 | { |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 142 | Framework newFramework(Map config); |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 143 | } |
| 144 | </pre> |
| 145 | </div></div> |
| 146 | |
| 147 | <p>The framework factory can be used to create configured framework instances. It is obtained following the standard <tt>META-INF/services</tt> approach.</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 148 | |
| 149 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-creatingandconfiguring"></a></p> |
| 150 | |
| 151 | <h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-CreatingandConfiguringtheFrameworkInstance"></a>Creating and Configuring the Framework Instance</h2> |
| 152 | |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 153 | <p>You use the framework factory to construct and configure a framework |
| 154 | instance (or by directly instantiating the Felix class). The |
Karl Pauls | bbc2957 | 2010-12-26 21:07:41 +0000 | [diff] [blame^] | 155 | configuration map may contain any of the framework configuration |
| 156 | properties listed in the <a href="http://felix.apache.org/site/apache-felix-framework-configuration-properties.html" title="Apache Felix Framework Configuration Properties">Apache Felix Framework Configuration Properties</a> |
| 157 | document, not the launcher configuration properties. The configuration |
| 158 | map is copied and the keys are treated as case insensitive. You are not |
| 159 | able to change the framework's configuration after construction. If you |
| 160 | need a different configuration, you must create a new framework |
| 161 | instance.</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 162 | |
Karl Pauls | bbc2957 | 2010-12-26 21:07:41 +0000 | [diff] [blame^] | 163 | <div class="panelMacro"><table class="warningMacro"><colgroup><col width="24"><col></colgroup><tbody><tr><td valign="top"><img src="apache-felix-framework-launching-and-embedding_files/forbidden.gif" alt="" align="absmiddle" border="0" height="16" width="16"></td><td><b>WARNING</b><br>Felix configuration properties have change considerably starting from <tt>1.4.0</tt>; if you are upgrading from an earlier version, the <a href="http://felix.apache.org/site/apache-felix-framework-configuration-properties.html#ApacheFelixFrameworkConfigurationProperties-migrating">configuration property document</a> describes the configuration property changes.</td></tr></tbody></table></div> |
Karl Pauls | 4994cb0 | 2008-12-20 17:15:21 +0000 | [diff] [blame] | 164 | |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 165 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-startinginstance"></a></p> |
| 166 | |
| 167 | <h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-StartingtheFrameworkInstance"></a>Starting the Framework Instance</h2> |
| 168 | |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 169 | <p>The <tt>start()</tt> method is used to start the framework instance. If the <tt>init()</tt> method was not invoked prior to calling <tt>start()</tt>, then it is invoked by <tt>start()</tt>. The two methods result in two different framework state transitions:</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 170 | |
| 171 | <ul> |
| 172 | <li><tt>init()</tt> results in the framework instance in the <tt>Bundle.STARTING</tt> state.</li> |
| 173 | <li><tt>start()</tt> results in the framework instance in the <tt>Bundle.ACTIVE</tt> state.</li> |
| 174 | </ul> |
| 175 | |
| 176 | |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 177 | <p>The <tt>init()</tt> method is necessary since the framework does not have a <tt>BundleContext</tt> when it is first created, so a transition to the <tt>Bundle.STARTING</tt> state is required to acquire its context (via <tt>Bundle.getBundleContext()</tt>) for performing various tasks, such as installing bundles. Note that the Felix framework also provides the <tt>felix.systembundle.activators</tt> property that serves a similar purpose, but is not standard. After the <tt>init()</tt> method completes, the follow actions have been performed:</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 178 | |
| 179 | <ul> |
| 180 | <li>Event handling is enabled.</li> |
| 181 | <li>The security manager is installed if it is enabled.</li> |
| 182 | <li>The framework is set to start level 0.</li> |
| 183 | <li>All bundles in the bundle caches are reified and their state is set to <tt>Bundle.INSTALLED</tt>.</li> |
| 184 | <li>The framework gets a valid <tt>BundleContext</tt>.</li> |
| 185 | <li>All framework-provided services are made available (e.g., PackageAdmin, StartLevel, etc.).</li> |
| 186 | <li>The framework enters the <tt>Bundle.STARTING</tt> state.</li> |
| 187 | </ul> |
| 188 | |
| 189 | |
| 190 | <p>A call to <tt>start()</tt> is necessary to start the framework instance, if the <tt>init()</tt> method is invoked manually. Invoking <tt>init()</tt> or <tt>start()</tt> on an already started framework as no effect.</p> |
| 191 | |
| 192 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-stoppinginstance"></a></p> |
| 193 | |
| 194 | <h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-StoppingtheFrameworkInstance"></a>Stopping the Framework Instance</h2> |
| 195 | |
| 196 | <p>To stop the framework instance, invoke the <tt>stop()</tt> method, which will asynchronously stop the framework. To know when the framework has finished its shutdown sequence, use the <tt>waitForStop()</tt> method to wait until it is complete. A stopped framework will be in the <tt>Bundle.RESOLVED</tt> state. It is possible to restart the framework, using the normal combination of <tt>init()</tt>/<tt>start()</tt> methods as previously described.</p> |
| 197 | |
| 198 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-launching"></a></p> |
| 199 | |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 200 | <h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-LaunchingaFramework"></a>Launching a Framework</h1> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 201 | |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 202 | <p>Launching a framework is fairly simple and involves only four steps:</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 203 | |
| 204 | <ol> |
| 205 | <li>Define some configuration properties.</li> |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 206 | <li>Obtain framework factory.</li> |
| 207 | <li>Use factory to create framework with the configuration properties.</li> |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 208 | <li>Invoke the <tt>Framework.start()</tt> method.</li> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 209 | </ol> |
| 210 | |
| 211 | |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 212 | <p>In reality, the first step is optional, since all properties will |
| 213 | have reasonable defaults, but if you are creating a launcher you will |
| 214 | generally want to more than that, such as automatically installing and |
| 215 | starting bundles when you start the framework instance. The default |
| 216 | Felix launcher defines reusable functionality to automatically install |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 217 | and/or start bundles upon framework startup; see the <a href="http://felix.apache.org/site/apache-felix-framework-usage-documentation.html#ApacheFelixFrameworkUsageDocumentation-configuringfelix">usage document</a> for more information on configuring the Felix framework and on the various configuration properties.</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 218 | |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 219 | <p>The remainder of this section describes how the standard Felix launcher works as well as how to create a custom launcher.</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 220 | |
| 221 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-standardlauncher"></a></p> |
| 222 | |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 223 | <h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-StandardFelixFrameworkLauncher"></a>Standard Felix Framework Launcher</h2> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 224 | |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 225 | <p>The standard Felix framework launcher is very simple and is not |
| 226 | intended to solve every possible requirement; it is intended to work |
| 227 | for most standard situations. Most special launching requirements |
| 228 | should be resolved by creating a custom launcher. This section |
| 229 | describes how the standard launcher works. The following code |
| 230 | represents the complete <tt>main()</tt> method of the standard launcher, each numbered comment will be described in more detail below:</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 231 | |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 232 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 233 | <pre class="code-java"><span class="code-keyword">public</span> <span class="code-keyword">static</span> void main(<span class="code-object">String</span>[] args) <span class="code-keyword">throws</span> Exception |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 234 | { |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 235 | <span class="code-comment">// (1) Check <span class="code-keyword">for</span> command line arguments and verify usage. |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 236 | </span> <span class="code-object">String</span> bundleDir = <span class="code-keyword">null</span>; |
| 237 | <span class="code-object">String</span> cacheDir = <span class="code-keyword">null</span>; |
| 238 | <span class="code-object">boolean</span> expectBundleDir = <span class="code-keyword">false</span>; |
| 239 | <span class="code-keyword">for</span> (<span class="code-object">int</span> i = 0; i < args.length; i++) |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 240 | { |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 241 | <span class="code-keyword">if</span> (args[i].equals(BUNDLE_DIR_SWITCH)) |
| 242 | { |
| 243 | expectBundleDir = <span class="code-keyword">true</span>; |
| 244 | } |
| 245 | <span class="code-keyword">else</span> <span class="code-keyword">if</span> (expectBundleDir) |
| 246 | { |
| 247 | bundleDir = args[i]; |
| 248 | expectBundleDir = <span class="code-keyword">false</span>; |
| 249 | } |
| 250 | <span class="code-keyword">else</span> |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 251 | { |
| 252 | cacheDir = args[i]; |
| 253 | } |
| 254 | } |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 255 | |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 256 | <span class="code-keyword">if</span> ((args.length > 3) || (expectBundleDir && bundleDir == <span class="code-keyword">null</span>)) |
| 257 | { |
| 258 | <span class="code-object">System</span>.out.println(<span class="code-quote">"Usage: [-b <bundle-deploy-dir>] [<bundle-cache-dir>]"</span>); |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 259 | <span class="code-object">System</span>.exit(0); |
| 260 | } |
| 261 | |
| 262 | <span class="code-comment">// (2) Load system properties. |
| 263 | </span> Main.loadSystemProperties(); |
| 264 | |
| 265 | <span class="code-comment">// (3) Read configuration properties. |
| 266 | </span> Properties configProps = Main.loadConfigProperties(); |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 267 | <span class="code-keyword">if</span> (configProps == <span class="code-keyword">null</span>) |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 268 | { |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 269 | <span class="code-object">System</span>.err.println(<span class="code-quote">"No "</span> + CONFIG_PROPERTIES_FILE_VALUE + <span class="code-quote">" found."</span>); |
| 270 | configProps = <span class="code-keyword">new</span> Properties(); |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 273 | <span class="code-comment">// (4) Copy framework properties from the system properties. |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 274 | </span> Main.copySystemProperties(configProps); |
| 275 | |
| 276 | <span class="code-comment">// (5) Use the specified auto-deploy directory over <span class="code-keyword">default</span>. |
| 277 | </span> <span class="code-keyword">if</span> (bundleDir != <span class="code-keyword">null</span>) |
| 278 | { |
| 279 | configProps.setProperty(AutoProcessor.AUTO_DEPLOY_DIR_PROPERY, bundleDir); |
| 280 | } |
| 281 | |
| 282 | <span class="code-comment">// (6) Use the specified bundle cache directory over <span class="code-keyword">default</span>. |
| 283 | </span> <span class="code-keyword">if</span> (cacheDir != <span class="code-keyword">null</span>) |
| 284 | { |
| 285 | configProps.setProperty(Constants.FRAMEWORK_STORAGE, cacheDir); |
| 286 | } |
| 287 | |
| 288 | <span class="code-comment">// (7) Add a shutdown hook to clean stop the framework. |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 289 | </span> <span class="code-object">String</span> enableHook = configProps.getProperty(SHUTDOWN_HOOK_PROP); |
| 290 | <span class="code-keyword">if</span> ((enableHook == <span class="code-keyword">null</span>) || !enableHook.equalsIgnoreCase(<span class="code-quote">"<span class="code-keyword">false</span>"</span>)) |
| 291 | { |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 292 | <span class="code-object">Runtime</span>.getRuntime().addShutdownHook(<span class="code-keyword">new</span> <span class="code-object">Thread</span>(<span class="code-quote">"Felix Shutdown Hook"</span>) { |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 293 | <span class="code-keyword">public</span> void run() |
| 294 | { |
| 295 | <span class="code-keyword">try</span> |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 296 | { |
| 297 | <span class="code-keyword">if</span> (m_fwk != <span class="code-keyword">null</span>) |
| 298 | { |
| 299 | m_fwk.stop(); |
| 300 | m_fwk.waitForStop(0); |
| 301 | } |
| 302 | } |
| 303 | <span class="code-keyword">catch</span> (Exception ex) |
| 304 | { |
| 305 | <span class="code-object">System</span>.err.println(<span class="code-quote">"Error stopping framework: "</span> + ex); |
| 306 | } |
| 307 | } |
| 308 | }); |
| 309 | } |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 310 | |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 311 | <span class="code-keyword">try</span> |
| 312 | { |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 313 | <span class="code-comment">// (8) Create an instance and initialize the framework. |
| 314 | </span> FrameworkFactory factory = getFrameworkFactory(); |
| 315 | m_fwk = factory.newFramework(configProps); |
| 316 | m_fwk.init(); |
| 317 | <span class="code-comment">// (9) Use the system bundle context to process the auto-deploy |
| 318 | </span> <span class="code-comment">// and auto-install/auto-start properties. |
| 319 | </span> AutoProcessor.process(configProps, m_fwk.getBundleContext()); |
| 320 | <span class="code-comment">// (10) Start the framework. |
| 321 | </span> m_fwk.start(); |
| 322 | <span class="code-comment">// (11) Wait <span class="code-keyword">for</span> framework to stop to exit the VM. |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 323 | </span> m_fwk.waitForStop(0); |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 324 | <span class="code-object">System</span>.exit(0); |
| 325 | } |
| 326 | <span class="code-keyword">catch</span> (Exception ex) |
| 327 | { |
| 328 | <span class="code-object">System</span>.err.println(<span class="code-quote">"Could not create framework: "</span> + ex); |
| 329 | ex.printStackTrace(); |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 330 | <span class="code-object">System</span>.exit(0); |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 331 | } |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 332 | } |
| 333 | </pre> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 334 | </div></div> |
| 335 | |
| 336 | <p>The general steps of the standard launcher are quite straightforward:</p> |
| 337 | |
| 338 | <ol> |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 339 | <li>The launcher supports setting the auto-deploy directory (with the <tt>-b</tt> |
| 340 | switch) and setting the bundle cache path with a single argument, so |
| 341 | check for this and issue a usage message it there are more than one |
| 342 | arguments.</li> |
| 343 | <li>Load any system properties specified in the <tt>system.properties</tt> file; this file is typically located in the <tt>conf/</tt> directory of the Felix installation directory, but it can be specified directly using the <tt>felix.system.properties</tt> |
| 344 | system property. This file is not needed to launch Felix and is |
| 345 | provided merely for convenience when system properties must be |
| 346 | specified. The file is a standard Java properties file, but it also |
| 347 | supports property substitution using <tt>${<property-name</tt>} syntax. Property substitution can be nested; only system properties will be used for substitution.</li> |
| 348 | <li>Load any configuration properties specified in the <tt>config.properties</tt> file; this file is typically located in the <tt>conf/</tt> directory of the Felix installation directory, but it can be specified directly using the <tt>felix.config.properties</tt> |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 349 | system property. This file is used to configure the framework instance |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 350 | created by the launcher. The file is a standard Java properties file, |
| 351 | but it also supports property substitution using "<tt>${<property-name></tt>}" |
| 352 | syntax. Property substitution can be nested; configuration and system |
| 353 | properties will be used for substitution with configuration properties |
| 354 | having precedence.</li> |
| 355 | <li>For convenience, any configuration |
| 356 | properties that are set as system properties are copied into the set of |
| 357 | configuration properties. This provide an easy way to add to or |
| 358 | override configuration properties specified in the <tt>config.properties</tt> file, since the Felix instance will never look at system properties for configuration.</li> |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 359 | <li>If the <tt>-b</tt> switch was used to specify an auto-deploy directory, then use that to set the value of <tt>felix.auto.deploy.dir</tt>.</li> |
| 360 | <li>If a single command-line argument is specified, then use that to set the value of <tt>org.osgi.framework.storage</tt>; relative paths are relative to the current directory unless the <tt>felix.cache.rootdir</tt> property is set.</li> |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 361 | <li>Add a shutdown hook to cleanly stop the framework, unless the hook is disabled.</li> |
| 362 | <li>Create a framework instance using the <tt>FrameworkFactory</tt> passing in the configuration properties, then initialize the factory instance; see the <a href="#ApacheFelixFrameworkLaunchingandEmbedding-customlauncher">custom launcher example</a> below to see how the META-INF/services <tt>FrameworkFactory</tt> is obtained.</li> |
| 363 | <li>Use <tt>org.apache.felix.main.AutoProcessor</tt>, which will automatically deploy any bundles in the auto-deploy directory as well as bundles specified in the <tt>felix.auto.install</tt> and <tt>felix.auto.start</tt> |
| 364 | configuration properties during framework startup to automatically |
| 365 | install and/or start bundles; see the usage document for more |
| 366 | information <a href="http://felix.apache.org/site/apache-felix-framework-usage-documentation.html#ApacheFelixFrameworkUsageDocumentation-configuringframework">configuration properties</a> and <a href="http://felix.apache.org/site/apache-felix-framework-usage-documentation.html#ApacheFelixFrameworkUsageDocumentation-autodeploy">bundle auto-deploy</a>.</li> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 367 | <li>Invoke <tt>waitForStop()</tt> to wait for the framework to stop to force the VM to exit; this is necessary because the framework never calls <tt>System.exit()</tt> and some libraries (e.g., Swing) create threads that will not allow the VM to exit.</li> |
| 368 | </ol> |
| 369 | |
| 370 | |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 371 | <p>The framework is not active until the <tt>start()</tt> method is |
| 372 | called. If no shell bundles are installed and started or if there is |
| 373 | difficulty locating the shell bundles specified in the auto-start |
| 374 | property, then it will appear as if the framework is hung, but it is |
| 375 | actually running without any way to interact with it since the shell |
| 376 | bundles provide the only means of interaction.</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 377 | |
| 378 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-customlauncher"></a></p> |
| 379 | |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 380 | <h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-CustomFrameworkLauncher"></a>Custom Framework Launcher</h2> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 381 | |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 382 | <p>This section creates a bare-bones launcher to demonstrate the |
| 383 | minimum requirements for creating an interactive launcher for the Felix |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 384 | framework. This example uses the standard Gogo shell bundles for |
| 385 | interactivity, but any other bundles could be used instead. This |
| 386 | example launcher project has the following directory structure:</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 387 | |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 388 | <div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent"> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 389 | <pre>launcher/ |
| 390 | lib/ |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 391 | org.apache.felix.main-3.0.0.jar |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 392 | bundle/ |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 393 | org.apache.felix.gogo.command-0.6.0.jar |
| 394 | org.apache.felix.gogo.runtime-0.6.0.jar |
| 395 | org.apache.felix.gogo.shell-0.6.0.jar |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 396 | src/ |
| 397 | example/ |
| 398 | Main.java |
| 399 | </pre> |
| 400 | </div></div> |
| 401 | |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 402 | <p>The <tt>lib/</tt> directory contains Felix' main JAR file, which |
| 403 | also contains the OSGi core interfaces. The main JAR file is used so |
| 404 | that we can reuse the default launcher's auto-install/auto-start |
| 405 | configuration property handling; if these capabilities are not needed, |
| 406 | then it would be possible to use the framework JAR file instead of the |
| 407 | main JAR file. The <tt>bundle/</tt> directory contains the shell |
| 408 | service and textual shell interface bundles that will be used for |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 409 | interacting with the framework instance. Note: If you do not launch the |
| 410 | framework with interactive bundles, it will appear as if the framework |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 411 | instance is hung, but it is actually just sitting there waiting for |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 412 | someone to tell it to do something. The <tt>src/example/</tt> directory contains the following <tt>Main.java</tt> file, which is a very simplistic framework launcher.</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 413 | |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 414 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 415 | <pre class="code-java"><span class="code-keyword">package</span> example; |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 416 | |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 417 | <span class="code-keyword">import</span> java.io.*; |
| 418 | <span class="code-keyword">import</span> org.osgi.framework.launch.*; |
| 419 | <span class="code-keyword">import</span> org.apache.felix.main.AutoProcessor; |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 420 | |
| 421 | <span class="code-keyword">public</span> class Main |
| 422 | { |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 423 | <span class="code-keyword">private</span> <span class="code-keyword">static</span> Framework m_fwk = <span class="code-keyword">null</span>; |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 424 | |
| 425 | <span class="code-keyword">public</span> <span class="code-keyword">static</span> void main(<span class="code-object">String</span>[] argv) <span class="code-keyword">throws</span> Exception |
| 426 | { |
| 427 | <span class="code-comment">// Print welcome banner. |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 428 | </span> <span class="code-object">System</span>.out.println(<span class="code-quote">"\nWelcome to My Launcher"</span>); |
| 429 | <span class="code-object">System</span>.out.println(<span class="code-quote">"======================\n"</span>); |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 430 | |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 431 | <span class="code-keyword">try</span> |
| 432 | { |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 433 | m_fwk = getFrameworkFactory().newFramework(<span class="code-keyword">null</span>); |
| 434 | m_fwk.init() |
| 435 | AutoProcessor.process(<span class="code-keyword">null</span>, m_fwk.getBundleContext()); |
| 436 | m_fwk.start(); |
Karl Pauls | bbc2957 | 2010-12-26 21:07:41 +0000 | [diff] [blame^] | 437 | m_fwk.waitForStop(0); |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 438 | <span class="code-object">System</span>.exit(0); |
| 439 | } |
| 440 | <span class="code-keyword">catch</span> (Exception ex) |
| 441 | { |
| 442 | <span class="code-object">System</span>.err.println(<span class="code-quote">"Could not create framework: "</span> + ex); |
| 443 | ex.printStackTrace(); |
| 444 | <span class="code-object">System</span>.exit(-1); |
| 445 | } |
| 446 | } |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 447 | |
| 448 | <span class="code-keyword">private</span> <span class="code-keyword">static</span> FrameworkFactory getFrameworkFactory() <span class="code-keyword">throws</span> Exception |
| 449 | { |
| 450 | URL url = Main.class.getClassLoader().getResource( |
| 451 | <span class="code-quote">"META-INF/services/org.osgi.framework.launch.FrameworkFactory"</span>); |
| 452 | <span class="code-keyword">if</span> (url != <span class="code-keyword">null</span>) |
| 453 | { |
| 454 | BufferedReader br = <span class="code-keyword">new</span> BufferedReader(<span class="code-keyword">new</span> InputStreamReader(url.openStream())); |
| 455 | <span class="code-keyword">try</span> |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 456 | { |
| 457 | <span class="code-keyword">for</span> (<span class="code-object">String</span> s = br.readLine(); s != <span class="code-keyword">null</span>; s = br.readLine()) |
| 458 | { |
| 459 | s = s.trim(); |
| 460 | <span class="code-comment">// Try to load first non-empty, non-commented line. |
| 461 | </span> <span class="code-keyword">if</span> ((s.length() > 0) && (s.charAt(0) != '#')) |
| 462 | { |
| 463 | <span class="code-keyword">return</span> (FrameworkFactory) <span class="code-object">Class</span>.forName(s).newInstance(); |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | <span class="code-keyword">finally</span> |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 468 | { |
| 469 | <span class="code-keyword">if</span> (br != <span class="code-keyword">null</span>) br.close(); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | <span class="code-keyword">throw</span> <span class="code-keyword">new</span> Exception(<span class="code-quote">"Could not find framework factory."</span>); |
| 474 | } |
| 475 | } |
| 476 | </pre> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 477 | </div></div> |
| 478 | |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 479 | <p>This launcher relies on the default behavior of <tt>AutoProcessor</tt> |
| 480 | to automatically deploy the shell bundles. This simple, generic |
| 481 | launcher provides a good starting point if the default Felix launcher |
| 482 | is not sufficient. Since very few configuration properties are |
| 483 | specified, the default values are used. For the bundle auto-deploy |
| 484 | directory, "<tt>bundle</tt>" in the current directory is used, while for the framework bundle cache, "<tt>felix-cache</tt>" in the current directory is used.</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 485 | |
| 486 | <p>By breaking down the above source code into small chunks, it is quite easy to see what is going on.</p> |
| 487 | |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 488 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 489 | <pre class="code-java"> m_fwk = getFrameworkFactory().newFramework(<span class="code-keyword">null</span>); |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 490 | m_fwk.init() |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 491 | </pre> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 492 | </div></div> |
| 493 | |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 494 | <p>These steps get a the framework factory service and use it to create |
| 495 | a framework instance with a default configuration. Once the framework |
| 496 | instance is created, it is initialized with <tt>init()</tt>.</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 497 | |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 498 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 499 | <pre class="code-java"> AutoProcessor.process(<span class="code-keyword">null</span>, m_fwk.getBundleContext()); |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 500 | </pre> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 501 | </div></div> |
| 502 | |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 503 | <p>The <tt>AutorProcessor</tt> will automatically deploy bundles in the |
| 504 | auto-deploy directory and any referenced from the auto-install/start |
| 505 | properties. Since we are using an empty configuration, the auto-deploy |
| 506 | directory is the <tt>bundle</tt> directory in the current directory |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 507 | and there are no auto properties. Therefore, in this case, the shell |
| 508 | bundles will be installed.</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 509 | |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 510 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 511 | <pre class="code-java"> m_fwk.start(); |
Karl Pauls | bbc2957 | 2010-12-26 21:07:41 +0000 | [diff] [blame^] | 512 | m_fwk.waitForStop(0); |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 513 | <span class="code-object">System</span>.exit(0); |
| 514 | </pre> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 515 | </div></div> |
| 516 | |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 517 | <p>These final steps start the framework and cause the launching |
| 518 | application thread to wait for the framework to stop and when it does |
| 519 | the launching thread calls <tt>System.exit()</tt> to make sure the VM actually exits.</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 520 | |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 521 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 522 | <pre class="code-java"> <span class="code-keyword">private</span> <span class="code-keyword">static</span> FrameworkFactory getFrameworkFactory() <span class="code-keyword">throws</span> Exception |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 523 | { |
| 524 | ... |
| 525 | } |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 526 | </pre> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 527 | </div></div> |
| 528 | |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 529 | <p>This method retrieves the framework factory service by doing a |
| 530 | META-INF/services resource lookup, which it can use to obtain the |
| 531 | concrete class name for the factory. If you are using Java 6, then you |
| 532 | can use the <tt>ServiceLoader</tt> API in the JRE to further simplify the factory service lookup.</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 533 | |
| 534 | <p>The following command compiles the launcher when run from the root directory of the launcher project:</p> |
| 535 | |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 536 | <div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent"> |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 537 | <pre>javac -d . -classpath lib/org.apache.felix.main-3.0.0.jar src/example/Main.java |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 538 | </pre> |
| 539 | </div></div> |
| 540 | |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 541 | <p>After executing this command, an <tt>example/</tt> directory is |
| 542 | created in the current directory, which contains the generated class |
| 543 | file. The following command executes the simple launcher when run from |
| 544 | the root directory of the launcher project:</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 545 | |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 546 | <div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent"> |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 547 | <pre>java -cp .:lib/org.apache.felix.main-3.0.0.jar example.Main |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 548 | </pre> |
| 549 | </div></div> |
| 550 | |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 551 | <p>After executing this command, a "<tt>felix-cache/</tt>" directory is created that contains the cached bundles, which were installed from the <tt>bundle/</tt> directory.</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 552 | |
| 553 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-embedding"></a></p> |
| 554 | |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 555 | <h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-EmbeddingtheFelixFramework"></a>Embedding the Felix Framework</h1> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 556 | |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 557 | <p>Embedding the Felix framework into a host application is a simple |
| 558 | way to provide a sophisticated extensibility mechanism (i.e., a plugin |
| 559 | system) to the host application. Embedding the Felix framework is very |
| 560 | similar to launching it as described above, the main difference is that |
| 561 | the host application typically wants to interact with the framework |
| 562 | instance and/or installed bundles/services from the outside. This is |
| 563 | fairly easy to achieve, but there are some subtle issues to understand. |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 564 | This section presents the mechanisms for embedding Felix into a host |
| 565 | application and the issues in doing so.</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 566 | |
| 567 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-hostinteraction"></a></p> |
| 568 | |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 569 | <h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-Host/FelixInteraction"></a>Host/Felix Interaction</h2> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 570 | |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 571 | <p>In the section on <a href="#ApacheFelixFrameworkLaunchingandEmbedding-launching">launching</a> the framework above, the <tt>Felix</tt> class accepts a configuration property called <tt>felix.systembundle.activators</tt>, |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 572 | which is a list of bundle activator instances. These bundle activator |
| 573 | instances provide a convenient way for host applications to interact |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 574 | with the Felix framework.</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 575 | |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 576 | <div class="panelMacro"><table class="warningMacro"><colgroup><col width="24"><col></colgroup><tbody><tr><td valign="top"><img src="apache-felix-framework-launching-and-embedding_files/forbidden.gif" alt="" align="absmiddle" border="0" height="16" width="16"></td><td><b>WARNING</b><br>The <tt>felix.systembundle.activators</tt> |
| 577 | configuration property is specific to the Felix framework |
| 578 | implementation. If you want your code to work with other framework |
| 579 | implementations, you should call <tt>init()</tt> on the framework instance and use <tt>getBundleContext()</tt> directly. Otherwise, the approach would be very similar.</td></tr></tbody></table></div> |
| 580 | |
| 581 | <p>Each activator instance passed into the constructor effectively becomes part of the system bundle. This means that the <tt>start()</tt>/<tt>stop()</tt> methods of each activator instance in the list gets invoked when the system bundle's activator <tt>start()</tt>/<tt>stop()</tt> methods gets invoked, respectively. Each activator instance will be given the system bundle's <tt>BundleContext</tt> object so that they can interact with the framework. Consider following snippet of a bundle activator:</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 582 | |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 583 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 584 | <pre class="code-java"><span class="code-keyword">public</span> class HostActivator <span class="code-keyword">implements</span> BundleActivator |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 585 | { |
| 586 | <span class="code-keyword">private</span> BundleContext m_context = <span class="code-keyword">null</span>; |
| 587 | |
| 588 | <span class="code-keyword">public</span> void start(BundleContext context) |
| 589 | { |
| 590 | m_context = context; |
| 591 | } |
| 592 | |
| 593 | <span class="code-keyword">public</span> void stop(BundleContext context) |
| 594 | { |
| 595 | m_context = <span class="code-keyword">null</span>; |
| 596 | } |
| 597 | |
| 598 | <span class="code-keyword">public</span> Bundle[] getBundles() |
| 599 | { |
| 600 | <span class="code-keyword">if</span> (m_context != <span class="code-keyword">null</span>) |
| 601 | { |
| 602 | <span class="code-keyword">return</span> m_context.getBundles(); |
| 603 | } |
| 604 | <span class="code-keyword">return</span> <span class="code-keyword">null</span>; |
| 605 | } |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 606 | } |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 607 | </pre> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 608 | </div></div> |
| 609 | |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 610 | <p>Given the above bundle activator, it is now possible to embed the |
| 611 | Felix framework into a host application and interact with it as the |
| 612 | following snippet illustrates:</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 613 | |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 614 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 615 | <pre class="code-java"><span class="code-keyword">public</span> class HostApplication |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 616 | { |
| 617 | <span class="code-keyword">private</span> HostActivator m_activator = <span class="code-keyword">null</span>; |
| 618 | <span class="code-keyword">private</span> Felix m_felix = <span class="code-keyword">null</span>; |
| 619 | |
| 620 | <span class="code-keyword">public</span> HostApplication() |
| 621 | { |
| 622 | <span class="code-comment">// Create a configuration property map. |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 623 | </span> Map config = <span class="code-keyword">new</span> HashMap(); |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 624 | <span class="code-comment">// Create host activator; |
| 625 | </span> m_activator = <span class="code-keyword">new</span> HostActivator(); |
| 626 | List list = <span class="code-keyword">new</span> ArrayList(); |
| 627 | list.add(m_activator); |
| 628 | configMap.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list); |
| 629 | |
| 630 | <span class="code-keyword">try</span> |
| 631 | { |
| 632 | <span class="code-comment">// Now create an instance of the framework with |
| 633 | </span> <span class="code-comment">// our configuration properties. |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 634 | </span> m_felix = <span class="code-keyword">new</span> Felix(config); |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 635 | <span class="code-comment">// Now start Felix instance. |
| 636 | </span> m_felix.start(); |
| 637 | } |
| 638 | <span class="code-keyword">catch</span> (Exception ex) |
| 639 | { |
| 640 | <span class="code-object">System</span>.err.println(<span class="code-quote">"Could not create framework: "</span> + ex); |
| 641 | ex.printStackTrace(); |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | <span class="code-keyword">public</span> Bundle[] getInstalledBundles() |
| 646 | { |
| 647 | <span class="code-comment">// Use the system bundle activator to gain external |
| 648 | </span> <span class="code-comment">// access to the set of installed bundles. |
| 649 | </span> <span class="code-keyword">return</span> m_activator.getBundles(); |
| 650 | } |
| 651 | |
| 652 | <span class="code-keyword">public</span> void shutdownApplication() |
| 653 | { |
| 654 | <span class="code-comment">// Shut down the felix framework when stopping the |
| 655 | </span> <span class="code-comment">// host application. |
| 656 | </span> m_felix.stop(); |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 657 | m_felix.waitForStop(0); |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 658 | } |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 659 | } |
| 660 | </pre> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 661 | </div></div> |
| 662 | |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 663 | <p>Notice how the <tt>HostApplication.getInstalledBundles()</tt> method |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 664 | uses its activator instance to get access to the system bundle's |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 665 | context in order to interact with the embedded Felix framework |
| 666 | instance. This approach provides the foundation for all interaction |
| 667 | between the host application and the embedded framework instance.</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 668 | |
| 669 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-hostservices"></a></p> |
| 670 | |
| 671 | <h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-ProvidingHostApplicationServices"></a>Providing Host Application Services</h2> |
| 672 | |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 673 | <p>Providing services from the host application to bundles inside the |
| 674 | embedded Felix framework instance follows the basic approach laid out |
| 675 | in <a href="#ApacheFelixFrameworkLaunchingandEmbedding-hostinteraction">above</a>. |
| 676 | The main complication for providing a host application service to |
| 677 | bundles is the fact that both the host application and the bundles must |
| 678 | be using the same class definitions for the service interface classes. |
| 679 | Since the host application cannot import classes from a bundle, this |
| 680 | means that the service interface classes <b>must</b> be accessible on |
| 681 | the class path, typically as part of the host application itself. The |
| 682 | host application then must export the service interface package via the |
| 683 | system bundle so that bundles installed into the embedded framework |
| 684 | instance can import it. This is achieved using the <tt>org.osgi.framework.system.packages.extra</tt> configuration property previously presented.</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 685 | |
| 686 | <p>Consider the follow simple property lookup service:</p> |
| 687 | |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 688 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 689 | <pre class="code-java"><span class="code-keyword">package</span> host.service.lookup; |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 690 | |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 691 | <span class="code-keyword">public</span> <span class="code-keyword">interface</span> Lookup |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 692 | { |
| 693 | <span class="code-keyword">public</span> <span class="code-object">Object</span> lookup(<span class="code-object">String</span> name); |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 694 | } |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 695 | </pre> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 696 | </div></div> |
| 697 | |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 698 | <p>This package is simply part of the host application, which is potentially packaged into a JAR file and started with the "<tt>java -jar</tt>" |
| 699 | command. Now consider the following host application bundle activator, |
| 700 | which will be used to register/unregister the property lookup service |
| 701 | when the embedded framework instance starts/stops:</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 702 | |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 703 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 704 | <pre class="code-java"><span class="code-keyword">package</span> host.core; |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 705 | |
| 706 | <span class="code-keyword">import</span> java.util.Map; |
| 707 | <span class="code-keyword">import</span> org.osgi.framework.BundleActivator; |
| 708 | <span class="code-keyword">import</span> org.osgi.framework.BundleContext; |
| 709 | <span class="code-keyword">import</span> org.osgi.framework.ServiceRegistration; |
| 710 | <span class="code-keyword">import</span> host.service.lookup; |
| 711 | |
| 712 | <span class="code-keyword">public</span> class HostActivator <span class="code-keyword">implements</span> BundleActivator |
| 713 | { |
| 714 | <span class="code-keyword">private</span> Map m_lookupMap = <span class="code-keyword">null</span>; |
| 715 | <span class="code-keyword">private</span> BundleContext m_context = <span class="code-keyword">null</span>; |
| 716 | <span class="code-keyword">private</span> ServiceRegistration m_registration = <span class="code-keyword">null</span>; |
| 717 | |
| 718 | <span class="code-keyword">public</span> HostActivator(Map lookupMap) |
| 719 | { |
| 720 | <span class="code-comment">// Save a reference to the service's backing store. |
| 721 | </span> m_lookupMap = lookupMap; |
| 722 | } |
| 723 | |
| 724 | <span class="code-keyword">public</span> void start(BundleContext context) |
| 725 | { |
| 726 | <span class="code-comment">// Save a reference to the bundle context. |
| 727 | </span> m_context = context; |
| 728 | <span class="code-comment">// Create a property lookup service implementation. |
| 729 | </span> Lookup lookup = <span class="code-keyword">new</span> Lookup() { |
| 730 | <span class="code-keyword">public</span> <span class="code-object">Object</span> lookup(<span class="code-object">String</span> name) |
| 731 | { |
| 732 | <span class="code-keyword">return</span> m_lookupMap.get(name); |
| 733 | } |
| 734 | }; |
| 735 | <span class="code-comment">// Register the property lookup service and save |
| 736 | </span> <span class="code-comment">// the service registration. |
| 737 | </span> m_registration = m_context.registerService( |
| 738 | Lookup.class.getName(), lookup, <span class="code-keyword">null</span>); |
| 739 | } |
| 740 | |
| 741 | <span class="code-keyword">public</span> void stop(BundleContext context) |
| 742 | { |
| 743 | <span class="code-comment">// Unregister the property lookup service. |
| 744 | </span> m_registration.unregister(); |
| 745 | m_context = <span class="code-keyword">null</span>; |
| 746 | } |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 747 | } |
| 748 | </pre> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 749 | </div></div> |
| 750 | |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 751 | <p>Given the above host application bundle activator, the following |
| 752 | code snippet shows how the host application could create an embedded |
| 753 | version of the Felix framework and provide the property lookup service |
| 754 | to installed bundles:</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 755 | |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 756 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 757 | <pre class="code-java"><span class="code-keyword">package</span> host.core; |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 758 | |
| 759 | <span class="code-keyword">import</span> java.util.List; |
| 760 | <span class="code-keyword">import</span> java.util.ArrayList; |
| 761 | <span class="code-keyword">import</span> java.util.Map; |
| 762 | <span class="code-keyword">import</span> java.util.HashMap; |
| 763 | <span class="code-keyword">import</span> host.service.lookup.Lookup; |
| 764 | <span class="code-keyword">import</span> org.apache.felix.framework.Felix; |
| 765 | <span class="code-keyword">import</span> org.apache.felix.framework.util.FelixConstants; |
| 766 | <span class="code-keyword">import</span> org.osgi.framework.Constants; |
| 767 | |
| 768 | <span class="code-keyword">public</span> class HostApplication |
| 769 | { |
| 770 | <span class="code-keyword">private</span> HostActivator m_activator = <span class="code-keyword">null</span>; |
| 771 | <span class="code-keyword">private</span> Felix m_felix = <span class="code-keyword">null</span>; |
| 772 | <span class="code-keyword">private</span> Map m_lookupMap = <span class="code-keyword">new</span> HashMap(); |
| 773 | |
| 774 | <span class="code-keyword">public</span> HostApplication() |
| 775 | { |
| 776 | <span class="code-comment">// Initialize the map <span class="code-keyword">for</span> the property lookup service. |
| 777 | </span> m_lookupMap.put(<span class="code-quote">"name1"</span>, <span class="code-quote">"value1"</span>); |
| 778 | |
| 779 | m_lookupMap.put(<span class="code-quote">"name2"</span>, <span class="code-quote">"value2"</span>); |
| 780 | m_lookupMap.put(<span class="code-quote">"name3"</span>, <span class="code-quote">"value3"</span>); |
| 781 | m_lookupMap.put(<span class="code-quote">"name4"</span>, <span class="code-quote">"value4"</span>); |
| 782 | |
| 783 | <span class="code-comment">// Create a configuration property map. |
| 784 | </span> Map configMap = <span class="code-keyword">new</span> HashMap(); |
| 785 | <span class="code-comment">// Export the host provided service <span class="code-keyword">interface</span> <span class="code-keyword">package</span>. |
| 786 | </span> configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, |
| 787 | <span class="code-quote">"host.service.lookup; version=1.0.0"</span>); |
| 788 | <span class="code-comment">// Create host activator; |
| 789 | </span> m_activator = <span class="code-keyword">new</span> HostActivator(m_lookupMap); |
| 790 | List list = <span class="code-keyword">new</span> ArrayList(); |
| 791 | list.add(m_activator); |
| 792 | configMap.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list); |
| 793 | |
| 794 | <span class="code-keyword">try</span> |
| 795 | { |
| 796 | <span class="code-comment">// Now create an instance of the framework with |
| 797 | </span> <span class="code-comment">// our configuration properties. |
| 798 | </span> m_felix = <span class="code-keyword">new</span> Felix(configMap); |
| 799 | <span class="code-comment">// Now start Felix instance. |
| 800 | </span> m_felix.start(); |
| 801 | } |
| 802 | <span class="code-keyword">catch</span> (Exception ex) |
| 803 | { |
| 804 | <span class="code-object">System</span>.err.println(<span class="code-quote">"Could not create framework: "</span> + ex); |
| 805 | ex.printStackTrace(); |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | <span class="code-keyword">public</span> void shutdownApplication() |
| 810 | { |
| 811 | <span class="code-comment">// Shut down the felix framework when stopping the |
| 812 | </span> <span class="code-comment">// host application. |
| 813 | </span> m_felix.stop(); |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 814 | m_felix.waitForStop(0); |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 815 | } |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 816 | } |
| 817 | </pre> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 818 | </div></div> |
| 819 | |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 820 | <p>Rather than having the host application bundle activator register |
| 821 | the service, it is also possible for the the host application to simply |
| 822 | get the bundle context from the bundle activator and register the |
| 823 | service directly, but the presented approach is perhaps a little |
| 824 | cleaner since it allows the host application to register/unregister the |
| 825 | service when the system bundle starts/stops.</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 826 | |
| 827 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-hostserviceusage"></a></p> |
| 828 | |
| 829 | <h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-UsingServicesProvidedbyBundles"></a>Using Services Provided by Bundles</h2> |
| 830 | |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 831 | <p>Using services provided by bundles follows the same general approach |
| 832 | of using a host application bundle activator. The main complication for |
| 833 | the host application using a service from a bundle is the fact that |
| 834 | both the host application and the bundle must be using the same class |
| 835 | definitions for the service interface classes. Since the host |
| 836 | application cannot import classes from a bundle, this means that the |
| 837 | service interface classes <b>must</b> be accessible on the class path, |
| 838 | typically as part of the host application itself. The host application |
| 839 | then must export the service interface package via the system bundle so |
| 840 | that bundles installed into the embedded framework instance can import |
| 841 | it. This is achieved using the <tt>org.osgi.framework.system.packages.extra</tt> configuration property previously presented.</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 842 | |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 843 | <p>Consider the following simple command service interface for which |
| 844 | bundles provide implementations, such as might be used to create an |
| 845 | extensible interactive shell:</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 846 | |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 847 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 848 | <pre class="code-java"><span class="code-keyword">package</span> host.service.command; |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 849 | |
| 850 | <span class="code-keyword">public</span> class Command |
| 851 | { |
| 852 | <span class="code-keyword">public</span> <span class="code-object">String</span> getName(); |
| 853 | <span class="code-keyword">public</span> <span class="code-object">String</span> getDescription(); |
| 854 | <span class="code-keyword">public</span> <span class="code-object">boolean</span> execute(<span class="code-object">String</span> commandline); |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 855 | } |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 856 | </pre> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 857 | </div></div> |
| 858 | |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 859 | <p>This package is simply part of the host application, which is potentially packaged into a JAR file and started with the "<tt>java -jar</tt>" |
| 860 | command. Now consider the previously introduced host application bundle |
| 861 | activator below, which simply provides access to the system bundle |
| 862 | context:</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 863 | |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 864 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 865 | <pre class="code-java"><span class="code-keyword">package</span> host.core; |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 866 | |
| 867 | <span class="code-keyword">import</span> org.osgi.framework.BundleActivator; |
| 868 | <span class="code-keyword">import</span> org.osgi.framework.BundleContext; |
| 869 | |
| 870 | <span class="code-keyword">public</span> class HostActivator <span class="code-keyword">implements</span> BundleActivator |
| 871 | { |
| 872 | <span class="code-keyword">private</span> BundleContext m_context = <span class="code-keyword">null</span>; |
| 873 | |
| 874 | <span class="code-keyword">public</span> void start(BundleContext context) |
| 875 | { |
| 876 | m_context = context; |
| 877 | } |
| 878 | |
| 879 | <span class="code-keyword">public</span> void stop(BundleContext context) |
| 880 | { |
| 881 | m_context = <span class="code-keyword">null</span>; |
| 882 | } |
| 883 | |
| 884 | <span class="code-keyword">public</span> BundleContext getContext() |
| 885 | { |
| 886 | <span class="code-keyword">return</span> m_context; |
| 887 | } |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 888 | } |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 889 | </pre> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 890 | </div></div> |
| 891 | |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 892 | <p>With this bundle activator, the host application can use command |
| 893 | services provided by bundles installed inside its embedded Felix |
| 894 | framework instance. The following code snippet illustrates one possible |
| 895 | approach:</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 896 | |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 897 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 898 | <pre class="code-java"><span class="code-keyword">package</span> host.core; |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 899 | |
| 900 | <span class="code-keyword">import</span> java.util.List; |
| 901 | <span class="code-keyword">import</span> java.util.ArrayList; |
| 902 | <span class="code-keyword">import</span> java.util.Map; |
| 903 | <span class="code-keyword">import</span> host.service.command.Command; |
| 904 | <span class="code-keyword">import</span> org.apache.felix.framework.Felix; |
| 905 | <span class="code-keyword">import</span> org.apache.felix.framework.util.FelixConstants; |
| 906 | <span class="code-keyword">import</span> org.apache.felix.framework.cache.BundleCache; |
| 907 | <span class="code-keyword">import</span> org.osgi.framework.Constants; |
| 908 | <span class="code-keyword">import</span> org.osgi.util.tracker.ServiceTracker; |
| 909 | |
| 910 | <span class="code-keyword">public</span> class HostApplication |
| 911 | { |
| 912 | <span class="code-keyword">private</span> HostActivator m_activator = <span class="code-keyword">null</span>; |
| 913 | <span class="code-keyword">private</span> Felix m_felix = <span class="code-keyword">null</span>; |
| 914 | <span class="code-keyword">private</span> ServiceTracker m_tracker = <span class="code-keyword">null</span>; |
| 915 | |
| 916 | <span class="code-keyword">public</span> HostApplication() |
| 917 | { |
| 918 | <span class="code-comment">// Create a configuration property map. |
| 919 | </span> Map configMap = <span class="code-keyword">new</span> HashMap(); |
| 920 | <span class="code-comment">// Export the host provided service <span class="code-keyword">interface</span> <span class="code-keyword">package</span>. |
| 921 | </span> configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, |
| 922 | <span class="code-quote">"host.service.command; version=1.0.0"</span>); |
| 923 | <span class="code-comment">// Create host activator; |
| 924 | </span> m_activator = <span class="code-keyword">new</span> HostActivator(); |
| 925 | List list = <span class="code-keyword">new</span> ArrayList(); |
| 926 | list.add(m_activator); |
| 927 | configMap.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list); |
| 928 | |
| 929 | <span class="code-keyword">try</span> |
| 930 | { |
| 931 | <span class="code-comment">// Now create an instance of the framework with |
| 932 | </span> <span class="code-comment">// our configuration properties. |
| 933 | </span> m_felix = <span class="code-keyword">new</span> Felix(configMap); |
| 934 | <span class="code-comment">// Now start Felix instance. |
| 935 | </span> m_felix.start(); |
| 936 | } |
| 937 | <span class="code-keyword">catch</span> (Exception ex) |
| 938 | { |
| 939 | <span class="code-object">System</span>.err.println(<span class="code-quote">"Could not create framework: "</span> + ex); |
| 940 | ex.printStackTrace(); |
| 941 | } |
| 942 | |
| 943 | m_tracker = <span class="code-keyword">new</span> ServiceTracker( |
| 944 | m_activator.getContext(), Command.class.getName(), <span class="code-keyword">null</span>); |
| 945 | m_tracker.open(); |
| 946 | } |
| 947 | |
| 948 | <span class="code-keyword">public</span> <span class="code-object">boolean</span> execute(<span class="code-object">String</span> name, <span class="code-object">String</span> commandline) |
| 949 | { |
| 950 | <span class="code-comment">// See <span class="code-keyword">if</span> any of the currently tracked command services |
| 951 | </span> <span class="code-comment">// match the specified command name, <span class="code-keyword">if</span> so then execute it. |
| 952 | </span> <span class="code-object">Object</span>[] services = m_tracker.getServices(); |
| 953 | <span class="code-keyword">for</span> (<span class="code-object">int</span> i = 0; (services != <span class="code-keyword">null</span>) && (i < services.length); i++) |
| 954 | { |
| 955 | <span class="code-keyword">try</span> |
| 956 | { |
| 957 | <span class="code-keyword">if</span> (((Command) services[i]).getName().equals(name)) |
| 958 | { |
| 959 | <span class="code-keyword">return</span> ((Command) services[i]).execute(commandline); |
| 960 | } |
| 961 | } |
| 962 | <span class="code-keyword">catch</span> (Exception ex) |
| 963 | { |
| 964 | <span class="code-comment">// Since the services returned by the tracker could become |
| 965 | </span> <span class="code-comment">// invalid at any moment, we will <span class="code-keyword">catch</span> all exceptions, log |
| 966 | </span> <span class="code-comment">// a message, and then ignore faulty services. |
| 967 | </span> <span class="code-object">System</span>.err.println(ex); |
| 968 | } |
| 969 | } |
| 970 | <span class="code-keyword">return</span> <span class="code-keyword">false</span>; |
| 971 | } |
| 972 | |
| 973 | <span class="code-keyword">public</span> void shutdownApplication() |
| 974 | { |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 975 | <span class="code-comment">// Shut down the felix framework when stopping the |
| 976 | </span> <span class="code-comment">// host application. |
| 977 | </span> m_felix.stop(); |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 978 | m_felix.waitForStop(0); |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 979 | } |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 980 | } |
Karl Pauls | 44c941b | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 981 | </pre> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 982 | </div></div> |
| 983 | |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 984 | <p>The above example is overly simplistic with respect to concurrency |
| 985 | issues and error conditions, but it demonstrates the overall approach |
| 986 | for using bundle-provided services from the host application.</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 987 | |
| 988 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-servicereflection"></a></p> |
| 989 | |
| 990 | <h3><a name="ApacheFelixFrameworkLaunchingandEmbedding-UsingBundleServicesviaReflection"></a>Using Bundle Services via Reflection</h3> |
| 991 | |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 992 | <p>It possible for the host application to use services provided by |
| 993 | bundles without having access to the service interface classes and thus |
| 994 | not needing to put the service interface classes on the class path. To |
| 995 | do this, the host application uses the same general approach to acquire |
| 996 | the system bundle context object, which it can use to look up service |
| 997 | objects. Using either an LDAP filter or the service interface class |
| 998 | name, the host application can retrieve the service object and then use |
| 999 | standard Java reflection to invoke methods on the service object.</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 1000 | |
| 1001 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-serviceother"></a></p> |
| 1002 | |
| 1003 | <h3><a name="ApacheFelixFrameworkLaunchingandEmbedding-OtherApproaches"></a>Other Approaches</h3> |
| 1004 | |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 1005 | <p>The <a href="http://code.google.com/p/transloader/" class="external-link" rel="nofollow">Transloader</a> project is another attempt at dealing with issues of classes loaded from different class loaders and may be of interest.</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 1006 | |
| 1007 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-caveat"></a></p> |
| 1008 | |
| 1009 | <h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-Caveat"></a>Caveat</h1> |
| 1010 | |
Karl Pauls | 93f7ea2 | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 1011 | <p>The code in this document has not been thoroughly tested nor even |
| 1012 | compiled and may be out of date with respect to the current Felix |
| 1013 | source code. If you find errors please report them so the that they can |
| 1014 | be corrected.</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 1015 | |
| 1016 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-feedback"></a></p> |
| 1017 | |
| 1018 | <h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-Feedback"></a>Feedback</h2> |
| 1019 | |
Karl Pauls | 34ab827 | 2010-06-06 21:44:50 +0000 | [diff] [blame] | 1020 | <p>Subscribe to the Felix users mailing list by sending a message to <a href="mailto:users-subscribe@felix.apache.org" class="external-link" rel="nofollow">users-subscribe@felix.apache.org</a>; after subscribing, email questions or feedback to <a href="mailto:users@felix.apache.org" class="external-link" rel="nofollow">users@felix.apache.org</a>.</p> |
Karl Pauls | f7d389c | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 1021 | </div> |
Karl Pauls | bbc2957 | 2010-12-26 21:07:41 +0000 | [diff] [blame^] | 1022 | </body></html> |