blob: 50d5ee620ce1e543ce48ddc84b4c45541f948174 [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html><head>
<title>Apache Felix - Providing OSGi services</title>
<link rel="stylesheet" href="providing-osgi-services_files/site.css" type="text/css" media="all">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head><body>
<div class="title"><div class="logo"><a href="http://felix.apache.org/site/index.html"><img alt="Apache Felix" src="providing-osgi-services_files/logo.png" border="0"></a></div><div class="header"><a href="http://www.apache.org/"><img alt="Apache" src="providing-osgi-services_files/apache.png" border="0"></a></div></div>
<div class="menu">
<ul>
<li><a href="http://felix.apache.org/site/news.html" title="news">news</a></li>
<li><a href="http://felix.apache.org/site/license.html" title="license">license</a></li>
<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="providing-osgi-services_files/linkext7.gif" alt="" align="absmiddle" border="0" width="7" height="7"></sup></a></span></li>
<li><a href="http://felix.apache.org/site/documentation.html" title="documentation">documentation</a></li>
<li><a href="http://felix.apache.org/site/mailinglists.html" title="mailinglists">mailing lists</a></li>
<li><a href="http://felix.apache.org/site/contributing.html" title="Contributing">contributing</a></li>
<li><span class="nobr"><a href="http://www.apache.org/" title="Visit page outside Confluence" rel="nofollow">asf<sup><img class="rendericon" src="providing-osgi-services_files/linkext7.gif" alt="" align="absmiddle" border="0" width="7" height="7"></sup></a></span></li>
<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="providing-osgi-services_files/linkext7.gif" alt="" align="absmiddle" border="0" width="7" height="7"></sup></a></span></li>
<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="providing-osgi-services_files/linkext7.gif" alt="" align="absmiddle" border="0" width="7" height="7"></sup></a></span>
<!-- ApacheCon Ad -->
<iframe src="providing-osgi-services_files/button.html" style="border-width: 0pt; float: left;" scrolling="no" width="135" frameborder="0" height="135"></iframe>
<p style="height: 100px;">
<!-- ApacheCon Ad -->
</p></li></ul> </div>
<div class="main">
<table class="sectionMacro" border="0" cellpadding="5" cellspacing="0" width="100%"><tbody><tr>
<td class="confluenceTd" valign="top" width="80%">
<h1><a name="ProvidingOSGiservices-ProvidingOSGiservices"></a>Providing OSGi services</h1>
<p>This handler allows publishing OSGi services. It manages:</p>
<ul>
<li>service publication</li>
<li>service object creation</li>
<li>service un-registration</li>
<li>configuration property propagation</li>
</ul>
<p><img src="providing-osgi-services_files/ps-simple.png" handler="" overview="" align="absmiddle" border="0"></p>
<h1><a name="ProvidingOSGiservices-Asimpleexample"></a><b>A simple example</b></h1>
<p>The following code snippet shows a simple class implementing the FooService interface:</p>
<div class="code"><div class="codeContent">
<pre class="code-java"><span class="code-keyword">public</span> class FooProviderType1 <span class="code-keyword">implements</span> FooService {
<span class="code-keyword">private</span> <span class="code-object">String</span> m_foo = <span class="code-quote">"foo"</span>;
<span class="code-keyword">public</span> void foo() {
<span class="code-object">System</span>.out.println(<span class="code-quote">"foo "</span> + m_foo);
}
}</pre>
</div></div>
<p>To provide a service, the implementation class <b>NEEDS</b> to implement the service interface. By the way, it guaranties that each methods of the service interface are implemented.</p>
<p>To provide the service, the component type needs to declare the providing:</p>
<div class="code"><div class="codeContent">
<pre class="code-xml"><span class="code-tag">&lt;component className=<span class="code-quote">"...FooProviderType1"</span>&gt;</span>
<span class="code-tag">&lt;provides/&gt;</span>
<span class="code-tag">&lt;/component&gt;</span></pre>
</div></div>
<p><img src="providing-osgi-services_files/ps-foo.png" align="absmiddle" border="0"></p>
<p>The &lt;provides/&gt; element suffice to declare that each instance
of this type will provide the FooService. Indeed, the provided
specification can be discovered by analyzing the implementation class.
By default, all implemented interface are published in the same service
registration. iPOJO looks down the entire inheritance tree.</p>
<h1><a name="ProvidingOSGiservices-ServicePublication"></a><b>Service Publication</b></h1>
<p>The provided service handler manages the service publication and providing. For each declared <em>provides</em>,
the handler register a service. The service is published as long as the
instance is valid. If the instance becomes invalid, the service is
removed from the service registry.</p>
<p>By default, it publishes all interfaces implemented by the
implementation class of the component class. It collects all
super-interfaces (interfaces implemented by implemented interfaces and
by the super class). However it is possible to set exposed
specifications with the <tt>specifications</tt> attribute to avoid to expose all collected interfaces.</p>
<p><em>Note</em>: In the 1.0.0 version, the <tt>specifications</tt> attribute was named <tt>interface</tt>.</p>
<p>The following xml snippet is equivalent to the previous example:</p>
<div class="code"><div class="codeContent">
<pre class="code-xml"><span class="code-tag">&lt;component className=<span class="code-quote">"...FooProviderType1"</span>&gt;</span>
<span class="code-tag">&lt;provides specifications=<span class="code-quote">"...FooService "</span>/&gt;</span>
<span class="code-tag">&lt;/component&gt;</span></pre>
</div></div>
<p>If the implementation class implements several interfaces, all these
interfaces are published by default in the same service publication.
You can use the <tt>specifications</tt> attribute to set published service interfaces. If you want to publish several interfaces, you can use the following syntax:</p>
<div class="code"><div class="codeContent">
<pre class="code-xml"><span class="code-tag">&lt;component className=<span class="code-quote">"...FooProviderType1"</span>&gt;</span>
<span class="code-tag">&lt;provides specifications=<span class="code-quote">"{...FooService, ...BarService}"</span>/&gt;</span>
<span class="code-tag">&lt;/component&gt;</span></pre>
</div></div>
<p><img src="providing-osgi-services_files/ps-foobar.png" align="absmiddle" border="0"></p>
<p><em>Note</em>: if you use the <tt>specifications</tt> attribute, the
handler check that all declared interfaces are really implemented by
the implementation class. If an interface is not implemented, the
handler throws an error.</p>
<p><em>Note</em>: if the implementation class does not implement any
interface, you cannot provide a service. In this case, the handler
throws an error.</p>
<h1><a name="ProvidingOSGiservices-ServiceProperties"></a><b>Service Properties</b></h1>
<p>The handler can manage service properties. Service properties are
attached to published service and allow consumer filtering providers. A
property can be attached to a field (contained in the component
implementation class), and so by handle dynamically.</p>
<p>Let's take a new example very closed of the last one:</p>
<div class="code"><div class="codeContent">
<pre class="code-java"><span class="code-keyword">public</span> class FooProviderType1 <span class="code-keyword">implements</span> FooService {
<span class="code-keyword">private</span> <span class="code-object">String</span> m_foo;
<span class="code-keyword">public</span> void foo() {
<span class="code-object">System</span>.out.println(<span class="code-quote">"foo "</span> + m_foo);
m_foo = <span class="code-quote">"bar"</span>;
}
}</pre>
</div></div>
<p>Remark that the m_foo field does not have any value. The following snippet shows a component publishing the <em>FooService</em> with two properties:</p>
<div class="code"><div class="codeContent">
<pre class="code-xml"><span class="code-tag">&lt;component className=<span class="code-quote">"...FooProviderType1"</span>&gt;</span>
<span class="code-tag">&lt;provides&gt;</span>
<span class="code-tag">&lt;property name=<span class="code-quote">"foo"</span> field=<span class="code-quote">"m_foo"</span> value=<span class="code-quote">"Foo"</span>/&gt;</span>
<span class="code-tag">&lt;property name=<span class="code-quote">"intProps"</span> type=<span class="code-quote">"int"</span> value=<span class="code-quote">"5"</span>/&gt;</span>
<span class="code-tag">&lt;/provides&gt;</span>
<span class="code-tag">&lt;/component&gt;</span></pre>
</div></div>
<p>The first declared property will be attached to the m_foo field. This property is published with the name <em>foo</em>.
This property has a default value "Foo". This value will be injected in
the _m_foo_ field, when this field asks for a value. A property with a
field attribute does not need to declare a type (the type can be
discovered by analyzing the implementation class).</p>
<p>The second property is published with the name <em>intProps</em>.
This property is not attached to a field, so, we need to declare the
property type. All primitive types or objects can be used has property
type (for object, the qualified name of the class is used as
java.lang.String).</p>
<p><img src="providing-osgi-services_files/ps-foo2.png" align="absmiddle" border="0"></p>
<p>The implementation class set a new value to the _m_foo_ field in the
code. When this action occurs, the handler will modify the service
publication to update the <em>foo</em> property published value. If a published property value becomes <em>null</em>, the property is unpublished since it has a new value.</p>
<p><img src="providing-osgi-services_files/ps-foo3.png" align="absmiddle" border="0"></p>
<p>If property does not have default value, the instance configuration
needs to set a value for each unvalued property. Moreover, the instance
can override the property value. The following xml snippet shows the
declaration of an instance overriding the property values:</p>
<div class="code"><div class="codeContent">
<pre class="code-xml"><span class="code-tag">&lt;instance component=<span class="code-quote">"...FooProviderType1"</span> name=<span class="code-quote">"myFooServiceProvider"</span>&gt;</span>
<span class="code-tag">&lt;property name=<span class="code-quote">"foo"</span> value=<span class="code-quote">"baz"</span>/&gt;</span>
<span class="code-tag">&lt;property name=<span class="code-quote">"intProps"</span> value=<span class="code-quote">"2"</span>/&gt;</span>
<span class="code-tag">&lt;/instance&gt;</span></pre>
</div></div>
<p><img src="providing-osgi-services_files/ps-foo4.png" align="absmiddle" border="0"></p>
<h1><a name="ProvidingOSGiservices-Advancedfeatures"></a>Advanced features</h1>
<h2><a name="ProvidingOSGiservices-ServiceServing&amp;ObjectCreation"></a><b>Service Serving &amp; Object Creation</b></h2>
<p>When a consumer requires the published service, the handler sends an
object (instance) of the implementation class. By default, it is always
the same instance. If no instance already exists, an instance is
created.</p>
<p>However, the handler supports the OSGi <em>Service Factory</em>. In this case, for each requester bundle, the handler sends a new object. To activate this policy, add the <em>factory</em> attribute in the <em>provides</em> element:</p>
<div class="code"><div class="codeContent">
<pre class="code-xml"><span class="code-tag">&lt;provides factory=<span class="code-quote">"SERVICE"</span>/&gt;</span></pre>
</div></div>
<p><b>NOTE [New in the 1.1.0-SNAPSHOT]</b>: The <tt>factory</tt> attribute became <tt>strategy</tt>. </p>
<div class="code"><div class="codeContent">
<pre class="code-xml"><span class="code-tag">&lt;provides strategy=<span class="code-quote">"SERVICE"</span>/&gt;</span></pre>
</div></div>
<p>Moreover, several features appeared:</p>
<ul>
<li><tt>strategy="instance"</tt> allows creating one service object per asking iPOJO instance (despite they are in the same bundle)</li>
<li>it is possible to create your own creation strategy by extending the <tt>org.apache.felix.ipojo.handlers.providedservice.CreationStrategy</tt> class and by indicating the qualified class name in the <tt>strategy</tt> attribute.</li>
</ul>
<h2><a name="ProvidingOSGiservices-SeveralServiceProviding"></a><b>Several Service Providing</b></h2>
<p>You can declare several <em>provides</em> inside the same component.
All this provided service will be manage by the same handler but
separately. Several services will be published (with different service
registrations). This case is useful when service properties are
different for the different services.</p>
<div class="code"><div class="codeContent">
<pre class="code-java">&lt;component className=<span class="code-quote">"...FooProviderType1"</span>&gt;
&lt;provides <span class="code-keyword">interface</span>=<span class="code-quote">"...Foo"</span>/&gt;
&lt;provides <span class="code-keyword">interface</span>=<span class="code-quote">"...Bar"</span>&gt;
&lt;property name=<span class="code-quote">"foo"</span> value=<span class="code-quote">"baz"</span>/&gt;
&lt;/provides&gt;
&lt;/component&gt;</pre>
</div></div>
<p><img src="providing-osgi-services_files/ps-foobar2.png" align="absmiddle" border="0"></p>
<h2><a name="ProvidingOSGiservices-ServicePropertyPropagation"></a><b>Service Property Propagation</b></h2>
<p>The configuration handler has the possibility to propagate received
properties to service publication. So, when the propagation is
activated, all properties received by the configuration handler will be
propagated to all published service. If some properties are mapped on
methods, these methods are invoked with the new value in argument.</p>
<p><img src="providing-osgi-services_files/ps-propagation.png" align="absmiddle" border="0"></p>
<h2><a name="ProvidingOSGiservices-Instancereconfiguration"></a><b>Instance reconfiguration</b></h2>
<p>The handler supports instance reconfiguration. When an instance is
dynamically reconfigured, if the new configuration updates property
values, these value are take into account (both for field, and service
publication). If some of these properties have methods, these methods
are invoked with the new value in argument.</p>
</td>
<td class="confluenceTd" valign="top" width="20%">
<h6><a name="ProvidingOSGiservices-Overview"></a><b>Overview</b></h6>
<ul>
<li><a href="http://felix.apache.org/site/apache-felix-ipojo.html" title="Apache Felix iPOJO">Home Page</a></li>
<li><a href="http://felix.apache.org/site/apache-felix-ipojo-feature-overview.html" title="Apache Felix iPOJO Feature Overview">iPOJO Feature Overview</a></li>
<li><a href="http://felix.apache.org/site/download.html" title="Download">Download &amp; Install </a></li>
</ul>
<h6><a name="ProvidingOSGiservices-GettingStarted"></a><b>Getting Started</b></h6>
<ul>
<li><a href="http://felix.apache.org/site/ipojo-in-10-minutes.html" title="iPOJO in 10 minutes">iPOJO in 10 minutes</a></li>
<li><a href="http://felix.apache.org/site/how-to-use-ipojo-annotations.html" title="How to use iPOJO Annotations">How to use iPOJO Annotations</a></li>
<li><a href="http://felix.apache.org/site/ipojo-hello-word-maven-based-tutorial.html" title="iPOJO Hello Word (Maven-Based) tutorial">iPOJO Hello Word (Maven-Based) tutorial</a></li>
<li><a href="http://felix.apache.org/site/ipojo-advanced-tutorial.html" title="iPOJO Advanced Tutorial">iPOJO Advanced Tutorial</a></li>
<li><a href="http://felix.apache.org/site/ipojo-composition-tutorial.html" title="iPOJO Composition Tutorial">iPOJO Composition Tutorial</a></li>
</ul>
<h6><a name="ProvidingOSGiservices-UserGuide"></a><b>User Guide</b></h6>
<ul>
<li><a href="http://felix.apache.org/site/describing-components.html" title="Describing components">Describing components (handler list) </a></li>
<li><a href="http://felix.apache.org/site/using-xml-schemas.html" title="Using XML Schemas">Using XML Schemas</a></li>
<li><a href="http://felix.apache.org/site/apache-felix-ipojo-testing-components.html" title="apache-felix-ipojo-testing-components">Testing components</a></li>
<li><a href="http://felix.apache.org/site/ipojo-advanced-topics.html" title="iPOJO Advanced Topics">Advanced Topics</a></li>
<li><a href="http://felix.apache.org/site/ipojo-faq.html" title="iPOJO FAQ">FAQ</a></li>
</ul>
<h6><a name="ProvidingOSGiservices-Tools"></a><b>Tools</b></h6>
<ul>
<li><a href="http://felix.apache.org/site/ipojo-eclipse-plug-in.html" title="iPOJO Eclipse Plug-in">iPOJO Eclipse Plug-in</a></li>
<li><a href="http://felix.apache.org/site/ipojo-ant-task.html" title="iPOJO Ant Task">iPOJO Ant Task</a></li>
<li><a href="http://felix.apache.org/site/ipojo-maven-plug-in.html" title="iPOJO Maven Plug-in">iPOJO Maven Plug-in</a></li>
<li><a href="http://felix.apache.org/site/apache-felix-ipojo-junit4osgi.html" title="apache-felix-ipojo-junit4osgi">Junit4OSGi</a></li>
<li><a href="http://felix.apache.org/site/ipojo-concepts-overview.html" title="iPOJO Concepts Overview">iPOJO concepts overview</a></li>
</ul>
<h6><a name="ProvidingOSGiservices-DeveloperGuide"></a><b>Developer Guide</b></h6>
<ul>
<li>API: <span class="nobr"><a href="http://people.apache.org/%7Eclement/ipojo/api/1.0/" title="Visit page outside Confluence" rel="nofollow">1.0<sup><img class="rendericon" src="providing-osgi-services_files/linkext7.gif" alt="" align="absmiddle" border="0" width="7" height="7"></sup></a></span></li>
<li><a href="http://felix.apache.org/site/how-to-write-your-own-handler.html" title="How to write your own handler">How to write your own handler</a></li>
<li><a href="http://felix.apache.org/site/how-to-use-ipojo-manipulation-metadata.html" title="How to use iPOJO Manipulation Metadata">How to use iPOJO Manipulation Metadata</a></li>
<li><a href="http://felix.apache.org/site/dive-into-the-ipojo-manipulation-depths.html" title="Dive into the iPOJO Manipulation depths">Dive into the iPOJO Manipulation depths</a></li>
</ul>
<h6><a name="ProvidingOSGiservices-Misc&amp;Contact"></a><b>Misc &amp; Contact</b></h6>
<ul>
<li><a href="http://felix.apache.org/site/apache-felix-ipojo-issuestracker.html" title="apache-felix-ipojo-issuestracker">Issues Tracker</a></li>
<li><a href="http://felix.apache.org/site/apache-felix-ipojo-supportedvms.html" title="apache-felix-ipojo-supportedVMs">Supported JVMs</a></li>
<li><a href="http://felix.apache.org/site/apache-felix-ipojo-supportedosgi.html" title="apache-felix-ipojo-supportedOSGi">Supported OSGi Implementations</a></li>
<li><span class="nobr"><a href="http://ipojo-dark-side.blogspot.com/" title="Visit page outside Confluence" rel="nofollow">iPOJO's Dark Side Blog<sup><img class="rendericon" src="providing-osgi-services_files/linkext7.gif" alt="" align="absmiddle" border="0" width="7" height="7"></sup></a></span></li>
<li><a href="http://felix.apache.org/site/future-ideas.html" title="Future Ideas">Future Ideas</a></li>
<li><a href="http://felix.apache.org/site/contact.html" title="Contact">Contact</a></li>
<li><a href="http://felix.apache.org/site/related-works.html" title="Related Works">Related Works</a></li>
<li><a href="http://felix.apache.org/site/article-presentations.html" title="Article &amp; Presentations">Article &amp; Presentations</a></li>
</ul>
<hr>
<div class="" align="center">
<p><span class="nobr"><a href="http://cwiki.apache.org/confluence/createrssfeed.action?types=blogpost&amp;statuses=created&amp;statuses=modified&amp;spaces=FELIX&amp;labelString=iPOJO&amp;rssType=atom&amp;maxResults=10&amp;timeSpan=5&amp;publicFeed=true&amp;title=iPOJO%20Atom%20Feed" title="Stay tuned!" rel="nofollow"><img src="providing-osgi-services_files/feed-icon-32x32.png" align="absmiddle" border="0"><sup><img class="rendericon" src="providing-osgi-services_files/linkext7.gif" alt="" align="absmiddle" border="0" width="7" height="7"></sup></a></span></p></div>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script><script src="providing-osgi-services_files/ga.js" type="text/javascript"></script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-1518442-4");
pageTracker._trackPageview();
</script>
</td></tr></tbody></table>
</div>
</body></html>