Karl Pauls | f7d389c | 2008-11-06 16:22:54 +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> |
| 3 | |
| 4 | |
| 5 | |
| 6 | <title>Apache Felix - Apache Felix Framework Launching and Embedding</title> |
| 7 | <link rel="stylesheet" href="apache-felix-framework-launching-and-embedding_files/site.css" type="text/css" media="all"> |
| 8 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| 9 | </head><body> |
| 10 | <div class="title"><div class="logo"><a href="http://felix.apache.org/site/index.html"><img alt="Apache Felix" src="apache-felix-framework-launching-and-embedding_files/logo.png" border="0"></a></div><div class="header"><a href="http://www.apache.org/"><img alt="Apache" src="apache-felix-framework-launching-and-embedding-Dateien/apache.png" border="0"></a></div></div> |
| 11 | <div class="menu"> |
| 12 | <ul> |
| 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/license.html" title="license">license</a></li> |
| 15 | <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="apache-felix-framework-launching-and-embedding_files/linkext7.gif" alt="" align="absmiddle" border="0" height="7" width="7"></sup></a></span></li> |
| 16 | <li><a href="http://felix.apache.org/site/documentation.html" title="documentation">documentation</a></li> |
| 17 | <li><a href="http://felix.apache.org/site/mailinglists.html" title="mailinglists">mailing lists</a></li> |
| 18 | <li><a href="http://felix.apache.org/site/contributing.html" title="Contributing">contributing</a></li> |
| 19 | <li><span class="nobr"><a href="http://www.apache.org/" title="Visit page outside Confluence" rel="nofollow">asf<sup><img class="rendericon" src="apache-felix-framework-launching-and-embedding_files/linkext7.gif" alt="" align="absmiddle" border="0" height="7" width="7"></sup></a></span></li> |
| 20 | <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="apache-felix-framework-launching-and-embedding_files/linkext7.gif" alt="" align="absmiddle" border="0" height="7" width="7"></sup></a></span></li> |
| 21 | <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="apache-felix-framework-launching-and-embedding_files/linkext7.gif" alt="" align="absmiddle" border="0" height="7" width="7"></sup></a></span> |
| 22 | <!-- ApacheCon Ad --> |
| 23 | <iframe src="apache-felix-framework-launching-and-embedding_files/button.html" style="border-width: 0pt; float: left;" frameborder="0" height="135" scrolling="no" width="135"></iframe> |
| 24 | <p style="height: 100px;"> |
| 25 | <!-- ApacheCon Ad --> |
| 26 | </p></li></ul> </div> |
| 27 | <div class="main"> |
| 28 | <h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-ApacheFelixFrameworkLaunchingandEmbedding"></a>Apache Felix Framework Launching and Embedding</h1> |
| 29 | |
| 30 | <p><em>[This document is based on Felix 1.4.0.]</em></p> |
| 31 | |
| 32 | <ul> |
| 33 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-introduction" title="introduction on Apache Felix Framework Launching and Embedding">Introduction</a></li> |
| 34 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-overview" title="overview on Apache Felix Framework Launching and Embedding">API Overview</a> |
| 35 | <ul> |
| 36 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-creatingandconfiguring" title="creating-and-configuring on Apache Felix Framework Launching and Embedding">Creating and Configuring the Framework Instance</a></li> |
| 37 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-startinginstance" title="starting-instance on Apache Felix Framework Launching and Embedding">Starting the Framework Instance</a></li> |
| 38 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-stoppinginstance" title="stopping-instance on Apache Felix Framework Launching and Embedding">Stopping the Framework Instance</a></li> |
| 39 | </ul> |
| 40 | </li> |
| 41 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-launching" title="launching on Apache Felix Framework Launching and Embedding">Launching Felix</a> |
| 42 | <ul> |
| 43 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-standardlauncher" title="standard-launcher on Apache Felix Framework Launching and Embedding">Standard Felix Launcher</a></li> |
| 44 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-customlauncher" title="custom-launcher on Apache Felix Framework Launching and Embedding">Custom Felix Launcher</a></li> |
| 45 | </ul> |
| 46 | </li> |
| 47 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-embedding" title="embedding on Apache Felix Framework Launching and Embedding">Embedding Felix</a> |
| 48 | <ul> |
| 49 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-hostinteraction" title="host-interaction on Apache Felix Framework Launching and Embedding">Host/Felix Interaction</a></li> |
| 50 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-hostservices" title="host-services on Apache Felix Framework Launching and Embedding">Providing Host Application Services</a></li> |
| 51 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-hostserviceusage" title="host-service-usage on Apache Felix Framework Launching and Embedding">Using Services Provided by Bundles</a> |
| 52 | <ul> |
| 53 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-servicereflection" title="service-reflection on Apache Felix Framework Launching and Embedding">Using Bundle Services via Reflection</a></li> |
| 54 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-serviceother" title="service-other on Apache Felix Framework Launching and Embedding">Other Approaches</a></li> |
| 55 | </ul> |
| 56 | </li> |
| 57 | </ul> |
| 58 | </li> |
| 59 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-caveat" title="caveat on Apache Felix Framework Launching and Embedding">Caveat</a></li> |
| 60 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-feedback" title="feedback on Apache Felix Framework Launching and Embedding">Feedback</a></li> |
| 61 | </ul> |
| 62 | |
| 63 | |
| 64 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-introduction"></a></p> |
| 65 | |
| 66 | <h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-Introduction"></a>Introduction</h1> |
| 67 | |
| 68 | <p>The Apache Felix framework is intended to be easily launchable and |
| 69 | embeddable. For example, Felix avoids the use of system properties for |
| 70 | configuration, since these are globals and can cause interference if |
| 71 | multiple framework instances are created in the same VM. Felix also |
| 72 | tries to multiplex singleton facilities, like the URL stream handler |
| 73 | factory. The goal is to make it possible to use Felix in as many |
| 74 | scenarios as possible; however, this is still just a goal. In other |
| 75 | words, this is a work in progress and if any issues arise, it would be |
| 76 | greatly appreciated if they are brought to the attention of the Felix |
| 77 | community. The next section provides a Felix API overview, while the |
| 78 | remainder of the document is divided into two sections, one focusing on |
| 79 | how to launch Felix and one focusing on how to embed Felix into a host |
| 80 | application.</p> |
| 81 | |
| 82 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-overview"></a></p> |
| 83 | |
| 84 | <h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-APIOverview"></a>API Overview</h1> |
| 85 | |
| 86 | <p>The Felix framework is implemented by the <tt>org.apache.felix.framework.Felix</tt> class or just <tt>Felix</tt> |
| 87 | for short. As part of the ongoing OSGi specification process, there is |
| 88 | a movement to standardize the API for launching and embedding OSGi |
| 89 | framework implementations. The approach is to have the framework |
| 90 | implement the <tt>org.osgi.framework.launch.Framework</tt> interface, which extends the <tt>org.osgi.framework.Bundle</tt> interface. These interfaces provide the necessary means to launch and manage framework instances. The <tt>Bundle</tt> interface is defined as:</p> |
| 91 | |
| 92 | <div class="code"><div class="codeContent"> |
| 93 | <pre class="code-java"><span class="code-keyword">public</span> <span class="code-keyword">interface</span> Bundle |
| 94 | { |
| 95 | BundleContext getBundleContext(); |
| 96 | <span class="code-object">long</span> getBundleId(); |
| 97 | URL getEntry(<span class="code-object">String</span> name); |
| 98 | Enumeration getEntryPaths(<span class="code-object">String</span> path); |
| 99 | Enumeration findEntries(<span class="code-object">String</span> path, <span class="code-object">String</span> filePattern, <span class="code-object">boolean</span> recurse); |
| 100 | Dictionary getHeaders(); |
| 101 | Dictionary getHeaders(<span class="code-object">String</span> locale); |
| 102 | <span class="code-object">long</span> getLastModified(); |
| 103 | <span class="code-object">String</span> getLocation(); |
| 104 | URL getResource(<span class="code-object">String</span> name); |
| 105 | Enumeration getResources(<span class="code-object">String</span> name) <span class="code-keyword">throws</span> IOException; |
| 106 | ServiceReference[] getRegisteredServices(); |
| 107 | ServiceReference[] getServicesInUse(); |
| 108 | <span class="code-object">int</span> getState(); |
| 109 | <span class="code-object">String</span> getSymbolicName(); |
| 110 | <span class="code-object">boolean</span> hasPermission(<span class="code-object">Object</span> obj); |
| 111 | <span class="code-object">Class</span> loadClass(<span class="code-object">String</span> name) <span class="code-keyword">throws</span> ClassNotFoundException; |
| 112 | void start() <span class="code-keyword">throws</span> BundleException; |
| 113 | void stop() <span class="code-keyword">throws</span> BundleException; |
| 114 | void uninstall() <span class="code-keyword">throws</span> BundleException; |
| 115 | void update() <span class="code-keyword">throws</span> BundleException; |
| 116 | void update(InputStream is) <span class="code-keyword">throws</span> BundleException; |
| 117 | }</pre> |
| 118 | </div></div> |
| 119 | |
| 120 | <p>The <tt>Framework</tt> interface is defined as:</p> |
| 121 | |
| 122 | <div class="code"><div class="codeContent"> |
| 123 | <pre class="code-java"><span class="code-keyword">public</span> <span class="code-keyword">interface</span> Framework <span class="code-keyword">extends</span> Bundle |
| 124 | { |
| 125 | void init(); |
| 126 | FrameworkEvent waitForStop(); |
| 127 | }</pre> |
| 128 | </div></div> |
| 129 | |
| 130 | <p>An additional requirement for framework implementations not captured |
| 131 | in the interface definitions is that they must implement a public |
| 132 | constructor that accepts a <tt>Map</tt>, which is used to pass in configuration properties. When you instantiate the <tt>Felix</tt> |
| 133 | class, the resulting object is the actual System Bundle that bundles |
| 134 | inside the framework will see if they get bundle 0, which is the System |
| 135 | Bundle as defined by the OSGi specification.</p> |
| 136 | |
| 137 | <table class="warningMacro" align="center" border="0" cellpadding="5" cellspacing="8" width="85%"><colgroup><col width="24"><col></colgroup><tbody><tr><td valign="top"><img src="apache-felix-framework-launching-and-embedding_files/forbidden.gif" alt="" align="absmiddle" border="0" height="16" width="16"></td><td><b class="strong">WARNING</b><br> |
| 138 | <p>This API is undergoing changes and is not completely finalized, so future changes are possible.</p></td></tr></tbody></table> |
| 139 | |
| 140 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-creatingandconfiguring"></a></p> |
| 141 | |
| 142 | <h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-CreatingandConfiguringtheFrameworkInstance"></a>Creating and Configuring the Framework Instance</h2> |
| 143 | |
| 144 | <p>To create a framework instance, simply instantiate the <tt>Felix</tt> class. A newly created framework instance is in the <tt>Bundle.INSTALLED</tt> state. You configure the instance by passing the constructor a <tt>Map</tt> containing its configurations properties. The configuration map may contain the following OSGi standard properties:</p> |
| 145 | |
| 146 | <ul> |
| 147 | <li><tt>org.osgi.framework.system.packages</tt> - specifies a |
| 148 | list of packages the system bundle should export from the environment; |
| 149 | if this is not set, then the framework uses a reasonable default fault.</li> |
| 150 | <li><tt>org.osgi.framework.system.packages.extra</tt> |
| 151 | - specifies a list of additional packages the system bundle should |
| 152 | export from the environment that are appended to the packages specified |
| 153 | in <tt>org.osgi.framework.system.packages</tt>; there is no default value for this property.</li> |
| 154 | <li><tt>org.osgi.framework.bootdelegation</tt> |
| 155 | - specifies a list of packages that should be made implicitly available |
| 156 | to all bundles from the environment (i.e., no need to import them); |
| 157 | there is no default value for this property and its use should be |
| 158 | avoided.</li> |
| 159 | <li><tt>org.osgi.framework.storage</tt> - specifies the |
| 160 | path to a directory, which will be created if it does not exist, to use |
| 161 | for bundle cache storage; the default value for this property is "<tt>felix-cache</tt>" in the current working directory.</li> |
| 162 | <li><tt>org.osgi.framework.storage.clean</tt> |
| 163 | - specifies whether the bundle cache should be flushed; the default |
| 164 | value for this property is "none", but it can be changed to |
| 165 | "onFirstInit" to flush the bundle cache when the framework is |
| 166 | initialized.</li> |
| 167 | <li><tt>org.osgi.framework.startlevel</tt> - specifies the start level the framework enters upon startup; the default value for this property is 1.</li> |
| 168 | </ul> |
| 169 | |
| 170 | |
| 171 | <p>Felix also has the following, non-standard configuration properties:</p> |
| 172 | |
| 173 | <ul> |
| 174 | <li><tt>felix.cache.rootdir</tt> - specifies which directory should be used to calculate absolute paths when relative paths are used for the <tt>org.osgi.framework.storage</tt> property; the default value for this property is the current working directory.</li> |
| 175 | <li><tt>felix.systembundle.activators</tt> - specifies a <tt>List</tt> of <tt>BundleActivator</tt> |
| 176 | instances that are started/stopped when the System Bundle is |
| 177 | started/stopped; the specified instances will receive the System |
| 178 | Bundle's <tt>BundleContext</tt> when invoked.</li> |
| 179 | <li><tt>felix.log.logger</tt> - specifies an instance of <tt>org.apache.felix.framework.util.Logger</tt> that the framework uses as its default logger.</li> |
| 180 | <li><tt>felix.log.level</tt> - specifies an integer <tt>String</tt> |
| 181 | whose value indicates the degree of logging reported by the framework; |
| 182 | the default value is "1" and "0" turns off logging completely, |
| 183 | otherwise log levels match those specified in the OSGi Log Service |
| 184 | (i.e., 1 = error, 2 = warning, 3 = information, and 4 = debug).</li> |
| 185 | <li><tt>felix.startlevel.bundle</tt> - specifies the start level for newly installed bundles; the default value is 1.</li> |
| 186 | <li><tt>framework.service.urlhandlers</tt> |
| 187 | - specifies whether or not to activate the URL Handlers service for the |
| 188 | framework instance; the default value is "<tt>true</tt>", |
| 189 | which results in the |
| 190 | <tt>URL.setURLStreamHandlerFactory()</tt> and |
| 191 | <tt>URLConnection.setContentHandlerFactory()</tt> being |
| 192 | called.</li> |
| 193 | </ul> |
| 194 | |
| 195 | |
| 196 | <p>The configuration map passed into the constructor is copied and the |
| 197 | keys are treated as case insensitive. You are not able to change the |
| 198 | framework's configuration after construction. If you need a different |
| 199 | configuration, you must create a new framework instance.</p> |
| 200 | |
| 201 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-startinginstance"></a></p> |
| 202 | |
| 203 | <h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-StartingtheFrameworkInstance"></a>Starting the Framework Instance</h2> |
| 204 | |
| 205 | <p>The <tt>start()</tt> method is used to start the framework instance. If the <tt>init()</tt> method was not invoked prior to calling <tt>start()</tt>, then it is implicitly invoked from <tt>start()</tt>. The two methods result in two different framework state transitions:</p> |
| 206 | |
| 207 | <ul> |
| 208 | <li><tt>init()</tt> results in the framework instance in the <tt>Bundle.STARTING</tt> state.</li> |
| 209 | <li><tt>start()</tt> results in the framework instance in the <tt>Bundle.ACTIVE</tt> state.</li> |
| 210 | </ul> |
| 211 | |
| 212 | |
| 213 | <p>The <tt>init()} method is necessary since the framework does not have a {{BundleContext</tt> when it is first created, so a transition to the <tt>Bundle.STARTING</tt> state is required to acquire its context (via <tt>Bundle.getBundleContext()</tt>) for performing various tasks, such as installing bundles. Note that Felix also provides the <tt>felix.systembundle.activators</tt> property that serves a similar purpose. After the <tt>init()</tt> method completes, the follow actions have been performed:</p> |
| 214 | |
| 215 | <ul> |
| 216 | <li>Event handling is enabled.</li> |
| 217 | <li>The security manager is installed if it is enabled.</li> |
| 218 | <li>The framework is set to start level 0.</li> |
| 219 | <li>All bundles in the bundle caches are reified and their state is set to <tt>Bundle.INSTALLED</tt>.</li> |
| 220 | <li>The framework gets a valid <tt>BundleContext</tt>.</li> |
| 221 | <li>All framework-provided services are made available (e.g., PackageAdmin, StartLevel, etc.).</li> |
| 222 | <li>The framework enters the <tt>Bundle.STARTING</tt> state.</li> |
| 223 | </ul> |
| 224 | |
| 225 | |
| 226 | <p>A call to <tt>start()</tt> is necessary to start the framework instance, if the <tt>init()</tt> method is invoked manually. Invoking <tt>init()</tt> or <tt>start()</tt> on an already started framework as no effect.</p> |
| 227 | |
| 228 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-stoppinginstance"></a></p> |
| 229 | |
| 230 | <h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-StoppingtheFrameworkInstance"></a>Stopping the Framework Instance</h2> |
| 231 | |
| 232 | <p>To stop the framework instance, invoke the <tt>stop()</tt> method, which will asynchronously stop the framework. To know when the framework has finished its shutdown sequence, use the <tt>waitForStop()</tt> method to wait until it is complete. A stopped framework will be in the <tt>Bundle.RESOLVED</tt> state. It is possible to restart the framework, using the normal combination of <tt>init()</tt>/<tt>start()</tt> methods as previously described.</p> |
| 233 | |
| 234 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-launching"></a></p> |
| 235 | |
| 236 | <h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-LaunchingFelix"></a>Launching Felix</h1> |
| 237 | |
| 238 | <p>Launching Felix is fairly simple and involves only three steps:</p> |
| 239 | |
| 240 | <ol> |
| 241 | <li>Define some configuration properties.</li> |
| 242 | <li>Create an instance of <tt>org.apache.felix.framework.Felix</tt> with the configuration properties.</li> |
| 243 | <li>Invoke the <tt>org.apache.felix.framework.Felix.start()</tt> method.</li> |
| 244 | </ol> |
| 245 | |
| 246 | |
| 247 | <p>In reality, the first step is optional, since all properties will |
| 248 | have reasonable defaults, but if you are creating a launcher you will |
| 249 | generally want to more than that, such as automatically installing and |
| 250 | starting bundles when you start the framework instance. The default |
| 251 | Felix launcher defines reusable functionality to automatically install |
| 252 | and/or start bundles upon framework startup; 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> |
| 253 | |
| 254 | <p>The remainder of this section describes how the standard Felix |
| 255 | launcher works as well as how to create a custom launcher for Felix.</p> |
| 256 | |
| 257 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-standardlauncher"></a></p> |
| 258 | |
| 259 | <h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-StandardFelixLauncher"></a>Standard Felix Launcher</h2> |
| 260 | |
| 261 | <p>The standard Felix launcher is very simple and is not intended to |
| 262 | solve every possible requirement; it is intended to work for most |
| 263 | standard situations. Most special launching requirements should be |
| 264 | resolved by creating a custom launcher. This section describes how the |
| 265 | 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> |
| 266 | |
| 267 | <div class="code"><div class="codeContent"> |
| 268 | <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 |
| 269 | { |
| 270 | <span class="code-comment">// (1) Check <span class="code-keyword">for</span> proper command line usage. |
| 271 | </span> <span class="code-keyword">if</span> (args.length > 1) |
| 272 | { |
| 273 | <span class="code-object">System</span>.out.println(<span class="code-quote">"Usage: [<bundle-cache-dir>]"</span>); |
| 274 | <span class="code-object">System</span>.exit(0); |
| 275 | } |
| 276 | |
| 277 | <span class="code-comment">// (2) Load system properties. |
| 278 | </span> Main.loadSystemProperties(); |
| 279 | |
| 280 | <span class="code-comment">// (3) Read configuration properties. |
| 281 | </span> Properties configProps = Main.loadConfigProperties(); |
| 282 | |
| 283 | <span class="code-comment">// (4) Copy framework properties from the system properties. |
| 284 | </span> Main.copySystemProperties(configProps); |
| 285 | |
| 286 | <span class="code-comment">// (5) If specified, use command-line argument as path to bundle cache. |
| 287 | </span> <span class="code-keyword">if</span> (args.length > 0) |
| 288 | { |
| 289 | configProps.setProperty(Constants.FRAMEWORK_STORAGE, args[0]); |
| 290 | } |
| 291 | |
| 292 | <span class="code-comment">// (6) Create a list <span class="code-keyword">for</span> custom framework activators and |
| 293 | </span> <span class="code-comment">// add an instance of the auto-activator it <span class="code-keyword">for</span> processing |
| 294 | </span> <span class="code-comment">// auto-install and auto-start properties. Add <span class="code-keyword">this</span> list |
| 295 | </span> <span class="code-comment">// to the configuration properties. |
| 296 | </span> List list = <span class="code-keyword">new</span> ArrayList(); |
| 297 | list.add(<span class="code-keyword">new</span> AutoActivator(configProps)); |
| 298 | configProps.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list); |
| 299 | |
| 300 | <span class="code-comment">// Print welcome banner. |
| 301 | </span> <span class="code-object">System</span>.out.println(<span class="code-quote">"\nWelcome to Felix."</span>); |
| 302 | <span class="code-object">System</span>.out.println(<span class="code-quote">"=================\n"</span>); |
| 303 | |
| 304 | <span class="code-keyword">try</span> |
| 305 | { |
| 306 | <span class="code-comment">// (7) Create an instance and start the framework. |
| 307 | </span> |
| 308 | m_felix = <span class="code-keyword">new</span> Felix(configProps); |
| 309 | m_felix.start(); |
| 310 | <span class="code-comment">// (8) Wait <span class="code-keyword">for</span> framework to stop to exit the VM. |
| 311 | </span> m_felix.waitForStop(); |
| 312 | <span class="code-object">System</span>.exit(0); |
| 313 | } |
| 314 | <span class="code-keyword">catch</span> (Exception ex) |
| 315 | { |
| 316 | <span class="code-object">System</span>.err.println(<span class="code-quote">"Could not create framework: "</span> + ex); |
| 317 | ex.printStackTrace(); |
| 318 | <span class="code-object">System</span>.exit(-1); |
| 319 | } |
| 320 | }</pre> |
| 321 | </div></div> |
| 322 | |
| 323 | <p>The general steps of the standard launcher are quite straightforward:</p> |
| 324 | |
| 325 | <ol> |
| 326 | <li>The launcher only supports a single, optional command-line |
| 327 | argument, which is the path to the bundle cache, so check for this and |
| 328 | issue a usage message it there are more than one arguments.</li> |
| 329 | <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> |
| 330 | system property. This file is not needed to launch Felix and is |
| 331 | provided merely for convenience when system properties must be |
| 332 | specified. The file is a standard Java properties file, but it also |
| 333 | supports property substitution using <tt>${<property-name</tt>} syntax. Property substitution can be nested; only system properties will be used for substitution.</li> |
| 334 | <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> |
| 335 | system property. This file is used to configure the Felix instance |
| 336 | created by the launcher. The file is a standard Java properties file, |
| 337 | but it also supports property substitution using "<tt>${<property-name</tt>}" |
| 338 | syntax. Property substitution can be nested; configuration and system |
| 339 | properties will be used for substitution with configuration properties |
| 340 | having precedence.</li> |
| 341 | <li>For convenience, any configuration |
| 342 | properties that are set as system properties will be copied into the |
| 343 | set of configuration properties to provide an easy way to add to or |
| 344 | override configuration properties specified in the <tt>config.properties</tt> file.</li> |
| 345 | <li>If there is a single command-line argument, then use that to set the value of <tt>org.osgi.framework.storage</tt>; relative paths are relative to the current directory unless the <tt>felix.cache.rootdir</tt> property is set.</li> |
| 346 | <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://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 configuration properties.</li> |
| 347 | <li>Create the Felix instance passing in the configuration properties, then call <tt>start()</tt>.</li> |
| 348 | <li>Invoke <tt>waitForStop()</tt> to wait for the framework to stop to force the VM to exit; this is necessary because the framework never calls <tt>System.exit()</tt> and some libraries (e.g., Swing) create threads that will not allow the VM to exit.</li> |
| 349 | </ol> |
| 350 | |
| 351 | |
| 352 | <p>The framework is not active until the <tt>start()</tt> method is |
| 353 | called. If no shell bundles are installed and started or if there is |
| 354 | difficulty locating the shell bundles specified in the auto-start |
| 355 | property, then it will appear as if the framework is hung, but it is |
| 356 | actually running without any way to interact with it since the shell |
| 357 | bundles provide the only means of interaction.</p> |
| 358 | |
| 359 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-customlauncher"></a></p> |
| 360 | |
| 361 | <h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-CustomFelixLauncher"></a>Custom Felix Launcher</h2> |
| 362 | |
| 363 | <p>This section creates a bare-bones launcher to demonstrate the |
| 364 | minimum requirements for creating an interactive launcher for the Felix |
| 365 | framework. This example uses the standard Felix shell bundles for |
| 366 | interactivity, but any other bundles could be used instead. For |
| 367 | example, the shell service and telnet bundles could be used to launch |
| 368 | Felix and make it remotely accessible.</p> |
| 369 | |
| 370 | <p>This example launcher project has the following directory structure:</p> |
| 371 | |
| 372 | <div class="preformatted"><div class="preformattedContent"> |
| 373 | <pre>launcher/ |
| 374 | lib/ |
| 375 | org.apache.felix.main-1.4.0.jar |
| 376 | bundle/ |
| 377 | org.apache.felix.shell-1.0.2.jar |
| 378 | org.apache.felix.shell.tui-1.0.2.jar |
| 379 | src/ |
| 380 | example/ |
| 381 | Main.java |
| 382 | </pre> |
| 383 | </div></div> |
| 384 | |
| 385 | <p>The <tt>lib/</tt> directory contains Felix' main JAR file, which |
| 386 | also contains the OSGi core interfaces. The main JAR file is used so |
| 387 | that we can reuse the default launcher's auto-install/auto-start |
| 388 | configuration property handling; if these capabilities are not needed, |
| 389 | then it would be possible to use the framework JAR file instead of the |
| 390 | main JAR file. The <tt>bundle/</tt> directory contains the shell |
| 391 | service and textual shell interface bundles that will be used for |
| 392 | interacting with the framework instance. Note: If you do not launch |
| 393 | Felix with interactive bundles, it will appear as if the framework |
| 394 | instance is hung, but it is actually just sitting there waiting for |
| 395 | 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> |
| 396 | |
| 397 | <div class="code"><div class="codeContent"> |
| 398 | <pre class="code-java"><span class="code-keyword">package</span> example; |
| 399 | |
| 400 | <span class="code-keyword">import</span> java.util.ArrayList; |
| 401 | <span class="code-keyword">import</span> java.util.List; |
| 402 | <span class="code-keyword">import</span> java.util.Map; |
| 403 | <span class="code-keyword">import</span> java.util.HashMap; |
| 404 | <span class="code-keyword">import</span> org.osgi.framework.Constants; |
| 405 | <span class="code-keyword">import</span> org.apache.felix.framework.Felix; |
| 406 | <span class="code-keyword">import</span> org.apache.felix.framework.util.FelixConstants; |
| 407 | <span class="code-keyword">import</span> org.apache.felix.main.AutoActivator; |
| 408 | |
| 409 | <span class="code-keyword">public</span> class Main |
| 410 | { |
| 411 | <span class="code-keyword">private</span> <span class="code-keyword">static</span> Felix m_felix = <span class="code-keyword">null</span>; |
| 412 | |
| 413 | <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 |
| 414 | { |
| 415 | <span class="code-comment">// Print welcome banner. |
| 416 | </span> <span class="code-object">System</span>.out.println(<span class="code-quote">"\nWelcome to Felix."</span>); |
| 417 | <span class="code-object">System</span>.out.println(<span class="code-quote">"=================\n"</span>); |
| 418 | |
| 419 | Map configMap = <span class="code-keyword">new</span> HashMap(); |
| 420 | configMap.put(AutoActivator.AUTO_START_PROP + <span class="code-quote">".1"</span>, |
| 421 | <span class="code-quote">"file:bundle/org.apache.felix.shell-1.0.2.jar "</span> + |
| 422 | <span class="code-quote">"file:bundle/org.apache.felix.shell.tui-1.0.2.jar"</span>); |
| 423 | List list = <span class="code-keyword">new</span> ArrayList(); |
| 424 | list.add(<span class="code-keyword">new</span> AutoActivator(configMap)); |
| 425 | configMap.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list); |
| 426 | |
| 427 | <span class="code-keyword">try</span> |
| 428 | { |
| 429 | m_felix = <span class="code-keyword">new</span> Felix(configMap); |
| 430 | m_felix.start(); |
| 431 | m_felix.waitForStop(); |
| 432 | <span class="code-object">System</span>.exit(0); |
| 433 | } |
| 434 | <span class="code-keyword">catch</span> (Exception ex) |
| 435 | { |
| 436 | <span class="code-object">System</span>.err.println(<span class="code-quote">"Could not create framework: "</span> + ex); |
| 437 | ex.printStackTrace(); |
| 438 | <span class="code-object">System</span>.exit(-1); |
| 439 | } |
| 440 | } |
| 441 | }</pre> |
| 442 | </div></div> |
| 443 | |
| 444 | <p>This launcher has all information hard coded in it, unlike the |
| 445 | default Felix launcher, which loads configuration properties from files |
| 446 | and performs variable substitution. This simple launcher provides a |
| 447 | good starting point if the features of the default launcher are not |
| 448 | necessary. Since very few configuration properties are specified, the |
| 449 | default values are used. In the case of the framework bundle cache, it |
| 450 | will use "<tt>felix-cache</tt>" in the current directory.</p> |
| 451 | |
| 452 | <p>By breaking down the above source code into small chunks, it is quite easy to see what is going on.</p> |
| 453 | |
| 454 | <div class="code"><div class="codeContent"> |
| 455 | <pre class="code-java">Map configMap = <span class="code-keyword">new</span> HashMap();</pre> |
| 456 | </div></div> |
| 457 | |
| 458 | <p>This simply creates a map to hold configuration properties.</p> |
| 459 | |
| 460 | <div class="code"><div class="codeContent"> |
| 461 | <pre class="code-java">configMap.put(AutoActivator.AUTO_START_PROP + <span class="code-quote">".1"</span>, |
| 462 | <span class="code-quote">"file:bundle/org.apache.felix.shell-1.0.2.jar "</span> + |
| 463 | <span class="code-quote">"file:bundle/org.apache.felix.shell.tui-1.0.2.jar"</span>);</pre> |
| 464 | </div></div> |
| 465 | |
| 466 | <p>This sets the <tt>AutoActivator.AUTO_START_PROP</tt> configuration property (string value "<tt>felix.auto.start</tt>"), |
| 467 | which is a space-delimited list of bundle URLs that the framework will |
| 468 | automatically install and start when the framework starts. However, |
| 469 | this property key cannot be used as is; it must be appended with a "." |
| 470 | and then a number, where the number represents the start level for the |
| 471 | bundle when it is installed. In this particular example, ".1" is |
| 472 | appended to the property name, thus the two bundles will be installed |
| 473 | into start level one. This example uses relative <tt>file:</tt> URLs, which will load the bundles from the <tt>bundle/</tt> |
| 474 | directory assuming that the launcher is started from the root directory |
| 475 | of the launcher project. It is also possible to specify absolute URLs |
| 476 | or remote URLs.</p> |
| 477 | |
| 478 | <div class="code"><div class="codeContent"> |
| 479 | <pre class="code-java">List list = <span class="code-keyword">new</span> ArrayList(); |
| 480 | list.add(<span class="code-keyword">new</span> AutoActivator(configMap)); |
| 481 | configMap.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);</pre> |
| 482 | </div></div> |
| 483 | |
| 484 | <p>This above creates a list to hold custom framework activators and adds an instance of <tt>org.apache.felix.main.AutoActivator</tt> |
| 485 | to it, which will process the auto-install and auto-start configuration |
| 486 | properties during framework startup. The list of activators is then |
| 487 | added to the configuration map.</p> |
| 488 | |
| 489 | <div class="code"><div class="codeContent"> |
| 490 | <pre class="code-java">m_felix = <span class="code-keyword">new</span> Felix(configMap); |
| 491 | m_felix.start();</pre> |
| 492 | </div></div> |
| 493 | |
| 494 | <p>These steps create the framework instance and start it. The configuration property map is passed into the <tt>Felix</tt> constructor.</p> |
| 495 | |
| 496 | <div class="code"><div class="codeContent"> |
| 497 | <pre class="code-java">m_felix.waitForStop(); |
| 498 | <span class="code-object">System</span>.exit(0);</pre> |
| 499 | </div></div> |
| 500 | |
| 501 | <p>These final steps cause the launching application thread to wait for |
| 502 | the framework to stop and when it does the launching thread calls <tt>System.exit()</tt> to make sure the VM actually exits.</p> |
| 503 | |
| 504 | <p>The following command compiles the launcher when run from the root directory of the launcher project:</p> |
| 505 | |
| 506 | <div class="preformatted"><div class="preformattedContent"> |
| 507 | <pre>javac -d . -classpath lib/org.apache.felix.main-1.4.0.jar src/example/Main.java |
| 508 | </pre> |
| 509 | </div></div> |
| 510 | |
| 511 | <p>After executing this command, an <tt>example/</tt> directory is |
| 512 | created in the current directory, which contains the generated class |
| 513 | file. The following command executes the simple launcher when run from |
| 514 | the root directory of the launcher project:</p> |
| 515 | |
| 516 | <div class="preformatted"><div class="preformattedContent"> |
| 517 | <pre>java -cp .:lib/org.apache.felix.main-1.4.0.jar example.Main |
| 518 | </pre> |
| 519 | </div></div> |
| 520 | |
| 521 | <p>After executing this command, a "<tt>felix-cache/</tt>" directory is created that contains the installed bundles, which were installed from the <tt>bundle/</tt> directory.</p> |
| 522 | |
| 523 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-embedding"></a></p> |
| 524 | |
| 525 | <h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-EmbeddingFelix"></a>Embedding Felix</h1> |
| 526 | |
| 527 | <p>Embedding Felix into a host application is a simple way to provide a |
| 528 | sophisticated extensibility mechanism (i.e., a plugin system) to the |
| 529 | host application. Embedding Felix is very similar to launching Felix as |
| 530 | described above, the main difference is that the host application |
| 531 | typically wants to interact with the framework instance and/or |
| 532 | installed bundles/services from the outside. This is fairly easy to |
| 533 | achieve with Felix, but there are some subtle issues to understand. |
| 534 | This section presents the mechanisms for embedding Felix into a host |
| 535 | application and the issues in doing so.</p> |
| 536 | |
| 537 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-hostinteraction"></a></p> |
| 538 | |
| 539 | <h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-Host/FelixInteraction"></a>Host/Felix Interaction</h2> |
| 540 | |
| 541 | <p>In the section on <a href="#ApacheFelixFrameworkLaunchingandEmbedding-launching" title="launching on Apache Felix Framework Launching and Embedding">launching</a> Felix above, the <tt>Felix</tt> accepts a configuration property called <tt>felix.systembundle.activators</tt>, |
| 542 | which is a list of bundle activator instances. These bundle activator |
| 543 | instances provide a convenient way for host applications to interact |
| 544 | with the Felix framework. The ability offered by these activators can |
| 545 | also be accomplished by invoking <tt>init()</tt> on the framework instance and the using <tt>getBundleContext()</tt> to get the System Bundle's context, but it can be more convenient to use an activator instance.</p> |
| 546 | |
| 547 | <p>Each activator instance passed into the constructor effectively becomes part of the System Bundle. This means that the <tt>start()</tt>/<tt>stop()</tt> methods of each activator instance in the list gets invoked when the System Bundle's activator <tt>start()</tt>/<tt>stop()</tt> methods gets invoked, respectively. Each activator instance will be given the System Bundle's <tt>BundleContext</tt> object so that they can interact with the framework. Consider following snippet of a bundle activator:</p> |
| 548 | |
| 549 | <div class="code"><div class="codeContent"> |
| 550 | <pre class="code-java"><span class="code-keyword">public</span> class HostActivator <span class="code-keyword">implements</span> BundleActivator |
| 551 | { |
| 552 | <span class="code-keyword">private</span> BundleContext m_context = <span class="code-keyword">null</span>; |
| 553 | |
| 554 | <span class="code-keyword">public</span> void start(BundleContext context) |
| 555 | { |
| 556 | m_context = context; |
| 557 | } |
| 558 | |
| 559 | <span class="code-keyword">public</span> void stop(BundleContext context) |
| 560 | { |
| 561 | m_context = <span class="code-keyword">null</span>; |
| 562 | } |
| 563 | |
| 564 | <span class="code-keyword">public</span> Bundle[] getBundles() |
| 565 | { |
| 566 | <span class="code-keyword">if</span> (m_context != <span class="code-keyword">null</span>) |
| 567 | { |
| 568 | <span class="code-keyword">return</span> m_context.getBundles(); |
| 569 | } |
| 570 | <span class="code-keyword">return</span> <span class="code-keyword">null</span>; |
| 571 | } |
| 572 | }</pre> |
| 573 | </div></div> |
| 574 | |
| 575 | <p>Given the above bundle activator, it is now possible to embed Felix |
| 576 | into a host application and interact with it as the following snippet |
| 577 | illustrates:</p> |
| 578 | |
| 579 | <div class="code"><div class="codeContent"> |
| 580 | <pre class="code-java"><span class="code-keyword">public</span> class HostApplication |
| 581 | { |
| 582 | <span class="code-keyword">private</span> HostActivator m_activator = <span class="code-keyword">null</span>; |
| 583 | <span class="code-keyword">private</span> Felix m_felix = <span class="code-keyword">null</span>; |
| 584 | |
| 585 | <span class="code-keyword">public</span> HostApplication() |
| 586 | { |
| 587 | <span class="code-comment">// Create a configuration property map. |
| 588 | </span> Map configMap = <span class="code-keyword">new</span> HashMap(); |
| 589 | <span class="code-comment">// Create host activator; |
| 590 | </span> m_activator = <span class="code-keyword">new</span> HostActivator(); |
| 591 | List list = <span class="code-keyword">new</span> ArrayList(); |
| 592 | list.add(m_activator); |
| 593 | configMap.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list); |
| 594 | |
| 595 | <span class="code-keyword">try</span> |
| 596 | { |
| 597 | <span class="code-comment">// Now create an instance of the framework with |
| 598 | </span> <span class="code-comment">// our configuration properties. |
| 599 | </span> m_felix = <span class="code-keyword">new</span> Felix(configMap); |
| 600 | <span class="code-comment">// Now start Felix instance. |
| 601 | </span> m_felix.start(); |
| 602 | } |
| 603 | <span class="code-keyword">catch</span> (Exception ex) |
| 604 | { |
| 605 | <span class="code-object">System</span>.err.println(<span class="code-quote">"Could not create framework: "</span> + ex); |
| 606 | ex.printStackTrace(); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | <span class="code-keyword">public</span> Bundle[] getInstalledBundles() |
| 611 | { |
| 612 | <span class="code-comment">// Use the system bundle activator to gain external |
| 613 | </span> <span class="code-comment">// access to the set of installed bundles. |
| 614 | </span> <span class="code-keyword">return</span> m_activator.getBundles(); |
| 615 | } |
| 616 | |
| 617 | <span class="code-keyword">public</span> void shutdownApplication() |
| 618 | { |
| 619 | <span class="code-comment">// Shut down the felix framework when stopping the |
| 620 | </span> <span class="code-comment">// host application. |
| 621 | </span> m_felix.stop(); |
| 622 | m_felix.waitForStop(); |
| 623 | } |
| 624 | }</pre> |
| 625 | </div></div> |
| 626 | |
| 627 | <p>Notice how the <tt>HostApplication.getInstalledBundles()</tt> method |
| 628 | uses its activator instance to get access to the System Bundle's |
| 629 | context in order to interact with the embedded Felix framework |
| 630 | instance. This approach provides the foundation for all interaction |
| 631 | between the host application and the embedded framework instance.</p> |
| 632 | |
| 633 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-hostservices"></a></p> |
| 634 | |
| 635 | <h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-ProvidingHostApplicationServices"></a>Providing Host Application Services</h2> |
| 636 | |
| 637 | <p>Providing services from the host application to bundles inside the |
| 638 | embedded Felix framework instance follows the basic approach laid out |
| 639 | in <a href="#ApacheFelixFrameworkLaunchingandEmbedding-hostinteraction" title="host-interaction on Apache Felix Framework Launching and Embedding">above</a>. |
| 640 | The main complication for providing a host application service to |
| 641 | bundles is the fact that both the host application and the bundles must |
| 642 | be using the same class definitions for the service interface classes. |
| 643 | Since the host application cannot import classes from a bundle, this |
| 644 | means that the service interface classes <b>must</b> be accessible on |
| 645 | the class path, typically as part of the host application itself. The |
| 646 | host application then must export the service interface package via the |
| 647 | system bundle so that bundles installed into the embedded framework |
| 648 | instance can import it. This is achieved using the <tt>org.osgi.framework.system.packages.extra</tt> configuration property previously presented.</p> |
| 649 | |
| 650 | <p>Consider the follow simple property lookup service:</p> |
| 651 | |
| 652 | <div class="code"><div class="codeContent"> |
| 653 | <pre class="code-java"><span class="code-keyword">package</span> host.service.lookup; |
| 654 | |
| 655 | <span class="code-keyword">public</span> class Lookup |
| 656 | { |
| 657 | <span class="code-keyword">public</span> <span class="code-object">Object</span> lookup(<span class="code-object">String</span> name); |
| 658 | }</pre> |
| 659 | </div></div> |
| 660 | |
| 661 | <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>" |
| 662 | command. Now consider the following host application bundle activator, |
| 663 | which will be used to register/unregister the property lookup service |
| 664 | when the embedded framework instance starts/stops:</p> |
| 665 | |
| 666 | <div class="code"><div class="codeContent"> |
| 667 | <pre class="code-java"><span class="code-keyword">package</span> host.core; |
| 668 | |
| 669 | <span class="code-keyword">import</span> java.util.Map; |
| 670 | <span class="code-keyword">import</span> org.osgi.framework.BundleActivator; |
| 671 | <span class="code-keyword">import</span> org.osgi.framework.BundleContext; |
| 672 | <span class="code-keyword">import</span> org.osgi.framework.ServiceRegistration; |
| 673 | <span class="code-keyword">import</span> host.service.lookup; |
| 674 | |
| 675 | <span class="code-keyword">public</span> class HostActivator <span class="code-keyword">implements</span> BundleActivator |
| 676 | { |
| 677 | <span class="code-keyword">private</span> Map m_lookupMap = <span class="code-keyword">null</span>; |
| 678 | <span class="code-keyword">private</span> BundleContext m_context = <span class="code-keyword">null</span>; |
| 679 | <span class="code-keyword">private</span> ServiceRegistration m_registration = <span class="code-keyword">null</span>; |
| 680 | |
| 681 | <span class="code-keyword">public</span> HostActivator(Map lookupMap) |
| 682 | { |
| 683 | <span class="code-comment">// Save a reference to the service's backing store. |
| 684 | </span> m_lookupMap = lookupMap; |
| 685 | } |
| 686 | |
| 687 | <span class="code-keyword">public</span> void start(BundleContext context) |
| 688 | { |
| 689 | <span class="code-comment">// Save a reference to the bundle context. |
| 690 | </span> m_context = context; |
| 691 | <span class="code-comment">// Create a property lookup service implementation. |
| 692 | </span> Lookup lookup = <span class="code-keyword">new</span> Lookup() { |
| 693 | <span class="code-keyword">public</span> <span class="code-object">Object</span> lookup(<span class="code-object">String</span> name) |
| 694 | { |
| 695 | <span class="code-keyword">return</span> m_lookupMap.get(name); |
| 696 | } |
| 697 | }; |
| 698 | <span class="code-comment">// Register the property lookup service and save |
| 699 | </span> <span class="code-comment">// the service registration. |
| 700 | </span> m_registration = m_context.registerService( |
| 701 | Lookup.class.getName(), lookup, <span class="code-keyword">null</span>); |
| 702 | } |
| 703 | |
| 704 | <span class="code-keyword">public</span> void stop(BundleContext context) |
| 705 | { |
| 706 | <span class="code-comment">// Unregister the property lookup service. |
| 707 | </span> m_registration.unregister(); |
| 708 | m_context = <span class="code-keyword">null</span>; |
| 709 | } |
| 710 | }</pre> |
| 711 | </div></div> |
| 712 | |
| 713 | <p>Given the above host application bundle activator, the following |
| 714 | code snippet shows how the host application could create an embedded |
| 715 | version of the Felix framework and provide the property lookup service |
| 716 | to installed bundles:</p> |
| 717 | |
| 718 | <div class="code"><div class="codeContent"> |
| 719 | <pre class="code-java"><span class="code-keyword">package</span> host.core; |
| 720 | |
| 721 | <span class="code-keyword">import</span> java.util.List; |
| 722 | <span class="code-keyword">import</span> java.util.ArrayList; |
| 723 | <span class="code-keyword">import</span> java.util.Map; |
| 724 | <span class="code-keyword">import</span> java.util.HashMap; |
| 725 | <span class="code-keyword">import</span> host.service.lookup.Lookup; |
| 726 | <span class="code-keyword">import</span> org.apache.felix.framework.Felix; |
| 727 | <span class="code-keyword">import</span> org.apache.felix.framework.util.FelixConstants; |
| 728 | <span class="code-keyword">import</span> org.osgi.framework.Constants; |
| 729 | |
| 730 | <span class="code-keyword">public</span> class HostApplication |
| 731 | { |
| 732 | <span class="code-keyword">private</span> HostActivator m_activator = <span class="code-keyword">null</span>; |
| 733 | <span class="code-keyword">private</span> Felix m_felix = <span class="code-keyword">null</span>; |
| 734 | <span class="code-keyword">private</span> Map m_lookupMap = <span class="code-keyword">new</span> HashMap(); |
| 735 | |
| 736 | <span class="code-keyword">public</span> HostApplication() |
| 737 | { |
| 738 | <span class="code-comment">// Initialize the map <span class="code-keyword">for</span> the property lookup service. |
| 739 | </span> m_lookupMap.put(<span class="code-quote">"name1"</span>, <span class="code-quote">"value1"</span>); |
| 740 | |
| 741 | m_lookupMap.put(<span class="code-quote">"name2"</span>, <span class="code-quote">"value2"</span>); |
| 742 | m_lookupMap.put(<span class="code-quote">"name3"</span>, <span class="code-quote">"value3"</span>); |
| 743 | m_lookupMap.put(<span class="code-quote">"name4"</span>, <span class="code-quote">"value4"</span>); |
| 744 | |
| 745 | <span class="code-comment">// Create a configuration property map. |
| 746 | </span> Map configMap = <span class="code-keyword">new</span> HashMap(); |
| 747 | <span class="code-comment">// Export the host provided service <span class="code-keyword">interface</span> <span class="code-keyword">package</span>. |
| 748 | </span> configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, |
| 749 | <span class="code-quote">"host.service.lookup; version=1.0.0"</span>); |
| 750 | <span class="code-comment">// Create host activator; |
| 751 | </span> m_activator = <span class="code-keyword">new</span> HostActivator(m_lookupMap); |
| 752 | List list = <span class="code-keyword">new</span> ArrayList(); |
| 753 | list.add(m_activator); |
| 754 | configMap.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list); |
| 755 | |
| 756 | <span class="code-keyword">try</span> |
| 757 | { |
| 758 | <span class="code-comment">// Now create an instance of the framework with |
| 759 | </span> <span class="code-comment">// our configuration properties. |
| 760 | </span> m_felix = <span class="code-keyword">new</span> Felix(configMap); |
| 761 | <span class="code-comment">// Now start Felix instance. |
| 762 | </span> m_felix.start(); |
| 763 | } |
| 764 | <span class="code-keyword">catch</span> (Exception ex) |
| 765 | { |
| 766 | <span class="code-object">System</span>.err.println(<span class="code-quote">"Could not create framework: "</span> + ex); |
| 767 | ex.printStackTrace(); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | <span class="code-keyword">public</span> void shutdownApplication() |
| 772 | { |
| 773 | <span class="code-comment">// Shut down the felix framework when stopping the |
| 774 | </span> <span class="code-comment">// host application. |
| 775 | </span> m_felix.stop(); |
| 776 | m_felix.waitForStop(); |
| 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="ApacheFelixFrameworkLaunchingandEmbedding-hostserviceusage"></a></p> |
| 789 | |
| 790 | <h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-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.extra</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 use 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.cache.BundleCache; |
| 866 | <span class="code-keyword">import</span> org.osgi.framework.Constants; |
| 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 configuration property map. |
| 878 | </span> Map configMap = <span class="code-keyword">new</span> HashMap(); |
| 879 | <span class="code-comment">// Export the host provided service <span class="code-keyword">interface</span> <span class="code-keyword">package</span>. |
| 880 | </span> configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, |
| 881 | <span class="code-quote">"host.service.command; version=1.0.0"</span>); |
| 882 | <span class="code-comment">// Create host activator; |
| 883 | </span> m_activator = <span class="code-keyword">new</span> HostActivator(); |
| 884 | List list = <span class="code-keyword">new</span> ArrayList(); |
| 885 | list.add(m_activator); |
| 886 | configMap.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list); |
| 887 | |
| 888 | <span class="code-keyword">try</span> |
| 889 | { |
| 890 | <span class="code-comment">// Now create an instance of the framework with |
| 891 | </span> <span class="code-comment">// our configuration properties. |
| 892 | </span> m_felix = <span class="code-keyword">new</span> Felix(configMap); |
| 893 | <span class="code-comment">// Now start Felix instance. |
| 894 | </span> m_felix.start(); |
| 895 | } |
| 896 | <span class="code-keyword">catch</span> (Exception ex) |
| 897 | { |
| 898 | <span class="code-object">System</span>.err.println(<span class="code-quote">"Could not create framework: "</span> + ex); |
| 899 | ex.printStackTrace(); |
| 900 | } |
| 901 | |
| 902 | m_tracker = <span class="code-keyword">new</span> ServiceTracker( |
| 903 | m_activator.getContext(), Command.class.getName(), <span class="code-keyword">null</span>); |
| 904 | m_tracker.open(); |
| 905 | } |
| 906 | |
| 907 | <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) |
| 908 | { |
| 909 | <span class="code-comment">// See <span class="code-keyword">if</span> any of the currently tracked command services |
| 910 | </span> <span class="code-comment">// match the specified command name, <span class="code-keyword">if</span> so then execute it. |
| 911 | </span> <span class="code-object">Object</span>[] services = m_tracker.getServices(); |
| 912 | <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++) |
| 913 | { |
| 914 | <span class="code-keyword">try</span> |
| 915 | { |
| 916 | <span class="code-keyword">if</span> (((Command) services[i]).getName().equals(name)) |
| 917 | { |
| 918 | <span class="code-keyword">return</span> ((Command) services[i]).execute(commandline); |
| 919 | } |
| 920 | } |
| 921 | <span class="code-keyword">catch</span> (Exception ex) |
| 922 | { |
| 923 | <span class="code-comment">// Since the services returned by the tracker could become |
| 924 | </span> <span class="code-comment">// invalid at any moment, we will <span class="code-keyword">catch</span> all exceptions, log |
| 925 | </span> <span class="code-comment">// a message, and then ignore faulty services. |
| 926 | </span> <span class="code-object">System</span>.err.println(ex); |
| 927 | } |
| 928 | } |
| 929 | <span class="code-keyword">return</span> <span class="code-keyword">false</span>; |
| 930 | } |
| 931 | |
| 932 | <span class="code-keyword">public</span> void shutdownApplication() |
| 933 | { |
| 934 | { |
| 935 | <span class="code-comment">// Shut down the felix framework when stopping the |
| 936 | </span> <span class="code-comment">// host application. |
| 937 | </span> m_felix.stop(); |
| 938 | m_felix.waitForStop(); |
| 939 | } |
| 940 | }</pre> |
| 941 | </div></div> |
| 942 | |
| 943 | <p>The above example is overly simplistic with respect to concurrency |
| 944 | issues and error conditions, but it demonstrates the overall approach |
| 945 | for using bundle-provided services from the host application.</p> |
| 946 | |
| 947 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-servicereflection"></a></p> |
| 948 | |
| 949 | <h3><a name="ApacheFelixFrameworkLaunchingandEmbedding-UsingBundleServicesviaReflection"></a>Using Bundle Services via Reflection</h3> |
| 950 | |
| 951 | <p>It possible for the host application to use services provided by |
| 952 | bundles without having access to the service interface classes and thus |
| 953 | not needing to put the service interface classes on the class path. To |
| 954 | do this, the host application uses the same general approach to acquire |
| 955 | the system bundle context object, which it can use to look up service |
| 956 | objects. Using either an LDAP filter or the service interface class |
| 957 | name, the host application can retrieve the service object and then use |
| 958 | standard Java reflection to invoke methods on the service object.</p> |
| 959 | |
| 960 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-serviceother"></a></p> |
| 961 | |
| 962 | <h3><a name="ApacheFelixFrameworkLaunchingandEmbedding-OtherApproaches"></a>Other Approaches</h3> |
| 963 | |
| 964 | <p>The <span class="nobr"><a href="http://code.google.com/p/transloader/" title="Visit page outside Confluence" rel="nofollow">Transloader<sup><img class="rendericon" src="apache-felix-framework-launching-and-embedding_files/linkext7.gif" alt="" align="absmiddle" border="0" height="7" width="7"></sup></a></span> project is another attempt at dealing with issues of classes loaded from different class loaders and may be of interest.</p> |
| 965 | |
| 966 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-caveat"></a></p> |
| 967 | |
| 968 | <h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-Caveat"></a>Caveat</h1> |
| 969 | |
| 970 | <p>The code in this document has not been thoroughly tested nor even |
| 971 | compiled and may be out of date with respect to the current Felix |
| 972 | source code. If you find errors please report them so the that they can |
| 973 | be corrected.</p> |
| 974 | |
| 975 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-feedback"></a></p> |
| 976 | |
| 977 | <h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-Feedback"></a>Feedback</h2> |
| 978 | |
| 979 | <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="apache-felix-framework-launching-and-embedding_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="apache-felix-framework-launching-and-embedding-Dateien/mail_small.gif" alt="" align="absmiddle" border="0" height="12" width="13"></sup></a></span>.</p> |
| 980 | </div> |
| 981 | </body></html> |