gov.sns.tools.services
Class ServiceDirectory

java.lang.Object
  |
  +--gov.sns.tools.services.ServiceDirectory

public final class ServiceDirectory
extends java.lang.Object

ServiceDirectory is a local point of access for registering and looking up services on a network. It wraps the standard Rendezvous mechanism to provide a simple way to register and lookup services by using a Java interface as a service name. XML-RPC is the communication protocol used for messaging. Both Rendezvous and XML-RPC are accepted protocols implemented in multiple languages.


Field Summary
protected  java.util.Map listenerMap
          Table mapping ServiceListener to the corresponding rendezvous service listener
 
Constructor Summary
ServiceDirectory()
          ServiceDirectory constructor.
 
Method Summary
 void addServiceListener(java.lang.Class protocol, ServiceListener listener)
          Add a listener for addition and removal of service providers.
 void addServiceListener(java.lang.String type, ServiceListener listener)
          Add a listener for addition and removal of service providers.
static ServiceDirectory defaultDirectory()
          Get the default ServiceDirectory instance.
 void dispose()
          Shutdown rendezvous and the web server and dispose of all resources.
 ServiceRef[] findServicesWithType(java.lang.Class protocol, long timeout)
          Convenience method for making a request to find service providers of a specific service type and waiting a specified amount of time to find those services.
 ServiceRef[] findServicesWithType(java.lang.String serviceType, long timeout)
          Convenience method for making a request to find service providers of a specific service type and waiting a specified amount of time to find those services.
protected static gov.sns.tools.services.ClientHandler getClientHandler(java.lang.Object proxy)
          Convenience method for getting the ClientHandler for the given proxy
protected static java.lang.String getDefaultType(java.lang.Class protocol)
          Form a valid type based on the specified protocol by replacing the protocol's name with a valid name in which "." is replaced by "_".
static java.lang.String getHost(java.lang.Object proxy)
          Get the remote host for the service
static java.lang.Class getProtocol(java.lang.Object proxy)
          Get the protocol of the remote proxy
 java.lang.Object getProxy(java.lang.Class protocol, ServiceRef serviceRef)
          Get a proxy to the service with the given service reference and protocol.
static java.lang.String getServiceName(java.lang.Object proxy)
          Get the service name for the remote service
 ServiceRef lookupService(java.lang.String type, java.lang.String name)
          Lookup the service given the fully qualified service type and the fully qualified service name
 ServiceRef lookupService(java.lang.String type, java.lang.String name, int timeout)
          Lookup the service given the fully qualified service type and the fully qualified service name and block until a match is found or the specified timeout has expired.
 ServiceRef registerService(java.lang.Class protocol, java.lang.String name, java.lang.Object provider)
          Register a local service provider.
 ServiceRef registerService(java.lang.String serviceType, java.lang.String name, java.lang.Object provider)
          Register a local service provider.
 ServiceRef registerService(java.lang.String serviceType, java.lang.String serviceName, java.lang.Object provider, java.util.Hashtable properties)
          Register a local service provider.
 void removeServiceListener(ServiceListener listener)
          Remove a listener of service availability events.
 boolean unregisterService(ServiceRef serviceRef)
          Unregister a local service.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

listenerMap

protected java.util.Map listenerMap
Table mapping ServiceListener to the corresponding rendezvous service listener

Constructor Detail

ServiceDirectory

public ServiceDirectory()
ServiceDirectory constructor.

Method Detail

defaultDirectory

public static ServiceDirectory defaultDirectory()
Get the default ServiceDirectory instance.

Returns:
The default ServiceDirectory instance.

dispose

public void dispose()
Shutdown rendezvous and the web server and dispose of all resources.


registerService

public ServiceRef registerService(java.lang.Class protocol,
                                  java.lang.String name,
                                  java.lang.Object provider)
Register a local service provider. Convenience method used when the type is derived from the protocol's name.

Parameters:
protocol - The protocol identifying the service type.
name - The unique name of the service provider.
provider - The provider which handles the service requests.
Returns:
a new service reference for successful registration and null otherwise.

registerService

public ServiceRef registerService(java.lang.String serviceType,
                                  java.lang.String name,
                                  java.lang.Object provider)
Register a local service provider.

Parameters:
serviceType - The type of service being registered.
name - The unique name of the service provider.
provider - The provider which handles the service requests.
Returns:
a new service reference for successful registration and null otherwise.

registerService

public ServiceRef registerService(java.lang.String serviceType,
                                  java.lang.String serviceName,
                                  java.lang.Object provider,
                                  java.util.Hashtable properties)
