Create an application that consumes a Web service
From BUG Wiki
Contents |
Create a basic application for BUG using a Web service that displays a message on the system output console.
Create the project
- If necessary, switch Perspectives to the Dragonfly perspective.
- Go to File > New > Project
Or, click the New BUG Project icon in the toolbar. - In the New Project wizard, click the arrow to open the Dragonfly node.
- Select BUG Application, then click Next.
- In the New BUG Project window:
In the Name field, type: LCD Helloworld
In the Target section, leave Virtual BUG selected. - Click Find BUG services.
- In the Find BUG services list, mark the checkbox next to com.bugalbs.status.IStatusBarProvider, then click Finish.
- In left-side Project Explorer view, expand the lcdhelloworld.servicetracker node.
- Double-click to edit the LCD_HelloworldServiceTracker.java file.
- 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.
- In doStart() add the code in Snippet A below.
- In doStop() add the code in Snippet B.
- Add the fields in Snippet C up towards the top of the class.
- 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 World!".length());
if (key != null) {
statusBar.write(key, "Hello World!");
}
Snippet B
if (key != null) {
statusBar.releaseRegion(key);
}
Snippet C
private IStatusBarProvider statusBar; private String key;
Snippet D
import 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
- 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()).
- 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.
