cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache CXF > DOSGi DS Demo page
Date Fri, 04 Dec 2009 15:45:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/11/_/styles/combined.css?spaceKey=CXF&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="http://cwiki.apache.org/confluence/display/CXF/DOSGi+DS+Demo+page">DOSGi
DS Demo page</a></h2>
     <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~davidb@apache.org">David
Bosschaert</a>
    </h4>
     Update to 1.1
          <div id="versionComment" class="noteMacro" style="display:none; padding: 5px;">
     Update to 1.1<br />
     </div>
          <br/>
     <div class="notificationGreySide">
         <p>This page describes the CXF Distributed OSGi with Declarative Services demo.</p>

<p>The Declarative Services demo uses a DS implementation to create a remoted OSGi service
from a DS component. The consumer side uses DS to create a component that consumes the remote
OSGi service. By using Declarative Services, you don't need to write code to interact with
the OSGi Service Registry. That's all handled through injection which hugely simpliefies the
code. <br/>
Declarative Services is similar to Spring-DM/OSGi Blueprint in that service dependencies are
satisfied through injection. There are a few differences as well. DS is lighter weight than
Spring-DM but also has less features. Declarative Services have been part of the OSGi specifications
since version 4.0.</p>

<p>This demo can be used with any DOSGi distribution, in this document the single-bundle
distribution is used with the Equinox implementation of DS.</p>

<h2><a name="DOSGiDSDemopage-Demodesign"></a>Demo design</h2>

<p>This demo is quite similar to the Spring-DM demo and the Greeter demo in structure.
It consists of 3 bundles:</p>
<ul>
	<li>An interface bundle defining the Adder Service interface.</li>
	<li>An Adder Service implementation bundle.</li>
	<li>An Adder Service consumer bundle.</li>
</ul>


<p>The service implementation and consumer bundle are built using DS.<br/>
<img src="/confluence/download/attachments/119430/dosgi_cxf_ds.png" align="absmiddle" border="0"
/> <br/>
The <a href="http://svn.apache.org/repos/asf/cxf/dosgi/trunk/samples/ds/interface/src/main/java/org/apache/cxf/dosgi/samples/ds/AdderService.java"
rel="nofollow">Adder Service interface</a> is as follows:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> <span
class="code-keyword">interface</span> AdderService {
    <span class="code-object">int</span> add(<span class="code-object">int</span>
a, <span class="code-object">int</span> b);
}</pre>
</div></div>

<h2><a name="DOSGiDSDemopage-TheAdderServiceImplementation"></a>The Adder
Service Implementation</h2>
<p>The service implementation providers a <a href="http://svn.apache.org/repos/asf/cxf/dosgi/trunk/samples/ds/impl/src/main/java/org/apache/cxf/dosgi/samples/ds/impl/AdderServiceImpl.java"
rel="nofollow">simplistic implementation of the AdderService interface</a>, which
is instantiated as a DS component.</p>

<p>In the <a href="http://svn.apache.org/repos/asf/cxf/dosgi/trunk/samples/ds/impl/src/main/resources/OSGI-INF/component.xml"
rel="nofollow"><tt>OSGI-INF/component.xml</tt></a> file the AdderServiceImpl
is instantiated and registered with the OSGi service registry with the distribution properties.
These properties instruct. Distributed OSGi into making the service available on <a href="http://localhost:9090/adder"
rel="nofollow">http://localhost:9090/adder</a>.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;scr:component <span class="code-keyword">xmlns:scr</span>=<span
class="code-quote">"http://www.osgi.org/xmlns/scr/v1.1.0"</span>&gt;</span>
  <span class="code-tag">&lt;implementation class=<span class="code-quote">"org.apache.cxf.dosgi.samples.ds.impl.AdderServiceImpl"</span>/&gt;</span>
  
  <span class="code-tag">&lt;property name=<span class="code-quote">"osgi.remote.interfaces"</span>&gt;</span>*<span
class="code-tag">&lt;/property&gt;</span>
  <span class="code-tag">&lt;property name=<span class="code-quote">"osgi.remote.configuration.type"</span>
value=<span class="code-quote">"pojo"</span> /&gt;</span>
  <span class="code-tag">&lt;property name=<span class="code-quote">"osgi.remote.configuration.pojo.address"</span>
