Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 2 | <html><head> |
| 3 | |
| 4 | |
| 5 | |
| 6 | <title>Apache Felix - Apache Felix Framework Launching and Embedding</title> |
| 7 | <link rel="stylesheet" href="apache-felix-framework-launching-and-embedding_files/site.css" type="text/css" media="all"> |
| 8 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| 9 | </head><body> |
Karl Pauls | 6721c02 | 2008-12-20 17:15:21 +0000 | [diff] [blame] | 10 | <div class="title"><div class="logo"><a href="http://felix.apache.org/site/index.html"><img alt="Apache Felix" src="apache-felix-framework-launching-and-embedding_files/logo.png" border="0"></a></div><div class="header"><a href="http://www.apache.org/"><img alt="Apache" src="apache-felix-framework-launching-and-embedding_files/apache.png" border="0"></a></div></div> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 11 | <div class="menu"> |
| 12 | <ul> |
| 13 | <li><a href="http://felix.apache.org/site/news.html" title="news">news</a></li> |
| 14 | <li><a href="http://felix.apache.org/site/license.html" title="license">license</a></li> |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 15 | <li><a href="http://felix.apache.org/site/downloads.cgi" class="external-link" rel="nofollow">downloads</a></li> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 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> |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 19 | <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 Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 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 --> |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 26 | </p></li></ul> |
| 27 | </div> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 28 | <div class="main"> |
| 29 | <h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-ApacheFelixFrameworkLaunchingandEmbedding"></a>Apache Felix Framework Launching and Embedding</h1> |
| 30 | |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 31 | <p><em>[This document describes framework launching introduced in Felix |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 32 | Framework 2.0.0 and continuing with the latest releases; it is |
| 33 | incompatible with older versions of the Felix framework.]</em></p> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 34 | |
| 35 | <ul> |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 36 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-introduction">Introduction</a></li> |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 37 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-overview">OSGi Launching and Embedding API Overview</a> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 38 | <ul> |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 39 | <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 Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 42 | </ul> |
| 43 | </li> |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 44 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-launching">Launching Felix</a> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 45 | <ul> |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 46 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-standardlauncher">Standard Felix Launcher</a></li> |
| 47 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-customlauncher">Custom Felix Launcher</a></li> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 48 | </ul> |
| 49 | </li> |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 50 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-embedding">Embedding Felix</a> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 51 | <ul> |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 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 Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 55 | <ul> |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 56 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-servicereflection">Using Bundle Services via Reflection</a></li> |
| 57 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-serviceother">Other Approaches</a></li> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 58 | </ul> |
| 59 | </li> |
| 60 | </ul> |
| 61 | </li> |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 62 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-caveat">Caveat</a></li> |
| 63 | <li><a href="#ApacheFelixFrameworkLaunchingandEmbedding-feedback">Feedback</a></li> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 64 | </ul> |
| 65 | |
| 66 | |
| 67 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-introduction"></a></p> |
| 68 | |
| 69 | <h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-Introduction"></a>Introduction</h1> |
| 70 | |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 71 | <p>The Apache Felix Framework is intended to be easily launchable and |
| 72 | embeddable. For example, the Felix framework implementation avoids the |
| 73 | use of system properties for configuration, since these are globals and |
| 74 | can cause interference if multiple framework instances are created in |
| 75 | the same VM. The framework also tries to multiplex singleton |
| 76 | facilities, like the URL stream handler factory. The goal is to make it |
| 77 | possible to use the framework in a variety of scenarios; however, this |
| 78 | is still just a goal. In other words, this is a work in progress and if |
| 79 | any issues arise, it would be greatly appreciated if they are brought |
| 80 | to the attention of the Felix community. The next section provides an |
| 81 | overview of the standard OSGi launching and embedding API for |
| 82 | frameworks, while the remainder of the document is divided into two |
| 83 | sections, one focusing on how to launch Felix and one focusing on how |
| 84 | to embed Felix into a host application.</p> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 85 | |
| 86 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-overview"></a></p> |
| 87 | |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 88 | <h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-OSGiLaunchingandEmbeddingAPIOverview"></a>OSGi Launching and Embedding API Overview</h1> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 89 | |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 90 | <p>The Felix framework is implemented by the <tt>org.apache.felix.framework.Felix</tt> class or just <tt>Felix</tt> |
| 91 | for short. As part of the R4.2 OSGi specification, the launching and |
| 92 | embedding API of the OSGi framework has been standardized. The approach |
| 93 | 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 Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 94 | |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 95 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 96 | <pre class="code-java"><span class="code-keyword">public</span> <span class="code-keyword">interface</span> Bundle |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 97 | { |
| 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 Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 113 | Version getVersion(); |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 114 | <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 Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 121 | } |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 122 | </pre> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 123 | </div></div> |
| 124 | |
| 125 | <p>The <tt>Framework</tt> interface is defined as:</p> |
| 126 | |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 127 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 128 | <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 Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 129 | { |
| 130 | void init(); |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 131 | FrameworkEvent waitForStop(<span class="code-object">long</span> timeout); |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 132 | } |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 133 | </pre> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 134 | </div></div> |
| 135 | |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 136 | <p>To actually construct a framework instance, the R4.2 specification defines the <tt>FrameworkFactory</tt> interface:</p> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 137 | |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 138 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 139 | <pre class="code-java"><span class="code-keyword">public</span> <span class="code-keyword">interface</span> FrameworkFactory |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 140 | { |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 141 | Framework newFramework(Map config); |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 142 | } |
| 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 Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 147 | |
| 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 Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 152 | <p>You use the framework factory to construct and configure a framework |
| 153 | instance (or by directly instantiating the Felix class). The |
| 154 | configuration map may contain the following OSGi standard properties:</p> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 155 | |
| 156 | <ul> |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 157 | <li><tt>org.osgi.framework.system.packages</tt> - specifies a |
| 158 | list of packages the system bundle should export from the environment; |
| 159 | if 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 |
| 162 | export from the environment that are appended to the packages specified |
| 163 | in <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 |
| 166 | to all bundles from the environment (i.e., no need to import them); |
| 167 | there is no default value for this property and its use should be |
| 168 | avoided.</li> |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 169 | <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 |
| 172 | not exist, to use for bundle cache storage; the default value for this |
| 173 | property 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 Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 175 | <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 Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 176 | </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 Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 183 | <li><tt>felix.systembundle.activators</tt> - specifies a <tt>List</tt> of <tt>BundleActivator</tt> |
| 184 | instances that are started/stopped when the System Bundle is |
| 185 | started/stopped; the specified instances will receive the System |
| 186 | Bundle's <tt>BundleContext</tt> when invoked.</li> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 187 | <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 Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 188 | <li><tt>felix.log.level</tt> - specifies an integer <tt>String</tt> |
| 189 | whose value indicates the degree of logging reported by the framework; |
| 190 | the default value is "1" and "0" turns off logging completely, |
| 191 | otherwise log levels match those specified in the OSGi Log Service |
| 192 | (i.e., 1 = error, 2 = warning, 3 = information, and 4 = debug).</li> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 193 | <li><tt>felix.startlevel.bundle</tt> - specifies the start level for newly installed bundles; the default value is 1.</li> |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 194 | <li><tt>felix.bootdelegation.implicit</tt> |
| 195 | - specifies whether or not the framework should try to guess when to |
| 196 | boot delegate when external code tries to load classes or resources; |
| 197 | the default value is "<tt>true</tt>".</li> |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 198 | <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 Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 199 | </ul> |
| 200 | |
| 201 | |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 202 | <p>The configuration map is copied and the keys are treated as case |
| 203 | insensitive. You are not able to change the framework's configuration |
| 204 | after construction. If you need a different configuration, you must |
| 205 | create a new framework instance.</p> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 206 | |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 207 | <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 Pauls | 6721c02 | 2008-12-20 17:15:21 +0000 | [diff] [blame] | 208 | |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 209 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-startinginstance"></a></p> |
| 210 | |
| 211 | <h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-StartingtheFrameworkInstance"></a>Starting the Framework Instance</h2> |
| 212 | |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 213 | <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 Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 214 | |
| 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 Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 221 | <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 Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 222 | |
| 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 Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 244 | <h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-LaunchingaFramework"></a>Launching a Framework</h1> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 245 | |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 246 | <p>Launching a framework is fairly simple and involves only four steps:</p> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 247 | |
| 248 | <ol> |
| 249 | <li>Define some configuration properties.</li> |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 250 | <li>Obtain framework factory.</li> |
| 251 | <li>Use factory to create framework with the configuration properties.</li> |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 252 | <li>Invoke the <tt>Framework.start()</tt> method.</li> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 253 | </ol> |
| 254 | |
| 255 | |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 256 | <p>In reality, the first step is optional, since all properties will |
| 257 | have reasonable defaults, but if you are creating a launcher you will |
| 258 | generally want to more than that, such as automatically installing and |
| 259 | starting bundles when you start the framework instance. The default |
| 260 | Felix launcher defines reusable functionality to automatically install |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 261 | and/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 Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 262 | |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 263 | <p>The remainder of this section describes how the standard Felix launcher works as well as how to create a custom launcher.</p> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 264 | |
| 265 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-standardlauncher"></a></p> |
| 266 | |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 267 | <h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-StandardFelixFrameworkLauncher"></a>Standard Felix Framework Launcher</h2> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 268 | |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 269 | <p>The standard Felix framework launcher is very simple and is not |
| 270 | intended to solve every possible requirement; it is intended to work |
| 271 | for most standard situations. Most special launching requirements |
| 272 | should be resolved by creating a custom launcher. This section |
| 273 | describes how the standard launcher works. The following code |
| 274 | represents the complete <tt>main()</tt> method of the standard launcher, each numbered comment will be described in more detail below:</p> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 275 | |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 276 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 277 | <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 Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 278 | { |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 279 | <span class="code-comment">// (1) Check <span class="code-keyword">for</span> command line arguments and verify usage. |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 280 | </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 < args.length; i++) |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 284 | { |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 285 | <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 Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 295 | { |
| 296 | cacheDir = args[i]; |
| 297 | } |
| 298 | } |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 299 | |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 300 | <span class="code-keyword">if</span> ((args.length > 3) || (expectBundleDir && bundleDir == <span class="code-keyword">null</span>)) |
| 301 | { |
| 302 | <span class="code-object">System</span>.out.println(<span class="code-quote">"Usage: [-b <bundle-deploy-dir>] [<bundle-cache-dir>]"</span>); |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 303 | <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 Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 311 | <span class="code-keyword">if</span> (configProps == <span class="code-keyword">null</span>) |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 312 | { |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 313 | <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 Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 317 | <span class="code-comment">// (4) Copy framework properties from the system properties. |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 318 | </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 Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 333 | </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 Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 336 | <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 Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 337 | <span class="code-keyword">public</span> void run() |
| 338 | { |
| 339 | <span class="code-keyword">try</span> |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 340 | { |
| 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 Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 354 | |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 355 | <span class="code-keyword">try</span> |
| 356 | { |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 357 | <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 Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 367 | </span> m_fwk.waitForStop(0); |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 368 | <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 Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 374 | <span class="code-object">System</span>.exit(0); |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 375 | } |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 376 | } |
| 377 | </pre> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 378 | </div></div> |
| 379 | |
| 380 | <p>The general steps of the standard launcher are quite straightforward:</p> |
| 381 | |
| 382 | <ol> |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 383 | <li>The launcher supports setting the auto-deploy directory (with the <tt>-b</tt> |
| 384 | switch) and setting the bundle cache path with a single argument, so |
| 385 | check for this and issue a usage message it there are more than one |
| 386 | arguments.</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> |
| 388 | system property. This file is not needed to launch Felix and is |
| 389 | provided merely for convenience when system properties must be |
| 390 | specified. The file is a standard Java properties file, but it also |
| 391 | supports property substitution using <tt>${<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 Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 393 | system property. This file is used to configure the framework instance |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 394 | created by the launcher. The file is a standard Java properties file, |
| 395 | but it also supports property substitution using "<tt>${<property-name></tt>}" |
| 396 | syntax. Property substitution can be nested; configuration and system |
| 397 | properties will be used for substitution with configuration properties |
| 398 | having precedence.</li> |
| 399 | <li>For convenience, any configuration |
| 400 | properties that are set as system properties are copied into the set of |
| 401 | configuration properties. This provide an easy way to add to or |
| 402 | override configuration properties specified in the <tt>config.properties</tt> file, since the Felix instance will never look at system properties for configuration.</li> |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 403 | <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 Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 405 | <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> |
| 408 | configuration properties during framework startup to automatically |
| 409 | install and/or start bundles; see the usage document for more |
| 410 | information <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 Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 411 | <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 Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 415 | <p>The framework is not active until the <tt>start()</tt> method is |
| 416 | called. If no shell bundles are installed and started or if there is |
| 417 | difficulty locating the shell bundles specified in the auto-start |
| 418 | property, then it will appear as if the framework is hung, but it is |
| 419 | actually running without any way to interact with it since the shell |
| 420 | bundles provide the only means of interaction.</p> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 421 | |
| 422 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-customlauncher"></a></p> |
| 423 | |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 424 | <h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-CustomFrameworkLauncher"></a>Custom Framework Launcher</h2> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 425 | |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 426 | <p>This section creates a bare-bones launcher to demonstrate the |
| 427 | minimum requirements for creating an interactive launcher for the Felix |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 428 | framework. This example uses the standard Gogo shell bundles for |
| 429 | interactivity, but any other bundles could be used instead. This |
| 430 | example launcher project has the following directory structure:</p> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 431 | |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 432 | <div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent"> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 433 | <pre>launcher/ |
| 434 | lib/ |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 435 | org.apache.felix.main-3.0.0.jar |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 436 | bundle/ |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 437 | 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 Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 440 | src/ |
| 441 | example/ |
| 442 | Main.java |
| 443 | </pre> |
| 444 | </div></div> |
| 445 | |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 446 | <p>The <tt>lib/</tt> directory contains Felix' main JAR file, which |
| 447 | also contains the OSGi core interfaces. The main JAR file is used so |
| 448 | that we can reuse the default launcher's auto-install/auto-start |
| 449 | configuration property handling; if these capabilities are not needed, |
| 450 | then it would be possible to use the framework JAR file instead of the |
| 451 | main JAR file. The <tt>bundle/</tt> directory contains the shell |
| 452 | service and textual shell interface bundles that will be used for |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 453 | interacting with the framework instance. Note: If you do not launch the |
| 454 | framework with interactive bundles, it will appear as if the framework |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 455 | instance is hung, but it is actually just sitting there waiting for |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 456 | 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 framework launcher.</p> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 457 | |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 458 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 459 | <pre class="code-java"><span class="code-keyword">package</span> example; |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 460 | |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 461 | <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 Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 464 | |
| 465 | <span class="code-keyword">public</span> class Main |
| 466 | { |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 467 | <span class="code-keyword">private</span> <span class="code-keyword">static</span> Framework m_fwk = <span class="code-keyword">null</span>; |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 468 | |
| 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 Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 472 | </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 Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 474 | |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 475 | <span class="code-keyword">try</span> |
| 476 | { |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 477 | 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 Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 482 | <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 Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 491 | |
| 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 Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 500 | { |
| 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() > 0) && (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 Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 512 | { |
| 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 Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 521 | </div></div> |
| 522 | |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 523 | <p>This launcher relies on the default behavior of <tt>AutoProcessor</tt> |
| 524 | to automatically deploy the shell bundles. This simple, generic |
| 525 | launcher provides a good starting point if the default Felix launcher |
| 526 | is not sufficient. Since very few configuration properties are |
| 527 | specified, the default values are used. For the bundle auto-deploy |
| 528 | 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 Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 529 | |
| 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 Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 532 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 533 | <pre class="code-java"> m_fwk = getFrameworkFactory().newFramework(<span class="code-keyword">null</span>); |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 534 | m_fwk.init() |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 535 | </pre> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 536 | </div></div> |
| 537 | |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 538 | <p>These steps get a the framework factory service and use it to create |
| 539 | a framework instance with a default configuration. Once the framework |
| 540 | instance is created, it is initialized with <tt>init()</tt>.</p> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 541 | |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 542 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 543 | <pre class="code-java"> AutoProcessor.process(<span class="code-keyword">null</span>, m_fwk.getBundleContext()); |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 544 | </pre> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 545 | </div></div> |
| 546 | |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 547 | <p>The <tt>AutorProcessor</tt> will automatically deploy bundles in the |
| 548 | auto-deploy directory and any referenced from the auto-install/start |
| 549 | properties. Since we are using an empty configuration, the auto-deploy |
| 550 | directory is the <tt>bundle</tt> directory in the current directory |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 551 | and there are no auto properties. Therefore, in this case, the shell |
| 552 | bundles will be installed.</p> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 553 | |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 554 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 555 | <pre class="code-java"> m_fwk.start(); |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 556 | m_fwk.waitForStop(); |
| 557 | <span class="code-object">System</span>.exit(0); |
| 558 | </pre> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 559 | </div></div> |
| 560 | |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 561 | <p>These final steps start the framework and cause the launching |
| 562 | application thread to wait for the framework to stop and when it does |
| 563 | the launching thread calls <tt>System.exit()</tt> to make sure the VM actually exits.</p> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 564 | |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 565 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 566 | <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 Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 567 | { |
| 568 | ... |
| 569 | } |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 570 | </pre> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 571 | </div></div> |
| 572 | |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 573 | <p>This method retrieves the framework factory service by doing a |
| 574 | META-INF/services resource lookup, which it can use to obtain the |
| 575 | concrete class name for the factory. If you are using Java 6, then you |
| 576 | can use the <tt>ServiceLoader</tt> API in the JRE to further simplify the factory service lookup.</p> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 577 | |
| 578 | <p>The following command compiles the launcher when run from the root directory of the launcher project:</p> |
| 579 | |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 580 | <div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent"> |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 581 | <pre>javac -d . -classpath lib/org.apache.felix.main-3.0.0.jar src/example/Main.java |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 582 | </pre> |
| 583 | </div></div> |
| 584 | |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 585 | <p>After executing this command, an <tt>example/</tt> directory is |
| 586 | created in the current directory, which contains the generated class |
| 587 | file. The following command executes the simple launcher when run from |
| 588 | the root directory of the launcher project:</p> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 589 | |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 590 | <div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent"> |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 591 | <pre>java -cp .:lib/org.apache.felix.main-3.0.0.jar example.Main |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 592 | </pre> |
| 593 | </div></div> |
| 594 | |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 595 | <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 Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 596 | |
| 597 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-embedding"></a></p> |
| 598 | |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 599 | <h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-EmbeddingtheFelixFramework"></a>Embedding the Felix Framework</h1> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 600 | |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 601 | <p>Embedding the Felix framework into a host application is a simple |
| 602 | way to provide a sophisticated extensibility mechanism (i.e., a plugin |
| 603 | system) to the host application. Embedding the Felix framework is very |
| 604 | similar to launching it as described above, the main difference is that |
| 605 | the host application typically wants to interact with the framework |
| 606 | instance and/or installed bundles/services from the outside. This is |
| 607 | fairly easy to achieve, but there are some subtle issues to understand. |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 608 | This section presents the mechanisms for embedding Felix into a host |
| 609 | application and the issues in doing so.</p> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 610 | |
| 611 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-hostinteraction"></a></p> |
| 612 | |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 613 | <h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-Host/FelixInteraction"></a>Host/Felix Interaction</h2> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 614 | |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 615 | <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 Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 616 | which is a list of bundle activator instances. These bundle activator |
| 617 | instances provide a convenient way for host applications to interact |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 618 | with the Felix framework.</p> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 619 | |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 620 | <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> |
| 621 | configuration property is specific to the Felix framework |
| 622 | implementation. If you want your code to work with other framework |
| 623 | implementations, 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 Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 626 | |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 627 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 628 | <pre class="code-java"><span class="code-keyword">public</span> class HostActivator <span class="code-keyword">implements</span> BundleActivator |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 629 | { |
| 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 Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 650 | } |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 651 | </pre> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 652 | </div></div> |
| 653 | |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 654 | <p>Given the above bundle activator, it is now possible to embed the |
| 655 | Felix framework into a host application and interact with it as the |
| 656 | following snippet illustrates:</p> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 657 | |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 658 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 659 | <pre class="code-java"><span class="code-keyword">public</span> class HostApplication |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 660 | { |
| 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 Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 667 | </span> Map config = <span class="code-keyword">new</span> HashMap(); |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 668 | <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 Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 678 | </span> m_felix = <span class="code-keyword">new</span> Felix(config); |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 679 | <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 Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 701 | m_felix.waitForStop(0); |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 702 | } |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 703 | } |
| 704 | </pre> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 705 | </div></div> |
| 706 | |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 707 | <p>Notice how the <tt>HostApplication.getInstalledBundles()</tt> method |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 708 | uses its activator instance to get access to the system bundle's |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 709 | context in order to interact with the embedded Felix framework |
| 710 | instance. This approach provides the foundation for all interaction |
| 711 | between the host application and the embedded framework instance.</p> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 712 | |
| 713 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-hostservices"></a></p> |
| 714 | |
| 715 | <h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-ProvidingHostApplicationServices"></a>Providing Host Application Services</h2> |
| 716 | |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 717 | <p>Providing services from the host application to bundles inside the |
| 718 | embedded Felix framework instance follows the basic approach laid out |
| 719 | in <a href="#ApacheFelixFrameworkLaunchingandEmbedding-hostinteraction">above</a>. |
| 720 | The main complication for providing a host application service to |
| 721 | bundles is the fact that both the host application and the bundles must |
| 722 | be using the same class definitions for the service interface classes. |
| 723 | Since the host application cannot import classes from a bundle, this |
| 724 | means that the service interface classes <b>must</b> be accessible on |
| 725 | the class path, typically as part of the host application itself. The |
| 726 | host application then must export the service interface package via the |
| 727 | system bundle so that bundles installed into the embedded framework |
| 728 | instance can import it. This is achieved using the <tt>org.osgi.framework.system.packages.extra</tt> configuration property previously presented.</p> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 729 | |
| 730 | <p>Consider the follow simple property lookup service:</p> |
| 731 | |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 732 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 733 | <pre class="code-java"><span class="code-keyword">package</span> host.service.lookup; |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 734 | |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 735 | <span class="code-keyword">public</span> <span class="code-keyword">interface</span> Lookup |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 736 | { |
| 737 | <span class="code-keyword">public</span> <span class="code-object">Object</span> lookup(<span class="code-object">String</span> name); |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 738 | } |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 739 | </pre> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 740 | </div></div> |
| 741 | |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 742 | <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>" |
| 743 | command. Now consider the following host application bundle activator, |
| 744 | which will be used to register/unregister the property lookup service |
| 745 | when the embedded framework instance starts/stops:</p> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 746 | |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 747 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 748 | <pre class="code-java"><span class="code-keyword">package</span> host.core; |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 749 | |
| 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 Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 791 | } |
| 792 | </pre> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 793 | </div></div> |
| 794 | |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 795 | <p>Given the above host application bundle activator, the following |
| 796 | code snippet shows how the host application could create an embedded |
| 797 | version of the Felix framework and provide the property lookup service |
| 798 | to installed bundles:</p> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 799 | |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 800 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 801 | <pre class="code-java"><span class="code-keyword">package</span> host.core; |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 802 | |
| 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 Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 858 | m_felix.waitForStop(0); |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 859 | } |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 860 | } |
| 861 | </pre> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 862 | </div></div> |
| 863 | |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 864 | <p>Rather than having the host application bundle activator register |
| 865 | the service, it is also possible for the the host application to simply |
| 866 | get the bundle context from the bundle activator and register the |
| 867 | service directly, but the presented approach is perhaps a little |
| 868 | cleaner since it allows the host application to register/unregister the |
| 869 | service when the system bundle starts/stops.</p> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 870 | |
| 871 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-hostserviceusage"></a></p> |
| 872 | |
| 873 | <h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-UsingServicesProvidedbyBundles"></a>Using Services Provided by Bundles</h2> |
| 874 | |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 875 | <p>Using services provided by bundles follows the same general approach |
| 876 | of using a host application bundle activator. The main complication for |
| 877 | the host application using a service from a bundle is the fact that |
| 878 | both the host application and the bundle must be using the same class |
| 879 | definitions for the service interface classes. Since the host |
| 880 | application cannot import classes from a bundle, this means that the |
| 881 | service interface classes <b>must</b> be accessible on the class path, |
| 882 | typically as part of the host application itself. The host application |
| 883 | then must export the service interface package via the system bundle so |
| 884 | that bundles installed into the embedded framework instance can import |
| 885 | it. This is achieved using the <tt>org.osgi.framework.system.packages.extra</tt> configuration property previously presented.</p> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 886 | |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 887 | <p>Consider the following simple command service interface for which |
| 888 | bundles provide implementations, such as might be used to create an |
| 889 | extensible interactive shell:</p> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 890 | |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 891 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 892 | <pre class="code-java"><span class="code-keyword">package</span> host.service.command; |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 893 | |
| 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 Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 899 | } |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 900 | </pre> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 901 | </div></div> |
| 902 | |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 903 | <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>" |
| 904 | command. Now consider the previously introduced host application bundle |
| 905 | activator below, which simply provides access to the system bundle |
| 906 | context:</p> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 907 | |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 908 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 909 | <pre class="code-java"><span class="code-keyword">package</span> host.core; |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 910 | |
| 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 Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 932 | } |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 933 | </pre> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 934 | </div></div> |
| 935 | |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 936 | <p>With this bundle activator, the host application can use command |
| 937 | services provided by bundles installed inside its embedded Felix |
| 938 | framework instance. The following code snippet illustrates one possible |
| 939 | approach:</p> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 940 | |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 941 | <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent"> |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 942 | <pre class="code-java"><span class="code-keyword">package</span> host.core; |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 943 | |
| 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>) && (i < 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 Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 1019 | <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 Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 1022 | m_felix.waitForStop(0); |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 1023 | } |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 1024 | } |
Karl Pauls | 3992b55 | 2009-09-06 22:21:18 +0000 | [diff] [blame] | 1025 | </pre> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 1026 | </div></div> |
| 1027 | |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 1028 | <p>The above example is overly simplistic with respect to concurrency |
| 1029 | issues and error conditions, but it demonstrates the overall approach |
| 1030 | for using bundle-provided services from the host application.</p> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 1031 | |
| 1032 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-servicereflection"></a></p> |
| 1033 | |
| 1034 | <h3><a name="ApacheFelixFrameworkLaunchingandEmbedding-UsingBundleServicesviaReflection"></a>Using Bundle Services via Reflection</h3> |
| 1035 | |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 1036 | <p>It possible for the host application to use services provided by |
| 1037 | bundles without having access to the service interface classes and thus |
| 1038 | not needing to put the service interface classes on the class path. To |
| 1039 | do this, the host application uses the same general approach to acquire |
| 1040 | the system bundle context object, which it can use to look up service |
| 1041 | objects. Using either an LDAP filter or the service interface class |
| 1042 | name, the host application can retrieve the service object and then use |
| 1043 | standard Java reflection to invoke methods on the service object.</p> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 1044 | |
| 1045 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-serviceother"></a></p> |
| 1046 | |
| 1047 | <h3><a name="ApacheFelixFrameworkLaunchingandEmbedding-OtherApproaches"></a>Other Approaches</h3> |
| 1048 | |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 1049 | <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 Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 1050 | |
| 1051 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-caveat"></a></p> |
| 1052 | |
| 1053 | <h1><a name="ApacheFelixFrameworkLaunchingandEmbedding-Caveat"></a>Caveat</h1> |
| 1054 | |
Karl Pauls | feff79c | 2009-10-11 20:18:06 +0000 | [diff] [blame] | 1055 | <p>The code in this document has not been thoroughly tested nor even |
| 1056 | compiled and may be out of date with respect to the current Felix |
| 1057 | source code. If you find errors please report them so the that they can |
| 1058 | be corrected.</p> |
Karl Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 1059 | |
| 1060 | <p><a name="ApacheFelixFrameworkLaunchingandEmbedding-feedback"></a></p> |
| 1061 | |
| 1062 | <h2><a name="ApacheFelixFrameworkLaunchingandEmbedding-Feedback"></a>Feedback</h2> |
| 1063 | |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 1064 | <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 Pauls | 3d69979 | 2008-11-06 16:22:54 +0000 | [diff] [blame] | 1065 | </div> |
Karl Pauls | 9edcb3a | 2010-06-06 21:44:50 +0000 | [diff] [blame^] | 1066 | </body></html> |