For the simple HelloWorld OSGi application, the service
bundle implements the HelloWorldEBA interface,
and exports it as an OSGi service.
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.
Rational® Application Developer Version 8.5 provides graphical support for creating and packaging bundles. The sample procedure that follows uses this tool. You can also use other tools, and the steps are adaptable to other tools.
This
sample procedure builds the following two bundles.
- com.ibm.ws.eba.helloWorld.api:
this bundle declares the HelloWorldEBA interface.
- com.ibm.ws.eba.helloWorld.service:
this bundle implements the HelloWorldEBA interface,
and exports it as an OSGi service. The exported service is used by
client bundle com.ibm.ws.eba.helloWorld.client,
as described in Creating a client bundle.
Demonstration of this task (9 min) 
Procedure
To create the service bundle, complete the following
steps:
- Create the com.ibm.ws.eba.helloWorld.api bundle. This bundle
declares the HelloWorldEBA interface.
- Click . The OSGi Bundle Project panel is displayed.
- Configure the project.
- For the Project name, enter com.ibm.ws.eba.helloWorld.api.
- Clear the Add bundle to application check box. If you leave this check box selected, a new OSGi application project is created automatically, and the bundle is added to it. Here, however, the application project will be created manually in a separate task, Creating an OSGi application.
- 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.
- Declare the HelloWorldEBA interface.
Create a package called com.ibm.ws.eba.helloWorld.api,
that includes an interface called HelloWorldEBA. Code
this interface to contain just one method: hello().
- Under your com.ibm.ws.eba.helloWorld.api project,
right-click the folder src, then select .
- Name the new package com.ibm.ws.eba.helloWorld.api.
- Click Finish.
- Right-click the new package, then select .
- Name the new interface HelloWorldEBA.
- Click Finish.
- Copy and paste the following method to replace the content
of the interface file:
package com.ibm.ws.eba.helloWorld.api;
public interface HelloWorldEBA {
public void hello();
}
- Save and close the file.
- Configure the com.ibm.ws.eba.helloWorld.api package
as an exported package.
Edit the bundle manifest in
the com.ibm.ws.eba.helloWorld.api project to allow
other bundles to load classes from the com.ibm.ws.eba.helloWorld.api package.
Classes that are in packages not exported in the bundle manifest are
private to the defining bundle and cannot be loaded by any other bundle.
- Open the bundle MANIFEST.MF file with the manifest editor. This file is in the BundleContent/META-INF directory.
- Click the Runtime tab.
- In the Exported Packages pane,
click Add.
- Select the com.ibm.ws.eba.helloWorld.api package
from the list, then click OK.
- In the Exported Packages pane,
click Properties.
- In the Properties dialog, set
the version to 1.0.0,
then click OK.
- Save and close the file.
- Create the com.ibm.ws.eba.helloWorld.service bundle. This
bundle implements the HelloWorldEBA interface.
- Click . The OSGi Bundle Project panel is displayed.
- Configure the project.
- For the Project name, enter com.ibm.ws.eba.helloWorld.service.
- Clear the Add bundle to application check box. If you leave this check box selected, a new OSGi application project is created automatically, and the bundle is added to it. Here, however, the application project will be created manually in a separate task, Creating an OSGi application.
- 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.
- Make the HelloWorldEBA interface available
to the service implementation bundle.
Edit the client
bundle manifest to make classes inside the com.ibm.ws.eba.helloWorld.api package
available to the service implementation bundle. The com.ibm.ws.eba.helloWorld.api package
is part of the com.ibm.ws.eba.helloWorld.api bundle.
- Expand the com.ibm.ws.eba.helloWorld.service project.
- 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, select com.ibm.ws.eba.helloWorld.api from
the Exported Packages list, then click OK. The package is added to the Imported Packages list.
- In the Imported Packages list,
select the com.ibm.ws.eba.helloWorld.api package
then click Properties.
- In the Properties dialog, set
the minimum version to 1.0.0
Inclusive, and set the maximum version to 1.1.0
Exclusive, then click OK. The entry for this package in the Imported Packages list
is updated to com.ibm.ws.eba.helloWorld.api [1.0.0,1.1.0).
This
version syntax means "exported packages with versions between 1.0.0
inclusive and 1.1.0 exclusive will match this import". For more
information on the version syntax, see section 3.2.6 "Version Ranges" of
the OSGi Service Platform Release 4 Version 4.2 Core Specification.
This
version range has been specified to ensure that the implementation
bundle uses an updated version of the package only if it differs in
the value of the micro version, because a major change, such as removing
a method from an interface, or a minor change, such as adding a method
to an interface, could cause the implementation bundle to cease functioning
correctly.
- Save and close the file.
- Implement the HelloWorld service.
Create
a package called com.ibm.ws.eba.helloWorld.service,
that includes an implementation class called HelloWorldService.
Code this class to implement the hello() method
from the HelloWorldEBA interface.
This implementation of the class causes OSGi Service: Hello World! to be displayed.
- Under your com.ibm.ws.eba.helloWorld.service project,
right-click the folder src, then select .
- Name the new package com.ibm.ws.eba.helloWorld.service.
- Click Finish.
- Right-click the new package, then select .
- Name the new interface implementation class HelloWorldService.
- Click Add alongside the Interfaces field.
- Enter Hello, select HelloWorldEBA from
the Matching items list, then click OK.
- Ensure that the Inherited abstract methods check
box is selected.
- Click Finish.
- Provide implementation code for the inherited hello() method. Replace the line
// TODO Auto-generated method stub
with
the line System.out.println(OSGi Service: Hello World!);
The
complete code for the implementation class should now be as follows:package com.ibm.ws.eba.helloWorld.service;
import com.ibm.ws.eba.helloWorld.api.HelloWorldEBA;
public class HelloWorldService implements HelloWorldEBA {
@Override
public void hello() {
System.out.println(OSGi Service: Hello World!);
}
}
- Save and close the file.
- Export the helloWorld service by using OSGi Blueprint XML.
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 components 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.helloWorld.service,
create a Blueprint XML file:
- Right-click the com.ibm.ws.eba.helloWorld.service project,
and select .
- Accept the default values for all the options on this panel.
- Click Finish.
- Add a bean element to the Blueprint XML file.
- In the Design tab, click Add in
the Overview pane.
- Select Bean, and click OK.
- Click Browse, select HelloWorldService,
and click OK.
- In the Bean ID field, enter HelloEBA,
then click OK to add the bean element.
- Add a service element to the Blueprint XML file.
- Select Blueprint in the Overview pane,
and click Add.
- Select Service, and click OK.
- Click Browse alongside the Service
Interface field.
- Enter Hello, select HelloWorldEBA from
the Matching items list, and click OK.
- Click Browse alongside the Bean
Reference field, select HelloEBA,
then click OK.
- Click OK to add the service element.
- Examine the Blueprint XML source code.
Select
the
Source tab. The source code should be as
follows:
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
<bean id="helloEBA"
class="com.ibm.ws.eba.helloWorld.service.HelloWorldService"/>
<service id=HelloEBAService ref="helloEBA"
interface="com.ibm.ws.eba.helloWorld.api.HelloWorldEBA"/>
</blueprint>
In the previous code block:- The bean element defines a
Blueprint component to be instantiated. In this example, the bean element
causes bean helloEBA to be instantiated,
by calling the constructor for the com.ibm.ws.eba.helloWorld.service.HelloWorldService class.
- The id attribute identifies
the bean. You must specify this attribute if the bean is referenced
from elsewhere in the Blueprint information, for example from the service element.
- The class attribute specifies
which implementation class of the bean is instantiated.
- The service element defines the registration
of a component in the OSGi service registry. In this example, the service element
registers the bean with the name helloEBA as
a service in the OSGi service registry with interface com.ibm.ws.eba.helloWorld.api.HelloWorldEBA, specified
by the interface attribute.
- The ref attribute refers
to the id of the bean to be
registered. This id is defined
in the bean element.
- The interface attribute
refers to the interface that the bean class implements.
For more information, see section 121.5
"Bean Manager" and
section 121.6
"Service Manager" of the OSGi Service Platform
Release 4 Version 4.2 Enterprise Specification.
- Save and close the file.
Note: You might get an exception message (visible in the Problems pane)
saying that there is no bin.include entry for OSGI-INF in
the build properties file. If you see this message, use the quick-fix option
to add the entry (right-click the problem, then select quick-fix).
Results
You have created two bundles,
com.ibm.ws.eba.helloWorld.api and
com.ibm.ws.eba.helloWorld.service.
The
com.ibm.ws.eba.helloWorld.service service
implements the HelloWorldEBA interface that is declared in the
com.ibm.ws.eba.helloWorld.api bundle,
and contains the business logic and metadata needed to export the
com.ibm.ws.eba.helloWorld.service service.
What to do next
You can now create
the client bundle that uses the com.ibm.ws.eba.helloWorld.service service.