Java Reference
In-Depth Information
naming convention, but the files can go anywhere within the bundle. It's possible to
include multiple component files in a single bundle using a comma-delimited set, a *
wildcard pattern, or a mixture of the two.
Fragmented components
It's also possible to place component descriptions into bundle fragments (which we
covered in chapter 5). In this scenario, only the host bundle's Service-Component
manifest header is read, although the XML files may reside in the bundle fragments.
A possible use case for doing this is if you want to support several different compo-
nent configuration options where you choose at deployment time which is instantiat-
ed. We don't classify this as a recommended use case, because fragments bring in
all sorts of complex lifecycle issues and break a number of best practices with re-
spect to modular boundaries, but we cover it here for the sake of completeness.
This explains the lack of a bundle activator for your component bundle, but how
exactly does your SimpleShape service get published? Next, we'll look more closely at
the component description file to see how components declare their provided services.
11.3.2
Providing services with Declarative Services
The following code snippet shows the declaration used to tell the Declarative Services
framework to publish your Circle class as a service in the OSG i service registry under
the SimpleShape interface:
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0">
<property name="simple.shape.name" value="Circle" />
<property name="simple.shape.icon" value="circle.png" />
<scr:implementation class="org.foo.shape.circle.Circle" />
<scr:service>
<scr:provide interface="org.foo.shape.SimpleShape"/>
</scr:service>
</scr:component>
In this metadata, you define two properties for your component. Properties may be
used to configure a component (which we'll look at shortly), but they also act as ser-
vice properties and are automatically attached to services published by a component.
In this case, these properties are used by your paint frame to identify the shape. You
define the component's implementation class, which must be reachable on the bun-
dle class path where this component description is located. Finally, you declare that
the component provides the SimpleShape service. Let's now look at the Circle class.
Listing 11.2 Circle class used in the Declarative Services paint example
public class Circle implements SimpleShape {
public void draw(Graphics2D g2, Point p) {
int x = p.x - 25;
int y = p.y - 25;
Search WWH ::




Custom Search