Java Reference
In-Depth Information
Service layer
Service registry
ManagedService
ManagedServiceFactory
Management layer
Figure 9.5 Difference between a ManagedService and ManagedServiceFactory
connects these two layers together when it delivers the configuration data. Of course,
the reverse is also possible, and the Configuration Admin Service may tell a managed
service that its configuration has gone away, which means it needs to stop performing
its functionality because it no longer has a valid configuration. This approach gives you
a flexible system, where you can configure and control any kind of service or any num-
ber of service instances in a common way. Let's look into the details of implementing
a managed service next.
IMPLEMENTING A MANAGED SERVICE
Now that you understand the underlying basics of how the Configuration Admin Ser-
vice works by associating configuration data to managed services, let's explore an
example. The actual interface you need to implement looks like the following:
public interface ManagedService {
public void updated(Dictionary properties) throws ConfigurationException;
}
The following listing shows an example ManagedService implementation.
Listing 9.1 Example of a managed service
public class ManagedServiceExample implements ManagedService {
private EchoServer m_server = null;
public synchronized void updated(Dictionary properties)
throws ConfigurationException {
if (m_server != null) {
m_server.stop();
m_server = null;
}
if (properties != null) {
String portString = (String) properties.get("port");
if (portString == null) {
throw new ConfigurationException(null, "Property missing");
}
int port;
try {
port = Integer.parseInt(portString);
} catch (NumberFormatException ex) {
throw new ConfigurationException(null, "Not a valid port number");
Search WWH ::




Custom Search