Java Reference
In-Depth Information
enabled. Concerned users can work around this using a nonservice helper delegate
that manages the reference list—although this is a lot of work compared to marking
the methods as nonpublic.
Let's use your newfound Blueprint knowledge and continue to convert the paint
program to use it.
PAINTING WITH BLUEPRINT
Your paint frame component has a dependency on SimpleShape services, and it pro-
vides a Window service. The following listing provides its definition in the Blueprint
XML syntax.
Listing 12.2 Blueprint definition of the PaintFrame component
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
<bean id="paintFrame" class="org.foo.paint.PaintFrame"
init-method="activate"
destroy-method="deactivate"/>
B
Listens for
SimpleShape
services
<reference-list id="shape"
interface="org.foo.shape.SimpleShape"
availability="optional">
<reference-listener
bind-method="addShape"
unbind-method="removeShape"
ref="paintFrame"/>
</reference-list>
C
Injects
services
<service id="window"
interface="org.foo.windowlistener.api.Window"
ref="paintFrame"/>
</blueprint>
You begin by defining the paint frame component and specifying lifecycle methods to
be invoked by the Blueprint container after its properties have been injected; we'll
leave the details of these for the next section. You use the <reference-list> element
at B to ask the Blueprint container to listen for all SimpleShape services it finds in the
OSG i service registry.
Notice that you use the availability attribute on the <reference-list> ele-
ment. As with Declarative Services, Blueprint includes the notion of mandatory and
optional service dependencies, where mandatory dependencies impact the compo-
nent lifecycle and optional ones don't. We'll discuss this more when we cover the
Blueprint component lifecycle. Possible values for the availability attribute are
optional and mandatory ; the default is mandatory .
Nested in this <reference-list> element is a <reference-listener> element C .
This tells the Blueprint container to inject the services into the addShape() and
removeShape() methods of the paint frame component. You choose this approach
because your paint frame needs to react dynamically to the arrival and departure of
shape services.
 
Search WWH ::




Custom Search