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