Java Reference
In-Depth Information
<?xml version="1.0" encoding="UTF-8"?>
<MetaData xmlns="http://www.osgi.org/xmlns/metatype/v1.0.0">
<OCD name="EchoServer" id="4.7.1.1" description="Echo Server Config">
<AD name="port" id="4.7.1.1.1" type="Integer"
description="The port the Echo Server listens on"/>
</OCD>
<Designate pid="org.foo.managed.service">
<Object ocdref="4.7.1.1"/>
</Designate>
</MetaData>
Don't let this somewhat obtuse
XML
fool you. It's simple. You first define an
Object-
ClassDefinition
(
OCD
) called
EchoServer
, with a unique identifier of
4.7.1.1
(if
you have a matching
LDAP
/X.500 object class
OSG
i object identifier (
OID
), you can
use that one; otherwise, use any other reasonably unique name that follows the same
grammar as the
LDAP
/X.500
OID
and a human-readable description). You specify an
attribute definition (
AD
) to describe the configuration properties the echo server
needs. In this case, there's only one:
port
. Notice the
Designate
element: this is
where you make the link between the type (the
OCD
) and the instance (the
PID
). In
this example, the
EchoServer
description applies to the configurations of managed
services with the
PID
org.foo.managed.service
.
USING METATYPE INFORMATION
To u s e m e t a t y p e i n f o r m a t i o n , y o u u s e t h e M e t a t y p e S e r v i c e t o l o o k u p m e t a t y p e d e f i -
nitions. The Metatype Service is represented by the following interface:
public interface MetaTypeService {
public MetaTypeInformation getMetaTypeInformation(Bundle bundle);
}
Using the discovered metatype information, you can generate user interfaces or vali-
date configurations, for example. To demonstrate how to use the Metatype Service,
let's add a
type
command to the shell to display metatype information, as follows.
Listing 9.6 Metatype Service shell command example
public class MetaDataCommand extends BasicCommand {
public void exec(String args, PrintStream out, PrintStream err)
throws Exception {
MetaTypeService mts = getMetaTypeService();
Bundle b = getBundle(args);
MetaTypeInformation mti = mts.getMetaTypeInformation(b);
String[] pids = mti.getPids();
for (int i = 0; i < pids.length; i++) {
out.println(pids[i]);
ObjectClassDefinition ocd = mti.getObjectClassDefinition(
pids[i], null);
AttributeDefinition[] ads = ocd
.getAttributeDefinitions(ObjectClassDefinition.ALL);
for (int j = 0; j < ads.length; j++) {
out.println("\tOCD=" + ocd.getName());