Java Reference
In-Depth Information
void <method-name>(ServiceReference);
void <method-name>(<parameter-type>);
void <method-name>(<parameter-type>, Map);
The first form injects the service's associated ServiceReference into the component
instead of the service object itself. This allows the component to find out which ser-
vices are available in the framework without retrieving them. This method is typically
used in conjunction with the ComponentContext , which we'll discuss a little later, to
implement extremely lightweight solutions where service objects are created only
when absolutely necessary.
The second form should look familiar to most programmers who have used some
form of dependency-injection framework. Using this binding method, the Declarative
Services implementation retrieves the actual service object from the OSG i service reg-
istry and injects it into the component. The component developer may choose to
store a reference to the service object; but you must take care to dereference the ser-
vice when the corresponding unbind method is called, to prevent memory leakage.
The third form behaves much like the second, except that the associated service
properties are also injected into the component. Because you need the service proper-
ties to retrieve the shape name and icon for the paint frame component, this is the
form you'll use. The PaintFrame.addShape() method is as follows:
void addShape(SimpleShape shape, Map attrs) {
final DefaultShape delegate = new DefaultShape(shape);
final String name = (String) attrs.get(SimpleShape.NAME_PROPERTY);
final Icon icon = new ImageIcon(shape.getClass().getResource(
(String) attrs.get(SimpleShape.ICON_PROPERTY)));
m_shapes.put(name, delegate);
...
}
The Declarative Services framework calls this addShape() method when any Simple-
Shape service is published in the OSG i service registry, passing in the service and the
associated map of service properties. You read the name property of the shape and
load its ImageIcon representation. As we mentioned earlier, the Declarative Services
specification is only able to handle simple property types, so in this version of the
paint frame component you have to explicitly load the resource via the shape object's
class loader. Finally, you store a reference to the shape service object in an internal
map for use later.
Conversely, when a shape service is removed from the service registry, the Declara-
tive Services framework invokes the PaintFrame.removeShape() method:
void removeShape(SimpleShape shape, Map attrs) {
final String name = (String) attrs.get(SimpleShape.NAME_PROPERTY);
DefaultShape delegate = (DefaultShape) m_shapes.remove(name);
...
}
Search WWH ::




Custom Search