Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
xml
xml
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0">
  <implementation class="org.apache.cxf.dosgi.samples.ds.impl.AdderServiceImpl"/>
  
  <property name="osgi.remote.interfaces">*</property>
  <property name="osgi.remote.configuration.type" value="pojo" />
  <property name="osgi.remote.configuration.pojo.address" value="http://localhost:9090/adder" />
  
  <service>
    <provide interface="org.apache.cxf.dosgi.samples.ds.AdderService"/>
  </service>
</scr:component>

Note that the META-INF/MANIFEST.MF 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:

Code Block
  Service-Component: OSGI-INF/component.xml

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.
I'm starting off by importing the Single Bundle Distribution as a binary project in Eclipse by going File -> Import | Plug-ins and Fragments and then I select the directory that contains the single bundle distribution JAR file. My workspace now looks like this:
@@@ image
Next I'll create an OSGi Framework launch configuration that includes DS. To do this

  1. deselect the 'Target Platform' tickbox in the Eclipse Launch configuration screen
  2. select org.eclipse.equinox.ds
  3. hit the 'Add Required Bundles' button

@@@ image

Now run the OSGi container, you will get a setup like this:

Code Block
osgi> 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

Now I can install the DOSGi DS bundles in the OSGi container directly from the maven repository.

Code Block
osgi> install https://repository.apache.org/content/groups/snapshots/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-ds-interface/1.1-SNAPSHOT/cxf-dosgi-ri-samples-ds-interface-1.1-SNAPSHOT.jar 
Bundle id is 5

osgi> install https://repository.apache.org/content/groups/snapshots/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-ds-impl/1.1-SNAPSHOT/cxf-dosgi-ri-samples-ds-impl-1.1-SNAPSHOT.jar
Bundle id is 6

osgi> start 6
... log messages may appear ...

...

Tip

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 (http://svn.apache.org/repos/asf/cxf/dosgi/trunkImage Added), run mvn eclipse:eclipse and import all Eclipse projects under the samples/ds directory in Eclipse with File -> Import | Existing Projects into Workspace.

At this point, the service should be available remotely, you can check this by obtaining the WSDL:
@@@ image

The Adder Service Consumer

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.

Code Block
java
java
public class AdderConsumer {
    private AdderService adder;
    
    public void bindAdder(AdderService a) {
        adder = a;
    }
    
    public void unbindAdder(AdderService a) {
        adder = null;
    }
    
    public void start(ComponentContext cc) {
        System.out.println("Using adder service: 1 + 1 = " + adder.add(1, 1));
    }
}

The client side bundle contains an OSGI-INF/component.xml which drives the component creation and injection:

Code Block
xml
xml
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="start">
   <implementation class="org.apache.cxf.dosgi.samples.ds.consumer.AdderConsumer"/>
   <reference interface="org.apache.cxf.dosgi.samples.ds.AdderService" name="AdderService" cardinality="1..1" policy="dynamic" bind="bindAdder" unbind="unbindAdder"/>
</scr:component>

As on the service provider side, the client side bundle needs to contain the DS header in the META-INF/MANIFEST.MF:

Code Block
  Service-Component: OSGI-INF/component.xml

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 OSGI-INF/remote-service/remote-services.xml file:

Code Block
xml
xml
<service-descriptions xmlns="http://www.osgi.org/xmlns/sd/v1.0.0">
  <service-description>
    <provide interface="org.apache.cxf.dosgi.samples.ds.AdderService" />
    <property name="osgi.remote.interfaces">*</property>
    <property name="osgi.remote.configuration.type">pojo</property>
    <property name="osgi.remote.configuration.pojo.address">http://localhost:9090/adder</property>
  </service-description>
</service-descriptions>

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):

Code Block
osgi> install https://repository.apache.org/content/groups/snapshots/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-ds-interface/1.1-SNAPSHOT/cxf-dosgi-ri-samples-ds-interface-1.1-SNAPSHOT.jar 
Bundle id is 5

osgi> install https://repository.apache.org/content/groups/snapshots/org/apache/cxf/dosgi/samples/cxf-dosgi-ri-samples-ds-client/1.1-SNAPSHOT/cxf-dosgi-ri-samples-ds-client-1.1-SNAPSHOT.jar
Bundle id is 6

osgi> start 6
... log messages may appear, after a little while the following message appears:
Using adder service: 1 + 1 = 2

The remote adder service has now been invoked. You will see the following line on the server side Equinox window:

Code Block
Adder service invoked: 1 + 1 = 2