Java Reference
In-Depth Information
method of the ServiceTracker super class. You check whether the service's intents
match the supported intents and configurations passed into the constructor. If so, you
determine the set of interfaces by which the service should be exported. Finally, the
service is exported to your remote registry.
NOTE This example exports the service multiple times for each desired inter-
face. In a real-world scenario, it might be more appropriate to register the ser-
vice once with multiple interfaces. The approach used in this topic is for
conceptual simplicity only.
The next listing shows how to check whether a matching service is supported by the
registry. The process compares the matching intents and configurations with the ones
supplied to your ExportedServiceTracker 's constructor.
Listing 15.15 Checking if a service matches supported intents and configurations
private boolean isValidService(ServiceReference ref) {
List<String> list = readIntents(ref);
list.removeAll(Arrays.asList(intents));
if (list.isEmpty()) {
list = readConfigs(ref);
list.removeAll(Arrays.asList(configs));
return list.isEmpty();
}
else {
return false;
}
}
This code reads the intent and configuration values from the matching service's ser-
vice properties. You then use String.equals() via List.removeAll() to verify that
the service doesn't export any intents and configurations your remote registry doesn't
support, respectively.
NOTE The isValidService() method is a naïve implementation for checking
whether a given service matches your remote registry's supplied intents and con-
figurations. It's naïve because it doesn't take into account the qualified naming
convention mentioned in section 15.2.1. A proper implementation needs to do
this, but the logic to achieve it is too long to list here and doesn't add much to
the discussion. We'll neatly skip over it and leave it as an exercise for you.
You now need to find out the inter faces that matching services wish to export
remotely. The following listing shows how to find these interfaces. The method find-
ExportedInterfaces() returns a String[] containing the interface names or null if
the service isn't exported.
Listing 15.16 Checking the exported interfaces of a service
private String[] findExportedInterfaces(ServiceReference ref) {
Object ifaces = ref.getProperty("service.exported.interfaces");
if (ifaces == null) {
 
Search WWH ::




Custom Search