value=<span class="code-quote">"http://localhost:9090/adder"</span> /&gt;</span>
  
  <span class="code-tag">&lt;service&gt;</span>
    <span class="code-tag">&lt;provide interface=<span class="code-quote">"org.apache.cxf.dosgi.samples.ds.AdderService"</span>/&gt;</span>
  <span class="code-tag">&lt;/service&gt;</span>
<span class="code-tag">&lt;/scr:component&gt;</span></pre>
</div></div>
<p>Note that the <tt>META-INF/MANIFEST.MF</tt> file needs to contain a special
DS header that tells the system where to find this file. In case of this demo, this header
is added by the Maven build system. The header used by the demo is:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">  Service-Component: OSGI-INF/component.xml</pre>
</div></div>

<p>So let's install the server side in Equinox, together with the Equinox DS implementation.
You can do this from the Equinox command line, but in this document I'll launch Equinox from
within Eclipse.<br/>
I'm starting off by importing the Single Bundle Distribution as a binary project in Eclipse
by going <em>File -&gt; Import | Plug-ins and Fragments</em> and then select
the directory that contains the single bundle distribution JAR file. My workspace now looks
like this:<br/>
<img src="/confluence/download/attachments/119430/start.JPG" align="absmiddle" border="0"
/><br/>
Next I'll create an OSGi Framework launch configuration that includes DS. To do this</p>
<ol>
	<li><em>deselect</em> the 'Target Platform' tickbox in the Eclipse Launch
configuration screen</li>
	<li>select org.eclipse.equinox.ds</li>
	<li>hit the 'Add Required Bundles' button</li>
</ol>


<p><img src="/confluence/download/attachments/119430/launch.JPG" align="absmiddle"
border="0" /></p>

<p>Now run the OSGi container, you will get a setup like this:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">osgi&gt; ss

Framework is launched.

id	State       Bundle
0	ACTIVE      org.eclipse.osgi_3.5.0.v20090520
1	ACTIVE      org.eclipse.equinox.util_1.0.100.v20090520-1800
2	ACTIVE      org.eclipse.osgi.services_3.2.0.v20090520-1800
3	ACTIVE      cxf-dosgi-ri-singlebundle-distribution_1.1.0.SNAPSHOT
4	ACTIVE      org.eclipse.equinox.ds_1.1.0.v20090520-1800</pre>
</div></div>
<p>Now I can install the DOSGi DS bundles in the OSGi container directly from the maven
repository. </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">osgi&gt; install http:<span class="code-comment">//repo1.maven.org/maven2/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-ds-<span
class="code-keyword">interface</span>/1.1/cxf-dosgi-ri-samples-ds-<span class="code-keyword">interface</span>-1.1.jar
</span>Bundle id is 5

osgi&gt; install http:<span class="code-comment">//repo1.maven.org/maven2/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-ds-impl/1.1/cxf-dosgi-ri-samples-ds-impl-1.1.jar
</span>Bundle id is 6

