Java Reference
In-Depth Information
}
try {
m_server = new EchoServer(port);
m_server.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
...
}
The
ManagedService
interface has a single
updated()
method. The argument to this
method is a
Dictionary
containing the configuration properties.
Configuration properties
A configuration dictionary contains a set of properties in a
Dictionary
object. The
name or key of a property must always be a
String
object and isn't case sensitive
during lookup, but preserves the original case. The values should be of type
String
,
Integer
,
Long
,
Float
,
Double
,
Byte
,
Short
,
Character
,
Boolean
, or the primitive
counterparts. Furthermore, they can be arrays or collections of them. For arrays and
collections, they must only contain values of the same type.
In this example, a simple echo server listens on a port and sends back whatever it
receives. Because it's good practice, you make the port configurable. When you
receive a new configuration, you first stop the existing server, if there is one. Then,
you check whether you received a
null
configuration, which indicates that the previ-
ous configuration was deleted and there is no new one. If this is the case, there's noth-
ing else to do. Otherwise, you get the port number from the dictionary and verify its
existence. If it exists, you parse it and create and start a new server for the given port.
A
ManagedService
is associated with one configuration object. A bundle can regis-
ter any number of
ManagedService
services, but each must be identified with its own
PID
. You should use a
ManagedService
when configuration is needed for a single
entity in the bundle or where the service represents an external entity like a device.
Then, for each detected device, a
ManagedService
is published with a
PID
related to
the identity of the device, such as the address or serial number.
What about cases where you want to configure more than a single entity using the
same
PID
, such as creating multiple instances of the same service with different config-
urations? You use a
ManagedServiceFactory
, which we'll explore next.
IMPLEMENTING A MANAGED SERVICE FACTORY
You should use a
ManagedServiceFactory
when a bundle doesn't have an internal or
external entity associated with the configuration information, but can handle more than
one configuration at the same time. Remember, with a
ManagedService
, there's only one
configuration: the configuration for the specific
PID
. With a
ManagedServiceFactory
,