blob: 533f42c520c152513061ca956691f660de47b791 [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>
Karl Pauls34ab8272010-06-06 21:44:50 +000015 <li><a href="http://felix.apache.org/site/downloads.cgi" class="external-link" rel="nofollow">downloads</a></li>
Karl Paulsf7d389c2008-11-06 16:22:54 +000016 <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>
Karl Pauls34ab8272010-06-06 21:44:50 +000019 <li><a href="http://www.apache.org/" class="external-link" rel="nofollow">asf</a></li>
20 <li><a href="http://www.apache.org/foundation/sponsorship.html" class="external-link" rel="nofollow">sponsorship</a></li>
21 <li><a href="http://www.apache.org/foundation/thanks.html" class="external-link" rel="nofollow">sponsors</a>
Karl Paulsf7d389c2008-11-06 16:22:54 +000022<!-- 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 -->
Karl Pauls34ab8272010-06-06 21:44:50 +000026</p></li></ul>
27 </div>
Karl Paulsf7d389c2008-11-06 16:22:54 +000028 <div class="main">
29<h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-ApacheFelixFrameworkLaunchingandEmbedding"></a>Apache Felix Framework Launching and Embedding</h1>
30
Karl Pauls93f7ea22009-10-11 20:18:06 +000031<p><em>[This document describes framework launching introduced in Felix
Karl Pauls34ab8272010-06-06 21:44:50 +000032Framework 2.0.0 and continuing with the latest releases; it is
33incompatible with older versions of the Felix framework.]</em></p>
Karl Paulsf7d389c2008-11-06 16:22:54 +000034
35<ul>
Karl Pauls44c941b2009-09-06 22:21:18 +000036 <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-introduction">Introduction</a></li>
Karl Pauls34ab8272010-06-06 21:44:50 +000037 <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-overview">OSGi Launching and Embedding API Overview</a>
Karl Paulsf7d389c2008-11-06 16:22:54 +000038 <ul>
Karl Pauls44c941b2009-09-06 22:21:18 +000039 <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-creatingandconfiguring">Creating and Configuring the Framework Instance</a></li>
40 <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-startinginstance">Starting the Framework Instance</a></li>
41 <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-stoppinginstance">Stopping the Framework Instance</a></li>
Karl Paulsf7d389c2008-11-06 16:22:54 +000042 </ul>
43 </li>
Karl Pauls44c941b2009-09-06 22:21:18 +000044 <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-launching">Launching Felix</a>
Karl Paulsf7d389c2008-11-06 16:22:54 +000045 <ul>
Karl Pauls44c941b2009-09-06 22:21:18 +000046 <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-standardlauncher">Standard Felix Launcher</a></li>
47 <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-customlauncher">Custom Felix Launcher</a></li>
Karl Paulsf7d389c2008-11-06 16:22:54 +000048 </ul>
49 </li>
Karl Pauls44c941b2009-09-06 22:21:18 +000050 <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-embedding">Embedding Felix</a>
Karl Paulsf7d389c2008-11-06 16:22:54 +000051 <ul>
Karl Pauls44c941b2009-09-06 22:21:18 +000052 <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-hostinteraction">Host/Felix Interaction</a></li>
53 <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-hostservices">Providing Host Application Services</a></li>
54 <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-hostserviceusage">Using Services Provided by Bundles</a>
Karl Paulsf7d389c2008-11-06 16:22:54 +000055 <ul>
Karl Pauls44c941b2009-09-06 22:21:18 +000056 <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-servicereflection">Using Bundle Services via Reflection</a></li>
57 <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-serviceother">Other Approaches</a></li>
Karl Paulsf7d389c2008-11-06 16:22:54 +000058 </ul>
59 </li>
60 </ul>
61 </li>
Karl Pauls44c941b2009-09-06 22:21:18 +000062 <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-caveat">Caveat</a></li>
63 <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-feedback">Feedback</a></li>
Karl Paulsf7d389c2008-11-06 16:22:54 +000064</ul>
65
66
67<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-introduction"></a></p>
68
69<h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-Introduction"></a>Introduction</h1>
70
Karl Pauls34ab8272010-06-06 21:44:50 +000071<p>The Apache Felix Framework is intended to be easily launchable and
72embeddable. For example, the Felix framework implementation avoids the
73use of system properties for configuration, since these are globals and
74can cause interference if multiple framework instances are created in
75the same VM. The framework also tries to multiplex singleton
76facilities, like the URL stream handler factory. The goal is to make it
77possible to use the framework in a variety of scenarios; however, this
78is still just a goal. In other words, this is a work in progress and if
79any issues arise, it would be greatly appreciated if they are brought
80to the attention of the Felix community. The next section provides an
81overview of the standard OSGi launching and embedding API for
82frameworks, while the remainder of the document is divided into two
83sections, one focusing on how to launch Felix and one focusing on how
84to embed Felix into a host application.</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +000085
86<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-overview"></a></p>
87
Karl Pauls34ab8272010-06-06 21:44:50 +000088<h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-OSGiLaunchingandEmbeddingAPIOverview"></a>OSGi Launching and Embedding API Overview</h1>
Karl Paulsf7d389c2008-11-06 16:22:54 +000089
Karl Pauls93f7ea22009-10-11 20:18:06 +000090<p>The Felix framework is implemented by the <tt>org.apache.felix.framework.Felix</tt> class or just <tt>Felix</tt>
91for short. As part of the R4.2 OSGi specification, the launching and
92embedding API of the OSGi framework has been standardized. The approach
93is to have the framework implement the <tt>org.osgi.framework.launch.Framework</tt> interface, which extends the <tt>org.osgi.framework.Bundle</tt> interface. These interfaces provide the necessary means to launch and manage framework instances. The <tt>Bundle</tt> interface is defined as:</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +000094
Karl Pauls44c941b2009-09-06 22:21:18 +000095<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
Karl Pauls93f7ea22009-10-11 20:18:06 +000096<pre class="code-java"><span class="code-keyword">public</span> <span class="code-keyword">interface</span> Bundle
Karl Paulsf7d389c2008-11-06 16:22:54 +000097{
98 BundleContext getBundleContext();
99 <span class="code-object">long</span> getBundleId();
100 URL getEntry(<span class="code-object">String</span> name);
101 Enumeration getEntryPaths(<span class="code-object">String</span> path);
102 Enumeration findEntries(<span class="code-object">String</span> path, <span class="code-object">String</span> filePattern, <span class="code-object">boolean</span> recurse);
103 Dictionary getHeaders();
104 Dictionary getHeaders(<span class="code-object">String</span> locale);
105 <span class="code-object">long</span> getLastModified();
106 <span class="code-object">String</span> getLocation();
107 URL getResource(<span class="code-object">String</span> name);
108 Enumeration getResources(<span class="code-object">String</span> name) <span class="code-keyword">throws</span> IOException;
109 ServiceReference[] getRegisteredServices();
110 ServiceReference[] getServicesInUse();
111 <span class="code-object">int</span> getState();
112 <span class="code-object">String</span> getSymbolicName();
Karl Pauls44c941b2009-09-06 22:21:18 +0000113 Version getVersion();
Karl Paulsf7d389c2008-11-06 16:22:54 +0000114 <span class="code-object">boolean</span> hasPermission(<span class="code-object">Object</span> obj);
115 <span class="code-object">Class</span> loadClass(<span class="code-object">String</span> name) <span class="code-keyword">throws</span> ClassNotFoundException;
116 void start() <span class="code-keyword">throws</span> BundleException;
117 void stop() <span class="code-keyword">throws</span> BundleException;
118 void uninstall() <span class="code-keyword">throws</span> BundleException;
119 void update() <span class="code-keyword">throws</span> BundleException;
120 void update(InputStream is) <span class="code-keyword">throws</span> BundleException;
Karl Pauls44c941b2009-09-06 22:21:18 +0000121}
Karl Pauls44c941b2009-09-06 22:21:18 +0000122</pre>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000123</div></div>
124
125<p>The <tt>Framework</tt> interface is defined as:</p>
126
Karl Pauls44c941b2009-09-06 22:21:18 +0000127<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
Karl Pauls93f7ea22009-10-11 20:18:06 +0000128<pre class="code-java"><span class="code-keyword">public</span> <span class="code-keyword">interface</span> Framework <span class="code-keyword">extends</span> Bundle
Karl Paulsf7d389c2008-11-06 16:22:54 +0000129{
130 void init();
Karl Pauls34ab8272010-06-06 21:44:50 +0000131 FrameworkEvent waitForStop(<span class="code-object">long</span> timeout);
Karl Pauls44c941b2009-09-06 22:21:18 +0000132}
Karl Pauls44c941b2009-09-06 22:21:18 +0000133</pre>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000134</div></div>
135
Karl Pauls34ab8272010-06-06 21:44:50 +0000136<p>To actually construct a framework instance, the R4.2 specification defines the <tt>FrameworkFactory</tt> interface:</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000137
Karl Pauls44c941b2009-09-06 22:21:18 +0000138<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
Karl Pauls93f7ea22009-10-11 20:18:06 +0000139<pre class="code-java"><span class="code-keyword">public</span> <span class="code-keyword">interface</span> FrameworkFactory
Karl Pauls44c941b2009-09-06 22:21:18 +0000140{
Karl Pauls34ab8272010-06-06 21:44:50 +0000141 Framework newFramework(Map config);
Karl Pauls44c941b2009-09-06 22:21:18 +0000142}
143</pre>
144</div></div>
145
146<p>The framework factory can be used to create configured framework instances. It is obtained following the standard <tt>META-INF/services</tt> approach.</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000147
148<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-creatingandconfiguring"></a></p>
149
150<h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-CreatingandConfiguringtheFrameworkInstance"></a>Creating and Configuring the Framework Instance</h2>
151
Karl Pauls93f7ea22009-10-11 20:18:06 +0000152<p>You use the framework factory to construct and configure a framework
153instance (or by directly instantiating the Felix class). The
154configuration map may contain the following OSGi standard properties:</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000155
156<ul>
Karl Pauls93f7ea22009-10-11 20:18:06 +0000157 <li><tt>org.osgi.framework.system.packages</tt> - specifies a
158list of packages the system bundle should export from the environment;
159if this is not set, then the framework uses a reasonable default fault.</li>
160 <li><tt>org.osgi.framework.system.packages.extra</tt>
161- specifies a list of additional packages the system bundle should
162export from the environment that are appended to the packages specified
163in <tt>org.osgi.framework.system.packages</tt>; there is no default value for this property.</li>
164 <li><tt>org.osgi.framework.bootdelegation</tt>
165- specifies a list of packages that should be made implicitly available
166to all bundles from the environment (i.e., no need to import them);
167there is no default value for this property and its use should be
168avoided.</li>
Karl Pauls34ab8272010-06-06 21:44:50 +0000169 <li><tt>org.osgi.framework.bundle.parent</tt> - Specifies which class loader is used for boot delegation. Possible values are: <tt>boot</tt> for the boot class loader, <tt>app</tt> for the application class loader, <tt>ext</tt> for the extension class loader, and <tt>framework</tt> for the framework's class loader. The default is <tt>boot</tt>.</li>
170 <li><tt>org.osgi.framework.storage</tt>
171- specifies the path to a directory, which will be created if it does
172not exist, to use for bundle cache storage; the default value for this
173property is "<tt>felix-cache</tt>" in the current working directory.</li>
174 <li><tt>org.osgi.framework.storage.clean</tt> - specifies whether the bundle cache should be flushed; the default value for this property is "<tt>none</tt>", but it can be changed to "<tt>onFirstInit</tt>" to flush the bundle cache when the framework is initialized.</li>
Karl Pauls44c941b2009-09-06 22:21:18 +0000175 <li><tt>org.osgi.framework.startlevel.beginning</tt> - specifies the start level the framework enters upon startup; the default value for this property is 1.</li>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000176</ul>
177
178
179<p>Felix also has the following, non-standard configuration properties:</p>
180
181<ul>
182 <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>
Karl Pauls93f7ea22009-10-11 20:18:06 +0000183 <li><tt>felix.systembundle.activators</tt> - specifies a <tt>List</tt> of <tt>BundleActivator</tt>
184instances that are started/stopped when the System Bundle is
185started/stopped; the specified instances will receive the System
186Bundle's <tt>BundleContext</tt> when invoked.</li>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000187 <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>
Karl Pauls93f7ea22009-10-11 20:18:06 +0000188 <li><tt>felix.log.level</tt> - specifies an integer <tt>String</tt>
189whose value indicates the degree of logging reported by the framework;
190the default value is "1" and "0" turns off logging completely,
191otherwise log levels match those specified in the OSGi Log Service
192(i.e., 1 = error, 2 = warning, 3 = information, and 4 = debug).</li>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000193 <li><tt>felix.startlevel.bundle</tt> - specifies the start level for newly installed bundles; the default value is 1.</li>
Karl Pauls93f7ea22009-10-11 20:18:06 +0000194 <li><tt>felix.bootdelegation.implicit</tt>
195- specifies whether or not the framework should try to guess when to
196boot delegate when external code tries to load classes or resources;
197the default value is "<tt>true</tt>".</li>
Karl Pauls44c941b2009-09-06 22:21:18 +0000198 <li><tt>framework.service.urlhandlers</tt> - specifies whether or not to activate the URL Handlers service for the framework instance; the default value is "<tt>true</tt>", which results in the <tt>URL.setURLStreamHandlerFactory()</tt> and <tt>URLConnection.setContentHandlerFactory()</tt> being called.</li>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000199</ul>
200
201
Karl Pauls93f7ea22009-10-11 20:18:06 +0000202<p>The configuration map is copied and the keys are treated as case
203insensitive. You are not able to change the framework's configuration
204after construction. If you need a different configuration, you must
205create a new framework instance.</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000206
Karl Pauls34ab8272010-06-06 21:44:50 +0000207<div class="panelMacro"><table class="warningMacro"><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>WARNING</b><br>Felix configuration properties have change considerably starting from <tt>1.4.0</tt>; if you are upgrading from an earlier version, the <a href="http://felix.apache.org/site/apache-felix-framework-usage-documentation.html#ApacheFelixFrameworkUsageDocumentation-migrating">usage document</a> describes the configuration property changes.</td></tr></tbody></table></div>
Karl Pauls4994cb02008-12-20 17:15:21 +0000208
Karl Paulsf7d389c2008-11-06 16:22:54 +0000209<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-startinginstance"></a></p>
210
211<h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-StartingtheFrameworkInstance"></a>Starting the Framework Instance</h2>
212
Karl Pauls44c941b2009-09-06 22:21:18 +0000213<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 invoked by <tt>start()</tt>. The two methods result in two different framework state transitions:</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000214
215<ul>
216 <li><tt>init()</tt> results in the framework instance in the <tt>Bundle.STARTING</tt> state.</li>
217 <li><tt>start()</tt> results in the framework instance in the <tt>Bundle.ACTIVE</tt> state.</li>
218</ul>
219
220
Karl Pauls34ab8272010-06-06 21:44:50 +0000221<p>The <tt>init()</tt> method is necessary since the framework does not have a <tt>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 the Felix framework also provides the <tt>felix.systembundle.activators</tt> property that serves a similar purpose, but is not standard. After the <tt>init()</tt> method completes, the follow actions have been performed:</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000222
223<ul>
224 <li>Event handling is enabled.</li>
225 <li>The security manager is installed if it is enabled.</li>
226 <li>The framework is set to start level 0.</li>
227 <li>All bundles in the bundle caches are reified and their state is set to <tt>Bundle.INSTALLED</tt>.</li>
228 <li>The framework gets a valid <tt>BundleContext</tt>.</li>
229 <li>All framework-provided services are made available (e.g., PackageAdmin, StartLevel, etc.).</li>
230 <li>The framework enters the <tt>Bundle.STARTING</tt> state.</li>
231</ul>
232
233
234<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>
235
236<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-stoppinginstance"></a></p>
237
238<h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-StoppingtheFrameworkInstance"></a>Stopping the Framework Instance</h2>
239
240<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>
241
242<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-launching"></a></p>
243
Karl Pauls44c941b2009-09-06 22:21:18 +0000244<h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-LaunchingaFramework"></a>Launching a Framework</h1>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000245
Karl Pauls44c941b2009-09-06 22:21:18 +0000246<p>Launching a framework is fairly simple and involves only four steps:</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000247
248<ol>
249 <li>Define some configuration properties.</li>
Karl Pauls44c941b2009-09-06 22:21:18 +0000250 <li>Obtain framework factory.</li>
251 <li>Use factory to create framework with the configuration properties.</li>
Karl Pauls44c941b2009-09-06 22:21:18 +0000252 <li>Invoke the <tt>Framework.start()</tt> method.</li>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000253</ol>
254
255
Karl Pauls93f7ea22009-10-11 20:18:06 +0000256<p>In reality, the first step is optional, since all properties will
257have reasonable defaults, but if you are creating a launcher you will
258generally want to more than that, such as automatically installing and
259starting bundles when you start the framework instance. The default
260Felix launcher defines reusable functionality to automatically install
Karl Pauls34ab8272010-06-06 21:44:50 +0000261and/or start bundles upon framework startup; see the <a href="http://felix.apache.org/site/apache-felix-framework-usage-documentation.html#ApacheFelixFrameworkUsageDocumentation-configuringfelix">usage document</a> for more information on configuring the Felix framework and on the various configuration properties.</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000262
Karl Pauls34ab8272010-06-06 21:44:50 +0000263<p>The remainder of this section describes how the standard Felix launcher works as well as how to create a custom launcher.</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000264
265<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-standardlauncher"></a></p>
266
Karl Pauls93f7ea22009-10-11 20:18:06 +0000267<h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-StandardFelixFrameworkLauncher"></a>Standard Felix Framework Launcher</h2>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000268
Karl Pauls34ab8272010-06-06 21:44:50 +0000269<p>The standard Felix framework launcher is very simple and is not
270intended to solve every possible requirement; it is intended to work
271for most standard situations. Most special launching requirements
272should be resolved by creating a custom launcher. This section
273describes how the standard launcher works. The following code
274represents the complete <tt>main()</tt> method of the standard launcher, each numbered comment will be described in more detail below:</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000275
Karl Pauls44c941b2009-09-06 22:21:18 +0000276<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
Karl Pauls93f7ea22009-10-11 20:18:06 +0000277<pre class="code-java"><span class="code-keyword">public</span> <span class="code-keyword">static</span> void main(<span class="code-object">String</span>[] args) <span class="code-keyword">throws</span> Exception
Karl Paulsf7d389c2008-11-06 16:22:54 +0000278{
Karl Pauls44c941b2009-09-06 22:21:18 +0000279 <span class="code-comment">// (1) Check <span class="code-keyword">for</span> command line arguments and verify usage.
Karl Pauls44c941b2009-09-06 22:21:18 +0000280</span> <span class="code-object">String</span> bundleDir = <span class="code-keyword">null</span>;
281 <span class="code-object">String</span> cacheDir = <span class="code-keyword">null</span>;
282 <span class="code-object">boolean</span> expectBundleDir = <span class="code-keyword">false</span>;
283 <span class="code-keyword">for</span> (<span class="code-object">int</span> i = 0; i &lt; args.length; i++)
Karl Paulsf7d389c2008-11-06 16:22:54 +0000284 {
Karl Pauls44c941b2009-09-06 22:21:18 +0000285 <span class="code-keyword">if</span> (args[i].equals(BUNDLE_DIR_SWITCH))
286 {
287 expectBundleDir = <span class="code-keyword">true</span>;
288 }
289 <span class="code-keyword">else</span> <span class="code-keyword">if</span> (expectBundleDir)
290 {
291 bundleDir = args[i];
292 expectBundleDir = <span class="code-keyword">false</span>;
293 }
294 <span class="code-keyword">else</span>
Karl Pauls44c941b2009-09-06 22:21:18 +0000295 {
296 cacheDir = args[i];
297 }
298 }
Karl Pauls34ab8272010-06-06 21:44:50 +0000299
Karl Pauls44c941b2009-09-06 22:21:18 +0000300 <span class="code-keyword">if</span> ((args.length &gt; 3) || (expectBundleDir &amp;&amp; bundleDir == <span class="code-keyword">null</span>))
301 {
302 <span class="code-object">System</span>.out.println(<span class="code-quote">"Usage: [-b &lt;bundle-deploy-dir&gt;] [&lt;bundle-cache-dir&gt;]"</span>);
Karl Paulsf7d389c2008-11-06 16:22:54 +0000303 <span class="code-object">System</span>.exit(0);
304 }
305
306 <span class="code-comment">// (2) Load system properties.
307</span> Main.loadSystemProperties();
308
309 <span class="code-comment">// (3) Read configuration properties.
310</span> Properties configProps = Main.loadConfigProperties();
Karl Pauls44c941b2009-09-06 22:21:18 +0000311 <span class="code-keyword">if</span> (configProps == <span class="code-keyword">null</span>)
Karl Paulsf7d389c2008-11-06 16:22:54 +0000312 {
Karl Pauls44c941b2009-09-06 22:21:18 +0000313 <span class="code-object">System</span>.err.println(<span class="code-quote">"No "</span> + CONFIG_PROPERTIES_FILE_VALUE + <span class="code-quote">" found."</span>);
314 configProps = <span class="code-keyword">new</span> Properties();
Karl Paulsf7d389c2008-11-06 16:22:54 +0000315 }
316
Karl Pauls44c941b2009-09-06 22:21:18 +0000317 <span class="code-comment">// (4) Copy framework properties from the system properties.
Karl Pauls44c941b2009-09-06 22:21:18 +0000318</span> Main.copySystemProperties(configProps);
319
320 <span class="code-comment">// (5) Use the specified auto-deploy directory over <span class="code-keyword">default</span>.
321</span> <span class="code-keyword">if</span> (bundleDir != <span class="code-keyword">null</span>)
322 {
323 configProps.setProperty(AutoProcessor.AUTO_DEPLOY_DIR_PROPERY, bundleDir);
324 }
325
326 <span class="code-comment">// (6) Use the specified bundle cache directory over <span class="code-keyword">default</span>.
327</span> <span class="code-keyword">if</span> (cacheDir != <span class="code-keyword">null</span>)
328 {
329 configProps.setProperty(Constants.FRAMEWORK_STORAGE, cacheDir);
330 }
331
332 <span class="code-comment">// (7) Add a shutdown hook to clean stop the framework.
Karl Pauls44c941b2009-09-06 22:21:18 +0000333</span> <span class="code-object">String</span> enableHook = configProps.getProperty(SHUTDOWN_HOOK_PROP);
334 <span class="code-keyword">if</span> ((enableHook == <span class="code-keyword">null</span>) || !enableHook.equalsIgnoreCase(<span class="code-quote">"<span class="code-keyword">false</span>"</span>))
335 {
Karl Pauls34ab8272010-06-06 21:44:50 +0000336 <span class="code-object">Runtime</span>.getRuntime().addShutdownHook(<span class="code-keyword">new</span> <span class="code-object">Thread</span>(<span class="code-quote">"Felix Shutdown Hook"</span>) {
Karl Pauls44c941b2009-09-06 22:21:18 +0000337 <span class="code-keyword">public</span> void run()
338 {
339 <span class="code-keyword">try</span>
Karl Pauls44c941b2009-09-06 22:21:18 +0000340 {
341 <span class="code-keyword">if</span> (m_fwk != <span class="code-keyword">null</span>)
342 {
343 m_fwk.stop();
344 m_fwk.waitForStop(0);
345 }
346 }
347 <span class="code-keyword">catch</span> (Exception ex)
348 {
349 <span class="code-object">System</span>.err.println(<span class="code-quote">"Error stopping framework: "</span> + ex);
350 }
351 }
352 });
353 }
Karl Paulsf7d389c2008-11-06 16:22:54 +0000354
Karl Paulsf7d389c2008-11-06 16:22:54 +0000355 <span class="code-keyword">try</span>
356 {
Karl Pauls44c941b2009-09-06 22:21:18 +0000357 <span class="code-comment">// (8) Create an instance and initialize the framework.
358</span> FrameworkFactory factory = getFrameworkFactory();
359 m_fwk = factory.newFramework(configProps);
360 m_fwk.init();
361 <span class="code-comment">// (9) Use the system bundle context to process the auto-deploy
362</span> <span class="code-comment">// and auto-install/auto-start properties.
363</span> AutoProcessor.process(configProps, m_fwk.getBundleContext());
364 <span class="code-comment">// (10) Start the framework.
365</span> m_fwk.start();
366 <span class="code-comment">// (11) Wait <span class="code-keyword">for</span> framework to stop to exit the VM.
Karl Pauls44c941b2009-09-06 22:21:18 +0000367</span> m_fwk.waitForStop(0);
Karl Paulsf7d389c2008-11-06 16:22:54 +0000368 <span class="code-object">System</span>.exit(0);
369 }
370 <span class="code-keyword">catch</span> (Exception ex)
371 {
372 <span class="code-object">System</span>.err.println(<span class="code-quote">"Could not create framework: "</span> + ex);
373 ex.printStackTrace();
Karl Pauls34ab8272010-06-06 21:44:50 +0000374 <span class="code-object">System</span>.exit(0);
Karl Paulsf7d389c2008-11-06 16:22:54 +0000375 }
Karl Pauls44c941b2009-09-06 22:21:18 +0000376}
377</pre>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000378</div></div>
379
380<p>The general steps of the standard launcher are quite straightforward:</p>
381
382<ol>
Karl Pauls93f7ea22009-10-11 20:18:06 +0000383 <li>The launcher supports setting the auto-deploy directory (with the <tt>-b</tt>
384switch) and setting the bundle cache path with a single argument, so
385check for this and issue a usage message it there are more than one
386arguments.</li>
387 <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>
388system property. This file is not needed to launch Felix and is
389provided merely for convenience when system properties must be
390specified. The file is a standard Java properties file, but it also
391supports property substitution using <tt>${&lt;property-name</tt>} syntax. Property substitution can be nested; only system properties will be used for substitution.</li>
392 <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>
Karl Pauls34ab8272010-06-06 21:44:50 +0000393system property. This file is used to configure the framework instance
Karl Pauls93f7ea22009-10-11 20:18:06 +0000394created by the launcher. The file is a standard Java properties file,
395but it also supports property substitution using "<tt>${&lt;property-name&gt;</tt>}"
396syntax. Property substitution can be nested; configuration and system
397properties will be used for substitution with configuration properties
398having precedence.</li>
399 <li>For convenience, any configuration
400properties that are set as system properties are copied into the set of
401configuration properties. This provide an easy way to add to or
402override configuration properties specified in the <tt>config.properties</tt> file, since the Felix instance will never look at system properties for configuration.</li>
Karl Pauls44c941b2009-09-06 22:21:18 +0000403 <li>If the <tt>-b</tt> switch was used to specify an auto-deploy directory, then use that to set the value of <tt>felix.auto.deploy.dir</tt>.</li>
404 <li>If a single command-line argument is specified, 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>
Karl Pauls93f7ea22009-10-11 20:18:06 +0000405 <li>Add a shutdown hook to cleanly stop the framework, unless the hook is disabled.</li>
406 <li>Create a framework instance using the <tt>FrameworkFactory</tt> passing in the configuration properties, then initialize the factory instance; see the <a href="#ApacheFelixFrameworkLaunchingandEmbedding-customlauncher">custom launcher example</a> below to see how the META-INF/services <tt>FrameworkFactory</tt> is obtained.</li>
407 <li>Use <tt>org.apache.felix.main.AutoProcessor</tt>, which will automatically deploy any bundles in the auto-deploy directory as well as bundles specified in the <tt>felix.auto.install</tt> and <tt>felix.auto.start</tt>
408configuration properties during framework startup to automatically
409install and/or start bundles; see the usage document for more
410information <a href="http://felix.apache.org/site/apache-felix-framework-usage-documentation.html#ApacheFelixFrameworkUsageDocumentation-configuringframework">configuration properties</a> and <a href="http://felix.apache.org/site/apache-felix-framework-usage-documentation.html#ApacheFelixFrameworkUsageDocumentation-autodeploy">bundle auto-deploy</a>.</li>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000411 <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>
412</ol>
413
414
Karl Pauls93f7ea22009-10-11 20:18:06 +0000415<p>The framework is not active until the <tt>start()</tt> method is
416called. If no shell bundles are installed and started or if there is
417difficulty locating the shell bundles specified in the auto-start
418property, then it will appear as if the framework is hung, but it is
419actually running without any way to interact with it since the shell
420bundles provide the only means of interaction.</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000421
422<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-customlauncher"></a></p>
423
Karl Pauls93f7ea22009-10-11 20:18:06 +0000424<h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-CustomFrameworkLauncher"></a>Custom Framework Launcher</h2>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000425
Karl Pauls93f7ea22009-10-11 20:18:06 +0000426<p>This section creates a bare-bones launcher to demonstrate the
427minimum requirements for creating an interactive launcher for the Felix
Karl Pauls34ab8272010-06-06 21:44:50 +0000428framework. This example uses the standard Gogo shell bundles for
429interactivity, but any other bundles could be used instead. This
430example launcher project has the following directory structure:</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000431
Karl Pauls44c941b2009-09-06 22:21:18 +0000432<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent">
Karl Paulsf7d389c2008-11-06 16:22:54 +0000433<pre>launcher/
434 lib/
Karl Pauls34ab8272010-06-06 21:44:50 +0000435 org.apache.felix.main-3.0.0.jar
Karl Paulsf7d389c2008-11-06 16:22:54 +0000436 bundle/
Karl Pauls34ab8272010-06-06 21:44:50 +0000437 org.apache.felix.gogo.command-0.6.0.jar
438 org.apache.felix.gogo.runtime-0.6.0.jar
439 org.apache.felix.gogo.shell-0.6.0.jar
Karl Paulsf7d389c2008-11-06 16:22:54 +0000440 src/
441 example/
442 Main.java
443</pre>
444</div></div>
445
Karl Pauls93f7ea22009-10-11 20:18:06 +0000446<p>The <tt>lib/</tt> directory contains Felix' main JAR file, which
447also contains the OSGi core interfaces. The main JAR file is used so
448that we can reuse the default launcher's auto-install/auto-start
449configuration property handling; if these capabilities are not needed,
450then it would be possible to use the framework JAR file instead of the
451main JAR file. The <tt>bundle/</tt> directory contains the shell
452service and textual shell interface bundles that will be used for
Karl Pauls34ab8272010-06-06 21:44:50 +0000453interacting with the framework instance. Note: If you do not launch the
454framework with interactive bundles, it will appear as if the framework
Karl Pauls93f7ea22009-10-11 20:18:06 +0000455instance is hung, but it is actually just sitting there waiting for
Karl Pauls34ab8272010-06-06 21:44:50 +0000456someone 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 framework launcher.</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000457
Karl Pauls44c941b2009-09-06 22:21:18 +0000458<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
Karl Pauls93f7ea22009-10-11 20:18:06 +0000459<pre class="code-java"><span class="code-keyword">package</span> example;
Karl Paulsf7d389c2008-11-06 16:22:54 +0000460
Karl Pauls44c941b2009-09-06 22:21:18 +0000461<span class="code-keyword">import</span> java.io.*;
462<span class="code-keyword">import</span> org.osgi.framework.launch.*;
463<span class="code-keyword">import</span> org.apache.felix.main.AutoProcessor;
Karl Paulsf7d389c2008-11-06 16:22:54 +0000464
465<span class="code-keyword">public</span> class Main
466{
Karl Pauls44c941b2009-09-06 22:21:18 +0000467 <span class="code-keyword">private</span> <span class="code-keyword">static</span> Framework m_fwk = <span class="code-keyword">null</span>;
Karl Paulsf7d389c2008-11-06 16:22:54 +0000468
469 <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
470 {
471 <span class="code-comment">// Print welcome banner.
Karl Pauls34ab8272010-06-06 21:44:50 +0000472</span> <span class="code-object">System</span>.out.println(<span class="code-quote">"\nWelcome to My Launcher"</span>);
473 <span class="code-object">System</span>.out.println(<span class="code-quote">"======================\n"</span>);
Karl Paulsf7d389c2008-11-06 16:22:54 +0000474
Karl Paulsf7d389c2008-11-06 16:22:54 +0000475 <span class="code-keyword">try</span>
476 {
Karl Pauls44c941b2009-09-06 22:21:18 +0000477 m_fwk = getFrameworkFactory().newFramework(<span class="code-keyword">null</span>);
478 m_fwk.init()
479 AutoProcessor.process(<span class="code-keyword">null</span>, m_fwk.getBundleContext());
480 m_fwk.start();
481 m_fwk.waitForStop();
Karl Paulsf7d389c2008-11-06 16:22:54 +0000482 <span class="code-object">System</span>.exit(0);
483 }
484 <span class="code-keyword">catch</span> (Exception ex)
485 {
486 <span class="code-object">System</span>.err.println(<span class="code-quote">"Could not create framework: "</span> + ex);
487 ex.printStackTrace();
488 <span class="code-object">System</span>.exit(-1);
489 }
490 }
Karl Pauls44c941b2009-09-06 22:21:18 +0000491
492 <span class="code-keyword">private</span> <span class="code-keyword">static</span> FrameworkFactory getFrameworkFactory() <span class="code-keyword">throws</span> Exception
493 {
494 URL url = Main.class.getClassLoader().getResource(
495 <span class="code-quote">"META-INF/services/org.osgi.framework.launch.FrameworkFactory"</span>);
496 <span class="code-keyword">if</span> (url != <span class="code-keyword">null</span>)
497 {
498 BufferedReader br = <span class="code-keyword">new</span> BufferedReader(<span class="code-keyword">new</span> InputStreamReader(url.openStream()));
499 <span class="code-keyword">try</span>
Karl Pauls44c941b2009-09-06 22:21:18 +0000500 {
501 <span class="code-keyword">for</span> (<span class="code-object">String</span> s = br.readLine(); s != <span class="code-keyword">null</span>; s = br.readLine())
502 {
503 s = s.trim();
504 <span class="code-comment">// Try to load first non-empty, non-commented line.
505</span> <span class="code-keyword">if</span> ((s.length() &gt; 0) &amp;&amp; (s.charAt(0) != '#'))
506 {
507 <span class="code-keyword">return</span> (FrameworkFactory) <span class="code-object">Class</span>.forName(s).newInstance();
508 }
509 }
510 }
511 <span class="code-keyword">finally</span>
Karl Pauls44c941b2009-09-06 22:21:18 +0000512 {
513 <span class="code-keyword">if</span> (br != <span class="code-keyword">null</span>) br.close();
514 }
515 }
516
517 <span class="code-keyword">throw</span> <span class="code-keyword">new</span> Exception(<span class="code-quote">"Could not find framework factory."</span>);
518 }
519}
520</pre>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000521</div></div>
522
Karl Pauls93f7ea22009-10-11 20:18:06 +0000523<p>This launcher relies on the default behavior of <tt>AutoProcessor</tt>
524to automatically deploy the shell bundles. This simple, generic
525launcher provides a good starting point if the default Felix launcher
526is not sufficient. Since very few configuration properties are
527specified, the default values are used. For the bundle auto-deploy
528directory, "<tt>bundle</tt>" in the current directory is used, while for the framework bundle cache, "<tt>felix-cache</tt>" in the current directory is used.</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000529
530<p>By breaking down the above source code into small chunks, it is quite easy to see what is going on.</p>
531
Karl Pauls44c941b2009-09-06 22:21:18 +0000532<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
Karl Pauls93f7ea22009-10-11 20:18:06 +0000533<pre class="code-java"> m_fwk = getFrameworkFactory().newFramework(<span class="code-keyword">null</span>);
Karl Pauls44c941b2009-09-06 22:21:18 +0000534 m_fwk.init()
Karl Pauls44c941b2009-09-06 22:21:18 +0000535</pre>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000536</div></div>
537
Karl Pauls93f7ea22009-10-11 20:18:06 +0000538<p>These steps get a the framework factory service and use it to create
539a framework instance with a default configuration. Once the framework
540instance is created, it is initialized with <tt>init()</tt>.</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000541
Karl Pauls44c941b2009-09-06 22:21:18 +0000542<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
Karl Pauls93f7ea22009-10-11 20:18:06 +0000543<pre class="code-java"> AutoProcessor.process(<span class="code-keyword">null</span>, m_fwk.getBundleContext());
Karl Pauls44c941b2009-09-06 22:21:18 +0000544</pre>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000545</div></div>
546
Karl Pauls93f7ea22009-10-11 20:18:06 +0000547<p>The <tt>AutorProcessor</tt> will automatically deploy bundles in the
548auto-deploy directory and any referenced from the auto-install/start
549properties. Since we are using an empty configuration, the auto-deploy
550directory is the <tt>bundle</tt> directory in the current directory
Karl Pauls34ab8272010-06-06 21:44:50 +0000551and there are no auto properties. Therefore, in this case, the shell
552bundles will be installed.</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000553
Karl Pauls44c941b2009-09-06 22:21:18 +0000554<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
Karl Pauls93f7ea22009-10-11 20:18:06 +0000555<pre class="code-java"> m_fwk.start();
Karl Pauls44c941b2009-09-06 22:21:18 +0000556 m_fwk.waitForStop();
557 <span class="code-object">System</span>.exit(0);
558</pre>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000559</div></div>
560
Karl Pauls93f7ea22009-10-11 20:18:06 +0000561<p>These final steps start the framework and cause the launching
562application thread to wait for the framework to stop and when it does
563the launching thread calls <tt>System.exit()</tt> to make sure the VM actually exits.</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000564
Karl Pauls44c941b2009-09-06 22:21:18 +0000565<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
Karl Pauls93f7ea22009-10-11 20:18:06 +0000566<pre class="code-java"> <span class="code-keyword">private</span> <span class="code-keyword">static</span> FrameworkFactory getFrameworkFactory() <span class="code-keyword">throws</span> Exception
Karl Pauls44c941b2009-09-06 22:21:18 +0000567 {
568 ...
569 }
Karl Pauls44c941b2009-09-06 22:21:18 +0000570</pre>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000571</div></div>
572
Karl Pauls93f7ea22009-10-11 20:18:06 +0000573<p>This method retrieves the framework factory service by doing a
574META-INF/services resource lookup, which it can use to obtain the
575concrete class name for the factory. If you are using Java 6, then you
576can use the <tt>ServiceLoader</tt> API in the JRE to further simplify the factory service lookup.</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000577
578<p>The following command compiles the launcher when run from the root directory of the launcher project:</p>
579
Karl Pauls44c941b2009-09-06 22:21:18 +0000580<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent">
Karl Pauls34ab8272010-06-06 21:44:50 +0000581<pre>javac -d . -classpath lib/org.apache.felix.main-3.0.0.jar src/example/Main.java
Karl Paulsf7d389c2008-11-06 16:22:54 +0000582</pre>
583</div></div>
584
Karl Pauls93f7ea22009-10-11 20:18:06 +0000585<p>After executing this command, an <tt>example/</tt> directory is
586created in the current directory, which contains the generated class
587file. The following command executes the simple launcher when run from
588the root directory of the launcher project:</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000589
Karl Pauls44c941b2009-09-06 22:21:18 +0000590<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent">
Karl Pauls34ab8272010-06-06 21:44:50 +0000591<pre>java -cp .:lib/org.apache.felix.main-3.0.0.jar example.Main
Karl Paulsf7d389c2008-11-06 16:22:54 +0000592</pre>
593</div></div>
594
Karl Pauls44c941b2009-09-06 22:21:18 +0000595<p>After executing this command, a "<tt>felix-cache/</tt>" directory is created that contains the cached bundles, which were installed from the <tt>bundle/</tt> directory.</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000596
597<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-embedding"></a></p>
598
Karl Pauls34ab8272010-06-06 21:44:50 +0000599<h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-EmbeddingtheFelixFramework"></a>Embedding the Felix Framework</h1>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000600
Karl Pauls34ab8272010-06-06 21:44:50 +0000601<p>Embedding the Felix framework into a host application is a simple
602way to provide a sophisticated extensibility mechanism (i.e., a plugin
603system) to the host application. Embedding the Felix framework is very
604similar to launching it as described above, the main difference is that
605the host application typically wants to interact with the framework
606instance and/or installed bundles/services from the outside. This is
607fairly easy to achieve, but there are some subtle issues to understand.
Karl Pauls93f7ea22009-10-11 20:18:06 +0000608This section presents the mechanisms for embedding Felix into a host
609application and the issues in doing so.</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000610
611<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-hostinteraction"></a></p>
612
Karl Pauls93f7ea22009-10-11 20:18:06 +0000613<h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-Host/FelixInteraction"></a>Host/Felix Interaction</h2>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000614
Karl Pauls34ab8272010-06-06 21:44:50 +0000615<p>In the section on <a href="#ApacheFelixFrameworkLaunchingandEmbedding-launching">launching</a> the framework above, the <tt>Felix</tt> class accepts a configuration property called <tt>felix.systembundle.activators</tt>,
Karl Pauls93f7ea22009-10-11 20:18:06 +0000616which is a list of bundle activator instances. These bundle activator
617instances provide a convenient way for host applications to interact
Karl Pauls34ab8272010-06-06 21:44:50 +0000618with the Felix framework.</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000619
Karl Pauls34ab8272010-06-06 21:44:50 +0000620<div class="panelMacro"><table class="warningMacro"><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>WARNING</b><br>The <tt>felix.systembundle.activators</tt>
621configuration property is specific to the Felix framework
622implementation. If you want your code to work with other framework
623implementations, you should call <tt>init()</tt> on the framework instance and use <tt>getBundleContext()</tt> directly. Otherwise, the approach would be very similar.</td></tr></tbody></table></div>
624
625<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>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000626
Karl Pauls44c941b2009-09-06 22:21:18 +0000627<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
Karl Pauls93f7ea22009-10-11 20:18:06 +0000628<pre class="code-java"><span class="code-keyword">public</span> class HostActivator <span class="code-keyword">implements</span> BundleActivator
Karl Paulsf7d389c2008-11-06 16:22:54 +0000629{
630 <span class="code-keyword">private</span> BundleContext m_context = <span class="code-keyword">null</span>;
631
632 <span class="code-keyword">public</span> void start(BundleContext context)
633 {
634 m_context = context;
635 }
636
637 <span class="code-keyword">public</span> void stop(BundleContext context)
638 {
639 m_context = <span class="code-keyword">null</span>;
640 }
641
642 <span class="code-keyword">public</span> Bundle[] getBundles()
643 {
644 <span class="code-keyword">if</span> (m_context != <span class="code-keyword">null</span>)
645 {
646 <span class="code-keyword">return</span> m_context.getBundles();
647 }
648 <span class="code-keyword">return</span> <span class="code-keyword">null</span>;
649 }
Karl Pauls44c941b2009-09-06 22:21:18 +0000650}
Karl Pauls44c941b2009-09-06 22:21:18 +0000651</pre>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000652</div></div>
653
Karl Pauls34ab8272010-06-06 21:44:50 +0000654<p>Given the above bundle activator, it is now possible to embed the
655Felix framework into a host application and interact with it as the
656following snippet illustrates:</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000657
Karl Pauls44c941b2009-09-06 22:21:18 +0000658<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
Karl Pauls93f7ea22009-10-11 20:18:06 +0000659<pre class="code-java"><span class="code-keyword">public</span> class HostApplication
Karl Paulsf7d389c2008-11-06 16:22:54 +0000660{
661 <span class="code-keyword">private</span> HostActivator m_activator = <span class="code-keyword">null</span>;
662 <span class="code-keyword">private</span> Felix m_felix = <span class="code-keyword">null</span>;
663
664 <span class="code-keyword">public</span> HostApplication()
665 {
666 <span class="code-comment">// Create a configuration property map.
Karl Pauls34ab8272010-06-06 21:44:50 +0000667</span> Map config = <span class="code-keyword">new</span> HashMap();
Karl Paulsf7d389c2008-11-06 16:22:54 +0000668 <span class="code-comment">// Create host activator;
669</span> m_activator = <span class="code-keyword">new</span> HostActivator();
670 List list = <span class="code-keyword">new</span> ArrayList();
671 list.add(m_activator);
672 configMap.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);
673
674 <span class="code-keyword">try</span>
675 {
676 <span class="code-comment">// Now create an instance of the framework with
677</span> <span class="code-comment">// our configuration properties.
Karl Pauls34ab8272010-06-06 21:44:50 +0000678</span> m_felix = <span class="code-keyword">new</span> Felix(config);
Karl Paulsf7d389c2008-11-06 16:22:54 +0000679 <span class="code-comment">// Now start Felix instance.
680</span> m_felix.start();
681 }
682 <span class="code-keyword">catch</span> (Exception ex)
683 {
684 <span class="code-object">System</span>.err.println(<span class="code-quote">"Could not create framework: "</span> + ex);
685 ex.printStackTrace();
686 }
687 }
688
689 <span class="code-keyword">public</span> Bundle[] getInstalledBundles()
690 {
691 <span class="code-comment">// Use the system bundle activator to gain external
692</span> <span class="code-comment">// access to the set of installed bundles.
693</span> <span class="code-keyword">return</span> m_activator.getBundles();
694 }
695
696 <span class="code-keyword">public</span> void shutdownApplication()
697 {
698 <span class="code-comment">// Shut down the felix framework when stopping the
699</span> <span class="code-comment">// host application.
700</span> m_felix.stop();
Karl Pauls34ab8272010-06-06 21:44:50 +0000701 m_felix.waitForStop(0);
Karl Paulsf7d389c2008-11-06 16:22:54 +0000702 }
Karl Pauls44c941b2009-09-06 22:21:18 +0000703}
704</pre>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000705</div></div>
706
Karl Pauls93f7ea22009-10-11 20:18:06 +0000707<p>Notice how the <tt>HostApplication.getInstalledBundles()</tt> method
Karl Pauls34ab8272010-06-06 21:44:50 +0000708uses its activator instance to get access to the system bundle's
Karl Pauls93f7ea22009-10-11 20:18:06 +0000709context in order to interact with the embedded Felix framework
710instance. This approach provides the foundation for all interaction
711between the host application and the embedded framework instance.</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000712
713<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-hostservices"></a></p>
714
715<h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-ProvidingHostApplicationServices"></a>Providing Host Application Services</h2>
716
Karl Pauls93f7ea22009-10-11 20:18:06 +0000717<p>Providing services from the host application to bundles inside the
718embedded Felix framework instance follows the basic approach laid out
719in <a href="#ApacheFelixFrameworkLaunchingandEmbedding-hostinteraction">above</a>.
720The main complication for providing a host application service to
721bundles is the fact that both the host application and the bundles must
722be using the same class definitions for the service interface classes.
723Since the host application cannot import classes from a bundle, this
724means that the service interface classes <b>must</b> be accessible on
725the class path, typically as part of the host application itself. The
726host application then must export the service interface package via the
727system bundle so that bundles installed into the embedded framework
728instance can import it. This is achieved using the <tt>org.osgi.framework.system.packages.extra</tt> configuration property previously presented.</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000729
730<p>Consider the follow simple property lookup service:</p>
731
Karl Pauls44c941b2009-09-06 22:21:18 +0000732<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
Karl Pauls93f7ea22009-10-11 20:18:06 +0000733<pre class="code-java"><span class="code-keyword">package</span> host.service.lookup;
Karl Paulsf7d389c2008-11-06 16:22:54 +0000734
Karl Pauls34ab8272010-06-06 21:44:50 +0000735<span class="code-keyword">public</span> <span class="code-keyword">interface</span> Lookup
Karl Paulsf7d389c2008-11-06 16:22:54 +0000736{
737 <span class="code-keyword">public</span> <span class="code-object">Object</span> lookup(<span class="code-object">String</span> name);
Karl Pauls44c941b2009-09-06 22:21:18 +0000738}
Karl Pauls44c941b2009-09-06 22:21:18 +0000739</pre>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000740</div></div>
741
Karl Pauls93f7ea22009-10-11 20:18:06 +0000742<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>"
743command. Now consider the following host application bundle activator,
744which will be used to register/unregister the property lookup service
745when the embedded framework instance starts/stops:</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000746
Karl Pauls44c941b2009-09-06 22:21:18 +0000747<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
Karl Pauls93f7ea22009-10-11 20:18:06 +0000748<pre class="code-java"><span class="code-keyword">package</span> host.core;
Karl Paulsf7d389c2008-11-06 16:22:54 +0000749
750<span class="code-keyword">import</span> java.util.Map;
751<span class="code-keyword">import</span> org.osgi.framework.BundleActivator;
752<span class="code-keyword">import</span> org.osgi.framework.BundleContext;
753<span class="code-keyword">import</span> org.osgi.framework.ServiceRegistration;
754<span class="code-keyword">import</span> host.service.lookup;
755
756<span class="code-keyword">public</span> class HostActivator <span class="code-keyword">implements</span> BundleActivator
757{
758 <span class="code-keyword">private</span> Map m_lookupMap = <span class="code-keyword">null</span>;
759 <span class="code-keyword">private</span> BundleContext m_context = <span class="code-keyword">null</span>;
760 <span class="code-keyword">private</span> ServiceRegistration m_registration = <span class="code-keyword">null</span>;
761
762 <span class="code-keyword">public</span> HostActivator(Map lookupMap)
763 {
764 <span class="code-comment">// Save a reference to the service's backing store.
765</span> m_lookupMap = lookupMap;
766 }
767
768 <span class="code-keyword">public</span> void start(BundleContext context)
769 {
770 <span class="code-comment">// Save a reference to the bundle context.
771</span> m_context = context;
772 <span class="code-comment">// Create a property lookup service implementation.
773</span> Lookup lookup = <span class="code-keyword">new</span> Lookup() {
774 <span class="code-keyword">public</span> <span class="code-object">Object</span> lookup(<span class="code-object">String</span> name)
775 {
776 <span class="code-keyword">return</span> m_lookupMap.get(name);
777 }
778 };
779 <span class="code-comment">// Register the property lookup service and save
780</span> <span class="code-comment">// the service registration.
781</span> m_registration = m_context.registerService(
782 Lookup.class.getName(), lookup, <span class="code-keyword">null</span>);
783 }
784
785 <span class="code-keyword">public</span> void stop(BundleContext context)
786 {
787 <span class="code-comment">// Unregister the property lookup service.
788</span> m_registration.unregister();
789 m_context = <span class="code-keyword">null</span>;
790 }
Karl Pauls44c941b2009-09-06 22:21:18 +0000791}
792</pre>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000793</div></div>
794
Karl Pauls93f7ea22009-10-11 20:18:06 +0000795<p>Given the above host application bundle activator, the following
796code snippet shows how the host application could create an embedded
797version of the Felix framework and provide the property lookup service
798to installed bundles:</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000799
Karl Pauls44c941b2009-09-06 22:21:18 +0000800<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
Karl Pauls93f7ea22009-10-11 20:18:06 +0000801<pre class="code-java"><span class="code-keyword">package</span> host.core;
Karl Paulsf7d389c2008-11-06 16:22:54 +0000802
803<span class="code-keyword">import</span> java.util.List;
804<span class="code-keyword">import</span> java.util.ArrayList;
805<span class="code-keyword">import</span> java.util.Map;
806<span class="code-keyword">import</span> java.util.HashMap;
807<span class="code-keyword">import</span> host.service.lookup.Lookup;
808<span class="code-keyword">import</span> org.apache.felix.framework.Felix;
809<span class="code-keyword">import</span> org.apache.felix.framework.util.FelixConstants;
810<span class="code-keyword">import</span> org.osgi.framework.Constants;
811
812<span class="code-keyword">public</span> class HostApplication
813{
814 <span class="code-keyword">private</span> HostActivator m_activator = <span class="code-keyword">null</span>;
815 <span class="code-keyword">private</span> Felix m_felix = <span class="code-keyword">null</span>;
816 <span class="code-keyword">private</span> Map m_lookupMap = <span class="code-keyword">new</span> HashMap();
817
818 <span class="code-keyword">public</span> HostApplication()
819 {
820 <span class="code-comment">// Initialize the map <span class="code-keyword">for</span> the property lookup service.
821</span> m_lookupMap.put(<span class="code-quote">"name1"</span>, <span class="code-quote">"value1"</span>);
822
823 m_lookupMap.put(<span class="code-quote">"name2"</span>, <span class="code-quote">"value2"</span>);
824 m_lookupMap.put(<span class="code-quote">"name3"</span>, <span class="code-quote">"value3"</span>);
825 m_lookupMap.put(<span class="code-quote">"name4"</span>, <span class="code-quote">"value4"</span>);
826
827 <span class="code-comment">// Create a configuration property map.
828</span> Map configMap = <span class="code-keyword">new</span> HashMap();
829 <span class="code-comment">// Export the host provided service <span class="code-keyword">interface</span> <span class="code-keyword">package</span>.
830</span> configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA,
831 <span class="code-quote">"host.service.lookup; version=1.0.0"</span>);
832 <span class="code-comment">// Create host activator;
833</span> m_activator = <span class="code-keyword">new</span> HostActivator(m_lookupMap);
834 List list = <span class="code-keyword">new</span> ArrayList();
835 list.add(m_activator);
836 configMap.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);
837
838 <span class="code-keyword">try</span>
839 {
840 <span class="code-comment">// Now create an instance of the framework with
841</span> <span class="code-comment">// our configuration properties.
842</span> m_felix = <span class="code-keyword">new</span> Felix(configMap);
843 <span class="code-comment">// Now start Felix instance.
844</span> m_felix.start();
845 }
846 <span class="code-keyword">catch</span> (Exception ex)
847 {
848 <span class="code-object">System</span>.err.println(<span class="code-quote">"Could not create framework: "</span> + ex);
849 ex.printStackTrace();
850 }
851 }
852
853 <span class="code-keyword">public</span> void shutdownApplication()
854 {
855 <span class="code-comment">// Shut down the felix framework when stopping the
856</span> <span class="code-comment">// host application.
857</span> m_felix.stop();
Karl Pauls34ab8272010-06-06 21:44:50 +0000858 m_felix.waitForStop(0);
Karl Paulsf7d389c2008-11-06 16:22:54 +0000859 }
Karl Pauls44c941b2009-09-06 22:21:18 +0000860}
861</pre>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000862</div></div>
863
Karl Pauls93f7ea22009-10-11 20:18:06 +0000864<p>Rather than having the host application bundle activator register
865the service, it is also possible for the the host application to simply
866get the bundle context from the bundle activator and register the
867service directly, but the presented approach is perhaps a little
868cleaner since it allows the host application to register/unregister the
869service when the system bundle starts/stops.</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000870
871<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-hostserviceusage"></a></p>
872
873<h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-UsingServicesProvidedbyBundles"></a>Using Services Provided by Bundles</h2>
874
Karl Pauls93f7ea22009-10-11 20:18:06 +0000875<p>Using services provided by bundles follows the same general approach
876of using a host application bundle activator. The main complication for
877the host application using a service from a bundle is the fact that
878both the host application and the bundle must be using the same class
879definitions for the service interface classes. Since the host
880application cannot import classes from a bundle, this means that the
881service interface classes <b>must</b> be accessible on the class path,
882typically as part of the host application itself. The host application
883then must export the service interface package via the system bundle so
884that bundles installed into the embedded framework instance can import
885it. This is achieved using the <tt>org.osgi.framework.system.packages.extra</tt> configuration property previously presented.</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000886
Karl Pauls93f7ea22009-10-11 20:18:06 +0000887<p>Consider the following simple command service interface for which
888bundles provide implementations, such as might be used to create an
889extensible interactive shell:</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000890
Karl Pauls44c941b2009-09-06 22:21:18 +0000891<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
Karl Pauls93f7ea22009-10-11 20:18:06 +0000892<pre class="code-java"><span class="code-keyword">package</span> host.service.command;
Karl Paulsf7d389c2008-11-06 16:22:54 +0000893
894<span class="code-keyword">public</span> class Command
895{
896 <span class="code-keyword">public</span> <span class="code-object">String</span> getName();
897 <span class="code-keyword">public</span> <span class="code-object">String</span> getDescription();
898 <span class="code-keyword">public</span> <span class="code-object">boolean</span> execute(<span class="code-object">String</span> commandline);
Karl Pauls44c941b2009-09-06 22:21:18 +0000899}
Karl Pauls44c941b2009-09-06 22:21:18 +0000900</pre>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000901</div></div>
902
Karl Pauls93f7ea22009-10-11 20:18:06 +0000903<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>"
904command. Now consider the previously introduced host application bundle
905activator below, which simply provides access to the system bundle
906context:</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000907
Karl Pauls44c941b2009-09-06 22:21:18 +0000908<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
Karl Pauls93f7ea22009-10-11 20:18:06 +0000909<pre class="code-java"><span class="code-keyword">package</span> host.core;
Karl Paulsf7d389c2008-11-06 16:22:54 +0000910
911<span class="code-keyword">import</span> org.osgi.framework.BundleActivator;
912<span class="code-keyword">import</span> org.osgi.framework.BundleContext;
913
914<span class="code-keyword">public</span> class HostActivator <span class="code-keyword">implements</span> BundleActivator
915{
916 <span class="code-keyword">private</span> BundleContext m_context = <span class="code-keyword">null</span>;
917
918 <span class="code-keyword">public</span> void start(BundleContext context)
919 {
920 m_context = context;
921 }
922
923 <span class="code-keyword">public</span> void stop(BundleContext context)
924 {
925 m_context = <span class="code-keyword">null</span>;
926 }
927
928 <span class="code-keyword">public</span> BundleContext getContext()
929 {
930 <span class="code-keyword">return</span> m_context;
931 }
Karl Pauls44c941b2009-09-06 22:21:18 +0000932}
Karl Pauls44c941b2009-09-06 22:21:18 +0000933</pre>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000934</div></div>
935
Karl Pauls93f7ea22009-10-11 20:18:06 +0000936<p>With this bundle activator, the host application can use command
937services provided by bundles installed inside its embedded Felix
938framework instance. The following code snippet illustrates one possible
939approach:</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +0000940
Karl Pauls44c941b2009-09-06 22:21:18 +0000941<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
Karl Pauls93f7ea22009-10-11 20:18:06 +0000942<pre class="code-java"><span class="code-keyword">package</span> host.core;
Karl Paulsf7d389c2008-11-06 16:22:54 +0000943
944<span class="code-keyword">import</span> java.util.List;
945<span class="code-keyword">import</span> java.util.ArrayList;
946<span class="code-keyword">import</span> java.util.Map;
947<span class="code-keyword">import</span> host.service.command.Command;
948<span class="code-keyword">import</span> org.apache.felix.framework.Felix;
949<span class="code-keyword">import</span> org.apache.felix.framework.util.FelixConstants;
950<span class="code-keyword">import</span> org.apache.felix.framework.cache.BundleCache;
951<span class="code-keyword">import</span> org.osgi.framework.Constants;
952<span class="code-keyword">import</span> org.osgi.util.tracker.ServiceTracker;
953
954<span class="code-keyword">public</span> class HostApplication
955{
956 <span class="code-keyword">private</span> HostActivator m_activator = <span class="code-keyword">null</span>;
957 <span class="code-keyword">private</span> Felix m_felix = <span class="code-keyword">null</span>;
958 <span class="code-keyword">private</span> ServiceTracker m_tracker = <span class="code-keyword">null</span>;
959
960 <span class="code-keyword">public</span> HostApplication()
961 {
962 <span class="code-comment">// Create a configuration property map.
963</span> Map configMap = <span class="code-keyword">new</span> HashMap();
964 <span class="code-comment">// Export the host provided service <span class="code-keyword">interface</span> <span class="code-keyword">package</span>.
965</span> configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA,
966 <span class="code-quote">"host.service.command; version=1.0.0"</span>);
967 <span class="code-comment">// Create host activator;
968</span> m_activator = <span class="code-keyword">new</span> HostActivator();
969 List list = <span class="code-keyword">new</span> ArrayList();
970 list.add(m_activator);
971 configMap.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);
972
973 <span class="code-keyword">try</span>
974 {
975 <span class="code-comment">// Now create an instance of the framework with
976</span> <span class="code-comment">// our configuration properties.
977</span> m_felix = <span class="code-keyword">new</span> Felix(configMap);
978 <span class="code-comment">// Now start Felix instance.
979</span> m_felix.start();
980 }
981 <span class="code-keyword">catch</span> (Exception ex)
982 {
983 <span class="code-object">System</span>.err.println(<span class="code-quote">"Could not create framework: "</span> + ex);
984 ex.printStackTrace();
985 }
986
987 m_tracker = <span class="code-keyword">new</span> ServiceTracker(
988 m_activator.getContext(), Command.class.getName(), <span class="code-keyword">null</span>);
989 m_tracker.open();
990 }
991
992 <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)
993 {
994 <span class="code-comment">// See <span class="code-keyword">if</span> any of the currently tracked command services
995</span> <span class="code-comment">// match the specified command name, <span class="code-keyword">if</span> so then execute it.
996</span> <span class="code-object">Object</span>[] services = m_tracker.getServices();
997 <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++)
998 {
999 <span class="code-keyword">try</span>
1000 {
1001 <span class="code-keyword">if</span> (((Command) services[i]).getName().equals(name))
1002 {
1003 <span class="code-keyword">return</span> ((Command) services[i]).execute(commandline);
1004 }
1005 }
1006 <span class="code-keyword">catch</span> (Exception ex)
1007 {
1008 <span class="code-comment">// Since the services returned by the tracker could become
1009</span> <span class="code-comment">// invalid at any moment, we will <span class="code-keyword">catch</span> all exceptions, log
1010</span> <span class="code-comment">// a message, and then ignore faulty services.
1011</span> <span class="code-object">System</span>.err.println(ex);
1012 }
1013 }
1014 <span class="code-keyword">return</span> <span class="code-keyword">false</span>;
1015 }
1016
1017 <span class="code-keyword">public</span> void shutdownApplication()
1018 {
Karl Paulsf7d389c2008-11-06 16:22:54 +00001019 <span class="code-comment">// Shut down the felix framework when stopping the
1020</span> <span class="code-comment">// host application.
1021</span> m_felix.stop();
Karl Pauls34ab8272010-06-06 21:44:50 +00001022 m_felix.waitForStop(0);
Karl Paulsf7d389c2008-11-06 16:22:54 +00001023 }
Karl Pauls44c941b2009-09-06 22:21:18 +00001024}
Karl Pauls44c941b2009-09-06 22:21:18 +00001025</pre>
Karl Paulsf7d389c2008-11-06 16:22:54 +00001026</div></div>
1027
Karl Pauls93f7ea22009-10-11 20:18:06 +00001028<p>The above example is overly simplistic with respect to concurrency
1029issues and error conditions, but it demonstrates the overall approach
1030for using bundle-provided services from the host application.</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +00001031
1032<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-servicereflection"></a></p>
1033
1034<h3><a name="ApacheFelixFrameworkLaunchingandEmbedding-UsingBundleServicesviaReflection"></a>Using Bundle Services via Reflection</h3>
1035
Karl Pauls93f7ea22009-10-11 20:18:06 +00001036<p>It possible for the host application to use services provided by
1037bundles without having access to the service interface classes and thus
1038not needing to put the service interface classes on the class path. To
1039do this, the host application uses the same general approach to acquire
1040the system bundle context object, which it can use to look up service
1041objects. Using either an LDAP filter or the service interface class
1042name, the host application can retrieve the service object and then use
1043standard Java reflection to invoke methods on the service object.</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +00001044
1045<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-serviceother"></a></p>
1046
1047<h3><a name="ApacheFelixFrameworkLaunchingandEmbedding-OtherApproaches"></a>Other Approaches</h3>
1048
Karl Pauls34ab8272010-06-06 21:44:50 +00001049<p>The <a href="http://code.google.com/p/transloader/" class="external-link" rel="nofollow">Transloader</a> project is another attempt at dealing with issues of classes loaded from different class loaders and may be of interest.</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +00001050
1051<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-caveat"></a></p>
1052
1053<h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-Caveat"></a>Caveat</h1>
1054
Karl Pauls93f7ea22009-10-11 20:18:06 +00001055<p>The code in this document has not been thoroughly tested nor even
1056compiled and may be out of date with respect to the current Felix
1057source code. If you find errors please report them so the that they can
1058be corrected.</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +00001059
1060<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-feedback"></a></p>
1061
1062<h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-Feedback"></a>Feedback</h2>
1063
Karl Pauls34ab8272010-06-06 21:44:50 +00001064<p>Subscribe to the Felix users mailing list by sending a message to <a href="mailto:users-subscribe@felix.apache.org" class="external-link" rel="nofollow">users-subscribe@felix.apache.org</a>; after subscribing, email questions or feedback to <a href="mailto:users@felix.apache.org" class="external-link" rel="nofollow">users@felix.apache.org</a>.</p>
Karl Paulsf7d389c2008-11-06 16:22:54 +00001065 </div>
Karl Pauls34ab8272010-06-06 21:44:50 +00001066 </body></html>