osgi&gt; start 6
... log messages may appear ...</pre>
</div></div>
<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td>You can also import the maven
projects of the DS demo into Eclipse, this would save you from installing it with a URL as
above. To do this, check out the CXF/DOSGi source from SVN (<a href="http://svn.apache.org/repos/asf/cxf/dosgi/trunk"
rel="nofollow">http://svn.apache.org/repos/asf/cxf/dosgi/trunk</a>), run <tt>mvn
eclipse:eclipse</tt> and import all Eclipse projects under the <tt>samples/ds</tt>
directory in Eclipse with <em>File -&gt; Import | Existing Projects into Workspace</em>.</td></tr></table></div>
<p>At this point, the service should be available remotely, you can check this by obtaining
the WSDL:<br/>
<img src="/confluence/download/attachments/119430/wsdl.JPG" align="absmiddle" border="0"
/></p>

<h2><a name="DOSGiDSDemopage-TheAdderServiceConsumer"></a>The Adder Service
Consumer</h2>
<p>The service consumer is also created using DS. DS creates an AdderConsumer component
which is injected with a reference to the remote AdderService. Like in Spring, the injection
is done by DS, which makes the code nice and simple. When the injection is done, the start()
method is called.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> class AdderConsumer
{
    <span class="code-keyword">private</span> AdderService adder;
    
    <span class="code-keyword">public</span> void bindAdder(AdderService a) {
        adder = a;
    }
    
    <span class="code-keyword">public</span> void unbindAdder(AdderService a)
{
        adder = <span class="code-keyword">null</span>;
    }
    
    <span class="code-keyword">public</span> void start(ComponentContext cc) {
        <span class="code-object">System</span>.out.println(<span class="code-quote">"Using
adder service: 1 + 1 = "</span> + adder.add(1, 1));
    }
}</pre>
</div></div>
<p>The client side bundle contains an <a href="http://svn.apache.org/repos/asf/cxf/dosgi/trunk/samples/ds/client/src/main/resources/OSGI-INF/component.xml"
rel="nofollow"><tt>OSGI-INF/component.xml</tt></a> which drives the component
creation and injection:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;scr:component <span class="code-keyword">xmlns:scr</span>=<span
class="code-quote">"http://www.osgi.org/xmlns/scr/v1.1.0"</span> activate=<span
class="code-quote">"start"</span>&gt;</span>
   <span class="code-tag">&lt;implementation class=<span class="code-quote">"org.apache.cxf.dosgi.samples.ds.consumer.AdderConsumer"</span>/&gt;</span>
   <span class="code-tag">&lt;reference interface=<span class="code-quote">"org.apache.cxf.dosgi.samples.ds.AdderService"</span>
name=<span class="code-quote">"AdderService"</span> cardinality=<span class="code-quote">"1..1"</span>
policy=<span class="code-quote">"dynamic"</span> bind=<span class="code-quote">"bindAdder"</span>
unbind=<span class="code-quote">"unbindAdder"</span>/&gt;</span>
<span class="code-tag">&lt;/scr:component&gt;</span></pre>
</div></div>
<p>As on the service provider side, the client side bundle needs to contain the DS header
in the <tt>META-INF/MANIFEST.MF</tt>:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">  Service-Component: OSGI-INF/component.xml</pre>
</div></div>
<p>As in the Greeter demo, the client side needs to be configured to knoe where teh
remote service actually is. This is one in the <a href="http://svn.apache.org/repos/asf/cxf/dosgi/trunk/samples/ds/client/src/main/resources/OSGI-INF/remote-service/remote-services.xml"
rel="nofollow"><tt>OSGI-INF/remote-service/remote-services.xml</tt></a>
file:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;service-descriptions xmlns=<span
class="code-quote">"http://www.osgi.org/xmlns/sd/v1.0.0"</span>&gt;</span>
  <span class="code-tag">&lt;service-description&gt;</span>
    <span class="code-tag">&lt;provide interface=<span class="code-quote">"org.apache.cxf.dosgi.samples.ds.AdderService"</span>
/&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"osgi.remote.interfaces"</span>&gt;</span>*<span
class="code-tag">&lt;/property&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"osgi.remote.configuration.type"</span>&gt;</span>pojo<span
class="code-tag">&lt;/property&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"osgi.remote.configuration.pojo.address"</span>&gt;</span>http://localhost:9090/adder<span
class="code-tag">&lt;/property&gt;</span>
  <span class="code-tag">&lt;/service-description&gt;</span>
<span class="code-tag">&lt;/service-descriptions&gt;</span></pre>
</div></div>
<p>Install and run the consumer side of the demo in a separate Equinox instance (tip:
you can duplicate the launch configuration used for the server side in the 'Run Configurations'
dialog):</p>


<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">osgi&gt; install http:<span class="code-comment">//repo1.maven.org/maven2/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-ds-<span
class="code-keyword">interface</span>/1.1/cxf-dosgi-ri-samples-ds-<span class="code-keyword">interface</span>-1.1.jar
</span>Bundle id is 5

osgi&gt; install http:<span class="code-comment">//repo1.maven.org/maven2/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-ds-client/1.1/cxf-dosgi-ri-samples-ds-client-1.1.jar
</span>Bundle id is 6

osgi&gt; start 6
... log messages may appear, after a little <span class="code-keyword">while</span>
the following message appears:
Using adder service: 1 + 1 = 2</pre>
</div></div>
<p>The remote adder service has now been invoked. You will see the following line on
the server side Equinox window:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">Adder service invoked: 1 + 1 = 2</pre>
</div></div>
     </div>
     <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>

       <a href="http://cwiki.apache.org/confluence/display/CXF/DOSGi+DS+Demo+page">View
Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=119430&revisedVersion=10&originalVersion=9">View
Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/CXF/DOSGi+DS+Demo+page?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message