Richard S. Hall | 2cd5bed | 2007-07-16 20:32:41 +0000 | [diff] [blame] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
Karl Pauls | 7bca0de | 2007-09-16 20:38:10 +0000 | [diff] [blame] | 2 | <html><head><title>Apache Felix - Launching and Embedding Apache Felix</title> |
Richard S. Hall | 2cd5bed | 2007-07-16 20:32:41 +0000 | [diff] [blame] | 3 | |
| 4 | |
| 5 | |
| 6 | |
Karl Pauls | 7bca0de | 2007-09-16 20:38:10 +0000 | [diff] [blame] | 7 | <link rel="stylesheet" href="launching-and-embedding-apache-felix_files/site.css" type="text/css" media="all"> |
| 8 | <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"></head><body> |
| 9 | <div class="title"><div class="logo"><a href="http://felix.apache.org/site/index.html"><img alt="Apache Felix" src="launching-and-embedding-apache-felix_files/logo.png" border="0"></a></div><div class="header"><a href="http://www.apache.org/"><img alt="Apache" src="launching-and-embedding-apache-felix_files/apache.png" border="0"></a></div></div> |
Richard S. Hall | 2cd5bed | 2007-07-16 20:32:41 +0000 | [diff] [blame] | 10 | <div class="menu"> |
Karl Pauls | 7bca0de | 2007-09-16 20:38:10 +0000 | [diff] [blame] | 11 | <ul> |
| 12 | <li><a href="http://felix.apache.org/site/index.html" title="Index">home</a></li> |
| 13 | <li><a href="http://felix.apache.org/site/news.html" title="news">news</a></li> |
| 14 | <li><a href="http://felix.apache.org/site/status.html" title="status">status</a></li> |
| 15 | <li><a href="http://felix.apache.org/site/license.html" title="license">license</a></li> |
| 16 | <li><span class="nobr"><a href="http://felix.apache.org/site/downloads.cgi" title="Visit page outside Confluence" rel="nofollow">downloads<sup><img class="rendericon" src="launching-and-embedding-apache-felix_files/linkext7.gif" alt="" align="absmiddle" border="0" height="7" width="7"></sup></a></span></li> |
| 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><span class="nobr"><a href="http://cwiki.apache.org/confluence/x/O-" title="Visit page outside Confluence" rel="nofollow">wiki<sup><img class="rendericon" src="launching-and-embedding-apache-felix_files/linkext7.gif" alt="" align="absmiddle" border="0" height="7" width="7"></sup></a></span></li> |
| 20 | <li><a href="http://felix.apache.org/site/committers.html" title="committers">committers</a></li> |
| 21 | <li><a href="http://felix.apache.org/site/faq.html" title="faq">faq</a></li> |
| 22 | <li><a href="http://felix.apache.org/site/roadmap.html" title="roadmap">roadmap</a></li> |
| 23 | <li><a href="http://felix.apache.org/site/sourcecode.html" title="sourcecode">source code</a></li> |
| 24 | <li><a href="http://felix.apache.org/site/codingstandards.html" title="codingstandards">coding standards</a></li> |
| 25 | <li><a href="http://felix.apache.org/site/issuetracking.html" title="issuetracking">issue tracking</a></li> |
| 26 | <li><a href="http://felix.apache.org/site/dependencies.html" title="dependencies">dependencies</a></li> |
| 27 | </ul> </div> |
Richard S. Hall | 2cd5bed | 2007-07-16 20:32:41 +0000 | [diff] [blame] | 28 | <div class="main"> |
| 29 | <h1><a name="LaunchingandEmbeddingApacheFelix-LaunchingandEmbeddingApacheFelix"></a>Launching and Embedding Apache Felix</h1> |
| 30 | |
| 31 | <ul> |
| 32 | <li><a href="#LaunchingandEmbeddingApacheFelix-introduction" title="introduction on Launching and Embedding Apache Felix">Introduction</a></li> |
| 33 | <li><a href="#LaunchingandEmbeddingApacheFelix-overview" title="overview on Launching and Embedding Apache Felix">API Overview</a></li> |
| 34 | <li><a href="#LaunchingandEmbeddingApacheFelix-launching" title="launching on Launching and Embedding Apache Felix">Launching Felix</a> |
| 35 | <ul> |
| 36 | <li><a href="#LaunchingandEmbeddingApacheFelix-standardlauncher" title="standard-launcher on Launching and Embedding Apache Felix">Standard Felix Launcher</a></li> |
| 37 | <li><a href="#LaunchingandEmbeddingApacheFelix-customlauncher" title="custom-launcher on Launching and Embedding Apache Felix">Custom Felix Launcher</a></li> |
| 38 | </ul> |
| 39 | </li> |
| 40 | <li><a href="#LaunchingandEmbeddingApacheFelix-embedding" title="embedding on Launching and Embedding Apache Felix">Embedding Felix</a> |
| 41 | <ul> |
| 42 | <li><a href="#LaunchingandEmbeddingApacheFelix-configproperty" title="config-property on Launching and Embedding Apache Felix">Embedded Execution Configuration Property</a></li> |
| 43 | <li><a href="#LaunchingandEmbeddingApacheFelix-hostinteraction" title="host-interaction on Launching and Embedding Apache Felix">Host/Felix Interaction</a></li> |
| 44 | <li><a href="#LaunchingandEmbeddingApacheFelix-hostservices" title="host-services on Launching and Embedding Apache Felix">Providing Host Application Services</a></li> |
| 45 | <li><a href="#LaunchingandEmbeddingApacheFelix-hostserviceusage" title="host-service-usage on Launching and Embedding Apache Felix">Using Services Provided by Bundles</a> |
| 46 | <ul> |
| 47 | <li><a href="#LaunchingandEmbeddingApacheFelix-servicereflection" title="service-reflection on Launching and Embedding Apache Felix">Using Bundle Services via Reflection</a></li> |
| 48 | </ul> |
| 49 | </li> |
| 50 | </ul> |
| 51 | </li> |
| 52 | <li><a href="#LaunchingandEmbeddingApacheFelix-caveat" title="caveat on Launching and Embedding Apache Felix">Caveat</a></li> |
Karl Pauls | 7bca0de | 2007-09-16 20:38:10 +0000 | [diff] [blame] | 53 | <li><a href="#LaunchingandEmbeddingApacheFelix-feedback" title="feedback on Launching and Embedding Apache Felix">Feedback</a></li> |
Richard S. Hall | 2cd5bed | 2007-07-16 20:32:41 +0000 | [diff] [blame] | 54 | </ul> |
| 55 | |
| 56 | |
| 57 | <p><a name="LaunchingandEmbeddingApacheFelix-introduction"></a></p> |
| 58 | |
| 59 | <h1><a name="LaunchingandEmbeddingApacheFelix-Introduction"></a>Introduction</h1> |
| 60 | |
| 61 | <p>The Apache Felix OSGi framework is intended to be easily launchable |
| 62 | and embeddable. For example, Felix avoids the use of system properties |
| 63 | for configuration, since these are globals that can cause interference |
| 64 | if multiple framework instances are created in the same VM. Felix is |
| 65 | also implemented to multiplex singleton facilities, like the URL stream |
| 66 | handler factory. The goal is to make it possible to use Felix in as |
| 67 | many scenarios as possible; however, this is still just a goal. In |
| 68 | other words, this is a work in progress and if any issues arise, it |
| 69 | would be greatly appreciated if they are brought to the attention of |
| 70 | the Felix community. The next section provides a Felix API overview, |
| 71 | while the remainder of the document is divided into two sections, one |
| 72 | focusing on how to launch Felix and one focusing on how to embed Felix |
| 73 | into a host application.</p> |
| 74 | |
| 75 | <p><a name="LaunchingandEmbeddingApacheFelix-overview"></a></p> |
| 76 | |
| 77 | <h1><a name="LaunchingandEmbeddingApacheFelix-APIOverview"></a>API Overview</h1> |
| 78 | |
| 79 | <p>The Felix class that implements the OSGi framework is <tt>org.apache.felix.framework.Felix</tt> or just <tt>Felix</tt> for short. The OSGi specification defines a special bundle, called the <em><b>System Bundle</b></em>, |
| 80 | that represents the framework at run time and appears like any other |
| 81 | bundle in the list of installed bundles. To make this notion even more |
| 82 | intuitive, the <tt>Felix</tt> class implements the <tt>org.osgi.framework.Bundle</tt> interface, which is reiterated here:</p> |
| 83 | |
| 84 | <div class="code"><div class="codeContent"> |
| 85 | <pre class="code-java"><span class="code-keyword">public</span> <span class="code-keyword">interface</span> Bundle |
| 86 | { |
| 87 | <span class="code-keyword">public</span> BundleContext getBundleContext(); |
| 88 | <span class="code-keyword">public</span> <span class="code-object">long</span> getBundleId(); |
| 89 | <span class="code-keyword">public</span> URL getEntry(<span class="code-object">String</span> name); |
| 90 | <span class="code-keyword">public</span> Enumeration getEntryPaths(<span class="code-object">String</span> path); |
| 91 | <span class="code-keyword">public</span> Enumeration findEntries(<span class="code-object">String</span> path, <span class="code-object">String</span> filePattern, <span class="code-object">boolean</span> recurse); |
| 92 | <span class="code-keyword">public</span> Dictionary getHeaders(); |
| 93 | <span class="code-keyword">public</span> Dictionary getHeaders(<span class="code-object">String</span> locale); |
| 94 | <span class="code-keyword">public</span> <span class="code-object">long</span> getLastModified(); |
| 95 | <span class="code-keyword">public</span> <span class="code-object">String</span> getLocation(); |
| 96 | <span class="code-keyword">public</span> URL getResource(<span class="code-object">String</span> name); |
| 97 | <span class="code-keyword">public</span> Enumeration getResources(<span class="code-object">String</span> name) <span class="code-keyword">throws</span> IOException; |
| 98 | <span class="code-keyword">public</span> ServiceReference[] getRegisteredServices(); |
| 99 | <span class="code-keyword">public</span> ServiceReference[] getServicesInUse(); |
| 100 | <span class="code-keyword">public</span> <span class="code-object">int</span> getState(); |
| 101 | <span class="code-keyword">public</span> <span class="code-object">String</span> getSymbolicName(); |
| 102 | <span class="code-keyword">public</span> <span class="code-object">boolean</span> hasPermission(<span class="code-object">Object</span> obj); |
| 103 | <span class="code-keyword">public</span> <span class="code-object">Class</span> loadClass(<span class="code-object">String</span> name) <span class="code-keyword">throws</span> ClassNotFoundException; |
| 104 | <span class="code-keyword">public</span> void start() <span class="code-keyword">throws</span> BundleException; |
| 105 | <span class="code-keyword">public</span> void stop() <span class="code-keyword">throws</span> BundleException; |
| 106 | <span class="code-keyword">public</span> void uninstall() <span class="code-keyword">throws</span> BundleException; |
| 107 | <span class="code-keyword">public</span> void update() <span class="code-keyword">throws</span> BundleException; |
| 108 | <span class="code-keyword">public</span> void update(InputStream is) <span class="code-keyword">throws</span> BundleException; |
| 109 | }</pre> |
| 110 | </div></div> |
| 111 | |
| 112 | <p>When you instantiate the <tt>Felix</tt> class, the resulting object is actually the System Bundle and can be cast to the <tt>Bundle</tt> interface. The <tt>start()</tt> method is used to start the framework instance, while the <tt>stop()</tt> method is used to asynchronously stop the framework instance. The <tt>Felix</tt> class also includes the following two additional public methods:</p> |
| 113 | |
| 114 | <div class="code"><div class="codeContent"> |
| 115 | <pre class="code-java"><span class="code-keyword">public</span> class Felix <span class="code-keyword">extends</span> AbstractBundle |
| 116 | { |
| 117 | <span class="code-keyword">public</span> Felix(Map configMutableMap, List activatorList); |
| 118 | <span class="code-keyword">public</span> void stopAndWait(); |
| 119 | }</pre> |
| 120 | </div></div> |
| 121 | |
| 122 | <p>The first method is the constructor used to instantiate framework |
| 123 | instances; the constructor accepts configuration properties and System |
| 124 | Bundle activators, which are both described in more detail later. The <tt>stopAndWait()</tt> method is a synchronous version of the <tt>stop()</tt> method, used to stop the framework and block the calling thread until the framework is completely stopped.</p> |
| 125 | |
| 126 | <p><a name="LaunchingandEmbeddingApacheFelix-launching"></a></p> |
| 127 | |
| 128 | <h1><a name="LaunchingandEmbeddingApacheFelix-LaunchingFelix"></a>Launching Felix</h1> |
| 129 | |
| 130 | <p>Launching Felix is fairly simple and involves only three steps:</p> |
| 131 | |
| 132 | <ol> |
| 133 | <li>Defining some configuration properties.</li> |
| 134 | <li>Creating an instance of <tt>org.apache.felix.framework.Felix</tt> with the configuration properties.</li> |
| 135 | <li>Invoking the <tt>org.apache.felix.framework.Felix.start()</tt> method.</li> |
| 136 | </ol> |
| 137 | |
| 138 | |
| 139 | <p>The only configuration properties that are actually required to |
| 140 | start Felix are ones that tell it where/how to locate the bundle cache |
| 141 | profile directory where installed bundles will be cached. Felix' bundle |
| 142 | cache implementation allows you to configure the location where bundles |
| 143 | are cached using configuration properties. At a minimum, either a |
Karl Pauls | 7bca0de | 2007-09-16 20:38:10 +0000 | [diff] [blame] | 144 | bundle cache profile name or directory must be specified; see the <a href="http://felix.apache.org/site/apache-felix-bundle-cache.html" title="Apache Felix Bundle Cache">bundle cache document</a> for more detailed information on configuring the bundle cache.</p> |
Richard S. Hall | 2cd5bed | 2007-07-16 20:32:41 +0000 | [diff] [blame] | 145 | |
Karl Pauls | 7bca0de | 2007-09-16 20:38:10 +0000 | [diff] [blame] | 146 | <p>Besides configuration properties for the bundle cache, it is usually necessary to set the <tt>org.osgi.framework.system.packages</tt> configuration property to export packages from the class path, such as the OSGi interface classes (e.g., <tt>org.osgi.framework</tt>) on which all bundles depend. If you are creating a launcher for Felix, then the <tt>felix.auto.start</tt> configuration property may also be used to automatically install and start various bundles; see the <a href="http://felix.apache.org/site/apache-felix-usage-documentation.html#ApacheFelixUsageDocumentation-configuringfelix" title="configuring-felix on Apache Felix Usage Documentation">usage document</a> for more information on configuring Felix and on the various configuration properties.</p> |
Richard S. Hall | 2cd5bed | 2007-07-16 20:32:41 +0000 | [diff] [blame] | 147 | |
| 148 | <p>The remainder of this section describes how the standard Felix |
| 149 | launcher works as well as how to create a custom launcher for Felix.</p> |
| 150 | |
| 151 | <p><a name="LaunchingandEmbeddingApacheFelix-standardlauncher"></a></p> |
| 152 | |
| 153 | <h2><a name="LaunchingandEmbeddingApacheFelix-StandardFelixLauncher"></a>Standard Felix Launcher</h2> |
| 154 | |
| 155 | <p>The standard Felix launcher is very simple and is not intended to |
| 156 | solve every possible requirement; it is intended to work for most |
| 157 | standard situations. Most special launching requirements should be |
| 158 | resolved by creating a custom launcher. This section describes how the |
| 159 | standard launcher works. The following code represents the complete <tt>main()</tt> method of the standard launcher, each numbered comment will be described in more detail below:</p> |
| 160 | |
| 161 | <div class="code"><div class="codeContent"> |
| 162 | <pre class="code-java"><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 |
| 163 | { |
| 164 | <span class="code-comment">// (1) Load system properties. |
| 165 | </span> Main.loadSystemProperties(); |
| 166 | |
| 167 | <span class="code-comment">// (2) Read configuration properties. |
| 168 | </span> Properties configProps = Main.loadConfigProperties(); |
| 169 | |
| 170 | <span class="code-comment">// (3) Copy framework properties from the system properties. |
| 171 | </span> Main.copySystemProperties(configProps); |
| 172 | |
| 173 | <span class="code-comment">// (4) See <span class="code-keyword">if</span> the profile name property was specified. |
| 174 | </span> <span class="code-object">String</span> profileName = configProps.getProperty(BundleCache.CACHE_PROFILE_PROP); |
| 175 | |
| 176 | <span class="code-comment">// (4) See <span class="code-keyword">if</span> the profile directory property was specified. |
| 177 | </span> <span class="code-object">String</span> profileDirName = configProps.getProperty(BundleCache.CACHE_PROFILE_DIR_PROP); |
| 178 | |
| 179 | <span class="code-comment">// Print welcome banner. |
| 180 | </span> <span class="code-object">System</span>.out.println(<span class="code-quote">"\nWelcome to Felix."</span>); |
| 181 | <span class="code-object">System</span>.out.println(<span class="code-quote">"=================\n"</span>); |
| 182 | |
| 183 | <span class="code-comment">// (5) If no profile or profile directory is specified in the |
| 184 | </span> <span class="code-comment">// properties, then ask <span class="code-keyword">for</span> a profile name. |
| 185 | </span> <span class="code-keyword">if</span> ((profileName == <span class="code-keyword">null</span>) && (profileDirName == <span class="code-keyword">null</span>)) |
| 186 | { |
| 187 | <span class="code-object">System</span>.out.print(<span class="code-quote">"Enter profile name: "</span>); |
| 188 | BufferedReader in = <span class="code-keyword">new</span> BufferedReader(<span class="code-keyword">new</span> InputStreamReader(<span class="code-object">System</span>.in)); |
| 189 | <span class="code-keyword">try</span> |
| 190 | { |
| 191 | profileName = in.readLine(); |
| 192 | } |
| 193 | <span class="code-keyword">catch</span> (IOException ex) |
| 194 | { |
| 195 | <span class="code-object">System</span>.err.println(<span class="code-quote">"Could not read input."</span>); |
| 196 | <span class="code-object">System</span>.exit(-1); |
| 197 | } |
| 198 | <span class="code-object">System</span>.out.println(""); |
| 199 | <span class="code-keyword">if</span> (profileName.length() != 0) |
| 200 | { |
| 201 | configProps.setProperty(BundleCache.CACHE_PROFILE_PROP, profileName); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | <span class="code-comment">// (6) A profile directory or name must be specified. |
| 206 | </span> <span class="code-keyword">if</span> ((profileDirName == <span class="code-keyword">null</span>) && (profileName.length() == 0)) |
| 207 | { |
| 208 | <span class="code-object">System</span>.err.println(<span class="code-quote">"You must specify a profile name or directory."</span>); |
| 209 | <span class="code-object">System</span>.exit(-1); |
| 210 | } |
| 211 | |
| 212 | <span class="code-keyword">try</span> |
| 213 | { |
| 214 | <span class="code-comment">// (7) Now create an instance of the framework. |
| 215 | </span> m_felix = <span class="code-keyword">new</span> Felix(<span class="code-keyword">new</span> StringMap(configProps, <span class="code-keyword">false</span>), <span class="code-keyword">null</span>); |
| 216 | m_felix.start(); |
| 217 | } |
| 218 | <span class="code-keyword">catch</span> (Exception ex) |
| 219 | { |
| 220 | <span class="code-object">System</span>.err.println(<span class="code-quote">"Could not create framework: "</span> + ex); |
| 221 | ex.printStackTrace(); |
| 222 | <span class="code-object">System</span>.exit(-1); |
| 223 | } |
| 224 | }</pre> |
| 225 | </div></div> |
| 226 | |
| 227 | <p>The general steps of the standard launcher are quite straightforward:</p> |
| 228 | |
| 229 | <ol> |
| 230 | <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> |
| 231 | system property. This file is not needed to launch Felix and is |
| 232 | provided merely for convenience when system properties must be |
| 233 | specified. The file is a standard Java properties file, but it also |
| 234 | supports property substitution using <tt>${<property-name</tt>} syntax. Property substitution can be nested; only system properties will be used for substitution.</li> |
| 235 | <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> |
| 236 | system property. This file is used to configure the Felix instance |
| 237 | created by the launcher. The file is a standard Java properties file, |
| 238 | but it also supports property substitution using "<tt>${<property-name</tt>}" |
| 239 | syntax. Property substitution can be nested; configuration and system |
| 240 | properties will be used for substitution with configuration properties |
| 241 | having precedence.</li> |
| 242 | <li>For convenience, any configuration |
| 243 | properties that are set as system properties will be copied into the |
| 244 | set of configuration properties to provide an easy way to add to or |
| 245 | override configuration properties specified in the <tt>config.properties</tt> file.</li> |
Karl Pauls | 7bca0de | 2007-09-16 20:38:10 +0000 | [diff] [blame] | 246 | <li>Try to load the profile name or profile directory configuration properties. At least one of these must be specified so that the <a href="http://felix.apache.org/site/apache-felix-bundle-cache.html" title="Apache Felix Bundle Cache">bundle cache</a> knows where to save installed bundles.</li> |
Richard S. Hall | 2cd5bed | 2007-07-16 20:32:41 +0000 | [diff] [blame] | 247 | <li>If |
| 248 | either the profile name or profile directory configuration property has |
| 249 | not been specified, then ask the user to specify a profile name and add |
| 250 | it to the current set of configuration properties.</li> |
| 251 | <li>Error if there is no profile name or profile directory.</li> |
| 252 | <li>Create the Felix instance passing in the configuration properties and then call <tt>start()</tt>.</li> |
| 253 | </ol> |
| 254 | |
| 255 | |
| 256 | <p>The framework is not active until the <tt>start()</tt> method is called. If no shell bundles are specified in the <tt>config.properties</tt> |
| 257 | file or if there is difficulty locating the shell bundles that are |
| 258 | specified, then it will appear as if the framework is hung, but it is |
| 259 | actually running without any way to interact with it since the shell |
| 260 | bundles provide the only means of interaction.</p> |
| 261 | |
| 262 | <p><a name="LaunchingandEmbeddingApacheFelix-customlauncher"></a></p> |
| 263 | |
| 264 | <h2><a name="LaunchingandEmbeddingApacheFelix-CustomFelixLauncher"></a>Custom Felix Launcher</h2> |
| 265 | |
| 266 | <p>This section creates a bare-bones launcher to demonstrate the |
| 267 | minimum requirements for creating an interactive launcher for the Felix |
| 268 | framework. This example uses the standard Felix shell bundles for |
| 269 | interactivity, but any other bundles could be used instead. For |
| 270 | example, the shell service and telnet bundles could be used to launch |
| 271 | Felix and make it remotely accessible.</p> |
| 272 | |
| 273 | <p>This example launcher project has the following directory structure:</p> |
| 274 | |
| 275 | <div class="preformatted"><div class="preformattedContent"> |
| 276 | <pre>launcher/ |
| 277 | lib/ |
| 278 | org.apache.felix.framework-1.0.0.jar |
| 279 | bundle/ |
| 280 | org.apache.felix.shell-1.0.0.jar |
| 281 | org.apache.felix.shell.tui-1.0.0.jar |
| 282 | src/ |
| 283 | example/ |
| 284 | Main.java |
| 285 | </pre> |
| 286 | </div></div> |
| 287 | |
| 288 | <p>The <tt>lib/</tt> directory contains the framework JAR file, which also contains the OSGi core interfaces. The <tt>bundle/</tt> |
| 289 | directory contains the shell service and textual shell interface |
| 290 | bundles that will be used for interacting with the framework instance. |
| 291 | Note: If you do not launch Felix with interactive bundles, it will |
| 292 | appear as if the framework instance is hung, but it is actually just |
| 293 | sitting there waiting for 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 Felix launcher.</p> |
| 294 | |
| 295 | <div class="code"><div class="codeContent"> |
| 296 | <pre class="code-java"><span class="code-keyword">package</span> example; |
| 297 | |
| 298 | <span class="code-keyword">import</span> java.util.Map; |
| 299 | <span class="code-keyword">import</span> org.osgi.framework.Constants; |
| 300 | <span class="code-keyword">import</span> org.apache.felix.framework.Felix; |
| 301 | <span class="code-keyword">import</span> org.apache.felix.framework.cache.BundleCache; |
| 302 | <span class="code-keyword">import</span> org.apache.felix.framework.util.StringMap; |
| 303 | <span class="code-keyword">import</span> org.apache.felix.framework.util.FelixConstants; |
| 304 | |
| 305 | <span class="code-keyword">public</span> class Main |
| 306 | { |
| 307 | <span class="code-keyword">private</span> <span class="code-keyword">static</span> Felix m_felix = <span class="code-keyword">null</span>; |
| 308 | |
| 309 | <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 |
| 310 | { |
| 311 | <span class="code-comment">// Print welcome banner. |
| 312 | </span> <span class="code-object">System</span>.out.println(<span class="code-quote">"\nWelcome to Felix."</span>); |
| 313 | <span class="code-object">System</span>.out.println(<span class="code-quote">"=================\n"</span>); |
| 314 | |
| 315 | Map configMap = <span class="code-keyword">new</span> StringMap(<span class="code-keyword">false</span>); |
| 316 | configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES, |
| 317 | <span class="code-quote">"org.osgi.framework; version=1.3.0,"</span> + |
| 318 | <span class="code-quote">"org.osgi.service.packageadmin; version=1.2.0,"</span> + |
| 319 | <span class="code-quote">"org.osgi.service.startlevel; version=1.0.0,"</span> + |
| 320 | <span class="code-quote">"org.osgi.service.url; version=1.0.0"</span>); |
| 321 | configMap.put(FelixConstants.AUTO_START_PROP + <span class="code-quote">".1"</span>, |
| 322 | <span class="code-quote">"file:bundle/org.apache.felix.shell-1.0.0.jar "</span> + |
| 323 | <span class="code-quote">"file:bundle/org.apache.felix.shell.tui-1.0.0.jar"</span>); |
| 324 | configMap.put(BundleCache.CACHE_PROFILE_DIR_PROP, <span class="code-quote">"cache"</span>); |
| 325 | |
| 326 | <span class="code-keyword">try</span> |
| 327 | { |
| 328 | <span class="code-comment">// Now create an instance of the framework. |
| 329 | </span> m_felix = <span class="code-keyword">new</span> Felix(configMap, <span class="code-keyword">null</span>); |
| 330 | m_felix.start(); |
| 331 | } |
| 332 | <span class="code-keyword">catch</span> (Exception ex) |
| 333 | { |
| 334 | <span class="code-object">System</span>.err.println(<span class="code-quote">"Could not create framework: "</span> + ex); |
| 335 | ex.printStackTrace(); |
| 336 | <span class="code-object">System</span>.exit(-1); |
| 337 | } |
| 338 | } |
| 339 | }</pre> |
| 340 | </div></div> |
| 341 | |
| 342 | <p>This launcher has all information hard coded in it, unlike the |
| 343 | default Felix launcher, which loads configuration properties from files |
| 344 | and performs variable substitution. This simple launcher provides a |
| 345 | good starting point if the features of the default launcher are not |
| 346 | necessary. For example, if you want to create a launcher that |
| 347 | automatically deletes the bundle cache directory each time it starts, |
| 348 | then it is quite easy to figure out how to do that with this simple |
| 349 | launcher.</p> |
| 350 | |
| 351 | <p>By breaking down the above source code into small chunks, it is quite easy to see what is going on.</p> |
| 352 | |
| 353 | <div class="code"><div class="codeContent"> |
| 354 | <pre class="code-java">Map configMap = <span class="code-keyword">new</span> StringMap(<span class="code-keyword">false</span>);</pre> |
| 355 | </div></div> |
| 356 | |
| 357 | <p>This simply creates a map to hold configuration properties where the keys are strings and lookups are case insensitive.</p> |
| 358 | |
| 359 | <div class="code"><div class="codeContent"> |
| 360 | <pre class="code-java">configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES, |
| 361 | <span class="code-quote">"org.osgi.framework; version=1.3.0,"</span> + |
| 362 | <span class="code-quote">"org.osgi.service.packageadmin; version=1.2.0,"</span> + |
| 363 | <span class="code-quote">"org.osgi.service.startlevel; version=1.0.0,"</span> + |
| 364 | <span class="code-quote">"org.osgi.service.url; version=1.0.0"</span>);</pre> |
| 365 | </div></div> |
| 366 | |
| 367 | <p>This sets the <tt>Constants.FRAMEWORK_SYSTEMPACKAGES</tt> configuration property (string value "<tt>org.osgi.framework.system.packages</tt>"), |
| 368 | which specifies the class path packages the system bundle should |
| 369 | export; this is how classes on the class path are made available to |
| 370 | bundles. This example only exports the core OSGi API packages, but |
| 371 | other JRE packages could also be added, such as <tt>javax.swing</tt>. |
| 372 | For example, the default Felix launcher defines properties for all |
| 373 | packages in various JRE versions (e.g., 1.3.x, 1.4.x, 1.5.x) and |
| 374 | appends them to this property using property substitution.</p> |
| 375 | |
| 376 | <div class="code"><div class="codeContent"> |
| 377 | <pre class="code-java">configMap.put(FelixConstants.AUTO_START_PROP + <span class="code-quote">".1"</span>, |
| 378 | <span class="code-quote">"file:bundle/org.apache.felix.shell-1.0.0.jar "</span> + |
| 379 | <span class="code-quote">"file:bundle/org.apache.felix.shell.tui-1.0.0.jar"</span>);</pre> |
| 380 | </div></div> |
| 381 | |
| 382 | <p>This sets the <tt>FelixConstants.AUTO_START_PROP</tt> configuration property (string value "<tt>felix.auto.start</tt>"), |
| 383 | which is a space-delimited list of bundle URLs that the framework will |
| 384 | automatically install and start when the framework starts. However, |
| 385 | this property key cannot be used as is; it must be appended with a "." |
| 386 | and then a number, where the number represents the start level for the |
| 387 | bundle when it is installed. In this particular example, ".1" is |
| 388 | appended to the property name, thus the two bundles will be installed |
| 389 | into start level one. This example uses relative <tt>file:</tt> URLs, which will load the bundles from the <tt>bundle/</tt> |
| 390 | directory assuming that the launcher is started from the root directory |
| 391 | of the launcher project. It is also possible to specify absolute URLs |
| 392 | or remote URLs.</p> |
| 393 | |
| 394 | <div class="code"><div class="codeContent"> |
| 395 | <pre class="code-java">configMap.put(BundleCache.CACHE_PROFILE_DIR_PROP, <span class="code-quote">"cache"</span>);</pre> |
| 396 | </div></div> |
| 397 | |
| 398 | <p>This sets the last configuration property, <tt>BundleCache.CACHE_PROFILE_DIR_PROP</tt> (string value "<tt>felix.cache.profiledir</tt>"), |
| 399 | which is a string that specifies the precise directory to be used as |
| 400 | the bundle cache profile directory; the Felix bundle cache supports |
| 401 | other properties to configure its behavior, but those are not covered |
| 402 | here. In this example, the bundle cache profile directory is specified |
| 403 | as a relative directory called "<tt>cache</tt>". Assuming that the |
| 404 | launcher is executed from the root directory of the launcher project, |
| 405 | then the bundle cache profile directory will be created in the root |
| 406 | directory of the project.</p> |
| 407 | |
| 408 | <div class="code"><div class="codeContent"> |
| 409 | <pre class="code-java">m_felix = <span class="code-keyword">new</span> Felix(configMap, <span class="code-keyword">null</span>); |
| 410 | m_felix.start();</pre> |
| 411 | </div></div> |
| 412 | |
| 413 | <p>The last steps create the framework instance and start it. The configuration property map is passed into the <tt>Felix</tt> constructor.</p> |
| 414 | |
| 415 | <p>The following command compiles the launcher when run from the root directory of the launcher project:</p> |
| 416 | |
| 417 | <div class="preformatted"><div class="preformattedContent"> |
| 418 | <pre>javac -d . -classpath lib/org.apache.felix.framework-1.0.0.jar src/example/Main.java |
| 419 | </pre> |
| 420 | </div></div> |
| 421 | |
| 422 | <p>After executing this command, an <tt>example/</tt> directory is |
| 423 | created in the current directory, which contains the generated class |
| 424 | file. The following command executes the simple launcher when run from |
| 425 | the root directory of the launcher project:</p> |
| 426 | |
| 427 | <div class="preformatted"><div class="preformattedContent"> |
| 428 | <pre>java -cp .:lib/org.apache.felix.framework-1.0.0.jar example.Main |
| 429 | </pre> |
| 430 | </div></div> |
| 431 | |
| 432 | <p>After executing this command, a <tt>cache/</tt> directory is created that contains the installed bundles, which were installed from the <tt>bundle/</tt> directory.</p> |
| 433 | |
| 434 | <p><a name="LaunchingandEmbeddingApacheFelix-embedding"></a></p> |
| 435 | |
| 436 | <h1><a name="LaunchingandEmbeddingApacheFelix-EmbeddingFelix"></a>Embedding Felix</h1> |
| 437 | |
| 438 | <p>Embedding Felix into a host application is a simple way to provide a |
| 439 | sophisticated extensibility mechanism (i.e., plugin system) to the host |
| 440 | application. Embedding Felix is very similar to launching Felix as |
| 441 | described above, the main difference is that the host application |
| 442 | typically wants to interact with the framework instance and/or |
| 443 | installed bundles/services from the outside. This is fairly easy to |
| 444 | achieve with Felix, but there are some subtle issues to understand. |
| 445 | This section presents the mechanisms for embedding Felix into a host |
| 446 | application and the issues in doing so.</p> |
| 447 | |
| 448 | <p><a name="LaunchingandEmbeddingApacheFelix-configproperty"></a></p> |
| 449 | |
| 450 | <h2><a name="LaunchingandEmbeddingApacheFelix-EmbeddedExecutionConfigurationProperty"></a>Embedded Execution Configuration Property</h2> |
| 451 | |
| 452 | <p>When a Felix instance is embedded in a host application, the host |
| 453 | application must inform the Felix instance that it is embedded. To do |
| 454 | this, the host application sets the "<tt>felix.embedded.execution</tt>" configuration property to "<tt>true</tt>"; |
| 455 | this can be accomplished in the same way that all configuration |
| 456 | properties are set, i.e., passing it into the Felix constructor via a |
| 457 | map. This property specifically controls whether or not the Felix |
| 458 | instance will shutdown the JVM (i.e., call <tt>System.exit()</tt> when |
| 459 | the framework is shutdown. When embedding Felix it is generally not |
| 460 | desirable for Felix to shutdown the JVM; therefore, by setting this |
| 461 | property to "<tt>true</tt>" it can be avoided.</p> |
| 462 | |
| 463 | <p><a name="LaunchingandEmbeddingApacheFelix-hostinteraction"></a></p> |
| 464 | |
| 465 | <h2><a name="LaunchingandEmbeddingApacheFelix-Host/FelixInteraction"></a>Host/Felix Interaction</h2> |
| 466 | |
| 467 | <p>In the section on <a href="#LaunchingandEmbeddingApacheFelix-launching" title="launching on Launching and Embedding Apache Felix">launching</a> Felix above, the <tt>Felix</tt> |
| 468 | constructor accepts two arguments, the first being the configuration |
| 469 | properties for the framework, the second being a list of bundle |
| 470 | activator instances. These bundle activator instances provide a |
| 471 | convenient way for host applications to interact with the Felix |
| 472 | framework.</p> |
| 473 | |
| 474 | <p>Each bundle activator instance passed into the constructor |
| 475 | effectively becomes part of the System Bundle. This means that the |
| 476 | start()/stop() method of each bundle activator instance in the passed |
| 477 | in list gets invoked when the System Bundle's activator start()/stop() |
| 478 | method gets invoked. Consequently, each bundle activator instance will |
| 479 | be given the system bundle's <tt>BundleContext</tt> object so that they can interact with the framework externally. While it is possible to get the System Bundle's <tt>BundleContext</tt> object directly by calling <tt>Felix.getBundleContext()</tt>, |
| 480 | this is generally not as convenient since it requires that you monitor |
| 481 | when the System Bundle starts and/or stops. Consider following snippet |
| 482 | of a bundle activator:</p> |
| 483 | |
| 484 | <div class="code"><div class="codeContent"> |
| 485 | <pre class="code-java"><span class="code-keyword">public</span> class HostActivator <span class="code-keyword">implements</span> BundleActivator |
| 486 | { |
| 487 | <span class="code-keyword">private</span> BundleContext m_context = <span class="code-keyword">null</span>; |
| 488 | |
| 489 | <span class="code-keyword">public</span> void start(BundleContext context) |
| 490 | { |
| 491 | m_context = context; |
| 492 | } |
| 493 | |
| 494 | <span class="code-keyword">public</span> void stop(BundleContext context) |
| 495 | { |
| 496 | m_context = <span class="code-keyword">null</span>; |
| 497 | } |
| 498 | |
| 499 | <span class="code-keyword">public</span> Bundle[] getBundles() |
| 500 | { |
| 501 | <span class="code-keyword">if</span> (m_context != <span class="code-keyword">null</span>) |
| 502 | { |
| 503 | <span class="code-keyword">return</span> m_context.getBundles(); |
| 504 | } |
| 505 | <span class="code-keyword">return</span> <span class="code-keyword">null</span>; |
| 506 | } |
| 507 | }</pre> |
| 508 | </div></div> |
| 509 | |
| 510 | <p>Given the above bundle activator, it is now possible to embed Felix |
| 511 | into a host application and interact with it as the following snippet |
| 512 | illustrates:</p> |
| 513 | |
| 514 | <div class="code"><div class="codeContent"> |
| 515 | <pre class="code-java"><span class="code-keyword">public</span> class HostApplication |
| 516 | { |
| 517 | <span class="code-keyword">private</span> HostActivator m_activator = <span class="code-keyword">null</span>; |
| 518 | <span class="code-keyword">private</span> Felix m_felix = <span class="code-keyword">null</span>; |
| 519 | |
| 520 | <span class="code-keyword">public</span> HostApplication() |
| 521 | { |
| 522 | <span class="code-comment">// Create a <span class="code-keyword">case</span>-insensitive configuration property map. |
| 523 | </span> Map configMap = <span class="code-keyword">new</span> StringMap(<span class="code-keyword">false</span>); |
| 524 | <span class="code-comment">// Configure the Felix instance to be embedded. |
| 525 | </span> configMap.put(FelixConstants.EMBEDDED_EXECUTION_PROP, <span class="code-quote">"<span class="code-keyword">true</span>"</span>); |
| 526 | <span class="code-comment">// Add core OSGi packages to be exported from the class path |
| 527 | </span> <span class="code-comment">// via the system bundle. |
| 528 | </span> configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES, |
| 529 | <span class="code-quote">"org.osgi.framework; version=1.3.0,"</span> + |
| 530 | <span class="code-quote">"org.osgi.service.packageadmin; version=1.2.0,"</span> + |
| 531 | <span class="code-quote">"org.osgi.service.startlevel; version=1.0.0,"</span> + |
| 532 | <span class="code-quote">"org.osgi.service.url; version=1.0.0"</span>); |
| 533 | <span class="code-comment">// Explicitly specify the directory to use <span class="code-keyword">for</span> caching bundles. |
| 534 | </span> configMap.put(BundleCache.CACHE_PROFILE_DIR_PROP, <span class="code-quote">"cache"</span>); |
| 535 | |
| 536 | <span class="code-keyword">try</span> |
| 537 | { |
| 538 | <span class="code-comment">// Create host activator; |
| 539 | </span> m_activator = <span class="code-keyword">new</span> HostActivator(); |
| 540 | List list = <span class="code-keyword">new</span> ArrayList(); |
| 541 | list.add(m_activator); |
| 542 | |
| 543 | <span class="code-comment">// Now create an instance of the framework with |
| 544 | </span> <span class="code-comment">// our configuration properties and activator. |
| 545 | </span> m_felix = <span class="code-keyword">new</span> Felix(configMap, list); |
| 546 | |
| 547 | <span class="code-comment">// Now start Felix instance. |
| 548 | </span> m_felix.start(); |
| 549 | } |
| 550 | <span class="code-keyword">catch</span> (Exception ex) |
| 551 | { |
| 552 | <span class="code-object">System</span>.err.println(<span class="code-quote">"Could not create framework: "</span> + ex); |
| 553 | ex.printStackTrace(); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | <span class="code-keyword">public</span> Bundle[] getInstalledBundles() |
| 558 | { |
| 559 | <span class="code-comment">// Use the system bundle activator to gain external |
| 560 | </span> <span class="code-comment">// access to the set of installed bundles. |
| 561 | </span> <span class="code-keyword">return</span> m_activator.getBundles(); |
| 562 | } |
| 563 | |
| 564 | <span class="code-keyword">public</span> void shutdownApplication() |
| 565 | { |
| 566 | <span class="code-comment">// Shut down the felix framework when stopping the |
| 567 | </span> <span class="code-comment">// host application. |
| 568 | </span> m_felix.shutdown(); |
| 569 | } |
| 570 | }</pre> |
| 571 | </div></div> |
| 572 | |
| 573 | <p>Notice how the <tt>HostApplication.getInstalledBundles()</tt> method |
| 574 | uses its activator instance to get access to the System Bundle's |
| 575 | context in order to interact with the embedded Felix framework |
| 576 | instance. This approach provides the foundation for all interaction |
| 577 | between the host application and the embedded framework instance.</p> |
| 578 | |
| 579 | <p><a name="LaunchingandEmbeddingApacheFelix-hostservices"></a></p> |
| 580 | |
| 581 | <h2><a name="LaunchingandEmbeddingApacheFelix-ProvidingHostApplicationServices"></a>Providing Host Application Services</h2> |
| 582 | |
| 583 | <p>Providing services from the host application to bundles inside the |
| 584 | embedded Felix framework instance follows the basic approach laid out |
| 585 | in <a href="#LaunchingandEmbeddingApacheFelix-hostinteraction" title="host-interaction on Launching and Embedding Apache Felix">above</a>. |
| 586 | The main complication for providing a host application service to |
| 587 | bundles is the fact that both the host application and the bundles must |
| 588 | be using the same class definitions for the service interface classes. |
| 589 | Since the host application cannot import classes from a bundle, this |
| 590 | means that the service interface classes <b>must</b> be accessible on |
| 591 | the class path, typically as part of the host application itself. The |
| 592 | host application then must export the service interface package via the |
| 593 | system bundle so that bundles installed into the embedded framework |
| 594 | instance can import it. This is achieved using the <tt>org.osgi.framework.system.packages</tt> configuration property previously presented.</p> |
| 595 | |
| 596 | <p>Consider the follow simple property lookup service:</p> |
| 597 | |
| 598 | <div class="code"><div class="codeContent"> |
| 599 | <pre class="code-java"><span class="code-keyword">package</span> host.service.lookup; |
| 600 | |
| 601 | <span class="code-keyword">public</span> class Lookup |
| 602 | { |
| 603 | <span class="code-keyword">public</span> <span class="code-object">Object</span> lookup(<span class="code-object">String</span> name); |
| 604 | }</pre> |
| 605 | </div></div> |
| 606 | |
| 607 | <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>" |
| 608 | command. Now consider the following host application bundle activator, |
| 609 | which will be used to register/unregister the property lookup service |
| 610 | when the embedded framework instance starts/stops:</p> |
| 611 | |
| 612 | <div class="code"><div class="codeContent"> |
| 613 | <pre class="code-java"><span class="code-keyword">package</span> host.core; |
| 614 | |
| 615 | <span class="code-keyword">import</span> java.util.Map; |
| 616 | <span class="code-keyword">import</span> org.osgi.framework.BundleActivator; |
| 617 | <span class="code-keyword">import</span> org.osgi.framework.BundleContext; |
| 618 | <span class="code-keyword">import</span> org.osgi.framework.ServiceRegistration; |
| 619 | <span class="code-keyword">import</span> host.service.lookup; |
| 620 | |
| 621 | <span class="code-keyword">public</span> class HostActivator <span class="code-keyword">implements</span> BundleActivator |
| 622 | { |
| 623 | <span class="code-keyword">private</span> Map m_lookupMap = <span class="code-keyword">null</span>; |
| 624 | <span class="code-keyword">private</span> BundleContext m_context = <span class="code-keyword">null</span>; |
| 625 | <span class="code-keyword">private</span> ServiceRegistration m_registration = <span class="code-keyword">null</span>; |
| 626 | |
| 627 | <span class="code-keyword">public</span> HostActivator(Map lookupMap) |
| 628 | { |
| 629 | <span class="code-comment">// Save a reference to the service's backing store. |
| 630 | </span> m_lookupMap = lookupMap; |
| 631 | } |
| 632 | |
| 633 | <span class="code-keyword">public</span> void start(BundleContext context) |
| 634 | { |
| 635 | <span class="code-comment">// Save a reference to the bundle context. |
| 636 | </span> m_context = context; |
| 637 | <span class="code-comment">// Create a property lookup service implementation. |
| 638 | </span> Lookup lookup = <span class="code-keyword">new</span> Lookup() { |
| 639 | <span class="code-keyword">public</span> <span class="code-object">Object</span> lookup(<span class="code-object">String</span> name) |
| 640 | { |
| 641 | <span class="code-keyword">return</span> m_lookupMap.get(name); |
| 642 | } |
| 643 | }; |
| 644 | <span class="code-comment">// Register the property lookup service and save |
| 645 | </span> <span class="code-comment">// the service registration. |
| 646 | </span> m_registration = m_context.registerService( |
| 647 | Lookup.class.getName(), lookup, <span class="code-keyword">null</span>); |
| 648 | } |
| 649 | |
| 650 | <span class="code-keyword">public</span> void stop(BundleContext context) |
| 651 | { |
| 652 | <span class="code-comment">// Unregister the property lookup service. |
| 653 | </span> m_registration.unregister(); |
| 654 | m_context = <span class="code-keyword">null</span>; |
| 655 | } |
| 656 | }</pre> |
| 657 | </div></div> |
| 658 | |
| 659 | <p>Given the above host application bundle activator, the following |
| 660 | code snippet shows how the host application could create an embedded |
| 661 | version of the Felix framework and provide the property lookup service |
| 662 | to installed bundles:</p> |
| 663 | |
| 664 | <div class="code"><div class="codeContent"> |
| 665 | <pre class="code-java"><span class="code-keyword">package</span> host.core; |
| 666 | |
| 667 | <span class="code-keyword">import</span> java.util.List; |
| 668 | <span class="code-keyword">import</span> java.util.ArrayList; |
| 669 | <span class="code-keyword">import</span> java.util.Map; |
| 670 | <span class="code-keyword">import</span> java.util.HashMap; |
| 671 | <span class="code-keyword">import</span> host.service.lookup.Lookup; |
| 672 | <span class="code-keyword">import</span> org.apache.felix.framework.Felix; |
| 673 | <span class="code-keyword">import</span> org.apache.felix.framework.util.FelixConstants; |
| 674 | <span class="code-keyword">import</span> org.apache.felix.framework.util.StringMap; |
| 675 | <span class="code-keyword">import</span> org.apache.felix.framework.cache.BundleCache; |
| 676 | |
| 677 | <span class="code-keyword">public</span> class HostApplication |
| 678 | { |
| 679 | <span class="code-keyword">private</span> HostActivator m_activator = <span class="code-keyword">null</span>; |
| 680 | <span class="code-keyword">private</span> Felix m_felix = <span class="code-keyword">null</span>; |
| 681 | <span class="code-keyword">private</span> Map m_lookupMap = <span class="code-keyword">new</span> HashMap(); |
| 682 | |
| 683 | <span class="code-keyword">public</span> HostApplication() |
| 684 | { |
| 685 | <span class="code-comment">// Initialize the map <span class="code-keyword">for</span> the property lookup service. |
| 686 | </span> m_lookupMap.put(<span class="code-quote">"name1"</span>, <span class="code-quote">"value1"</span>); |
| 687 | m_lookupMap.put(<span class="code-quote">"name2"</span>, <span class="code-quote">"value2"</span>); |
| 688 | m_lookupMap.put(<span class="code-quote">"name3"</span>, <span class="code-quote">"value3"</span>); |
| 689 | m_lookupMap.put(<span class="code-quote">"name4"</span>, <span class="code-quote">"value4"</span>); |
| 690 | |
| 691 | <span class="code-comment">// Create a <span class="code-keyword">case</span>-insensitive configuration property map. |
| 692 | </span> Map configMap = <span class="code-keyword">new</span> StringMap(<span class="code-keyword">false</span>); |
| 693 | <span class="code-comment">// Configure the Felix instance to be embedded. |
| 694 | </span> configMap.put(FelixConstants.EMBEDDED_EXECUTION_PROP, <span class="code-quote">"<span class="code-keyword">true</span>"</span>); |
| 695 | <span class="code-comment">// Add the host provided service <span class="code-keyword">interface</span> <span class="code-keyword">package</span> and the core OSGi |
| 696 | </span> <span class="code-comment">// packages to be exported from the class path via the system bundle. |
| 697 | </span> configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES, |
| 698 | <span class="code-quote">"org.osgi.framework; version=1.3.0,"</span> + |
| 699 | <span class="code-quote">"org.osgi.service.packageadmin; version=1.2.0,"</span> + |
| 700 | <span class="code-quote">"org.osgi.service.startlevel; version=1.0.0,"</span> + |
| 701 | <span class="code-quote">"org.osgi.service.url; version=1.0.0,"</span> + |
| 702 | <span class="code-quote">"host.service.lookup; version=1.0.0"</span>); |
| 703 | <span class="code-comment">// Explicitly specify the directory to use <span class="code-keyword">for</span> caching bundles. |
| 704 | </span> configMap.put(BundleCache.CACHE_PROFILE_DIR_PROP, <span class="code-quote">"cache"</span>); |
| 705 | |
| 706 | <span class="code-keyword">try</span> |
| 707 | { |
| 708 | <span class="code-comment">// Create host activator; |
| 709 | </span> m_activator = <span class="code-keyword">new</span> HostActivator(m_lookupMap); |
| 710 | List list = <span class="code-keyword">new</span> ArrayList(); |
| 711 | list.add(m_activator); |
| 712 | |
| 713 | <span class="code-comment">// Now create an instance of the framework with |
| 714 | </span> <span class="code-comment">// our configuration properties and activator. |
| 715 | </span> m_felix = <span class="code-keyword">new</span> Felix(configMap, list); |
| 716 | |
| 717 | <span class="code-comment">// Now start Felix instance. |
| 718 | </span> m_felix.start(); |
| 719 | } |
| 720 | <span class="code-keyword">catch</span> (Exception ex) |
| 721 | { |
| 722 | <span class="code-object">System</span>.err.println(<span class="code-quote">"Could not create framework: "</span> + ex); |
| 723 | ex.printStackTrace(); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | <span class="code-keyword">public</span> void shutdownApplication() |
| 728 | { |
| 729 | <span class="code-comment">// Shut down the felix framework when stopping the |
| 730 | </span> <span class="code-comment">// host application. |
| 731 | </span> m_felix.shutdown(); |
| 732 | } |
| 733 | }</pre> |
| 734 | </div></div> |
| 735 | |
| 736 | <p>Rather than having the host application bundle activator register |
| 737 | the service, it is also possible for the the host application to simply |
| 738 | get the bundle context from the bundle activator and register the |
| 739 | service directly, but the presented approach is perhaps a little |
| 740 | cleaner since it allows the host application to register/unregister the |
| 741 | service when the system bundle starts/stops.</p> |
| 742 | |
| 743 | <p><a name="LaunchingandEmbeddingApacheFelix-hostserviceusage"></a></p> |
| 744 | |
| 745 | <h2><a name="LaunchingandEmbeddingApacheFelix-UsingServicesProvidedbyBundles"></a>Using Services Provided by Bundles</h2> |
| 746 | |
| 747 | <p>Using services provided by bundles follows the same general approach |
| 748 | of using a host application bundle activator. The main complication for |
| 749 | the host application using a service from a bundle is the fact that |
| 750 | both the host application and the bundle must be using the same class |
| 751 | definitions for the service interface classes. Since the host |
| 752 | application cannot import classes from a bundle, this means that the |
| 753 | service interface classes <b>must</b> be accessible on the class path, |
| 754 | typically as part of the host application itself. The host application |
| 755 | then must export the service interface package via the system bundle so |
| 756 | that bundles installed into the embedded framework instance can import |
| 757 | it. This is achieved using the <tt>org.osgi.framework.system.packages</tt> configuration property previously presented.</p> |
| 758 | |
| 759 | <p>Consider the following simple command service interface for which |
| 760 | bundles provide implementations, such as might be used to create an |
| 761 | extensible interactive shell:</p> |
| 762 | |
| 763 | <div class="code"><div class="codeContent"> |
| 764 | <pre class="code-java"><span class="code-keyword">package</span> host.service.command; |
| 765 | |
| 766 | <span class="code-keyword">public</span> class Command |
| 767 | { |
| 768 | <span class="code-keyword">public</span> <span class="code-object">String</span> getName(); |
| 769 | <span class="code-keyword">public</span> <span class="code-object">String</span> getDescription(); |
| 770 | <span class="code-keyword">public</span> <span class="code-object">boolean</span> execute(<span class="code-object">String</span> commandline); |
| 771 | }</pre> |
| 772 | </div></div> |
| 773 | |
| 774 | <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>" |
| 775 | command. Now consider the previously introduced host application bundle |
| 776 | activator below, which simply provides access to the system bundle |
| 777 | context:</p> |
| 778 | |
| 779 | <div class="code"><div class="codeContent"> |
| 780 | <pre class="code-java"><span class="code-keyword">package</span> host.core; |
| 781 | |
| 782 | <span class="code-keyword">import</span> org.osgi.framework.BundleActivator; |
| 783 | <span class="code-keyword">import</span> org.osgi.framework.BundleContext; |
| 784 | |
| 785 | <span class="code-keyword">public</span> class HostActivator <span class="code-keyword">implements</span> BundleActivator |
| 786 | { |
| 787 | <span class="code-keyword">private</span> BundleContext m_context = <span class="code-keyword">null</span>; |
| 788 | |
| 789 | <span class="code-keyword">public</span> void start(BundleContext context) |
| 790 | { |
| 791 | m_context = context; |
| 792 | } |
| 793 | |
| 794 | <span class="code-keyword">public</span> void stop(BundleContext context) |
| 795 | { |
| 796 | m_context = <span class="code-keyword">null</span>; |
| 797 | } |
| 798 | |
| 799 | <span class="code-keyword">public</span> BundleContext getContext() |
| 800 | { |
| 801 | <span class="code-keyword">return</span> m_context; |
| 802 | } |
| 803 | }</pre> |
| 804 | </div></div> |
| 805 | |
| 806 | <p>With this bundle activator, the host application can command |
| 807 | services provided by bundles installed inside its embedded Felix |
| 808 | framework instance. The following code snippet illustrates one possible |
| 809 | approach:</p> |
| 810 | |
| 811 | <div class="code"><div class="codeContent"> |
| 812 | <pre class="code-java"><span class="code-keyword">package</span> host.core; |
| 813 | |
| 814 | <span class="code-keyword">import</span> java.util.List; |
| 815 | <span class="code-keyword">import</span> java.util.ArrayList; |
| 816 | <span class="code-keyword">import</span> java.util.Map; |
| 817 | <span class="code-keyword">import</span> host.service.command.Command; |
| 818 | <span class="code-keyword">import</span> org.apache.felix.framework.Felix; |
| 819 | <span class="code-keyword">import</span> org.apache.felix.framework.util.FelixConstants; |
| 820 | <span class="code-keyword">import</span> org.apache.felix.framework.util.StringMap; |
| 821 | <span class="code-keyword">import</span> org.apache.felix.framework.cache.BundleCache; |
| 822 | <span class="code-keyword">import</span> org.osgi.util.tracker.ServiceTracker; |
| 823 | |
| 824 | <span class="code-keyword">public</span> class HostApplication |
| 825 | { |
| 826 | <span class="code-keyword">private</span> HostActivator m_activator = <span class="code-keyword">null</span>; |
| 827 | <span class="code-keyword">private</span> Felix m_felix = <span class="code-keyword">null</span>; |
| 828 | <span class="code-keyword">private</span> ServiceTracker m_tracker = <span class="code-keyword">null</span>; |
| 829 | |
| 830 | <span class="code-keyword">public</span> HostApplication() |
| 831 | { |
| 832 | <span class="code-comment">// Create a <span class="code-keyword">case</span>-insensitive configuration property map. |
| 833 | </span> Map configMap = <span class="code-keyword">new</span> StringMap(<span class="code-keyword">false</span>); |
| 834 | <span class="code-comment">// Configure the Felix instance to be embedded. |
| 835 | </span> configMap.put(FelixConstants.EMBEDDED_EXECUTION_PROP, <span class="code-quote">"<span class="code-keyword">true</span>"</span>); |
| 836 | <span class="code-comment">// Add the bundle provided service <span class="code-keyword">interface</span> <span class="code-keyword">package</span> and the core OSGi |
| 837 | </span> <span class="code-comment">// packages to be exported from the class path via the system bundle. |
| 838 | </span> configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES, |
| 839 | <span class="code-quote">"org.osgi.framework; version=1.3.0,"</span> + |
| 840 | <span class="code-quote">"org.osgi.service.packageadmin; version=1.2.0,"</span> + |
| 841 | <span class="code-quote">"org.osgi.service.startlevel; version=1.0.0,"</span> + |
| 842 | <span class="code-quote">"org.osgi.service.url; version=1.0.0,"</span> + |
| 843 | <span class="code-quote">"host.service.command; version=1.0.0"</span>); |
| 844 | <span class="code-comment">// Explicitly specify the directory to use <span class="code-keyword">for</span> caching bundles. |
| 845 | </span> configMap.put(BundleCache.CACHE_PROFILE_DIR_PROP, <span class="code-quote">"cache"</span>); |
| 846 | |
| 847 | <span class="code-keyword">try</span> |
| 848 | { |
| 849 | <span class="code-comment">// Create host activator; |
| 850 | </span> m_activator = <span class="code-keyword">new</span> HostActivator(m_lookupMap); |
| 851 | List list = <span class="code-keyword">new</span> ArrayList(); |
| 852 | list.add(m_activator); |
| 853 | |
| 854 | <span class="code-comment">// Now create an instance of the framework with |
| 855 | </span> <span class="code-comment">// our configuration properties and activator. |
| 856 | </span> m_felix = <span class="code-keyword">new</span> Felix(configMap, list); |
| 857 | |
| 858 | <span class="code-comment">// Now start Felix instance. |
| 859 | </span> m_felix.start(); |
| 860 | } |
| 861 | <span class="code-keyword">catch</span> (Exception ex) |
| 862 | { |
| 863 | <span class="code-object">System</span>.err.println(<span class="code-quote">"Could not create framework: "</span> + ex); |
| 864 | ex.printStackTrace(); |
| 865 | } |
| 866 | |
| 867 | m_tracker = <span class="code-keyword">new</span> ServiceTracker( |
| 868 | m_activator.getContext(), Command.class.getName(), <span class="code-keyword">null</span>); |
| 869 | m_tracker.open(); |
| 870 | } |
| 871 | |
| 872 | <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) |
| 873 | { |
| 874 | <span class="code-comment">// See <span class="code-keyword">if</span> any of the currently tracked command services |
| 875 | </span> <span class="code-comment">// match the specified command name, <span class="code-keyword">if</span> so then execute it. |
| 876 | </span> <span class="code-object">Object</span>[] services = m_tracker.getServices(); |
| 877 | <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++) |
| 878 | { |
| 879 | <span class="code-keyword">try</span> |
| 880 | { |
| 881 | <span class="code-keyword">if</span> (((Command) services[i]).getName().equals(name)) |
| 882 | { |
| 883 | <span class="code-keyword">return</span> ((Command) services[i]).execute(commandline); |
| 884 | } |
| 885 | } |
| 886 | <span class="code-keyword">catch</span> (Exception ex) |
| 887 | { |
| 888 | <span class="code-comment">// Since the services returned by the tracker could become |
| 889 | </span> <span class="code-comment">// invalid at any moment, we will <span class="code-keyword">catch</span> all exceptions, log |
| 890 | </span> <span class="code-comment">// a message, and then ignore faulty services. |
| 891 | </span> <span class="code-object">System</span>.err.println(ex); |
| 892 | } |
| 893 | } |
| 894 | <span class="code-keyword">return</span> <span class="code-keyword">false</span>; |
| 895 | } |
| 896 | |
| 897 | <span class="code-keyword">public</span> void shutdownApplication() |
| 898 | { |
| 899 | <span class="code-comment">// Shut down the felix framework when stopping the |
| 900 | </span> <span class="code-comment">// host application. |
| 901 | </span> m_felix.shutdown(); |
| 902 | } |
| 903 | }</pre> |
| 904 | </div></div> |
| 905 | |
| 906 | <p>The above example is overly simplistic with respect to concurrency |
| 907 | issues and error conditions, but it demonstrates the overall approach |
| 908 | for using bundle-provided services from the host application. Note, to |
| 909 | compile the above code you will need to compile against the Felix |
| 910 | framework and Felix OSGi compendium JAR files, since the <tt>ServiceTracker</tt> classes are included in the compendium JAR file, not the framework JAR file.</p> |
| 911 | |
| 912 | <p><a name="LaunchingandEmbeddingApacheFelix-servicereflection"></a></p> |
| 913 | |
| 914 | <h3><a name="LaunchingandEmbeddingApacheFelix-UsingBundleServicesviaReflection"></a>Using Bundle Services via Reflection</h3> |
| 915 | |
| 916 | <p>It possible for the host application to use services provided by |
| 917 | bundles without having access to the service interface classes and thus |
| 918 | not needing to put the service interface classes on the class path. To |
| 919 | do this, the host application uses the same general approach to acquire |
| 920 | the system bundle context object, which it can use to look up service |
| 921 | objects. Using either an LDAP filter or the service interface class |
| 922 | name, the host application can retrieve the service object and then use |
| 923 | standard Java reflection to invoke methods on the service object.</p> |
| 924 | |
| 925 | <p><a name="LaunchingandEmbeddingApacheFelix-caveat"></a></p> |
| 926 | |
| 927 | <h1><a name="LaunchingandEmbeddingApacheFelix-Caveat"></a>Caveat</h1> |
| 928 | |
| 929 | <p>The code in this document has not been thoroughly tested or even |
| 930 | compiled and may be out of date with respect to the current Felix |
| 931 | source code. If you find errors please report them so the that they can |
| 932 | be corrected.</p> |
Karl Pauls | 7bca0de | 2007-09-16 20:38:10 +0000 | [diff] [blame] | 933 | |
| 934 | <p><a name="LaunchingandEmbeddingApacheFelix-feedback"></a></p> |
| 935 | |
| 936 | <h2><a name="LaunchingandEmbeddingApacheFelix-Feedback"></a>Feedback</h2> |
| 937 | |
| 938 | <p>Subscribe to the Felix users mailing list by sending a message to <span class="nobr"><a href="mailto:users-subscribe@felix.apache.org" title="Send mail to users-subscribe@felix.apache.org" rel="nofollow">users-subscribe@felix.apache.org<sup><img class="rendericon" src="launching-and-embedding-apache-felix_files/mail_small.gif" alt="" align="absmiddle" border="0" height="12" width="13"></sup></a></span>; after subscribing, email questions or feedback to <span class="nobr"><a href="mailto:users@felix.apache.org" title="Send mail to users@felix.apache.org" rel="nofollow">users@felix.apache.org<sup><img class="rendericon" src="launching-and-embedding-apache-felix_files/mail_small.gif" alt="" align="absmiddle" border="0" height="12" width="13"></sup></a></span>.</p> |
Richard S. Hall | 2cd5bed | 2007-07-16 20:32:41 +0000 | [diff] [blame] | 939 | </div> |
Karl Pauls | 7bca0de | 2007-09-16 20:38:10 +0000 | [diff] [blame] | 940 | |
| 941 | </body></html> |