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