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


Create a service bundle

For the simple hello-world OSGi application, the service bundle declares 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.
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.helloWorldService. This bundle declares the HelloWorldEBA interface, and exports it as an OSGi service. The exported service is used by client bundle com.ibm.ws.eba.helloWorldClient, as described in Create a client bundle.

In this simple example, the interface definition and implementation are co-located in the same bundle. In a more realistic business scenario, you would separate the service interface declaration and service implementation into separate bundles, because the interface declaration is likely to change far less often than the business implementation of the service.


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.helloWorldService.
      • 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. Declare and implement the HelloWorld service.

    1. Create a package called com.ibm.ws.eba.helloWorld, that includes an interface called HelloWorldEBA. Code this interface to contain just one method: hello().

      In a later step, you create code in a Blueprint XML file that exposes this interface as a service.

      1. Under your com.ibm.ws.eba.helloWorldService project, right-click the folder src, then select new > package.
      2. Name the new package com.ibm.ws.eba.helloWorld.

      3. Click Finish.
      4. Right-click the new package, then select new > interface.
      5. Name the new interface HelloWorldEBA.

      6. Click Finish.
      7. Copy and paste the following method to replace the content of the interface file:
        package com.ibm.ws.eba.helloWorld;
        public interface HelloWorldEBA {
        
            public void hello();
        }
        

      8. Save and close the file.

    2. Create a package called com.ibm.ws.eba.helloWorld.impl, that includes an implementation class called HelloWorldEBAImpl. 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.

      1. Under your com.ibm.ws.eba.helloWorldService project, right-click the folder src, then select new > package.
      2. Name the new package com.ibm.ws.eba.helloWorld.impl.

      3. Click Finish.
      4. Right-click the new package, then select new > class.
      5. Name the new interface implementation class HelloWorldEBAImpl.

      6. Click Finish.
      7. Copy and paste the following method to replace the content of the interface implementation class file:
        package com.ibm.ws.eba.helloWorld.impl;
        
        import com.ibm.ws.eba.helloWorld.HelloWorldEBA;
        
        public class HelloWorldEBAImpl implements HelloWorldEBA {
            @Override
            public void hello() {
                System.out.println("OSGi Service: Hello World!");
            }
        }
        

      8. Save and close the file.

  3. 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.helloWorldService, create a Blueprint XML file:

      1. Click File > New > Other > OSGi > Blueprint File.
      2. Accept the default values for all the options on this panel.

      3. Click Finish.

    2. Update the Blueprint XML file to contain the following Blueprint information:
      <?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 id="helloEBA" class="com.ibm.ws.eba.helloWorld.impl.HelloWorldEBAImpl"/>
      <service ref="helloEBA" interface="com.ibm.ws.eba.helloWorld.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.impl.HelloWorldEBAImpl class.

        • The id attribute identifies the bean. We 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.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.

      See section 121.5 "Bean Manager" and section 121.6 "Service Manager" of the OSGi Service Platform Release 4 v4.2 Enterprise Specification.

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

  4. Configure the com.ibm.ws.eba.helloWorld package as an exported package.

    Edit the bundle manifest to allow other bundles to load classes from the com.ibm.ws.eba.helloWorld 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.

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

    2. Click the Runtime tab.

    3. In the Exported Packages pane, click Add.

    4. Select the com.ibm.ws.eba.helloWorld package from the list, then click OK.

    5. In the Exported Packages pane, click Properties.

    6. In the Properties dialog, set the version to 1.0.0, then click OK.

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


Results

You have created a bundle called com.ibm.ws.eba.helloWorldService. This bundle contains the business logic and metadata needed to export the HelloWorldEBA service.


What to do next

We can now create the client bundle that uses the HelloWorldEBA service.

Parent topic: Develop and deploying an OSGi application

Related concepts:

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

Related tasks:

Create a client 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