Create a User Feature with an OSGi Service

This article is a continuation from the previous “Create your own simple “Hello World” User Feature”.

This article is a continuation from the previous “Create your own simple “Hello World” User Feature” which can be found here.

Updating the User Feature Bundle

Open the “UserFeatureBundle” user feature again and perform the following actions:

  1. Right click the “src” folder and select New > Interface.
  2. Give it the following properties:
    • Package: “com.ibm.wasdev.tutorial.service”.
    • Name: “ServiceProvider”.
  3. Click finish.
  4. Create the method:
    				String getHelloWorld();
  5. Double click the manifest file under BundleContent > META-INF.
  6. Click the “Runtime” tab.
  7. Under “Exported Packages” click add, and select the “com.ibm.wasdev.tutorial.service” package.

Next, open the previously created “HelloWorld” class which looks like:

package com.ibm.wasdev.tutorial.userfeature;
public class HelloWorld {
	public String getHelloWorld() {
		return "Hello World!";
	}
}
			

and change it to implement the new Interface:

package com.ibm.wasdev.tutorial.userfeature;
import com.ibm.wasdev.tutorial.service.ServiceProvider;
public class HelloWorld implements ServiceProvider{
	public String getHelloWorld() {
		return "Hello World!";
	}
}
			

Next we need to make the Service available, to do this we are going to use blueprint:

  1. Right Click the “UserFeatureBundle” bundle and select New > blueprint file
  2. Click Finish.
  3. Double click the newly created blueprint file under BundleContent > OSGI-INF > blueprint.
  4. Select the “Design” tab
  5. Under “Overview” click “Add…”
  6. Select “Service” and click “OK”.
  7. Under “Service Interface” click “Browse…” and point it to the interface class.
  8. Under “Service ID” type OSGIServiceRef.
  9. Under “Bean Reference” click “New…”, then “Browse…” in the new window and finally point it to the “HelloWorld” class.
  10. Click “OK” twice.

Updating the User Feature

Now we need to update the user feature manifest to include IBM-API-Package and IBM-API-Service, to do this open the previously created Liberty Feature “UserFeature” and double click “Manifest: UserFeature” and add the following to the bottom of the file:

	IBM-API-Package: com.ibm.wasdev.tutorial.service
	IBM-API-Service: com.ibm.wasdev.tutorial.service.ServiceProvider 

The final step for the User Feature is to now re-install it, to do this right click “UserFeature” and click “Update Feature…”.

Creating a Web Application Bundle to consume our service

We’ve now created and registered an OSGi service in a User Feature, lets use it from a servlet:

  1. Click file and select New > OSGi Bundle Project.
  2. Set the following properties:
  • Name: “HelloWorldWAB”.
  • Target runtime: “WebSphere Application Server V8.5 Liberty Profile”.
  • Add Web Support: checked and set to “Web 3.0″.
  • Click finish.
  • Open the manifest on the new project and go to the “Dependencies” tab.
  • Under “Imported Packages” delete all of the packages except “javax.servlet”, “javax.servlet.annotation” and “javax.servlet.http”
  • Click “Add…” by the imported packages and add “com.ibm.wasdev.tutorial.service”
  • Click “Add…” by the imported packages and add “javax.naming”

Next, we need to create a blueprint file to reference the OSGi Service thats been made available in the User Feature.

  1. Right Click the “HelloWorldWAB” and select New > blueprint file
  2. Click Finish.
  3. Double click the newly created blueprint file under BundleContent > OSGI-INF > blueprint.
  4. Select the “Design” tab
  5. Under “Overview” click “Add…”
  6. Select “Reference” and click “OK”.
  7. Under “Reference Interface” click “Browse…” and point it to the interface class.
  8. Under “Reference ID” type OSGIServiceRef.
  9. Click “OK” twice.

Finally, we need to create a servlet to consume the User Feature’s service

  1. Right click on the project and select New > Servlet
  2. Set the properties:
    • Java package: “com.ibm.wasdev.tutorial.servlet”
    • Class name: “HelloWorldServlet”
  3. Click finish.
  4. Add the following code to the class:
    @WebServlet("/HelloWorldServlet")
    public class HelloWorldServlet extends HttpServlet {
    	private static final long serialVersionUID = 1L;
    
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		try {
    		      InitialContext ic = new InitialContext();
    		      ServiceProvider serviceProvider = (ServiceProvider) ic.lookup("blueprint:comp/OSGIServiceRef");
    		      response.getWriter().println(serviceProvider.getHelloWorld());
    		    } catch (NamingException e) {
    		      e.printStackTrace();
    		    }
    	}
    }
    					

Running the application

Our application is now ready to print the date in a browser! If you had the application installed into a Liberty Profile server then as you made changes it should of been updated and new features installed. If not drag the application project onto the server in the “Servers” view. The “wab-1.0″ and “jndi-1.0″ should now be enabled automatically in the feature manager of the server configuration. To run the servlet right click on the HelloWorldServlet class and select “Run as…” -> “Run on server”. Next time we will look at how to make writing servlets even simpler by using blueprint to define and manage the service dependencies.

  • Dave Westerman

    I’m not sure why, since I’m pretty sure I have the latest up-to-date Eclipse Juno, but when I create the web app OSGi bundle, Eclipse is not creating a web.xml file. So I end up getting the following error:

    Error 404: SRVE0190E: File not found: /

    In case anyone else runs into this problem, I created the following web.xml file, which allows the app to work (sorry about the ugliness, I can’t get it to format the code, it’s ignoring my indentations):

    HelloWorldWAB

    HelloWorldServlet
    HelloWorldServlet
    com.ibm.wasdev.tutorial.servlet.HelloWorldServlet
    1
    true
    false

    HelloWorldServlet

    /*

    • Dave Westerman

      Oops, never mind – the web.xml file is not needed. The problem I ran into is that the annotation in your servlet requires you to append ‘/HelloWorldServlet’ to your URL, if you are using a browser (which I was). If I’d have followed your directions exactly, by running the servlet directly, I would have been ok. :-)