Java Reference
In-Depth Information
Providing services is straightforward: a component declares which internal class
implements which provided service interface, and the Declarative Services frame-
work publishes the service at execution time after the bundle containing the compo-
nent descriptions is activated. What happens if the component has dependencies on
other services?
11.3.3
Consuming services with Declarative Services
To s e e h o w D e c l a r a t i v e S e r v i c e s c o m p o n e n t s c a n u s e s e r v i c e s , l e t 's t u r n o u r a t t e n t i o n
to the paint program's paint frame. Again, you modify the bundle to contain a compo-
nent description in OSGI-INF /paint.xml, as shown next.
Listing 11.3 Declarative Services' description of PaintFrame component
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0"
name="paint"
immediate="true">
<scr:implementation class="org.foo.paint.PaintFrame" />
<scr:reference
interface="org.foo.shape.SimpleShape"
cardinality="0..n"
policy="dynamic"
bind="addShape"
unbind="removeShape"/>
<scr:service>
<scr:provide interface="java.awt.Window"/>
</scr:service>
<scr:property name="name" value="main"/>
</scr:component>
There's quite a bit of information in this component description. Looking at some of
the aspects that are similar to the circle component, you see the component declara-
tion, but this time you assign paint as its name. You specify the component implemen-
tation class, which you indicate as providing a java.awt.Window service with a service
property, called name . You introduce a new element B , defining a “reference” (a
dependency) to a service implementing the SimpleShape interface.
If you're familiar with other dependency-injection frameworks, such as Spring, this
should be starting to feel familiar. You specify the method PaintFrame.addShape() to
use for injecting discovered SimpleShape services. Also, because you're in an OSG i
environment where services can come and go, you specify the method Paint-
Frame.removeShape() to use when a shape service goes away. There are other service-
dependency characteristics, but before we go into the details of those, let's talk more
about binding methods.
B
Defines reference
to service
BINDING METHOD SIGNATURES
The Declarative Services specification defines the following method signatures for
binding methods:
Search WWH ::




Custom Search