blob: c65aaeab25a732d6ca1edd236b219f7499f2e308 [file] [log] [blame]
Karl Pauls3d699792008-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 Pauls6721c022008-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 Pauls3d699792008-11-06 16:22:54 +000011 <div class="menu">
12<ul>
13 <li><a href="http://felix.apache.org/site/news.html" title="news">news</a></li>
14 <li><a href="http://felix.apache.org/site/license.html" title="license">license</a></li>
15 <li><span class="nobr"><a href="http://felix.apache.org/site/downloads.cgi" title="Visit page outside Confluence" rel="nofollow">downloads<sup><img class="rendericon" src="apache-felix-framework-launching-and-embedding_files/linkext7.gif" alt="" align="absmiddle" border="0" height="7" width="7"></sup></a></span></li>
16 <li><a href="http://felix.apache.org/site/documentation.html" title="documentation">documentation</a></li>
17 <li><a href="http://felix.apache.org/site/mailinglists.html" title="mailinglists">mailing lists</a></li>
18 <li><a href="http://felix.apache.org/site/contributing.html" title="Contributing">contributing</a></li>
19 <li><span class="nobr"><a href="http://www.apache.org/" title="Visit page outside Confluence" rel="nofollow">asf<sup><img class="rendericon" src="apache-felix-framework-launching-and-embedding_files/linkext7.gif" alt="" align="absmiddle" border="0" height="7" width="7"></sup></a></span></li>
20 <li><span class="nobr"><a href="http://www.apache.org/foundation/sponsorship.html" title="Visit page outside Confluence" rel="nofollow">sponsorship<sup><img class="rendericon" src="apache-felix-framework-launching-and-embedding_files/linkext7.gif" alt="" align="absmiddle" border="0" height="7" width="7"></sup></a></span></li>
21 <li><span class="nobr"><a href="http://www.apache.org/foundation/thanks.html" title="Visit page outside Confluence" rel="nofollow">sponsors<sup><img class="rendericon" src="apache-felix-framework-launching-and-embedding_files/linkext7.gif" alt="" align="absmiddle" border="0" height="7" width="7"></sup></a></span>
22<!-- ApacheCon Ad -->
23<iframe src="apache-felix-framework-launching-and-embedding_files/button.html" style="border-width: 0pt; float: left;" frameborder="0" height="135" scrolling="no" width="135"></iframe>
24<p style="height: 100px;">
25<!-- ApacheCon Ad -->
26</p></li></ul> </div>
27 <div class="main">
28<h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-ApacheFelixFrameworkLaunchingandEmbedding"></a>Apache Felix Framework Launching and Embedding</h1>
29
Karl Pauls3992b552009-09-06 22:21:18 +000030<p><em>[This document describes framework launching introduced in Felix 2.0.0 and is incompatible with older versions of the Felix framework.]</em></p>
Karl Pauls3d699792008-11-06 16:22:54 +000031
32<ul>
Karl Pauls3992b552009-09-06 22:21:18 +000033 <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-introduction">Introduction</a></li>
34 <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-overview">API Overview</a>
35
Karl Pauls3d699792008-11-06 16:22:54 +000036 <ul>
Karl Pauls3992b552009-09-06 22:21:18 +000037 <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-creatingandconfiguring">Creating and Configuring the Framework Instance</a></li>
38 <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-startinginstance">Starting the Framework Instance</a></li>
39 <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-stoppinginstance">Stopping the Framework Instance</a></li>
Karl Pauls3d699792008-11-06 16:22:54 +000040 </ul>
41 </li>
Karl Pauls3992b552009-09-06 22:21:18 +000042 <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-launching">Launching Felix</a>
43
Karl Pauls3d699792008-11-06 16:22:54 +000044 <ul>
Karl Pauls3992b552009-09-06 22:21:18 +000045 <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-standardlauncher">Standard Felix Launcher</a></li>
46 <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-customlauncher">Custom Felix Launcher</a></li>
Karl Pauls3d699792008-11-06 16:22:54 +000047 </ul>
48 </li>
Karl Pauls3992b552009-09-06 22:21:18 +000049 <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-embedding">Embedding Felix</a>
Karl Pauls3d699792008-11-06 16:22:54 +000050 <ul>
Karl Pauls3992b552009-09-06 22:21:18 +000051
52 <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 Pauls3d699792008-11-06 16:22:54 +000055 <ul>
Karl Pauls3992b552009-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>
58
Karl Pauls3d699792008-11-06 16:22:54 +000059 </ul>
60 </li>
61 </ul>
62 </li>
Karl Pauls3992b552009-09-06 22:21:18 +000063 <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-caveat">Caveat</a></li>
64 <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-feedback">Feedback</a></li>
Karl Pauls3d699792008-11-06 16:22:54 +000065</ul>
66
67
68<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-introduction"></a></p>
69
70<h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-Introduction"></a>Introduction</h1>
71
Karl Pauls3992b552009-09-06 22:21:18 +000072<p>The Apache Felix framework is intended to be easily launchable and embeddable. For example, Felix avoids the use of system properties for configuration, since these are globals and can cause interference if multiple framework instances are created in the same VM. Felix also tries to multiplex singleton facilities, like the URL stream handler factory. The goal is to make it possible to use Felix in as many scenarios as possible; however, this is still just a goal. In other words, this is a work in progress and if any issues arise, it would be greatly appreciated if they are brought to the attention of the Felix community. The next section provides a Felix API overview, while the remainder of the document is divided into two sections, one focusing on how to launch Felix and one focusing on how to embed Felix into a host application.</p>
Karl Pauls3d699792008-11-06 16:22:54 +000073
74<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-overview"></a></p>
75
76<h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-APIOverview"></a>API Overview</h1>
77
Karl Pauls3992b552009-09-06 22:21:18 +000078<p>The Felix framework is implemented by the <tt>org.apache.felix.framework.Felix</tt> class or just <tt>Felix</tt> for short. As part of the R4.2 OSGi specification, the launching and embedding API of the OSGi framework has been standardized. The approach is 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 Pauls3d699792008-11-06 16:22:54 +000079
Karl Pauls3992b552009-09-06 22:21:18 +000080<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
81<pre class="code-java">
82<span class="code-keyword">public</span> <span class="code-keyword">interface</span> Bundle
Karl Pauls3d699792008-11-06 16:22:54 +000083{
84 BundleContext getBundleContext();
85 <span class="code-object">long</span> getBundleId();
86 URL getEntry(<span class="code-object">String</span> name);
87 Enumeration getEntryPaths(<span class="code-object">String</span> path);
88 Enumeration findEntries(<span class="code-object">String</span> path, <span class="code-object">String</span> filePattern, <span class="code-object">boolean</span> recurse);
89 Dictionary getHeaders();
90 Dictionary getHeaders(<span class="code-object">String</span> locale);
91 <span class="code-object">long</span> getLastModified();
92 <span class="code-object">String</span> getLocation();
93 URL getResource(<span class="code-object">String</span> name);
94 Enumeration getResources(<span class="code-object">String</span> name) <span class="code-keyword">throws</span> IOException;
95 ServiceReference[] getRegisteredServices();
96 ServiceReference[] getServicesInUse();
97 <span class="code-object">int</span> getState();
98 <span class="code-object">String</span> getSymbolicName();
Karl Pauls3992b552009-09-06 22:21:18 +000099 Version getVersion();
Karl Pauls3d699792008-11-06 16:22:54 +0000100 <span class="code-object">boolean</span> hasPermission(<span class="code-object">Object</span> obj);
101 <span class="code-object">Class</span> loadClass(<span class="code-object">String</span> name) <span class="code-keyword">throws</span> ClassNotFoundException;
102 void start() <span class="code-keyword">throws</span> BundleException;
103 void stop() <span class="code-keyword">throws</span> BundleException;
104 void uninstall() <span class="code-keyword">throws</span> BundleException;
105 void update() <span class="code-keyword">throws</span> BundleException;
106 void update(InputStream is) <span class="code-keyword">throws</span> BundleException;
Karl Pauls3992b552009-09-06 22:21:18 +0000107}
108
109</pre>
Karl Pauls3d699792008-11-06 16:22:54 +0000110</div></div>
111
112<p>The <tt>Framework</tt> interface is defined as:</p>
113
Karl Pauls3992b552009-09-06 22:21:18 +0000114<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
115<pre class="code-java">
116<span class="code-keyword">public</span> <span class="code-keyword">interface</span> Framework <span class="code-keyword">extends</span> Bundle
Karl Pauls3d699792008-11-06 16:22:54 +0000117{
118 void init();
119 FrameworkEvent waitForStop();
Karl Pauls3992b552009-09-06 22:21:18 +0000120}
121
122</pre>
Karl Pauls3d699792008-11-06 16:22:54 +0000123</div></div>
124
Karl Pauls3992b552009-09-06 22:21:18 +0000125<p>To actually construct a framework instance, the R4.2 specification defines the FrameworkFactory interface:</p>
Karl Pauls3d699792008-11-06 16:22:54 +0000126
Karl Pauls3992b552009-09-06 22:21:18 +0000127<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
128<pre class="code-java">
129<span class="code-keyword">public</span> <span class="code-keyword">interface</span> FrameworkFactory
130{
131 Framework newFramework(Map configMap);
132}
133</pre>
134</div></div>
135
136<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 Pauls3d699792008-11-06 16:22:54 +0000137
138<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-creatingandconfiguring"></a></p>
139
140<h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-CreatingandConfiguringtheFrameworkInstance"></a>Creating and Configuring the Framework Instance</h2>
141
Karl Pauls3992b552009-09-06 22:21:18 +0000142<p>You use the framework factory to construct and configure a framework instance (or by directly instantiating the Felix class). The configuration map may contain the following OSGi standard properties:</p>
Karl Pauls3d699792008-11-06 16:22:54 +0000143
144<ul>
Karl Pauls3992b552009-09-06 22:21:18 +0000145 <li><tt>org.osgi.framework.system.packages</tt> - specifies a list of packages the system bundle should export from the environment; if this is not set, then the framework uses a reasonable default fault.</li>
146 <li><tt>org.osgi.framework.system.packages.extra</tt> - specifies a list of additional packages the system bundle should export from the environment that are appended to the packages specified in <tt>org.osgi.framework.system.packages</tt>; there is no default value for this property.</li>
147
148 <li><tt>org.osgi.framework.bootdelegation</tt> - specifies a list of packages that should be made implicitly available to all bundles from the environment (i.e., no need to import them); there is no default value for this property and its use should be avoided.</li>
149 <li><tt>org.osgi.framework.storage</tt> - specifies the path to a directory, which will be created if it does not exist, to use for bundle cache storage; the default value for this property is "<tt>felix-cache</tt>" in the current working directory.</li>
150 <li><tt>org.osgi.framework.storage.clean</tt> - specifies whether the bundle cache should be flushed; the default value for this property is "none", but it can be changed to "onFirstInit" to flush the bundle cache when the framework is initialized.</li>
151
152 <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 Pauls3d699792008-11-06 16:22:54 +0000153</ul>
154
155
156<p>Felix also has the following, non-standard configuration properties:</p>
157
158<ul>
159 <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 Pauls3992b552009-09-06 22:21:18 +0000160
161 <li><tt>felix.systembundle.activators</tt> - specifies a <tt>List</tt> of <tt>BundleActivator</tt> instances that are started/stopped when the System Bundle is started/stopped; the specified instances will receive the System Bundle's <tt>BundleContext</tt> when invoked.</li>
Karl Pauls3d699792008-11-06 16:22:54 +0000162 <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 Pauls3992b552009-09-06 22:21:18 +0000163
164 <li><tt>felix.log.level</tt> - specifies an integer <tt>String</tt> whose value indicates the degree of logging reported by the framework; the default value is "1" and "0" turns off logging completely, otherwise log levels match those specified in the OSGi Log Service (i.e., 1 = error, 2 = warning, 3 = information, and 4 = debug).</li>
Karl Pauls3d699792008-11-06 16:22:54 +0000165 <li><tt>felix.startlevel.bundle</tt> - specifies the start level for newly installed bundles; the default value is 1.</li>
Karl Pauls3992b552009-09-06 22:21:18 +0000166 <li><tt>felix.bootdelegation.implicit</tt> - specifies whether or not the framework should try to guess when to boot delegate when external code tries to load classes or resources; the default value is "<tt>true</tt>".</li>
167
168 <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 Pauls3d699792008-11-06 16:22:54 +0000169</ul>
170
171
Karl Pauls3992b552009-09-06 22:21:18 +0000172<p>The configuration map is copied and the keys are treated as case insensitive. You are not able to change the framework's configuration after construction. If you need a different configuration, you must create a new framework instance.</p>
Karl Pauls3d699792008-11-06 16:22:54 +0000173
Karl Pauls3992b552009-09-06 22:21:18 +0000174<div class='panelMacro'><table class='warningMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/confluence/images/icons/emoticons/forbidden.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td><b>WARNING</b><br /><p>Felix configuration properties have change considerably starting from <tt>1.4.0</tt>; if you are upgrading from an earlier version, the <a href="/confluence/display/FELIX/Apache+Felix+Framework+Usage+Documentation#ApacheFelixFrameworkUsageDocumentation-migrating">usage document</a> describes the configuration property changes.</p></td></tr></table></div>
Karl Pauls6721c022008-12-20 17:15:21 +0000175
Karl Pauls3d699792008-11-06 16:22:54 +0000176<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-startinginstance"></a></p>
177
178<h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-StartingtheFrameworkInstance"></a>Starting the Framework Instance</h2>
179
Karl Pauls3992b552009-09-06 22:21:18 +0000180<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 Pauls3d699792008-11-06 16:22:54 +0000181
182<ul>
183 <li><tt>init()</tt> results in the framework instance in the <tt>Bundle.STARTING</tt> state.</li>
184 <li><tt>start()</tt> results in the framework instance in the <tt>Bundle.ACTIVE</tt> state.</li>
Karl Pauls3992b552009-09-06 22:21:18 +0000185
Karl Pauls3d699792008-11-06 16:22:54 +0000186</ul>
187
188
Karl Pauls3992b552009-09-06 22:21:18 +0000189<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 Felix 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 Pauls3d699792008-11-06 16:22:54 +0000190
191<ul>
192 <li>Event handling is enabled.</li>
193 <li>The security manager is installed if it is enabled.</li>
194 <li>The framework is set to start level 0.</li>
195 <li>All bundles in the bundle caches are reified and their state is set to <tt>Bundle.INSTALLED</tt>.</li>
196 <li>The framework gets a valid <tt>BundleContext</tt>.</li>
Karl Pauls3992b552009-09-06 22:21:18 +0000197
Karl Pauls3d699792008-11-06 16:22:54 +0000198 <li>All framework-provided services are made available (e.g., PackageAdmin, StartLevel, etc.).</li>
199 <li>The framework enters the <tt>Bundle.STARTING</tt> state.</li>
200</ul>
201
202
203<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>
204
205<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-stoppinginstance"></a></p>
206
207<h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-StoppingtheFrameworkInstance"></a>Stopping the Framework Instance</h2>
208
209<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>
210
211<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-launching"></a></p>
212
Karl Pauls3992b552009-09-06 22:21:18 +0000213<h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-LaunchingaFramework"></a>Launching a Framework</h1>
Karl Pauls3d699792008-11-06 16:22:54 +0000214
Karl Pauls3992b552009-09-06 22:21:18 +0000215<p>Launching a framework is fairly simple and involves only four steps:</p>
Karl Pauls3d699792008-11-06 16:22:54 +0000216
217<ol>
218 <li>Define some configuration properties.</li>
Karl Pauls3992b552009-09-06 22:21:18 +0000219 <li>Obtain framework factory.</li>
220 <li>Use factory to create framework with the configuration properties.</li>
221
222 <li>Invoke the <tt>Framework.start()</tt> method.</li>
Karl Pauls3d699792008-11-06 16:22:54 +0000223</ol>
224
225
Karl Pauls3992b552009-09-06 22:21:18 +0000226<p>In reality, the first step is optional, since all properties will have reasonable defaults, but if you are creating a launcher you will generally want to more than that, such as automatically installing and starting bundles when you start the framework instance. The default Felix launcher defines reusable functionality to automatically install and/or start bundles upon framework startup; see the <a href="/confluence/display/FELIX/Apache+Felix+Framework+Usage+Documentation#ApacheFelixFrameworkUsageDocumentation-configuringfelix">usage document</a> for more information on configuring Felix and on the various configuration properties.</p>
Karl Pauls3d699792008-11-06 16:22:54 +0000227
Karl Pauls3992b552009-09-06 22:21:18 +0000228<p>The remainder of this section describes how the standard Felix launcher works as well as how to create a custom launcher for Felix.</p>
Karl Pauls3d699792008-11-06 16:22:54 +0000229
230<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-standardlauncher"></a></p>
231
232<h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-StandardFelixLauncher"></a>Standard Felix Launcher</h2>
233
Karl Pauls3992b552009-09-06 22:21:18 +0000234<p>The standard Felix launcher is very simple and is not intended to solve every possible requirement; it is intended to work for most standard situations. Most special launching requirements should be resolved by creating a custom launcher. This section describes how the standard launcher works. The following code represents the complete <tt>main()</tt> method of the standard launcher, each numbered comment will be described in more detail below:</p>
Karl Pauls3d699792008-11-06 16:22:54 +0000235
Karl Pauls3992b552009-09-06 22:21:18 +0000236<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
237<pre class="code-java">
238<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 Pauls3d699792008-11-06 16:22:54 +0000239{
Karl Pauls3992b552009-09-06 22:21:18 +0000240 <span class="code-comment">// (1) Check <span class="code-keyword">for</span> command line arguments and verify usage.
241
242</span> <span class="code-object">String</span> bundleDir = <span class="code-keyword">null</span>;
243 <span class="code-object">String</span> cacheDir = <span class="code-keyword">null</span>;
244 <span class="code-object">boolean</span> expectBundleDir = <span class="code-keyword">false</span>;
245 <span class="code-keyword">for</span> (<span class="code-object">int</span> i = 0; i &lt; args.length; i++)
Karl Pauls3d699792008-11-06 16:22:54 +0000246 {
Karl Pauls3992b552009-09-06 22:21:18 +0000247 <span class="code-keyword">if</span> (args[i].equals(BUNDLE_DIR_SWITCH))
248 {
249 expectBundleDir = <span class="code-keyword">true</span>;
250 }
251 <span class="code-keyword">else</span> <span class="code-keyword">if</span> (expectBundleDir)
252 {
253 bundleDir = args[i];
254 expectBundleDir = <span class="code-keyword">false</span>;
255 }
256 <span class="code-keyword">else</span>
257
258 {
259 cacheDir = args[i];
260 }
261 }
262 <span class="code-keyword">if</span> ((args.length &gt; 3) || (expectBundleDir &amp;&amp; bundleDir == <span class="code-keyword">null</span>))
263 {
264 <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 Pauls3d699792008-11-06 16:22:54 +0000265 <span class="code-object">System</span>.exit(0);
266 }
267
268 <span class="code-comment">// (2) Load system properties.
Karl Pauls3992b552009-09-06 22:21:18 +0000269
Karl Pauls3d699792008-11-06 16:22:54 +0000270</span> Main.loadSystemProperties();
271
272 <span class="code-comment">// (3) Read configuration properties.
273</span> Properties configProps = Main.loadConfigProperties();
Karl Pauls3992b552009-09-06 22:21:18 +0000274 <span class="code-keyword">if</span> (configProps == <span class="code-keyword">null</span>)
Karl Pauls3d699792008-11-06 16:22:54 +0000275 {
Karl Pauls3992b552009-09-06 22:21:18 +0000276 <span class="code-object">System</span>.err.println(<span class="code-quote">"No "</span> + CONFIG_PROPERTIES_FILE_VALUE + <span class="code-quote">" found."</span>);
277 configProps = <span class="code-keyword">new</span> Properties();
Karl Pauls3d699792008-11-06 16:22:54 +0000278 }
279
Karl Pauls3992b552009-09-06 22:21:18 +0000280 <span class="code-comment">// (4) Copy framework properties from the system properties.
281
282</span> Main.copySystemProperties(configProps);
283
284 <span class="code-comment">// (5) Use the specified auto-deploy directory over <span class="code-keyword">default</span>.
285</span> <span class="code-keyword">if</span> (bundleDir != <span class="code-keyword">null</span>)
286 {
287 configProps.setProperty(AutoProcessor.AUTO_DEPLOY_DIR_PROPERY, bundleDir);
288 }
289
290 <span class="code-comment">// (6) Use the specified bundle cache directory over <span class="code-keyword">default</span>.
291</span> <span class="code-keyword">if</span> (cacheDir != <span class="code-keyword">null</span>)
292 {
293 configProps.setProperty(Constants.FRAMEWORK_STORAGE, cacheDir);
294 }
295
296 <span class="code-comment">// (7) Add a shutdown hook to clean stop the framework.
297
298</span> <span class="code-object">String</span> enableHook = configProps.getProperty(SHUTDOWN_HOOK_PROP);
299 <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>))
300 {
301 <span class="code-object">Runtime</span>.getRuntime().addShutdownHook(<span class="code-keyword">new</span> <span class="code-object">Thread</span>() {
302 <span class="code-keyword">public</span> void run()
303 {
304 <span class="code-keyword">try</span>
305
306 {
307 <span class="code-keyword">if</span> (m_fwk != <span class="code-keyword">null</span>)
308 {
309 m_fwk.stop();
310 m_fwk.waitForStop(0);
311 }
312 }
313 <span class="code-keyword">catch</span> (Exception ex)
314 {
315 <span class="code-object">System</span>.err.println(<span class="code-quote">"Error stopping framework: "</span> + ex);
316 }
317 }
318 });
319 }
Karl Pauls3d699792008-11-06 16:22:54 +0000320
321 <span class="code-comment">// Print welcome banner.
Karl Pauls3992b552009-09-06 22:21:18 +0000322</span> <span class="code-object">System</span>.out.println(<span class="code-quote">"\nWelcome to Felix"</span>);
323 <span class="code-object">System</span>.out.println(<span class="code-quote">"================\n"</span>);
Karl Pauls3d699792008-11-06 16:22:54 +0000324
325 <span class="code-keyword">try</span>
Karl Pauls3992b552009-09-06 22:21:18 +0000326
Karl Pauls3d699792008-11-06 16:22:54 +0000327 {
Karl Pauls3992b552009-09-06 22:21:18 +0000328 <span class="code-comment">// (8) Create an instance and initialize the framework.
329</span> FrameworkFactory factory = getFrameworkFactory();
330 m_fwk = factory.newFramework(configProps);
331 m_fwk.init();
332 <span class="code-comment">// (9) Use the system bundle context to process the auto-deploy
333</span> <span class="code-comment">// and auto-install/auto-start properties.
334</span> AutoProcessor.process(configProps, m_fwk.getBundleContext());
335 <span class="code-comment">// (10) Start the framework.
336</span> m_fwk.start();
337 <span class="code-comment">// (11) Wait <span class="code-keyword">for</span> framework to stop to exit the VM.
338
339</span> m_fwk.waitForStop(0);
Karl Pauls3d699792008-11-06 16:22:54 +0000340 <span class="code-object">System</span>.exit(0);
341 }
342 <span class="code-keyword">catch</span> (Exception ex)
343 {
344 <span class="code-object">System</span>.err.println(<span class="code-quote">"Could not create framework: "</span> + ex);
345 ex.printStackTrace();
346 <span class="code-object">System</span>.exit(-1);
347 }
Karl Pauls3992b552009-09-06 22:21:18 +0000348}
349</pre>
Karl Pauls3d699792008-11-06 16:22:54 +0000350</div></div>
351
352<p>The general steps of the standard launcher are quite straightforward:</p>
353
354<ol>
Karl Pauls3992b552009-09-06 22:21:18 +0000355 <li>The launcher supports setting the auto-deploy directory (with the <tt>-b</tt> switch) and setting the bundle cache path with a single argument, so check for this and issue a usage message it there are more than one arguments.</li>
356 <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> system property. This file is not needed to launch Felix and is provided merely for convenience when system properties must be specified. The file is a standard Java properties file, but it also supports property substitution using <tt>${&lt;property-name</tt>} syntax. Property substitution can be nested; only system properties will be used for substitution.</li>
357
358 <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> system property. This file is used to configure the Felix instance created by the launcher. The file is a standard Java properties file, but it also supports property substitution using "<tt>${&lt;property-name</tt>}" syntax. Property substitution can be nested; configuration and system properties will be used for substitution with configuration properties having precedence.</li>
359 <li>For convenience, any configuration properties that are set as system properties are copied into the set of configuration properties. This provide an easy way to add to or override configuration properties specified in the <tt>config.properties</tt> file, since the Felix instance will never look at system properties for configuration.</li>
360
361 <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>
362 <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>
363 <li>Create a framework instance using the <tt>FrameworkFactory</tt> passing in the configuration properties, then initialize the factory instance.</li>
364
365 <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> configuration properties during framework startup to automatically install and/or start bundles; see the usage document for more information <a href="/confluence/display/FELIX/Apache+Felix+Framework+Usage+Documentation#ApacheFelixFrameworkUsageDocumentation-configuringframework">configuration properties</a> and <a href="/confluence/display/FELIX/Apache+Felix+Framework+Usage+Documentation#ApacheFelixFrameworkUsageDocumentation-autodeploy">bundle auto-deploy</a>.</li>
Karl Pauls3d699792008-11-06 16:22:54 +0000366 <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>
Karl Pauls3992b552009-09-06 22:21:18 +0000367
Karl Pauls3d699792008-11-06 16:22:54 +0000368</ol>
369
370
Karl Pauls3992b552009-09-06 22:21:18 +0000371<p>The framework is not active until the <tt>start()</tt> method is called. If no shell bundles are installed and started or if there is difficulty locating the shell bundles specified in the auto-start property, then it will appear as if the framework is hung, but it is actually running without any way to interact with it since the shell bundles provide the only means of interaction.</p>
Karl Pauls3d699792008-11-06 16:22:54 +0000372
373<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-customlauncher"></a></p>
374
375<h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-CustomFelixLauncher"></a>Custom Felix Launcher</h2>
376
Karl Pauls3992b552009-09-06 22:21:18 +0000377<p>This section creates a bare-bones launcher to demonstrate the minimum requirements for creating an interactive launcher for the Felix framework. This example uses the standard Felix shell bundles for interactivity, but any other bundles could be used instead. For example, the shell service and telnet bundles could be used to launch Felix and make it remotely accessible.</p>
Karl Pauls3d699792008-11-06 16:22:54 +0000378
379<p>This example launcher project has the following directory structure:</p>
380
Karl Pauls3992b552009-09-06 22:21:18 +0000381<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent">
Karl Pauls3d699792008-11-06 16:22:54 +0000382<pre>launcher/
383 lib/
Karl Pauls3992b552009-09-06 22:21:18 +0000384 org.apache.felix.main-2.0.0.jar
Karl Pauls3d699792008-11-06 16:22:54 +0000385 bundle/
Karl Pauls3992b552009-09-06 22:21:18 +0000386 org.apache.felix.shell-1.4.0.jar
387 org.apache.felix.shell.tui-1.4.0.jar
Karl Pauls3d699792008-11-06 16:22:54 +0000388 src/
389 example/
390 Main.java
391</pre>
392</div></div>
393
Karl Pauls3992b552009-09-06 22:21:18 +0000394<p>The <tt>lib/</tt> directory contains Felix' main JAR file, which also contains the OSGi core interfaces. The main JAR file is used so that we can reuse the default launcher's auto-install/auto-start configuration property handling; if these capabilities are not needed, then it would be possible to use the framework JAR file instead of the main JAR file. The <tt>bundle/</tt> directory contains the shell service and textual shell interface bundles that will be used for interacting with the framework instance. Note: If you do not launch Felix with interactive bundles, it will appear as if the framework instance is hung, but it is actually just sitting there waiting for someone to tell it to do something. The <tt>src/example/</tt> directory contains the following <tt>Main.java</tt> file, which is a very simplistic Felix launcher.</p>
Karl Pauls3d699792008-11-06 16:22:54 +0000395
Karl Pauls3992b552009-09-06 22:21:18 +0000396<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
397<pre class="code-java">
398<span class="code-keyword">package</span> example;
Karl Pauls3d699792008-11-06 16:22:54 +0000399
Karl Pauls3992b552009-09-06 22:21:18 +0000400<span class="code-keyword">import</span> java.io.*;
401<span class="code-keyword">import</span> org.osgi.framework.launch.*;
402<span class="code-keyword">import</span> org.apache.felix.main.AutoProcessor;
Karl Pauls3d699792008-11-06 16:22:54 +0000403
404<span class="code-keyword">public</span> class Main
405{
Karl Pauls3992b552009-09-06 22:21:18 +0000406 <span class="code-keyword">private</span> <span class="code-keyword">static</span> Framework m_fwk = <span class="code-keyword">null</span>;
Karl Pauls3d699792008-11-06 16:22:54 +0000407
408 <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
409 {
410 <span class="code-comment">// Print welcome banner.
Karl Pauls3992b552009-09-06 22:21:18 +0000411
Karl Pauls3d699792008-11-06 16:22:54 +0000412</span> <span class="code-object">System</span>.out.println(<span class="code-quote">"\nWelcome to Felix."</span>);
413 <span class="code-object">System</span>.out.println(<span class="code-quote">"=================\n"</span>);
414
Karl Pauls3d699792008-11-06 16:22:54 +0000415 <span class="code-keyword">try</span>
416 {
Karl Pauls3992b552009-09-06 22:21:18 +0000417 m_fwk = getFrameworkFactory().newFramework(<span class="code-keyword">null</span>);
418 m_fwk.init()
419 AutoProcessor.process(<span class="code-keyword">null</span>, m_fwk.getBundleContext());
420 m_fwk.start();
421 m_fwk.waitForStop();
Karl Pauls3d699792008-11-06 16:22:54 +0000422 <span class="code-object">System</span>.exit(0);
423 }
424 <span class="code-keyword">catch</span> (Exception ex)
425 {
426 <span class="code-object">System</span>.err.println(<span class="code-quote">"Could not create framework: "</span> + ex);
427 ex.printStackTrace();
428 <span class="code-object">System</span>.exit(-1);
429 }
430 }
Karl Pauls3992b552009-09-06 22:21:18 +0000431
432 <span class="code-keyword">private</span> <span class="code-keyword">static</span> FrameworkFactory getFrameworkFactory() <span class="code-keyword">throws</span> Exception
433 {
434 URL url = Main.class.getClassLoader().getResource(
435 <span class="code-quote">"META-INF/services/org.osgi.framework.launch.FrameworkFactory"</span>);
436 <span class="code-keyword">if</span> (url != <span class="code-keyword">null</span>)
437 {
438 BufferedReader br = <span class="code-keyword">new</span> BufferedReader(<span class="code-keyword">new</span> InputStreamReader(url.openStream()));
439 <span class="code-keyword">try</span>
440
441 {
442 <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())
443 {
444 s = s.trim();
445 <span class="code-comment">// Try to load first non-empty, non-commented line.
446</span> <span class="code-keyword">if</span> ((s.length() &gt; 0) &amp;&amp; (s.charAt(0) != '#'))
447 {
448 <span class="code-keyword">return</span> (FrameworkFactory) <span class="code-object">Class</span>.forName(s).newInstance();
449 }
450 }
451 }
452 <span class="code-keyword">finally</span>
453
454 {
455 <span class="code-keyword">if</span> (br != <span class="code-keyword">null</span>) br.close();
456 }
457 }
458
459 <span class="code-keyword">throw</span> <span class="code-keyword">new</span> Exception(<span class="code-quote">"Could not find framework factory."</span>);
460 }
461}
462</pre>
Karl Pauls3d699792008-11-06 16:22:54 +0000463</div></div>
464
Karl Pauls3992b552009-09-06 22:21:18 +0000465<p>This launcher relies on the default behavior of <tt>AutoProcessor</tt> to automatically deploy the shell bundles. This simple, generic launcher provides a good starting point if the default Felix launcher is not sufficient. Since very few configuration properties are specified, the default values are used. For the bundle auto-deploy directory, "<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 Pauls3d699792008-11-06 16:22:54 +0000466
467<p>By breaking down the above source code into small chunks, it is quite easy to see what is going on.</p>
468
Karl Pauls3992b552009-09-06 22:21:18 +0000469<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
470<pre class="code-java">
471 m_fwk = getFrameworkFactory().newFramework(<span class="code-keyword">null</span>);
472 m_fwk.init()
473
474</pre>
Karl Pauls3d699792008-11-06 16:22:54 +0000475</div></div>
476
Karl Pauls3992b552009-09-06 22:21:18 +0000477<p>These steps get a the framework factory service and use it to create a framework instance with a default configuration. Once the framework instance is created, it is initialized with <tt>init()</tt>.</p>
Karl Pauls3d699792008-11-06 16:22:54 +0000478
Karl Pauls3992b552009-09-06 22:21:18 +0000479<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
480<pre class="code-java">
481 AutoProcessor.process(<span class="code-keyword">null</span>, m_fwk.getBundleContext());
482</pre>
Karl Pauls3d699792008-11-06 16:22:54 +0000483</div></div>
484
Karl Pauls3992b552009-09-06 22:21:18 +0000485<p>The <tt>AutorProcessor</tt> will automatically deploy bundles in the auto-deploy directory and any referenced from the auto-install/start properties. Since we are using an empty configuration, the auto-deploy directory is the <tt>bundle</tt> directory in the current directory and there are no auto properties. Therefore, in this case, the two shell bundles will be installed.</p>
Karl Pauls3d699792008-11-06 16:22:54 +0000486
Karl Pauls3992b552009-09-06 22:21:18 +0000487<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
488<pre class="code-java">
489 m_fwk.start();
490 m_fwk.waitForStop();
491 <span class="code-object">System</span>.exit(0);
492</pre>
Karl Pauls3d699792008-11-06 16:22:54 +0000493</div></div>
494
Karl Pauls3992b552009-09-06 22:21:18 +0000495<p>These final steps start the framework and cause the launching application thread to wait for the framework to stop and when it does the launching thread calls <tt>System.exit()</tt> to make sure the VM actually exits.</p>
Karl Pauls3d699792008-11-06 16:22:54 +0000496
Karl Pauls3992b552009-09-06 22:21:18 +0000497<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
498<pre class="code-java">
499 <span class="code-keyword">private</span> <span class="code-keyword">static</span> FrameworkFactory getFrameworkFactory() <span class="code-keyword">throws</span> Exception
500 {
501 ...
502 }
503
504</pre>
Karl Pauls3d699792008-11-06 16:22:54 +0000505</div></div>
506
Karl Pauls3992b552009-09-06 22:21:18 +0000507<p>This method retrieves the framework factory service by doing a META-INF/services resource lookup, which it can use to obtain the concrete class name for the factory. If you are using Java 6, then you can use the <tt>ServiceLoader</tt> API in the JRE to further simplify the factory service lookup.</p>
Karl Pauls3d699792008-11-06 16:22:54 +0000508
509<p>The following command compiles the launcher when run from the root directory of the launcher project:</p>
510
Karl Pauls3992b552009-09-06 22:21:18 +0000511<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent">
512<pre>javac -d . -classpath lib/org.apache.felix.main-2.0.0.jar src/example/Main.java
Karl Pauls3d699792008-11-06 16:22:54 +0000513</pre>
514</div></div>
515
Karl Pauls3992b552009-09-06 22:21:18 +0000516<p>After executing this command, an <tt>example/</tt> directory is created in the current directory, which contains the generated class file. The following command executes the simple launcher when run from the root directory of the launcher project:</p>
Karl Pauls3d699792008-11-06 16:22:54 +0000517
Karl Pauls3992b552009-09-06 22:21:18 +0000518<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent">
519<pre>java -cp .:lib/org.apache.felix.main-2.0.0.jar example.Main
Karl Pauls3d699792008-11-06 16:22:54 +0000520</pre>
521</div></div>
522
Karl Pauls3992b552009-09-06 22:21:18 +0000523<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 Pauls3d699792008-11-06 16:22:54 +0000524
525<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-embedding"></a></p>
526
527<h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-EmbeddingFelix"></a>Embedding Felix</h1>
528
Karl Pauls3992b552009-09-06 22:21:18 +0000529<p>Embedding Felix into a host application is a simple way to provide a sophisticated extensibility mechanism (i.e., a plugin system) to the host application. Embedding Felix is very similar to launching Felix as described above, the main difference is that the host application typically wants to interact with the framework instance and/or installed bundles/services from the outside. This is fairly easy to achieve with Felix, but there are some subtle issues to understand. This section presents the mechanisms for embedding Felix into a host application and the issues in doing so.</p>
Karl Pauls3d699792008-11-06 16:22:54 +0000530
531<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-hostinteraction"></a></p>
532
Karl Pauls3992b552009-09-06 22:21:18 +0000533<h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-Host%2FFelixInteraction"></a>Host/Felix Interaction</h2>
Karl Pauls3d699792008-11-06 16:22:54 +0000534
Karl Pauls3992b552009-09-06 22:21:18 +0000535<p>In the section on <a href="#ApacheFelixFrameworkLaunchingandEmbedding-launching">launching</a> Felix above, the <tt>Felix</tt> accepts a configuration property called <tt>felix.systembundle.activators</tt>, which is a list of bundle activator instances. These bundle activator instances provide a convenient way for host applications to interact with the Felix framework. The ability offered by these activators can also be accomplished by invoking <tt>init()</tt> on the framework instance and the using <tt>getBundleContext()</tt> to get the System Bundle's context, but it can be more convenient to use an activator instance.</p>
Karl Pauls3d699792008-11-06 16:22:54 +0000536
537<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>
538
Karl Pauls3992b552009-09-06 22:21:18 +0000539<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
540<pre class="code-java">
541<span class="code-keyword">public</span> class HostActivator <span class="code-keyword">implements</span> BundleActivator
Karl Pauls3d699792008-11-06 16:22:54 +0000542{
543 <span class="code-keyword">private</span> BundleContext m_context = <span class="code-keyword">null</span>;
544
545 <span class="code-keyword">public</span> void start(BundleContext context)
546 {
547 m_context = context;
548 }
549
550 <span class="code-keyword">public</span> void stop(BundleContext context)
551 {
552 m_context = <span class="code-keyword">null</span>;
553 }
554
555 <span class="code-keyword">public</span> Bundle[] getBundles()
556 {
557 <span class="code-keyword">if</span> (m_context != <span class="code-keyword">null</span>)
558 {
559 <span class="code-keyword">return</span> m_context.getBundles();
560 }
561 <span class="code-keyword">return</span> <span class="code-keyword">null</span>;
562 }
Karl Pauls3992b552009-09-06 22:21:18 +0000563}
564
565</pre>
Karl Pauls3d699792008-11-06 16:22:54 +0000566</div></div>
567
Karl Pauls3992b552009-09-06 22:21:18 +0000568<p>Given the above bundle activator, it is now possible to embed Felix into a host application and interact with it as the following snippet illustrates:</p>
Karl Pauls3d699792008-11-06 16:22:54 +0000569
Karl Pauls3992b552009-09-06 22:21:18 +0000570<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
571<pre class="code-java">
572<span class="code-keyword">public</span> class HostApplication
Karl Pauls3d699792008-11-06 16:22:54 +0000573{
574 <span class="code-keyword">private</span> HostActivator m_activator = <span class="code-keyword">null</span>;
575 <span class="code-keyword">private</span> Felix m_felix = <span class="code-keyword">null</span>;
576
577 <span class="code-keyword">public</span> HostApplication()
578 {
579 <span class="code-comment">// Create a configuration property map.
Karl Pauls3992b552009-09-06 22:21:18 +0000580
Karl Pauls3d699792008-11-06 16:22:54 +0000581</span> Map configMap = <span class="code-keyword">new</span> HashMap();
582 <span class="code-comment">// Create host activator;
583</span> m_activator = <span class="code-keyword">new</span> HostActivator();
584 List list = <span class="code-keyword">new</span> ArrayList();
585 list.add(m_activator);
586 configMap.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);
587
588 <span class="code-keyword">try</span>
589 {
590 <span class="code-comment">// Now create an instance of the framework with
Karl Pauls3992b552009-09-06 22:21:18 +0000591
Karl Pauls3d699792008-11-06 16:22:54 +0000592</span> <span class="code-comment">// our configuration properties.
593</span> m_felix = <span class="code-keyword">new</span> Felix(configMap);
594 <span class="code-comment">// Now start Felix instance.
595</span> m_felix.start();
596 }
597 <span class="code-keyword">catch</span> (Exception ex)
598 {
599 <span class="code-object">System</span>.err.println(<span class="code-quote">"Could not create framework: "</span> + ex);
600 ex.printStackTrace();
601 }
602 }
603
604 <span class="code-keyword">public</span> Bundle[] getInstalledBundles()
605 {
606 <span class="code-comment">// Use the system bundle activator to gain external
Karl Pauls3992b552009-09-06 22:21:18 +0000607
Karl Pauls3d699792008-11-06 16:22:54 +0000608</span> <span class="code-comment">// access to the set of installed bundles.
609</span> <span class="code-keyword">return</span> m_activator.getBundles();
610 }
611
612 <span class="code-keyword">public</span> void shutdownApplication()
613 {
614 <span class="code-comment">// Shut down the felix framework when stopping the
615</span> <span class="code-comment">// host application.
616</span> m_felix.stop();
617 m_felix.waitForStop();
618 }
Karl Pauls3992b552009-09-06 22:21:18 +0000619}
620</pre>
Karl Pauls3d699792008-11-06 16:22:54 +0000621</div></div>
622
Karl Pauls3992b552009-09-06 22:21:18 +0000623<p>Notice how the <tt>HostApplication.getInstalledBundles()</tt> method uses its activator instance to get access to the System Bundle's context in order to interact with the embedded Felix framework instance. This approach provides the foundation for all interaction between the host application and the embedded framework instance.</p>
Karl Pauls3d699792008-11-06 16:22:54 +0000624
625<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-hostservices"></a></p>
626
627<h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-ProvidingHostApplicationServices"></a>Providing Host Application Services</h2>
628
Karl Pauls3992b552009-09-06 22:21:18 +0000629<p>Providing services from the host application to bundles inside the embedded Felix framework instance follows the basic approach laid out in <a href="#ApacheFelixFrameworkLaunchingandEmbedding-hostinteraction">above</a>. The main complication for providing a host application service to bundles is the fact that both the host application and the bundles must be using the same class definitions for the service interface classes. Since the host application cannot import classes from a bundle, this means that the service interface classes <b>must</b> be accessible on the class path, typically as part of the host application itself. The host application then must export the service interface package via the system bundle so that bundles installed into the embedded framework instance can import it. This is achieved using the <tt>org.osgi.framework.system.packages.extra</tt> configuration property previously presented.</p>
Karl Pauls3d699792008-11-06 16:22:54 +0000630
631<p>Consider the follow simple property lookup service:</p>
632
Karl Pauls3992b552009-09-06 22:21:18 +0000633<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
634<pre class="code-java">
635<span class="code-keyword">package</span> host.service.lookup;
Karl Pauls3d699792008-11-06 16:22:54 +0000636
637<span class="code-keyword">public</span> class Lookup
638{
639 <span class="code-keyword">public</span> <span class="code-object">Object</span> lookup(<span class="code-object">String</span> name);
Karl Pauls3992b552009-09-06 22:21:18 +0000640}
641
642</pre>
Karl Pauls3d699792008-11-06 16:22:54 +0000643</div></div>
644
Karl Pauls3992b552009-09-06 22:21:18 +0000645<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>" command. Now consider the following host application bundle activator, which will be used to register/unregister the property lookup service when the embedded framework instance starts/stops:</p>
Karl Pauls3d699792008-11-06 16:22:54 +0000646
Karl Pauls3992b552009-09-06 22:21:18 +0000647<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
648<pre class="code-java">
649<span class="code-keyword">package</span> host.core;
Karl Pauls3d699792008-11-06 16:22:54 +0000650
651<span class="code-keyword">import</span> java.util.Map;
652<span class="code-keyword">import</span> org.osgi.framework.BundleActivator;
Karl Pauls3992b552009-09-06 22:21:18 +0000653
Karl Pauls3d699792008-11-06 16:22:54 +0000654<span class="code-keyword">import</span> org.osgi.framework.BundleContext;
655<span class="code-keyword">import</span> org.osgi.framework.ServiceRegistration;
656<span class="code-keyword">import</span> host.service.lookup;
657
658<span class="code-keyword">public</span> class HostActivator <span class="code-keyword">implements</span> BundleActivator
659{
660 <span class="code-keyword">private</span> Map m_lookupMap = <span class="code-keyword">null</span>;
661 <span class="code-keyword">private</span> BundleContext m_context = <span class="code-keyword">null</span>;
662 <span class="code-keyword">private</span> ServiceRegistration m_registration = <span class="code-keyword">null</span>;
663
664 <span class="code-keyword">public</span> HostActivator(Map lookupMap)
665 {
666 <span class="code-comment">// Save a reference to the service's backing store.
Karl Pauls3992b552009-09-06 22:21:18 +0000667
Karl Pauls3d699792008-11-06 16:22:54 +0000668</span> m_lookupMap = lookupMap;
669 }
670
671 <span class="code-keyword">public</span> void start(BundleContext context)
672 {
673 <span class="code-comment">// Save a reference to the bundle context.
674</span> m_context = context;
675 <span class="code-comment">// Create a property lookup service implementation.
676</span> Lookup lookup = <span class="code-keyword">new</span> Lookup() {
677 <span class="code-keyword">public</span> <span class="code-object">Object</span> lookup(<span class="code-object">String</span> name)
678 {
679 <span class="code-keyword">return</span> m_lookupMap.get(name);
680 }
681 };
682 <span class="code-comment">// Register the property lookup service and save
Karl Pauls3992b552009-09-06 22:21:18 +0000683
Karl Pauls3d699792008-11-06 16:22:54 +0000684</span> <span class="code-comment">// the service registration.
685</span> m_registration = m_context.registerService(
686 Lookup.class.getName(), lookup, <span class="code-keyword">null</span>);
687 }
688
689 <span class="code-keyword">public</span> void stop(BundleContext context)
690 {
691 <span class="code-comment">// Unregister the property lookup service.
692</span> m_registration.unregister();
693 m_context = <span class="code-keyword">null</span>;
694 }
Karl Pauls3992b552009-09-06 22:21:18 +0000695}
696</pre>
Karl Pauls3d699792008-11-06 16:22:54 +0000697</div></div>
698
Karl Pauls3992b552009-09-06 22:21:18 +0000699<p>Given the above host application bundle activator, the following code snippet shows how the host application could create an embedded version of the Felix framework and provide the property lookup service to installed bundles:</p>
Karl Pauls3d699792008-11-06 16:22:54 +0000700
Karl Pauls3992b552009-09-06 22:21:18 +0000701<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
702<pre class="code-java">
703<span class="code-keyword">package</span> host.core;
Karl Pauls3d699792008-11-06 16:22:54 +0000704
705<span class="code-keyword">import</span> java.util.List;
706<span class="code-keyword">import</span> java.util.ArrayList;
707<span class="code-keyword">import</span> java.util.Map;
Karl Pauls3992b552009-09-06 22:21:18 +0000708
Karl Pauls3d699792008-11-06 16:22:54 +0000709<span class="code-keyword">import</span> java.util.HashMap;
710<span class="code-keyword">import</span> host.service.lookup.Lookup;
711<span class="code-keyword">import</span> org.apache.felix.framework.Felix;
712<span class="code-keyword">import</span> org.apache.felix.framework.util.FelixConstants;
713<span class="code-keyword">import</span> org.osgi.framework.Constants;
714
715<span class="code-keyword">public</span> class HostApplication
716{
717 <span class="code-keyword">private</span> HostActivator m_activator = <span class="code-keyword">null</span>;
718 <span class="code-keyword">private</span> Felix m_felix = <span class="code-keyword">null</span>;
719 <span class="code-keyword">private</span> Map m_lookupMap = <span class="code-keyword">new</span> HashMap();
720
721 <span class="code-keyword">public</span> HostApplication()
722 {
723 <span class="code-comment">// Initialize the map <span class="code-keyword">for</span> the property lookup service.
Karl Pauls3992b552009-09-06 22:21:18 +0000724
Karl Pauls3d699792008-11-06 16:22:54 +0000725</span> m_lookupMap.put(<span class="code-quote">"name1"</span>, <span class="code-quote">"value1"</span>);
726
727 m_lookupMap.put(<span class="code-quote">"name2"</span>, <span class="code-quote">"value2"</span>);
728 m_lookupMap.put(<span class="code-quote">"name3"</span>, <span class="code-quote">"value3"</span>);
729 m_lookupMap.put(<span class="code-quote">"name4"</span>, <span class="code-quote">"value4"</span>);
730
731 <span class="code-comment">// Create a configuration property map.
Karl Pauls3992b552009-09-06 22:21:18 +0000732
Karl Pauls3d699792008-11-06 16:22:54 +0000733</span> Map configMap = <span class="code-keyword">new</span> HashMap();
734 <span class="code-comment">// Export the host provided service <span class="code-keyword">interface</span> <span class="code-keyword">package</span>.
735</span> configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA,
736 <span class="code-quote">"host.service.lookup; version=1.0.0"</span>);
737 <span class="code-comment">// Create host activator;
738</span> m_activator = <span class="code-keyword">new</span> HostActivator(m_lookupMap);
739 List list = <span class="code-keyword">new</span> ArrayList();
740 list.add(m_activator);
741 configMap.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);
742
743 <span class="code-keyword">try</span>
Karl Pauls3992b552009-09-06 22:21:18 +0000744
Karl Pauls3d699792008-11-06 16:22:54 +0000745 {
746 <span class="code-comment">// Now create an instance of the framework with
747</span> <span class="code-comment">// our configuration properties.
748</span> m_felix = <span class="code-keyword">new</span> Felix(configMap);
749 <span class="code-comment">// Now start Felix instance.
750</span> m_felix.start();
751 }
752 <span class="code-keyword">catch</span> (Exception ex)
753 {
754 <span class="code-object">System</span>.err.println(<span class="code-quote">"Could not create framework: "</span> + ex);
755 ex.printStackTrace();
756 }
757 }
758
759 <span class="code-keyword">public</span> void shutdownApplication()
760 {
761 <span class="code-comment">// Shut down the felix framework when stopping the
Karl Pauls3992b552009-09-06 22:21:18 +0000762
Karl Pauls3d699792008-11-06 16:22:54 +0000763</span> <span class="code-comment">// host application.
764</span> m_felix.stop();
765 m_felix.waitForStop();
766 }
Karl Pauls3992b552009-09-06 22:21:18 +0000767}
768</pre>
Karl Pauls3d699792008-11-06 16:22:54 +0000769</div></div>
770
Karl Pauls3992b552009-09-06 22:21:18 +0000771<p>Rather than having the host application bundle activator register the service, it is also possible for the the host application to simply get the bundle context from the bundle activator and register the service directly, but the presented approach is perhaps a little cleaner since it allows the host application to register/unregister the service when the system bundle starts/stops.</p>
Karl Pauls3d699792008-11-06 16:22:54 +0000772
773<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-hostserviceusage"></a></p>
774
775<h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-UsingServicesProvidedbyBundles"></a>Using Services Provided by Bundles</h2>
776
Karl Pauls3992b552009-09-06 22:21:18 +0000777<p>Using services provided by bundles follows the same general approach of using a host application bundle activator. The main complication for the host application using a service from a bundle is the fact that both the host application and the bundle must be using the same class definitions for the service interface classes. Since the host application cannot import classes from a bundle, this means that the service interface classes <b>must</b> be accessible on the class path, typically as part of the host application itself. The host application then must export the service interface package via the system bundle so that bundles installed into the embedded framework instance can import it. This is achieved using the <tt>org.osgi.framework.system.packages.extra</tt> configuration property previously presented.</p>
Karl Pauls3d699792008-11-06 16:22:54 +0000778
Karl Pauls3992b552009-09-06 22:21:18 +0000779<p>Consider the following simple command service interface for which bundles provide implementations, such as might be used to create an extensible interactive shell:</p>
Karl Pauls3d699792008-11-06 16:22:54 +0000780
Karl Pauls3992b552009-09-06 22:21:18 +0000781<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
782<pre class="code-java">
783<span class="code-keyword">package</span> host.service.command;
Karl Pauls3d699792008-11-06 16:22:54 +0000784
785<span class="code-keyword">public</span> class Command
786{
787 <span class="code-keyword">public</span> <span class="code-object">String</span> getName();
788 <span class="code-keyword">public</span> <span class="code-object">String</span> getDescription();
789 <span class="code-keyword">public</span> <span class="code-object">boolean</span> execute(<span class="code-object">String</span> commandline);
Karl Pauls3992b552009-09-06 22:21:18 +0000790}
791
792</pre>
Karl Pauls3d699792008-11-06 16:22:54 +0000793</div></div>
794
Karl Pauls3992b552009-09-06 22:21:18 +0000795<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>" command. Now consider the previously introduced host application bundle activator below, which simply provides access to the system bundle context:</p>
Karl Pauls3d699792008-11-06 16:22:54 +0000796
Karl Pauls3992b552009-09-06 22:21:18 +0000797<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
798<pre class="code-java">
799<span class="code-keyword">package</span> host.core;
Karl Pauls3d699792008-11-06 16:22:54 +0000800
801<span class="code-keyword">import</span> org.osgi.framework.BundleActivator;
802<span class="code-keyword">import</span> org.osgi.framework.BundleContext;
803
Karl Pauls3992b552009-09-06 22:21:18 +0000804
Karl Pauls3d699792008-11-06 16:22:54 +0000805<span class="code-keyword">public</span> class HostActivator <span class="code-keyword">implements</span> BundleActivator
806{
807 <span class="code-keyword">private</span> BundleContext m_context = <span class="code-keyword">null</span>;
808
809 <span class="code-keyword">public</span> void start(BundleContext context)
810 {
811 m_context = context;
812 }
813
814 <span class="code-keyword">public</span> void stop(BundleContext context)
815 {
816 m_context = <span class="code-keyword">null</span>;
817 }
818
819 <span class="code-keyword">public</span> BundleContext getContext()
820 {
821 <span class="code-keyword">return</span> m_context;
822 }
Karl Pauls3992b552009-09-06 22:21:18 +0000823}
824
825</pre>
Karl Pauls3d699792008-11-06 16:22:54 +0000826</div></div>
827
Karl Pauls3992b552009-09-06 22:21:18 +0000828<p>With this bundle activator, the host application can use command services provided by bundles installed inside its embedded Felix framework instance. The following code snippet illustrates one possible approach:</p>
Karl Pauls3d699792008-11-06 16:22:54 +0000829
Karl Pauls3992b552009-09-06 22:21:18 +0000830<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
831<pre class="code-java">
832<span class="code-keyword">package</span> host.core;
Karl Pauls3d699792008-11-06 16:22:54 +0000833
834<span class="code-keyword">import</span> java.util.List;
835<span class="code-keyword">import</span> java.util.ArrayList;
Karl Pauls3992b552009-09-06 22:21:18 +0000836
Karl Pauls3d699792008-11-06 16:22:54 +0000837<span class="code-keyword">import</span> java.util.Map;
838<span class="code-keyword">import</span> host.service.command.Command;
839<span class="code-keyword">import</span> org.apache.felix.framework.Felix;
840<span class="code-keyword">import</span> org.apache.felix.framework.util.FelixConstants;
841<span class="code-keyword">import</span> org.apache.felix.framework.cache.BundleCache;
842<span class="code-keyword">import</span> org.osgi.framework.Constants;
Karl Pauls3992b552009-09-06 22:21:18 +0000843
Karl Pauls3d699792008-11-06 16:22:54 +0000844<span class="code-keyword">import</span> org.osgi.util.tracker.ServiceTracker;
845
846<span class="code-keyword">public</span> class HostApplication
847{
848 <span class="code-keyword">private</span> HostActivator m_activator = <span class="code-keyword">null</span>;
849 <span class="code-keyword">private</span> Felix m_felix = <span class="code-keyword">null</span>;
850 <span class="code-keyword">private</span> ServiceTracker m_tracker = <span class="code-keyword">null</span>;
851
852 <span class="code-keyword">public</span> HostApplication()
853 {
854 <span class="code-comment">// Create a configuration property map.
Karl Pauls3992b552009-09-06 22:21:18 +0000855
Karl Pauls3d699792008-11-06 16:22:54 +0000856</span> Map configMap = <span class="code-keyword">new</span> HashMap();
857 <span class="code-comment">// Export the host provided service <span class="code-keyword">interface</span> <span class="code-keyword">package</span>.
858</span> configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA,
859 <span class="code-quote">"host.service.command; version=1.0.0"</span>);
860 <span class="code-comment">// Create host activator;
861</span> m_activator = <span class="code-keyword">new</span> HostActivator();
862 List list = <span class="code-keyword">new</span> ArrayList();
863 list.add(m_activator);
864 configMap.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);
865
866 <span class="code-keyword">try</span>
Karl Pauls3992b552009-09-06 22:21:18 +0000867
Karl Pauls3d699792008-11-06 16:22:54 +0000868 {
869 <span class="code-comment">// Now create an instance of the framework with
870</span> <span class="code-comment">// our configuration properties.
871</span> m_felix = <span class="code-keyword">new</span> Felix(configMap);
872 <span class="code-comment">// Now start Felix instance.
873</span> m_felix.start();
874 }
875 <span class="code-keyword">catch</span> (Exception ex)
876 {
877 <span class="code-object">System</span>.err.println(<span class="code-quote">"Could not create framework: "</span> + ex);
878 ex.printStackTrace();
879 }
880
881 m_tracker = <span class="code-keyword">new</span> ServiceTracker(
882 m_activator.getContext(), Command.class.getName(), <span class="code-keyword">null</span>);
883 m_tracker.open();
884 }
885
886 <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)
887 {
888 <span class="code-comment">// See <span class="code-keyword">if</span> any of the currently tracked command services
Karl Pauls3992b552009-09-06 22:21:18 +0000889
Karl Pauls3d699792008-11-06 16:22:54 +0000890</span> <span class="code-comment">// match the specified command name, <span class="code-keyword">if</span> so then execute it.
891</span> <span class="code-object">Object</span>[] services = m_tracker.getServices();
892 <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++)
893 {
894 <span class="code-keyword">try</span>
Karl Pauls3992b552009-09-06 22:21:18 +0000895
Karl Pauls3d699792008-11-06 16:22:54 +0000896 {
897 <span class="code-keyword">if</span> (((Command) services[i]).getName().equals(name))
898 {
899 <span class="code-keyword">return</span> ((Command) services[i]).execute(commandline);
900 }
901 }
902 <span class="code-keyword">catch</span> (Exception ex)
903 {
904 <span class="code-comment">// Since the services returned by the tracker could become
905</span> <span class="code-comment">// invalid at any moment, we will <span class="code-keyword">catch</span> all exceptions, log
Karl Pauls3992b552009-09-06 22:21:18 +0000906
Karl Pauls3d699792008-11-06 16:22:54 +0000907</span> <span class="code-comment">// a message, and then ignore faulty services.
908</span> <span class="code-object">System</span>.err.println(ex);
909 }
910 }
911 <span class="code-keyword">return</span> <span class="code-keyword">false</span>;
912 }
913
914 <span class="code-keyword">public</span> void shutdownApplication()
915 {
916 {
917 <span class="code-comment">// Shut down the felix framework when stopping the
918</span> <span class="code-comment">// host application.
919</span> m_felix.stop();
920 m_felix.waitForStop();
921 }
Karl Pauls3992b552009-09-06 22:21:18 +0000922}
923
924</pre>
Karl Pauls3d699792008-11-06 16:22:54 +0000925</div></div>
926
Karl Pauls3992b552009-09-06 22:21:18 +0000927<p>The above example is overly simplistic with respect to concurrency issues and error conditions, but it demonstrates the overall approach for using bundle-provided services from the host application.</p>
Karl Pauls3d699792008-11-06 16:22:54 +0000928
929<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-servicereflection"></a></p>
930
931<h3><a name="ApacheFelixFrameworkLaunchingandEmbedding-UsingBundleServicesviaReflection"></a>Using Bundle Services via Reflection</h3>
932
Karl Pauls3992b552009-09-06 22:21:18 +0000933<p>It possible for the host application to use services provided by bundles without having access to the service interface classes and thus not needing to put the service interface classes on the class path. To do this, the host application uses the same general approach to acquire the system bundle context object, which it can use to look up service objects. Using either an LDAP filter or the service interface class name, the host application can retrieve the service object and then use standard Java reflection to invoke methods on the service object.</p>
Karl Pauls3d699792008-11-06 16:22:54 +0000934
935<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-serviceother"></a></p>
936
937<h3><a name="ApacheFelixFrameworkLaunchingandEmbedding-OtherApproaches"></a>Other Approaches</h3>
938
Karl Pauls3992b552009-09-06 22:21:18 +0000939<p>The <a href="http://code.google.com/p/transloader/" 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 Pauls3d699792008-11-06 16:22:54 +0000940
941<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-caveat"></a></p>
942
943<h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-Caveat"></a>Caveat</h1>
944
Karl Pauls3992b552009-09-06 22:21:18 +0000945<p>The code in this document has not been thoroughly tested nor even compiled and may be out of date with respect to the current Felix source code. If you find errors please report them so the that they can be corrected.</p>
Karl Pauls3d699792008-11-06 16:22:54 +0000946
947<p><a name="ApacheFelixFrameworkLaunchingandEmbedding-feedback"></a></p>
948
949<h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-Feedback"></a>Feedback</h2>
950
Karl Pauls3992b552009-09-06 22:21:18 +0000951<p>Subscribe to the Felix users mailing list by sending a message to <a href="mailto:users-subscribe@felix.apache.org" rel="nofollow">users-subscribe@felix.apache.org</a>; after subscribing, email questions or feedback to <a href="mailto:users@felix.apache.org" rel="nofollow">users@felix.apache.org</a>.</p>
952
Karl Pauls3d699792008-11-06 16:22:54 +0000953 </div>
Karl Pauls3992b552009-09-06 22:21:18 +0000954 </body></html>