Package xal.extension.service
Class ServiceDirectory
java.lang.Object
xal.extension.service.ServiceDirectory
ServiceDirectory is a local point of access for registering and looking up
services on a network. It wraps the standard Bonjour 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
Bonjour and XML-RPC are accepted protocols implemented in multiple languages.
- Author:
- tap
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static class
service listener information for Bonjour -
Field Summary
FieldsModifier and TypeFieldDescriptionTable mapping ServiceListener to the corresponding bonjour service listener -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addServiceListener
(Class<?> protocol, ServiceListener listener) Add a listener for addition and removal of service providers.void
addServiceListener
(String type, ServiceListener listener) Add a listener for addition and removal of service providers.static ServiceDirectory
Get the default ServiceDirectory instance.void
dispose()
Shutdown bonjour and the RPC server and dispose of all resources.findServicesWithType
(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.findServicesWithType
(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 xal.extension.service.ClientHandler<?>
getClientHandler
(Object proxy) Convenience method for getting the ClientHandler for the given proxyprotected static String
getDefaultType
(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 String
Get the remote host for the servicestatic int
Get the remote port at which the service is available.static Class<?>
getProtocol
(Object proxy) Get the protocol of the remote proxy<T> T
getProxy
(Class<T> protocol, ServiceRef serviceRef) Get a proxy to the service with the given service reference and protocol.static String
getServiceName
(Object proxy) Get the service name for the remote serviceGet a list of standard data types which are supported for coding and decodingGet a list of all data types which are supported for coding and decodingboolean
isActive()
Check if service support is active.boolean
Determine if bonjour is running in loopback mode which would indicate that the computer is isolated from the network.lookupService
(String type, String name) Lookup the service given the fully qualified service type and the fully qualified service namelookupService
(String type, 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.<C,
R> void registerCodingType
(Class<C> type, ConversionAdaptor<C, R> adaptor) Register the custom type and its associated adaptor to use for encoding and decoding objects of the custom type<T> ServiceRef
registerService
(Class<T> protocol, String name, T provider) Register a local service provider.<T> ServiceRef
Register a local service provider.void
removeServiceListener
(ServiceListener listener) Remove a listener of service availability events.boolean
unregisterService
(ServiceRef serviceRef) Unregister a local service.
-
Field Details
-
listenerMap
Table mapping ServiceListener to the corresponding bonjour service listener
-
-
Constructor Details
-
ServiceDirectory
ServiceDirectory constructor.- Throws:
ServiceException
-
-
Method Details
-
defaultDirectory
Get the default ServiceDirectory instance.- Returns:
- The default ServiceDirectory instance.
-
dispose
public void dispose()Shutdown bonjour and the RPC server and dispose of all resources. -
isActive
public boolean isActive()Check if service support is active. If initialization fails, then services will not be active.- Returns:
- true if services are available
-
isLoopback
public boolean isLoopback()Determine if bonjour is running in loopback mode which would indicate that the computer is isolated from the network. This flag is meaningful only if the service directory is active.- Returns:
- true if the service is running in loopback mode and false if it is on a network
-
getStandardCodingTypes
Get a list of standard data types which are supported for coding and decoding -
getSupportedCodingTypes
Get a list of all data types which are supported for coding and decoding -
registerCodingType
Register the custom type and its associated adaptor to use for encoding and decoding objects of the custom type- Parameters:
type
- type to identify and process for encoding and decodingadaptor
- translator between the custom type and representation constructs
-
registerService
public <T> ServiceRef registerService(Class<T> protocol, String name, T provider) throws ServiceException Register a local service provider.- 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.
- Throws:
ServiceException
-
registerService
public <T> ServiceRef registerService(Class<T> protocol, String serviceName, T provider, Map<String, Object> properties) Register a local service provider.- Parameters:
protocol
- The protocol identifying the service type.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
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.
- Throws:
ServiceException
-
getProxy
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
Lookup the service given the fully qualified service type and the fully qualified service name- Parameters:
type
- The fully qualified service typename
- The fully qualified service name- Returns:
- the matching service reference or null if no match is found
-
lookupService
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 typename
- The fully qualified service nametimeout
- The timeout in milliseconds to block until a match is found- Returns:
- the matching service reference or null if no match is found
- Throws:
ServiceException
-
findServicesWithType
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
- Throws:
ServiceException
- See Also:
-
findServicesWithType
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
- Throws:
ServiceException
- See Also:
-
addServiceListener
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.- Throws:
ServiceException
-
addServiceListener
Add a listener for addition and removal of service providers.- Parameters:
type
- The type of service provided.listener
- The receiver of service availability events.- Throws:
ServiceException
-
removeServiceListener
Remove a listener of service availability events.- Parameters:
listener
- The listener of service availability events.
-
getDefaultType
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
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
-
getPort
Get the remote port at which the service is available.- Parameters:
proxy
- the proxy to the service at the remote host- Returns:
- the remote port at which the service is available
-
getServiceName
Get the service name for the remote service- Parameters:
proxy
- the proxy to the remote service- Returns:
- The name of the remote service
-
getProtocol
Get the protocol of the remote proxy- Parameters:
proxy
- the proxy to the remote service- Returns:
- the interface implemented by the proxy
-
getClientHandler
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
-