Java Reference
In-Depth Information
shape and use metadata to describe various nonfunctional attributes like its name and
icon. Remember that the first thing to define when creating a new service is the con-
tract. What should a shape service look like?
4.4.1
Defining a shape service
Let's use the previous interface as the basis of the new service contract—but this time,
instead of extension names, you'll declare service property names. These names will
tell the client where to find additional metadata about the shape:
public interface SimpleShape {
public static final String NAME_PROPERTY = "simple.shape.name";
public static final String ICON_PROPERTY = "simple.shape.icon";
public void draw(Graphics2D g2, Point p);
}
This isn't much different from the interface defined in section 3.4. You can see how
easy it is to switch over to services when you're programming with interfaces. With this
contract in hand, you now need to update each shape bundle to publish its implemen-
tation as a service, and update the paint frame bundle to track and consume these
shape services.
4.4.2
Publishing a shape service
Before you can publish a shape implementation as a service, you need a bundle con-
text. To get the bundle context, you need to add a bundle activator to each shape bun-
dle, as shown in the following listing.
Listing 4.15 Publishing a shape service
public class Activator implements BundleActivator {
private BundleContext m_context = null;
public void start(BundleContext context) {
m_context = context;
Hashtable dict = new Hashtable();
dict.put(SimpleShape.NAME_PROPERTY, "Circle");
dict.put(SimpleShape.ICON_PROPERTY,
new ImageIcon(this.getClass().getResource("circle.png")));
m_context.registerService(SimpleShape.class.getName(),
new Circle(), dict);
}
Publishes new
shape service
public void stop(BundleContext context) {}
}
You record the name and icon under their correct service properties. The shape bun-
dles will now publish their shape services when they start and remove them when they
stop. To use these shapes when painting, you need to update the paint frame bundle
so it uses services instead of bundles, as shown in figure 4.14.
 
Search WWH ::




Custom Search