For the simple hello-world OSGi
application, the client
bundle consumes the HelloWorldEBA service, and
uses it to produce the message "OSGi Service: Hello World!".
Before you begin
This task assumes that you have
already created the
HelloWorldEBA service,
as described in
Creating a service bundle.
About this task
A bundle, the modular unit in the OSGi model, is a JAR file that includes the OSGi application metadata. This metadata is defined in the manifest file of the JAR file, META-INF/MANIFEST.MF.
IBM® Rational® Application Developer Version 8 provides graphical support for creating and packaging bundles. The sample procedure below uses this tool. You can also use other tools, and the steps are adaptable to other tools.
This
sample procedure builds a bundle called com.ibm.ws.eba.helloWorldClient.
This bundle uses the HelloWorldEBA service that
is exported by the bundle com.ibm.ws.eba.helloWorldService,
as described in Creating a service bundle.
Procedure
- Create an OSGi bundle project.
- Click .
The Select a wizard panel is displayed.
- Click , then click Next.
The OSGi Bundle Project panel is displayed.
- Configure the project.
- For Project name, enter com.ibm.ws.eba.helloWorldClient.
- Leave the other options as the default values.
- Click Next.
The Java Configuration panel is displayed. Accept the default values for all the options on this panel.
- Click Next.
The OSGi bundle settings panel is displayed. Accept the default values for all the options on this panel.
- Click Finish.
You have created your OSGi project.
- Make the HelloWorldEBA service
available
to the client bundle.
Edit the client bundle manifest
to make classes inside the com.ibm.ws.eba.helloWorld package
available to the client bundle. The com.ibm.ws.eba.helloWorld package
is part of the com.ibm.ws.eba.helloWorldService bundle.
- Open the bundle MANIFEST.MF file with the manifest editor.
This file is in the BundleContent/META-INF directory.
- Click the Dependencies tab.
- In the Imported Packages pane,
click Add.
- In
the Package Selection dialog,
enter com.ibm.ws.eba.helloWorld then click OK. The package is added to the Imported Packages list.
- In the Imported Packages list,
select the com.ibm.ws.eba.helloWorld package then
click Properties.
- In
the Properties dialog, set
the minimum version to 1.0.0
Inclusive, and set the maximum version to 2.0.0
Exclusive, then click OK. The entry for this package in the Imported Packages list
is updated to com.ibm.ws.eba.helloWorld [1.0.0,2.0.0).
Note: This
version syntax means "exported packages with versions between 1.0.0
inclusive and 2.0.0 exclusive will match this import". For more
information, see section 3.2.6 "Version Ranges" of the OSGi Service
Platform Release 4 Version 4.2 Core Specification.
- Save the changes in the manifest editor. Click .
- Create
a class HelloWorldRefEBA.
- In the project source folder src,
create a package called com.ibm.ws.eba.helloWorld.client.
In the package, create an interface called HelloWorldRefEBA with
the following contents:
package com.ibm.ws.eba.helloWorld.client;
import com.ibm.ws.eba.helloWorld.HelloWorldEBA;
public interface HelloWorldRefEBA {
public void refHello();
public HelloWorldEBA getHelloWorldEBAService(); //property getter
}
- In the same package, to
implement the HelloWorldRefEBA interface,
create a class called HelloWorldRefEBAImpl with
the following contents:
package com.ibm.ws.eba.helloWorld.client;
import com.ibm.ws.eba.helloWorld.HelloWorldEBA;
public class HelloWorldRefEBAImpl implements HelloWorldRefEBA {
private HelloWorldEBA helloWorldEBAService = null; //a reference to the service
public void refHello() {
System.out.println("Client: Start...");
helloWorldEBAService.hello();
System.out.println("Client: End...");
}
public HelloWorldEBA getHelloWorldEBAService() {
return helloWorldEBAService;
}
public void setHelloWorldEBAService(HelloWorldEBA helloWorldEBAService) {
this.helloWorldEBAService = helloWorldEBAService;
}
}
In the previous code block:
- The line HelloWorldEBA
helloWorldEBAService = null; defines the service dependency
(the HelloWorldRefEBAImpl
class depends on a service with a HelloWorldEBA interface).
- The line helloWorldEBAService.hello(); demonstrates
that a service has been injected for the helloWorldEBAService dependency.
- Create a Blueprint configuration.
A Blueprint configuration contains the bundle component assembly and configuration information. It also describes how components are registered in the OSGi service registry, or how they look up services from the OSGi service registry. This information is used at run time to instantiate and configure the required components when the bundle is started.
- In the project com.ibm.ws.eba.helloWorldClient,
create a Blueprint XML file:
- Click .
- (Optional) Specify the file
name. This can be any name provided
it is an XML file. For example, helloWorldRef.xml.
- Leave the other options as the default values.
- Click Finish.
- Update the Blueprint XML file to contain the
following
Blueprint information. This Blueprint file specifies the
internal wiring of the components.
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
<bean class="com.ibm.ws.eba.helloWorld.client.HelloWorldRefEBAImpl" init-method="refHello">
<property name="helloWorldEBAService" ref="helloEBARef"/>
</bean>
<reference id="helloEBARef" interface="com.ibm.ws.eba.helloWorld.HelloWorldEBA"/>
</blueprint>
In the previous code block:- The init-method refHello is
called when the com.ibm.ws.eba.helloWorld.client.HelloWorldRefEBAImpl bean
is created.
- The property element
specifies
how a dependency is injected.
- The property helloWorldEBAService is
set by invoking the property setter public void setHelloWorldEBAService(HelloWorldEBA
helloWorldEBAService). You can use setter injection for bean
properties that are defined in accordance with Java™ bean conventions.
- The attribute ref specifies
the Blueprint element ID of the component to be injected. The ref attribute
must match a top-level element in this file. In this case, it matches
the reference element.
- The reference element defines
a dependency of this Blueprint module on an OSGi service with interface com.ibm.ws.eba.helloWorld.HelloWorldRefEBA.
When the Blueprint Container starts this module, it must match the
name of the interface attribute with an available service in the OSGi
service registry. If this cannot be done, the Blueprint module is
not started. In this case, the service is defined in the service element
of the Blueprint XML of the service bundle.
For more information,
see section 121.5
"Bean Manager" and
section 121.7
"Service Reference Managers" of the OSGi Service
Platform Release 4 Version 4.2 Enterprise Specification.
Results
You have created a bundle
called
com.ibm.ws.eba.helloWorldClient.
This bundle consumes the
HelloWorldEBA service,
and uses it to produce the message
"OSGi Service: Hello World!".
What to do next
You can now create
an OSGi application, in which multiple bundles are packaged
together.