Create an application that consumes a Web service

From BUG Wiki

Jump to: navigation, search

Contents

Create a basic application for BUG using a Web service that displays a message on the system output console.

Create the project

  1. If necessary, switch Perspectives to the Dragonfly perspective.
  2. Go to File > New > Project
    Or, click the New BUG Project icon in the toolbar.
  3. In the New Project wizard, click the arrow to open the Dragonfly node.
  4. Select BUG Application, then click Next.
  5. In the New BUG Project window:
    In the Name field, type: LCD Helloworld
    In the Target section, leave Virtual BUG selected.
  6. Click Find BUG services.
  7. In the Find BUG services list, mark the checkbox next to com.bugalbs.status.IStatusBarProvider, then click Finish.
  8. In left-side Project Explorer view, expand the lcdhelloworld.servicetracker node.
  9. Double-click to edit the LCD_HelloworldServiceTracker.java file.
  10. Find the doStart() method. This method gets called by Concierge when all our requested services are available. In this instance, we can expect IStatusBarProvider to be available.
  11. In doStart() add the code in Snippet A below.
  12. In doStop() add the code in Snippet B.
  13. Add the fields in Snippet C up towards after public class LCD_HelloworldServiceTracker extends AbstractServiceTracker {
  14. At the very top, with the other import statements, add Snippet D.

LCD Helloworld code snippets

Snippet A

statusBar = (IStatusBarProvider) this.getService(IStatusBarProvider.class);
key = statusBar.acquireRegion("Hello".length());

if (key != null) {
    statusBar.write(key, "Hello");
}


Snippet B

if (key != null) {
    statusBar.releaseRegion(key);
}


Snippet C

private IStatusBarProvider statusBar;
private String key;

Snippet D

import com.buglabs.status.IStatusBarProvider;

Example after adding Snippets

*	Generated by Dragonfly SDK
 *
 */
package lcd_helloworld.servicetracker;

import org.osgi.framework.BundleContext;
import com.buglabs.application.AbstractServiceTracker;
import com.buglabs.status.*;

 /**
 *	Service tracker for the BugApp Bundle;
 *
 */
public class LCD_HelloworldServiceTracker extends AbstractServiceTracker {	

	private IStatusBarProvider statusBar; 
	private String key;
	
	public LCD_HelloworldServiceTracker(BundleContext context) {
		super(context);
	}
	
	/**
	 * Determines if the application can start.
	 */
	public boolean canStart() {
		return super.canStart();
	}
	
	/**
	 * If canStart returns true
     * this method is called to start the application.
     * Place your fun logic here. 
	 */
	public void doStart() {
		System.out.println("LCD_HelloworldServiceTracker: start");
		statusBar = (IStatusBarProvider) this.getService(IStatusBarProvider.class);
			key = statusBar.acquireRegion("Hello".length());
			if (key != null) { 
				statusBar.write(key, "Hello");
			} 
	}

	/**
	 * Called when a service that this application depends is unregistered.
	 */
	public void doStop() {
		System.out.println("LCD_HelloworldServiceTracker: stop");
		if (key != null) { statusBar.releaseRegion(key);
		} 
	}

	/**
	 * Allows the user to set the service dependencies by
     * adding them to services list returned by getServices().
     * i.e.nl getServices().add(MyService.class.getName());
	 */
	public void initServices() {
		getServices().add("com.buglabs.status.IStatusBarProvider");
	}
	
}

Run it!

Stop the Virtual BUG by pressing Esc when the Virtual BUG is selected, and restart the Virtual BUG. You should see the global greeting on the LCD screen.

Concepts

  1. We use an OSGi utility class called the ServiceTracker so that our code runs when and if the services we need are available. When the Activator creates the service, it tells the OSGi runtime which services we are interested in. This information is actually contained in the ServiceTracker implementation (initServices()).
  2. The Status Bar is a shared resource, so we must interact with it in such a way that we do not overwhelm other programs. The IStatusBar.aquireRegion method is the way in which we do this. The IStatusBar implementation manages its resources and will give us what we ask for if it is available. Be sure to release the region when your application stops so that it is free for use by other bundles.