Register a local service provider.

Parameters:
serviceType - The type of service being registered.
serviceName - The unique name of the service provider.
provider - The provider which handles the service requests.
properties - Properties.
Returns:
a new service reference for successful registration and null otherwise.

unregisterService

public boolean unregisterService(ServiceRef serviceRef)
Unregister a local service.

Parameters:
serviceRef - The service reference whose service should be shutdown.
Returns:
true if the service has been unregistered and false otherwise.

getProxy

public java.lang.Object getProxy(java.lang.Class protocol,
                                 ServiceRef serviceRef)
Get a proxy to the service with the given service reference and protocol.

Parameters:
protocol - The protocol implemented by the service.
serviceRef - The service reference.
Returns:
A proxy implementing the specified protocol for the specified service reference

lookupService

public ServiceRef lookupService(java.lang.String type,
                                java.lang.String name)
Lookup the service given the fully qualified service type and the fully qualified service name

Parameters:
type - The fully qualified service type
name - The fully qualified service name
Returns:
the matching service reference or null if no match is found

lookupService

public ServiceRef lookupService(java.lang.String type,
                                java.lang.String name,
                                int timeout)
Lookup the service given the fully qualified service type and the fully qualified service name and block until a match is found or the specified timeout has expired.

Parameters:
type - The fully qualified service type
name - The fully qualified service name
timeout - The timeout in milliseconds to block until a match is found
Returns:
the matching service reference or null if no match is found

findServicesWithType

public ServiceRef[] findServicesWithType(java.lang.Class protocol,
                                         long timeout)
Convenience method for making a request to find service providers of a specific service type and waiting a specified amount of time to find those services. It is more preferable, however, to use the "addServiceListener()" method instead so as to monitor the availability of services. Convenience method used when the type is derived from the protocol's name.

Parameters:
protocol - The protocol identifying the service type for which to find providers.
timeout - Time to block in milliseconds while waiting for services to be discovered.
Returns:
An array of services which were found within the specified timeout
See Also:
addServiceListener(java.lang.Class, gov.sns.tools.services.ServiceListener)

findServicesWithType

public ServiceRef[] findServicesWithType(java.lang.String serviceType,
                                         long timeout)
Convenience method for making a request to find service providers of a specific service type and waiting a specified amount of time to find those services. It is more preferable, however, to use the "addServiceListener()" method instead so as to monitor the availability of services.

Parameters:
serviceType - The type of service to find.
timeout - Time to block in milliseconds while waiting for services to be discovered.
Returns:
An array of services which were found within the specified timeout
See Also:
addServiceListener(java.lang.Class, gov.sns.tools.services.ServiceListener)

addServiceListener

public void addServiceListener(java.lang.Class protocol,
                               ServiceListener listener)
Add a listener for addition and removal of service providers. Convenience method used when the type is derived from the protocol's name.

Parameters:
protocol - The protocol identifying the service type.
listener - The receiver of service availability events.

addServiceListener

public void addServiceListener(java.lang.String type,
                               ServiceListener listener)
Add a listener for addition and removal of service providers.

Parameters:
type - The type of service provided.
listener - The receiver of service availability events.

removeServiceListener

public void removeServiceListener(ServiceListener listener)
Remove a listener of service availability events.

Parameters:
listener - The listener of service availability events.

getDefaultType

protected static java.lang.String getDefaultType(java.lang.Class protocol)
Form a valid type based on the specified protocol by replacing the protocol's name with a valid name in which "." is replaced by "_".

Parameters:
protocol - The protocol for which to get a valid type
Returns:
A valid type to represent the given protocol.

getHost

public static java.lang.String getHost(java.lang.Object proxy)
Get the remote host for the service

Parameters:
proxy - the proxy to the service at the remote host
Returns:
a string representation of the remote host

getServiceName

public static java.lang.String getServiceName(java.lang.Object proxy)
Get the service name for the remote service

Parameters:
proxy - the proxy to the remote service
Returns:
The name of the remote service

getProtocol

public static java.lang.Class getProtocol(java.lang.Object proxy)
Get the protocol of the remote proxy

Parameters:
proxy - the proxy to the remote service
Returns:
the interface implemented by the proxy

getClientHandler

protected static gov.sns.tools.services.ClientHandler getClientHandler(java.lang.Object proxy)
Convenience method for getting the ClientHandler for the given proxy

Parameters:
proxy - the proxy for which we seek its client handler
Returns:
the client handler for the proxy