blob: d2d0f828f5e378232125ab277e614d20cf347edb [file] [log] [blame]
Karl Paulsf7d389c2008-11-06 16:22:54 +00001<!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>
Karl Pauls4994cb02008-12-20 17:15:21 +000010 <div class="title"><div class="logo"><a href="http://felix.apache.org/site/index.html"><img alt="Apache Felix" src="apache-felix-framework-launching-and-embedding_files/logo.png" border="0"></a></div><div class="header"><a href="http://www.apache.org/"><img alt="Apache" src="apache-felix-framework-launching-and-embedding_files/apache.png" border="0"></a></div></div>
Karl Paulsf7d389c2008-11-06 16:22:54 +000011 <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
69embeddable. For example, Felix avoids the use of system properties for
70configuration, since these are globals and can cause interference if
71multiple framework instances are created in the same VM. Felix also
72tries to multiplex singleton facilities, like the URL stream handler
73factory. The goal is to make it possible to use Felix in as many
74scenarios as possible; however, this is still just a goal. In other
75words, this is a work in progress and if any issues arise, it would be
76greatly appreciated if they are brought to the attention of the Felix
77community. The next section provides a Felix API overview, while the
78remainder of the document is divided into two sections, one focusing on
79how to launch Felix and one focusing on how to embed Felix into a host
80application.</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>
87for short. As part of the ongoing OSGi specification process, there is
88a movement to standardize the API for launching and embedding OSGi
89framework implementations. The approach is to have the framework
90implement 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
131in the interface definitions is that they must implement a public
132constructor that accepts a <tt>Map</tt>, which is used to pass in configuration properties. When you instantiate the <tt>Felix</tt>
133class, the resulting object is the actual System Bundle that bundles
134inside the framework will see if they get bundle 0, which is the System
135Bundle 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
148list of packages the system bundle should export from the environment;
149if 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
152export from the environment that are appended to the packages specified
153in <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
156to all bundles from the environment (i.e., no need to import them);
157there is no default value for this property and its use should be
158avoided.</li>
159 <li><tt>org.osgi.framework.storage</tt> - specifies the
160path to a directory, which will be created if it does not exist, to use
161for 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
164value for this property is "none", but it can be changed to
165"onFirstInit" to flush the bundle cache when the framework is
166initialized.</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>
176instances that are started/stopped when the System Bundle is
177started/stopped; the specified instances will receive the System
178Bundle'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>
181whose value indicates the degree of logging reported by the framework;
182the default value is "1" and "0" turns off logging completely,
183otherwise 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
188framework instance; the default value is "&lt;tt&gt;true&lt;/tt&gt;",
189which results in the
190&lt;tt&gt;URL.setURLStreamHandlerFactory()&lt;/tt&gt; and
191&lt;tt&gt;URLConnection.setContentHandlerFactory()&lt;/tt&gt; being
192called.</li>
193</ul>
194
195
196<p>The configuration map passed into the constructor is copied and the
197keys are treated as case insensitive. You are not able to change the
198framework's configuration after construction. If you need a different
199configuration, you must create a new framework instance.</p>
200
Karl Pauls4994cb02008-12-20 17:15:21 +0000201<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>
202<p>Felix <tt>1.4.0</tt> introduced some significant changes to its configuration properties; if you are upgrading from a previous version, the <a href="http://felix.apache.org/site/apache-felix-usage-documentation.html#ApacheFelixUsageDocumentation-migrating" title="migrating on Apache Felix Usage Documentation">usage document</a> describes the configuration property changes.</p></td></tr></tbody></table>
203
Karl Paulsf7d389c2008-11-06 16:22:54 +0000204<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-startinginstance"></a></p>
205
206<h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-StartingtheFrameworkInstance"></a>Starting the Framework Instance</h2>
207
208<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>
209
210<ul>
211 <li><tt>init()</tt> results in the framework instance in the <tt>Bundle.STARTING</tt> state.</li>
212 <li><tt>start()</tt> results in the framework instance in the <tt>Bundle.ACTIVE</tt> state.</li>
213</ul>
214
215
216<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>
217
218<ul>
219 <li>Event handling is enabled.</li>
220 <li>The security manager is installed if it is enabled.</li>
221 <li>The framework is set to start level 0.</li>
222 <li>All bundles in the bundle caches are reified and their state is set to <tt>Bundle.INSTALLED</tt>.</li>
223 <li>The framework gets a valid <tt>BundleContext</tt>.</li>
224 <li>All framework-provided services are made available (e.g., PackageAdmin, StartLevel, etc.).</li>
225 <li>The framework enters the <tt>Bundle.STARTING</tt> state.</li>
226</ul>
227
228
229<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>
230
231<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-stoppinginstance"></a></p>
232
233<h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-StoppingtheFrameworkInstance"></a>Stopping the Framework Instance</h2>
234
235<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>
236
237<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-launching"></a></p>
238
239<h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-LaunchingFelix"></a>Launching Felix</h1>
240
241<p>Launching Felix is fairly simple and involves only three steps:</p>
242
243<ol>
244 <li>Define some configuration properties.</li>
245 <li>Create an instance of <tt>org.apache.felix.framework.Felix</tt> with the configuration properties.</li>
246 <li>Invoke the <tt>org.apache.felix.framework.Felix.start()</tt> method.</li>
247</ol>
248
249
250<p>In reality, the first step is optional, since all properties will
251have reasonable defaults, but if you are creating a launcher you will
252generally want to more than that, such as automatically installing and
253starting bundles when you start the framework instance. The default
254Felix launcher defines reusable functionality to automatically install
255and/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>
256
257<p>The remainder of this section describes how the standard Felix
258launcher works as well as how to create a custom launcher for Felix.</p>
259
260<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-standardlauncher"></a></p>
261
262<h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-StandardFelixLauncher"></a>Standard Felix Launcher</h2>
263
264<p>The standard Felix launcher is very simple and is not intended to
265solve every possible requirement; it is intended to work for most
266standard situations. Most special launching requirements should be
267resolved by creating a custom launcher. This section describes how the
268standard 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>
269
270<div class="code"><div class="codeContent">
271<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
272{
273 <span class="code-comment">// (1) Check <span class="code-keyword">for</span> proper command line usage.
274</span> <span class="code-keyword">if</span> (args.length &gt; 1)
275 {
276 <span class="code-object">System</span>.out.println(<span class="code-quote">"Usage: [&lt;bundle-cache-dir&gt;]"</span>);
277 <span class="code-object">System</span>.exit(0);
278 }
279
280 <span class="code-comment">// (2) Load system properties.
281</span> Main.loadSystemProperties();
282
283 <span class="code-comment">// (3) Read configuration properties.
284</span> Properties configProps = Main.loadConfigProperties();
285
286 <span class="code-comment">// (4) Copy framework properties from the system properties.
287</span> Main.copySystemProperties(configProps);
288
289 <span class="code-comment">// (5) If specified, use command-line argument as path to bundle cache.
290</span> <span class="code-keyword">if</span> (args.length &gt; 0)
291 {
292 configProps.setProperty(Constants.FRAMEWORK_STORAGE, args[0]);
293 }
294
295 <span class="code-comment">// (6) Create a list <span class="code-keyword">for</span> custom framework activators and
296</span> <span class="code-comment">// add an instance of the auto-activator it <span class="code-keyword">for</span> processing
297</span> <span class="code-comment">// auto-install and auto-start properties. Add <span class="code-keyword">this</span> list
298</span> <span class="code-comment">// to the configuration properties.
299</span> List list = <span class="code-keyword">new</span> ArrayList();
300 list.add(<span class="code-keyword">new</span> AutoActivator(configProps));
301 configProps.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);
302
303 <span class="code-comment">// Print welcome banner.
304</span> <span class="code-object">System</span>.out.println(<span class="code-quote">"\nWelcome to Felix."</span>);
305 <span class="code-object">System</span>.out.println(<span class="code-quote">"=================\n"</span>);
306
307 <span class="code-keyword">try</span>
308 {
309 <span class="code-comment">// (7) Create an instance and start the framework.
310</span>
311 m_felix = <span class="code-keyword">new</span> Felix(configProps);
312 m_felix.start();
313 <span class="code-comment">// (8) Wait <span class="code-keyword">for</span> framework to stop to exit the VM.
314</span> m_felix.waitForStop();
315 <span class="code-object">System</span>.exit(0);
316 }
317 <span class="code-keyword">catch</span> (Exception ex)
318 {
319 <span class="code-object">System</span>.err.println(<span class="code-quote">"Could not create framework: "</span> + ex);
320 ex.printStackTrace();
321 <span class="code-object">System</span>.exit(-1);
322 }
323}</pre>
324</div></div>
325
326<p>The general steps of the standard launcher are quite straightforward:</p>
327
328<ol>
329 <li>The launcher only supports a single, optional command-line
330argument, which is the path to the bundle cache, so check for this and
331issue a usage message it there are more than one arguments.</li>
332 <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>
333system property. This file is not needed to launch Felix and is
334provided merely for convenience when system properties must be
335specified. The file is a standard Java properties file, but it also
336supports property substitution using <tt>${&lt;property-name</tt>} syntax. Property substitution can be nested; only system properties will be used for substitution.</li>
337 <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>
338system property. This file is used to configure the Felix instance
339created by the launcher. The file is a standard Java properties file,
340but it also supports property substitution using "<tt>${&lt;property-name</tt>}"
341syntax. Property substitution can be nested; configuration and system
342properties will be used for substitution with configuration properties
343having precedence.</li>
344 <li>For convenience, any configuration
345properties that are set as system properties will be copied into the
346set of configuration properties to provide an easy way to add to or
347override configuration properties specified in the <tt>config.properties</tt> file.</li>
348 <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>
349 <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>
350 <li>Create the Felix instance passing in the configuration properties, then call <tt>start()</tt>.</li>
351 <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>
352</ol>
353
354
355<p>The framework is not active until the <tt>start()</tt> method is
356called. If no shell bundles are installed and started or if there is
357difficulty locating the shell bundles specified in the auto-start
358property, then it will appear as if the framework is hung, but it is
359actually running without any way to interact with it since the shell
360bundles provide the only means of interaction.</p>
361
362<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-customlauncher"></a></p>
363
364<h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-CustomFelixLauncher"></a>Custom Felix Launcher</h2>
365
366<p>This section creates a bare-bones launcher to demonstrate the
367minimum requirements for creating an interactive launcher for the Felix
368framework. This example uses the standard Felix shell bundles for
369interactivity, but any other bundles could be used instead. For
370example, the shell service and telnet bundles could be used to launch
371Felix and make it remotely accessible.</p>
372
373<p>This example launcher project has the following directory structure:</p>
374
375<div class="preformatted"><div class="preformattedContent">
376<pre>launcher/
377 lib/
378 org.apache.felix.main-1.4.0.jar
379 bundle/
380 org.apache.felix.shell-1.0.2.jar
381 org.apache.felix.shell.tui-1.0.2.jar
382 src/
383 example/
384 Main.java
385</pre>
386</div></div>
387
388<p>The <tt>lib/</tt> directory contains Felix' main JAR file, which
389also contains the OSGi core interfaces. The main JAR file is used so
390that we can reuse the default launcher's auto-install/auto-start
391configuration property handling; if these capabilities are not needed,
392then it would be possible to use the framework JAR file instead of the
393main JAR file. The <tt>bundle/</tt> directory contains the shell
394service and textual shell interface bundles that will be used for
395interacting with the framework instance. Note: If you do not launch
396Felix with interactive bundles, it will appear as if the framework
397instance is hung, but it is actually just sitting there waiting for
398someone 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>
399
400<div class="code"><div class="codeContent">
401<pre class="code-java"><span class="code-keyword">package</span> example;
402
403<span class="code-keyword">import</span> java.util.ArrayList;
404<span class="code-keyword">import</span> java.util.List;
405<span class="code-keyword">import</span> java.util.Map;
406<span class="code-keyword">import</span> java.util.HashMap;
407<span class="code-keyword">import</span> org.osgi.framework.Constants;
408<span class="code-keyword">import</span> org.apache.felix.framework.Felix;
409<span class="code-keyword">import</span> org.apache.felix.framework.util.FelixConstants;
410<span class="code-keyword">import</span> org.apache.felix.main.AutoActivator;
411
412<span class="code-keyword">public</span> class Main
413{
414 <span class="code-keyword">private</span> <span class="code-keyword">static</span> Felix m_felix = <span class="code-keyword">null</span>;
415
416 <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
417 {
418 <span class="code-comment">// Print welcome banner.
419</span> <span class="code-object">System</span>.out.println(<span class="code-quote">"\nWelcome to Felix."</span>);
420 <span class="code-object">System</span>.out.println(<span class="code-quote">"=================\n"</span>);
421
422 Map configMap = <span class="code-keyword">new</span> HashMap();
423 configMap.put(AutoActivator.AUTO_START_PROP + <span class="code-quote">".1"</span>,
424 <span class="code-quote">"file:bundle/org.apache.felix.shell-1.0.2.jar "</span> +
425 <span class="code-quote">"file:bundle/org.apache.felix.shell.tui-1.0.2.jar"</span>);
426 List list = <span class="code-keyword">new</span> ArrayList();
427 list.add(<span class="code-keyword">new</span> AutoActivator(configMap));
428 configMap.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);
429
430 <span class="code-keyword">try</span>
431 {
432 m_felix = <span class="code-keyword">new</span> Felix(configMap);
433 m_felix.start();
434 m_felix.waitForStop();
435 <span class="code-object">System</span>.exit(0);
436 }
437 <span class="code-keyword">catch</span> (Exception ex)
438 {
439 <span class="code-object">System</span>.err.println(<span class="code-quote">"Could not create framework: "</span> + ex);
440 ex.printStackTrace();
441 <span class="code-object">System</span>.exit(-1);
442 }
443 }
444}</pre>
445</div></div>
446
447<p>This launcher has all information hard coded in it, unlike the
448default Felix launcher, which loads configuration properties from files
449and performs variable substitution. This simple launcher provides a
450good starting point if the features of the default launcher are not
451necessary. Since very few configuration properties are specified, the
452default values are used. In the case of the framework bundle cache, it
453will use "<tt>felix-cache</tt>" in the current directory.</p>
454
455<p>By breaking down the above source code into small chunks, it is quite easy to see what is going on.</p>
456
457<div class="code"><div class="codeContent">
458<pre class="code-java">Map configMap = <span class="code-keyword">new</span> HashMap();</pre>
459</div></div>
460
461<p>This simply creates a map to hold configuration properties.</p>
462
463<div class="code"><div class="codeContent">
464<pre class="code-java">configMap.put(AutoActivator.AUTO_START_PROP + <span class="code-quote">".1"</span>,
465 <span class="code-quote">"file:bundle/org.apache.felix.shell-1.0.2.jar "</span> +
466 <span class="code-quote">"file:bundle/org.apache.felix.shell.tui-1.0.2.jar"</span>);</pre>
467</div></div>
468
469<p>This sets the <tt>AutoActivator.AUTO_START_PROP</tt> configuration property (string value "<tt>felix.auto.start</tt>"),
470which is a space-delimited list of bundle URLs that the framework will
471automatically install and start when the framework starts. However,
472this property key cannot be used as is; it must be appended with a "."
473and then a number, where the number represents the start level for the
474bundle when it is installed. In this particular example, ".1" is
475appended to the property name, thus the two bundles will be installed
476into start level one. This example uses relative <tt>file:</tt> URLs, which will load the bundles from the <tt>bundle/</tt>
477directory assuming that the launcher is started from the root directory
478of the launcher project. It is also possible to specify absolute URLs
479or remote URLs.</p>
480
481<div class="code"><div class="codeContent">
482<pre class="code-java">List list = <span class="code-keyword">new</span> ArrayList();
483 list.add(<span class="code-keyword">new</span> AutoActivator(configMap));
484 configMap.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);</pre>
485</div></div>
486
487<p>This above creates a list to hold custom framework activators and adds an instance of <tt>org.apache.felix.main.AutoActivator</tt>
488to it, which will process the auto-install and auto-start configuration
489properties during framework startup. The list of activators is then
490added to the configuration map.</p>
491
492<div class="code"><div class="codeContent">
493<pre class="code-java">m_felix = <span class="code-keyword">new</span> Felix(configMap);
494 m_felix.start();</pre>
495</div></div>
496
497<p>These steps create the framework instance and start it. The configuration property map is passed into the <tt>Felix</tt> constructor.</p>
498
499<div class="code"><div class="codeContent">
500<pre class="code-java">m_felix.waitForStop();
501 <span class="code-object">System</span>.exit(0);</pre>
502</div></div>
503
504<p>These final steps cause the launching application thread to wait for
505the framework to stop and when it does the launching thread calls <tt>System.exit()</tt> to make sure the VM actually exits.</p>
506
507<p>The following command compiles the launcher when run from the root directory of the launcher project:</p>
508
509<div class="preformatted"><div class="preformattedContent">
510<pre>javac -d . -classpath lib/org.apache.felix.main-1.4.0.jar src/example/Main.java
511</pre>
512</div></div>
513
514<p>After executing this command, an <tt>example/</tt> directory is
515created in the current directory, which contains the generated class
516file. The following command executes the simple launcher when run from
517the root directory of the launcher project:</p>
518
519<div class="preformatted"><div class="preformattedContent">
520<pre>java -cp .:lib/org.apache.felix.main-1.4.0.jar example.Main
521</pre>
522</div></div>
523
524<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>
525
526<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-embedding"></a></p>
527
528<h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-EmbeddingFelix"></a>Embedding Felix</h1>
529
530<p>Embedding Felix into a host application is a simple way to provide a
531sophisticated extensibility mechanism (i.e., a plugin system) to the
532host application. Embedding Felix is very similar to launching Felix as
533described above, the main difference is that the host application
534typically wants to interact with the framework instance and/or
535installed bundles/services from the outside. This is fairly easy to
536achieve with Felix, but there are some subtle issues to understand.
537This section presents the mechanisms for embedding Felix into a host
538application and the issues in doing so.</p>
539
540<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-hostinteraction"></a></p>
541
542<h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-Host/FelixInteraction"></a>Host/Felix Interaction</h2>
543
544<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>,
545which is a list of bundle activator instances. These bundle activator
546instances provide a convenient way for host applications to interact
547with the Felix framework. The ability offered by these activators can
548also 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>
549
550<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>
551
552<div class="code"><div class="codeContent">
553<pre class="code-java"><span class="code-keyword">public</span> class HostActivator <span class="code-keyword">implements</span> BundleActivator
554{
555 <span class="code-keyword">private</span> BundleContext m_context = <span class="code-keyword">null</span>;
556
557 <span class="code-keyword">public</span> void start(BundleContext context)
558 {
559 m_context = context;
560 }
561
562 <span class="code-keyword">public</span> void stop(BundleContext context)
563 {
564 m_context = <span class="code-keyword">null</span>;
565 }
566
567 <span class="code-keyword">public</span> Bundle[] getBundles()
568 {
569 <span class="code-keyword">if</span> (m_context != <span class="code-keyword">null</span>)
570 {
571 <span class="code-keyword">return</span> m_context.getBundles();
572 }
573 <span class="code-keyword">return</span> <span class="code-keyword">null</span>;
574 }
575}</pre>
576</div></div>
577
578<p>Given the above bundle activator, it is now possible to embed Felix
579into a host application and interact with it as the following snippet
580illustrates:</p>
581
582<div class="code"><div class="codeContent">
583<pre class="code-java"><span class="code-keyword">public</span> class HostApplication
584{
585 <span class="code-keyword">private</span> HostActivator m_activator = <span class="code-keyword">null</span>;
586 <span class="code-keyword">private</span> Felix m_felix = <span class="code-keyword">null</span>;
587
588 <span class="code-keyword">public</span> HostApplication()
589 {
590 <span class="code-comment">// Create a configuration property map.
591</span> Map configMap = <span class="code-keyword">new</span> HashMap();
592 <span class="code-comment">// Create host activator;
593</span> m_activator = <span class="code-keyword">new</span> HostActivator();
594 List list = <span class="code-keyword">new</span> ArrayList();
595 list.add(m_activator);
596 configMap.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);
597
598 <span class="code-keyword">try</span>
599 {
600 <span class="code-comment">// Now create an instance of the framework with
601</span> <span class="code-comment">// our configuration properties.
602</span> m_felix = <span class="code-keyword">new</span> Felix(configMap);
603 <span class="code-comment">// Now start Felix instance.
604</span> m_felix.start();
605 }
606 <span class="code-keyword">catch</span> (Exception ex)
607 {
608 <span class="code-object">System</span>.err.println(<span class="code-quote">"Could not create framework: "</span> + ex);
609 ex.printStackTrace();
610 }
611 }
612
613 <span class="code-keyword">public</span> Bundle[] getInstalledBundles()
614 {
615 <span class="code-comment">// Use the system bundle activator to gain external
616</span> <span class="code-comment">// access to the set of installed bundles.
617</span> <span class="code-keyword">return</span> m_activator.getBundles();
618 }
619
620 <span class="code-keyword">public</span> void shutdownApplication()
621 {
622 <span class="code-comment">// Shut down the felix framework when stopping the
623</span> <span class="code-comment">// host application.
624</span> m_felix.stop();
625 m_felix.waitForStop();
626 }
627}</pre>
628</div></div>
629
630<p>Notice how the <tt>HostApplication.getInstalledBundles()</tt> method
631uses its activator instance to get access to the System Bundle's
632context in order to interact with the embedded Felix framework
633instance. This approach provides the foundation for all interaction
634between the host application and the embedded framework instance.</p>
635
636<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-hostservices"></a></p>
637
638<h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-ProvidingHostApplicationServices"></a>Providing Host Application Services</h2>
639
640<p>Providing services from the host application to bundles inside the
641embedded Felix framework instance follows the basic approach laid out
642in <a href="#ApacheFelixFrameworkLaunchingandEmbedding-hostinteraction" title="host-interaction on Apache Felix Framework Launching and Embedding">above</a>.
643The main complication for providing a host application service to
644bundles is the fact that both the host application and the bundles must
645be using the same class definitions for the service interface classes.
646Since the host application cannot import classes from a bundle, this
647means that the service interface classes <b>must</b> be accessible on
648the class path, typically as part of the host application itself. The
649host application then must export the service interface package via the
650system bundle so that bundles installed into the embedded framework
651instance can import it. This is achieved using the <tt>org.osgi.framework.system.packages.extra</tt> configuration property previously presented.</p>
652
653<p>Consider the follow simple property lookup service:</p>
654
655<div class="code"><div class="codeContent">
656<pre class="code-java"><span class="code-keyword">package</span> host.service.lookup;
657
658<span class="code-keyword">public</span> class Lookup
659{
660 <span class="code-keyword">public</span> <span class="code-object">Object</span> lookup(<span class="code-object">String</span> name);
661}</pre>
662</div></div>
663
664<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>"
665command. Now consider the following host application bundle activator,
666which will be used to register/unregister the property lookup service
667when the embedded framework instance starts/stops:</p>
668
669<div class="code"><div class="codeContent">
670<pre class="code-java"><span class="code-keyword">package</span> host.core;
671
672<span class="code-keyword">import</span> java.util.Map;
673<span class="code-keyword">import</span> org.osgi.framework.BundleActivator;
674<span class="code-keyword">import</span> org.osgi.framework.BundleContext;
675<span class="code-keyword">import</span> org.osgi.framework.ServiceRegistration;
676<span class="code-keyword">import</span> host.service.lookup;
677
678<span class="code-keyword">public</span> class HostActivator <span class="code-keyword">implements</span> BundleActivator
679{
680 <span class="code-keyword">private</span> Map m_lookupMap = <span class="code-keyword">null</span>;
681 <span class="code-keyword">private</span> BundleContext m_context = <span class="code-keyword">null</span>;
682 <span class="code-keyword">private</span> ServiceRegistration m_registration = <span class="code-keyword">null</span>;
683
684 <span class="code-keyword">public</span> HostActivator(Map lookupMap)
685 {
686 <span class="code-comment">// Save a reference to the service's backing store.
687</span> m_lookupMap = lookupMap;
688 }
689
690 <span class="code-keyword">public</span> void start(BundleContext context)
691 {
692 <span class="code-comment">// Save a reference to the bundle context.
693</span> m_context = context;
694 <span class="code-comment">// Create a property lookup service implementation.
695</span> Lookup lookup = <span class="code-keyword">new</span> Lookup() {
696 <span class="code-keyword">public</span> <span class="code-object">Object</span> lookup(<span class="code-object">String</span> name)
697 {
698 <span class="code-keyword">return</span> m_lookupMap.get(name);
699 }
700 };
701 <span class="code-comment">// Register the property lookup service and save
702</span> <span class="code-comment">// the service registration.
703</span> m_registration = m_context.registerService(
704 Lookup.class.getName(), lookup, <span class="code-keyword">null</span>);
705 }
706
707 <span class="code-keyword">public</span> void stop(BundleContext context)
708 {
709 <span class="code-comment">// Unregister the property lookup service.
710</span> m_registration.unregister();
711 m_context = <span class="code-keyword">null</span>;
712 }
713}</pre>
714</div></div>
715
716<p>Given the above host application bundle activator, the following
717code snippet shows how the host application could create an embedded
718version of the Felix framework and provide the property lookup service
719to installed bundles:</p>
720
721<div class="code"><div class="codeContent">
722<pre class="code-java"><span class="code-keyword">package</span> host.core;
723
724<span class="code-keyword">import</span> java.util.List;
725<span class="code-keyword">import</span> java.util.ArrayList;
726<span class="code-keyword">import</span> java.util.Map;
727<span class="code-keyword">import</span> java.util.HashMap;
728<span class="code-keyword">import</span> host.service.lookup.Lookup;
729<span class="code-keyword">import</span> org.apache.felix.framework.Felix;
730<span class="code-keyword">import</span> org.apache.felix.framework.util.FelixConstants;
731<span class="code-keyword">import</span> org.osgi.framework.Constants;
732
733<span class="code-keyword">public</span> class HostApplication
734{
735 <span class="code-keyword">private</span> HostActivator m_activator = <span class="code-keyword">null</span>;
736 <span class="code-keyword">private</span> Felix m_felix = <span class="code-keyword">null</span>;
737 <span class="code-keyword">private</span> Map m_lookupMap = <span class="code-keyword">new</span> HashMap();
738
739 <span class="code-keyword">public</span> HostApplication()
740 {
741 <span class="code-comment">// Initialize the map <span class="code-keyword">for</span> the property lookup service.
742</span> m_lookupMap.put(<span class="code-quote">"name1"</span>, <span class="code-quote">"value1"</span>);
743
744 m_lookupMap.put(<span class="code-quote">"name2"</span>, <span class="code-quote">"value2"</span>);
745 m_lookupMap.put(<span class="code-quote">"name3"</span>, <span class="code-quote">"value3"</span>);
746 m_lookupMap.put(<span class="code-quote">"name4"</span>, <span class="code-quote">"value4"</span>);
747
748 <span class="code-comment">// Create a configuration property map.
749</span> Map configMap = <span class="code-keyword">new</span> HashMap();
750 <span class="code-comment">// Export the host provided service <span class="code-keyword">interface</span> <span class="code-keyword">package</span>.
751</span> configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA,
752 <span class="code-quote">"host.service.lookup; version=1.0.0"</span>);
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 configMap.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);
758
759 <span class="code-keyword">try</span>
760 {
761 <span class="code-comment">// Now create an instance of the framework with
762</span> <span class="code-comment">// our configuration properties.
763</span> m_felix = <span class="code-keyword">new</span> Felix(configMap);
764 <span class="code-comment">// Now start Felix instance.
765</span> m_felix.start();
766 }
767 <span class="code-keyword">catch</span> (Exception ex)
768 {
769 <span class="code-object">System</span>.err.println(<span class="code-quote">"Could not create framework: "</span> + ex);
770 ex.printStackTrace();
771 }
772 }
773
774 <span class="code-keyword">public</span> void shutdownApplication()
775 {
776 <span class="code-comment">// Shut down the felix framework when stopping the
777</span> <span class="code-comment">// host application.
778</span> m_felix.stop();
779 m_felix.waitForStop();
780 }
781}</pre>
782</div></div>
783
784<p>Rather than having the host application bundle activator register
785the service, it is also possible for the the host application to simply
786get the bundle context from the bundle activator and register the
787service directly, but the presented approach is perhaps a little
788cleaner since it allows the host application to register/unregister the
789service when the system bundle starts/stops.</p>
790
791<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-hostserviceusage"></a></p>
792
793<h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-UsingServicesProvidedbyBundles"></a>Using Services Provided by Bundles</h2>
794
795<p>Using services provided by bundles follows the same general approach
796of using a host application bundle activator. The main complication for
797the host application using a service from a bundle is the fact that
798both the host application and the bundle must be using the same class
799definitions for the service interface classes. Since the host
800application cannot import classes from a bundle, this means that the
801service interface classes <b>must</b> be accessible on the class path,
802typically as part of the host application itself. The host application
803then must export the service interface package via the system bundle so
804that bundles installed into the embedded framework instance can import
805it. This is achieved using the <tt>org.osgi.framework.system.packages.extra</tt> configuration property previously presented.</p>
806
807<p>Consider the following simple command service interface for which
808bundles provide implementations, such as might be used to create an
809extensible interactive shell:</p>
810
811<div class="code"><div class="codeContent">
812<pre class="code-java"><span class="code-keyword">package</span> host.service.command;
813
814<span class="code-keyword">public</span> class Command
815{
816 <span class="code-keyword">public</span> <span class="code-object">String</span> getName();
817 <span class="code-keyword">public</span> <span class="code-object">String</span> getDescription();
818 <span class="code-keyword">public</span> <span class="code-object">boolean</span> execute(<span class="code-object">String</span> commandline);
819}</pre>
820</div></div>
821
822<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>"
823command. Now consider the previously introduced host application bundle
824activator below, which simply provides access to the system bundle
825context:</p>
826
827<div class="code"><div class="codeContent">
828<pre class="code-java"><span class="code-keyword">package</span> host.core;
829
830<span class="code-keyword">import</span> org.osgi.framework.BundleActivator;
831<span class="code-keyword">import</span> org.osgi.framework.BundleContext;
832
833<span class="code-keyword">public</span> class HostActivator <span class="code-keyword">implements</span> BundleActivator
834{
835 <span class="code-keyword">private</span> BundleContext m_context = <span class="code-keyword">null</span>;
836
837 <span class="code-keyword">public</span> void start(BundleContext context)
838 {
839 m_context = context;
840 }
841
842 <span class="code-keyword">public</span> void stop(BundleContext context)
843 {
844 m_context = <span class="code-keyword">null</span>;
845 }
846
847 <span class="code-keyword">public</span> BundleContext getContext()
848 {
849 <span class="code-keyword">return</span> m_context;
850 }
851}</pre>
852</div></div>
853
854<p>With this bundle activator, the host application can use command
855services provided by bundles installed inside its embedded Felix
856framework instance. The following code snippet illustrates one possible
857approach:</p>
858
859<div class="code"><div class="codeContent">
860<pre class="code-java"><span class="code-keyword">package</span> host.core;
861
862<span class="code-keyword">import</span> java.util.List;
863<span class="code-keyword">import</span> java.util.ArrayList;
864<span class="code-keyword">import</span> java.util.Map;
865<span class="code-keyword">import</span> host.service.command.Command;
866<span class="code-keyword">import</span> org.apache.felix.framework.Felix;
867<span class="code-keyword">import</span> org.apache.felix.framework.util.FelixConstants;
868<span class="code-keyword">import</span> org.apache.felix.framework.cache.BundleCache;
869<span class="code-keyword">import</span> org.osgi.framework.Constants;
870<span class="code-keyword">import</span> org.osgi.util.tracker.ServiceTracker;
871
872<span class="code-keyword">public</span> class HostApplication
873{
874 <span class="code-keyword">private</span> HostActivator m_activator = <span class="code-keyword">null</span>;
875 <span class="code-keyword">private</span> Felix m_felix = <span class="code-keyword">null</span>;
876 <span class="code-keyword">private</span> ServiceTracker m_tracker = <span class="code-keyword">null</span>;
877
878 <span class="code-keyword">public</span> HostApplication()
879 {
880 <span class="code-comment">// Create a configuration property map.
881</span> Map configMap = <span class="code-keyword">new</span> HashMap();
882 <span class="code-comment">// Export the host provided service <span class="code-keyword">interface</span> <span class="code-keyword">package</span>.
883</span> configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA,
884 <span class="code-quote">"host.service.command; version=1.0.0"</span>);
885 <span class="code-comment">// Create host activator;
886</span> m_activator = <span class="code-keyword">new</span> HostActivator();
887 List list = <span class="code-keyword">new</span> ArrayList();
888 list.add(m_activator);
889 configMap.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);
890
891 <span class="code-keyword">try</span>
892 {
893 <span class="code-comment">// Now create an instance of the framework with
894</span> <span class="code-comment">// our configuration properties.
895</span> m_felix = <span class="code-keyword">new</span> Felix(configMap);
896 <span class="code-comment">// Now start Felix instance.
897</span> m_felix.start();
898 }
899 <span class="code-keyword">catch</span> (Exception ex)
900 {
901 <span class="code-object">System</span>.err.println(<span class="code-quote">"Could not create framework: "</span> + ex);
902 ex.printStackTrace();
903 }
904
905 m_tracker = <span class="code-keyword">new</span> ServiceTracker(
906 m_activator.getContext(), Command.class.getName(), <span class="code-keyword">null</span>);
907 m_tracker.open();
908 }
909
910 <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)
911 {
912 <span class="code-comment">// See <span class="code-keyword">if</span> any of the currently tracked command services
913</span> <span class="code-comment">// match the specified command name, <span class="code-keyword">if</span> so then execute it.
914</span> <span class="code-object">Object</span>[] services = m_tracker.getServices();
915 <span class="code-keyword">for</span> (<span class="code-object">int</span> i = 0; (services != <span class="code-keyword">null</span>) &amp;&amp; (i &lt; services.length); i++)
916 {
917 <span class="code-keyword">try</span>
918 {
919 <span class="code-keyword">if</span> (((Command) services[i]).getName().equals(name))
920 {
921 <span class="code-keyword">return</span> ((Command) services[i]).execute(commandline);
922 }
923 }
924 <span class="code-keyword">catch</span> (Exception ex)
925 {
926 <span class="code-comment">// Since the services returned by the tracker could become
927</span> <span class="code-comment">// invalid at any moment, we will <span class="code-keyword">catch</span> all exceptions, log
928</span> <span class="code-comment">// a message, and then ignore faulty services.
929</span> <span class="code-object">System</span>.err.println(ex);
930 }
931 }
932 <span class="code-keyword">return</span> <span class="code-keyword">false</span>;
933 }
934
935 <span class="code-keyword">public</span> void shutdownApplication()
936 {
937 {
938 <span class="code-comment">// Shut down the felix framework when stopping the
939</span> <span class="code-comment">// host application.
940</span> m_felix.stop();
941 m_felix.waitForStop();
942 }
943}</pre>
944</div></div>
945
946<p>The above example is overly simplistic with respect to concurrency
947issues and error conditions, but it demonstrates the overall approach
948for using bundle-provided services from the host application.</p>
949
950<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-servicereflection"></a></p>
951
952<h3><a name="ApacheFelixFrameworkLaunchingandEmbedding-UsingBundleServicesviaReflection"></a>Using Bundle Services via Reflection</h3>
953
954<p>It possible for the host application to use services provided by
955bundles without having access to the service interface classes and thus
956not needing to put the service interface classes on the class path. To
957do this, the host application uses the same general approach to acquire
958the system bundle context object, which it can use to look up service
959objects. Using either an LDAP filter or the service interface class
960name, the host application can retrieve the service object and then use
961standard Java reflection to invoke methods on the service object.</p>
962
963<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-serviceother"></a></p>
964
965<h3><a name="ApacheFelixFrameworkLaunchingandEmbedding-OtherApproaches"></a>Other Approaches</h3>
966
967<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>
968
969<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-caveat"></a></p>
970
971<h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-Caveat"></a>Caveat</h1>
972
973<p>The code in this document has not been thoroughly tested nor even
974compiled and may be out of date with respect to the current Felix
975source code. If you find errors please report them so the that they can
976be corrected.</p>
977
978<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-feedback"></a></p>
979
980<h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-Feedback"></a>Feedback</h2>
981
Karl Pauls4994cb02008-12-20 17:15:21 +0000982<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_files/mail_small.gif" alt="" align="absmiddle" border="0" height="12" width="13"></sup></a></span>.</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000983 </div>
Karl Pauls4994cb02008-12-20 17:15:21 +0000984 </body></html>