Network Deployment (Distributed operating systems), v8.0 > Develop and deploying applications > Develop OSGi applications > Develop and deploying an OSGi application


Create a client bundle

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 we have already created the HelloWorldEBA service, as described in Create 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 v8 provides graphical support for creating and packaging bundles. The sample procedure below uses this tool. We 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 Create a service bundle.


Procedure

  1. Create an OSGi bundle project.

    1. Click File > New > Project.
      The Select a wizard panel is displayed.

    2. Click OSGi > OSGi Bundle Project, then click Next.
      The OSGi Bundle Project panel is displayed.

    3. Configure the project.

      • For Project name, enter com.ibm.ws.eba.helloWorldClient.
      • Leave the other options as the default values.

    4. Click Next.
      The Java Configuration panel is displayed. Accept the default values for all the options on this panel.

    5. Click Next.
      The OSGi bundle settings panel is displayed. Accept the default values for all the options on this panel.

    6. Click Finish.

    You have created your OSGi project.

  2. 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.

    1. Open the bundle MANIFEST.MF file with the manifest editor.
      This file is in the BundleContent/META-INF directory.

    2. Click the Dependencies tab.

    3. In the Imported Packages pane, click Add.

    4. In the Package Selection dialog, enter com.ibm.ws.eba.helloWorld then click OK. The package is added to the Imported Packages list.

    5. In the Imported Packages list, select the com.ibm.ws.eba.helloWorld package then click Properties.

    6. 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).

      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 v4.2 Core Specification.

    7. Save the changes in the manifest editor. Click File > Save.

  3. Create a class HelloWorldRefEBA.

    1. 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
      
      }
      

    2. 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.

  4. 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.

    1. In the project com.ibm.ws.eba.helloWorldClient, create a Blueprint XML file:

      1. Click File > New > Other > OSGi > Blueprint File.
      2. (Optional) Specify the file name. This can be any name provided it is an XML file. For example, helloWorldRef.xml.
      3. Leave the other options as the default values.

      4. Click Finish.

    2. 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="//publib.boulder.ibm.com/infocenter/wasinfo/v8r0/index.jsp?topic=/  ://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). We 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.

      See section 121.5 "Bean Manager" and section 121.7 "Service Reference Managers" of the OSGi Service Platform Release 4 v4.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

We can now create an OSGi application, in which multiple bundles are packaged together.

Parent topic: Develop and deploying an OSGi application

Related concepts:

About OSGi Applications
The Blueprint Container
Blueprint bundles
Blueprint XML
OSGi bundles and bundle archives

Related tasks:

Create a service bundle
Create an OSGi application
Deploy an OSGi application as a business-level application
Debug bundles at run time using the command-line console
Secure OSGi Applications
Develop a composite bundle

Related reference:

OSGi Service Platform Release 4 v4.2 Enterprise Specification

Accept OSGi applications with the Blueprint Container specification

Best practices for developing and working with OSGi applications

Develop enterprise OSGi applications for WAS
OSGi Applications: Troubleshooting tips
Task topic Feedback
Copyright IBM Corporation 2009, 2011. All Rights Reserved.
This information center is powered by Eclipse technology.

+

Search Tips   |   Advanced Search