< Previous | Next >

Lesson 2: Develop the business logic

About this task

In this lesson, you create the business logic for your simple OSGi application. The business logic uses a POJO component assembly model. This example application provides a simple counter that increments each time it is accessed.

Creating the package

Procedure

  1. In Enterprise Explorer, right-click CounterServiceBundle/src and then click New > Package. The New Java Package wizard opens.
  2. In the Name field, type com.ibm.ws.eba.counter and then click Finish. The package is created in the src folder.

Creating the interface class

Procedure

  1. Right-click the package com.ibm.ws.eba.counter and then click New > Interface. The New Java Interface wizard opens.
  2. In the Name field, type Counter and then click Finish. The interface is created in the package and it opens in the editor.
  3. Add the getCount() method to the interface. The following code is the result:
    package com.ibm.ws.eba.counter;
    public interface Counter {
    	public int getCount();
    }
  4. Save Counter.java.

Creating the implementation class

Procedure

  1. Right-click the package com.ibm.ws.eba.counter and then click New > Class. The New Java Class wizard opens.
  2. In the Name field, type CounterImpl.
  3. Click Add. The Implemented Interfaces Selection dialog opens.
  4. In the Choose interfaces field, type Counter. Select the Counter interface for com.ibm.ws.eba.counter and then click OK.
  5. Click Finish. The class is created in the package and it opens in the editor.
  6. Add the implementation for the getCount() method. Add an initialization method that confirms that the service starts on the server. The following code is the result:
    package com.ibm.ws.eba.counter;
    
    public class CounterImpl implements Counter {
    
         private int count = 0;
    
         @Override
         public int getCount() {
              return count++;
         }
    		
         public void init() {
              System.out.println("CounterImpl.init() called.");
         }
    }
  7. Save CounterImpl.java.

Results

Your OSGi bundle project.

Exporting the package

About this task

By adding your package to the Export Packages list, you expose only this package to other clients outside of the bundle. All other packages are hidden from clients outside of the bundle. Use the Export Packages list to specify the name of any package that you want your bundle to export to the run time. If you do not specify the packages that are required by other bundles, the dependent bundles might not resolve.

Procedure

  1. Double-click Manifest:CounterServiceBundle to open the bundle manifest file in the editor.
  2. Select the Runtime tab.
  3. In the Exported Packages section of the editor, click Add. The Exported Packages dialog opens.
  4. Click com.ibm.ws.eba.counter from the packages list and then click OK.
  5. Save the bundle manifest file.

Lesson checkpoint

You created the business logic for the OSGi Counter Service application.

In this lesson, you learned about the following topics:
  • How to create a Java package.
  • How to create a Java interface file.
  • How to create a Java method.
  • How to create an implementation class.
  • How to declare the packages that are visible outside of the bundle by using the Export-Package property in the bundle manifest file.
< Previous | Next >
Icon that indicates the type of topic Tutorial lesson topic
Timestamp icon Last updated: July 17, 2017 21:58

File name: counter_lesson2.html