Java Reference
In-Depth Information
the same factory can have any number of configurations. Using this approach, you can
instantiate a service for each configuration associated with your managed service factory,
for example. This way, by creating a new configuration for the managed service factory,
you create new service instances. A slightly different use case is related to services rep-
resenting entities that can't be identified directly, such as devices on a USB port that can't
provide information about their type. Using a ManagedServiceFactory , you can define
configuration information for each available device attached to the USB port.
How does this work with respect to the PID s? The trick in this case is that you regis-
ter the ManagedServiceFactory with a factory.pid service property. This way, the
Configuration Admin Service can differentiate between a managed service factory
and a managed service. For the managed service factory, it assigns a new and unique
PID to each created configuration for the factory. The interface to implement looks
like this:
public interface ManagedServiceFactory{
public String getName();
public void updated(String pid, Dictionary properties)
throws ConfigurationException;
public void deleted(String pid);
}
The following example uses a ManagedServiceFactory to configure echo services that
read from their configured port and send back whatever they receive along with their
name.
Listing 9.2 ManagedServiceFactory example
public class ManagedServiceFactoryExample implements
ManagedServiceFactory {
private final Map<String, EchoServer> m_servers =
new HashMap<String, EchoServer>();
public synchronized void deleted(String pid) {
EchoServer server = m_servers.remove(pid);
if (server != null) {
server.stop();
}
}
public String getName() {
return getClass().getName();
}
public synchronized void updated(String pid, Dictionary properties)
throws ConfigurationException {
EchoServer server = m_servers.remove(pid);
if (server != null) {
server.stop();
}
if (properties != null) {
String portString = (String) properties.get("port");
if (portString == null) {
Accepts
PID
B
 
Search WWH ::




Custom Search