com.continuent.tungsten.common.network
Class HostAddressService

java.lang.Object
  extended by com.continuent.tungsten.common.network.HostAddressService

public class HostAddressService
extends java.lang.Object

Implements a service for performing operations on Internet addresses, such as testing liveness. This class is designed to make calls to ping hosts as robust and as simple as possible, at the cost of a little more up-front configuration in some cases, for example to set timeouts.

This class is thread-safe through the use of synchronized methods to access the method table and enabled names list. The timeout is volatile, which obviates the need for synchronization.

Author:
Robert Hodges

Field Summary
static java.lang.String DEFAULT
          Default Java ping method using InetAddress.isReachable().
static java.lang.String PING
          Ping method using operating system ping command.
 
Constructor Summary
HostAddressService(boolean autoEnable)
          Creates a new service.
 
Method Summary
 PingNotification _isReachableByMethod(java.lang.String name, HostAddress host)
           
 void addMethod(java.lang.String name, java.lang.String methodClass, boolean enable)
          Adds a ping method to the service.
static boolean addressesAreEqual(java.lang.String host1, java.lang.String host2)
          This method returns true if the host addresses are equal to each other, otherwise false.
static boolean addressesAreInSameSubnet(java.lang.String host1, java.lang.String host2, short prefix)
          Given a pair of addresses and a single network prefix, determines if hosts are on the same subnet.
 void enableMethod(java.lang.String name)
          Enables a ping method.
 java.util.List<java.lang.String> getAvailableMethodNames()
          Returns names of available ping methods, whether enabled or not.
static HostAddress getByName(java.lang.String host)
          Returns a host address instance.
static java.lang.String getCanonicalAddress(java.lang.String host)
          This method returns the host address, in string format, or UNKNOWN if there's a problem resolving the address.
 java.util.List<java.lang.String> getEnabledMethodNames()
          Returns names of available ping methods.
static short getLocalNetworkPrefix(java.lang.String hostName)
          This method returns a prefix for a given internet address.
 java.lang.String getMethodName(java.lang.String name)
          Returns a ping method by name or null if no such method exists.
 int getTimeout()
          Returns current timeout in milliseconds.
 PingResponse isReachable(HostAddress host)
          Returns true if the host is reachable by an available ping method.
 PingResponse isReachableByMethod(java.lang.String name, HostAddress host)
          Returns true if the host is reachable by an available ping method.
static byte[] netMaskFromPrefixLength(short prefix)
           
 void setTimeout(int timeoutMillis)
          Sets the timeout for ping methods.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT

public static java.lang.String DEFAULT
Default Java ping method using InetAddress.isReachable().


PING

public static java.lang.String PING
Ping method using operating system ping command.

Constructor Detail

HostAddressService

public HostAddressService(boolean autoEnable)
                   throws HostException
Creates a new service.

Parameters:
autoEnable - If true, enable ping methods automatically.
Throws:
HostException - Thrown if there is a problem enabling a method
Method Detail

setTimeout

public void setTimeout(int timeoutMillis)
Sets the timeout for ping methods. Methods will try for up to this time before giving up.

Parameters:
timeoutMillis - Timeout in milliseconds

getTimeout

public int getTimeout()
Returns current timeout in milliseconds.


addMethod

public void addMethod(java.lang.String name,
                      java.lang.String methodClass,
                      boolean enable)
               throws HostException
Adds a ping method to the service.

Parameters:
name - Logical name of the method
methodClass - Method class name
enable - If true, enable the method for use
Throws:
HostException - Thrown if there is a problem enabling a method.

enableMethod

public void enableMethod(java.lang.String name)
                  throws HostException
Enables a ping method.

Parameters:
name - of method to enable
Throws:
HostException - Thrown if method name does not exist

getAvailableMethodNames

public java.util.List<java.lang.String> getAvailableMethodNames()
Returns names of available ping methods, whether enabled or not.


getEnabledMethodNames

public java.util.List<java.lang.String> getEnabledMethodNames()
Returns names of available ping methods.


getMethodName

public java.lang.String getMethodName(java.lang.String name)
Returns a ping method by name or null if no such method exists.


getByName

public static HostAddress getByName(java.lang.String host)
                             throws java.net.UnknownHostException
Returns a host address instance.

Throws:
java.net.UnknownHostException

getCanonicalAddress

public static java.lang.String getCanonicalAddress(java.lang.String host)
This method returns the host address, in string format, or UNKNOWN if there's a problem resolving the address.

Parameters:
host -
Returns:
host address in string form or UNKNOWN

addressesAreInSameSubnet

public static boolean addressesAreInSameSubnet(java.lang.String host1,
                                               java.lang.String host2,
                                               short prefix)
                                        throws java.lang.Exception
Given a pair of addresses and a single network prefix, determines if hosts are on the same subnet.

Throws:
java.lang.Exception

addressesAreEqual

public static boolean addressesAreEqual(java.lang.String host1,
                                        java.lang.String host2)
This method returns true if the host addresses are equal to each other, otherwise false.

Parameters:
host1 -
host2 -
Returns:
true if host addresses match, otherwise false.

isReachable

public PingResponse isReachable(HostAddress host)
                         throws HostException
Returns true if the host is reachable by an available ping method. This method clears previous notifications.

Parameters:
host - Name of host for which we want to test reachability
Returns:
True if host is reachable, otherwise false
Throws:
HostException - Thrown if a ping method fails

isReachableByMethod

public PingResponse isReachableByMethod(java.lang.String name,
                                        HostAddress host)
                                 throws HostException
Returns true if the host is reachable by an available ping method. This method clears previous notifications.

Parameters:
name - Name of ping method to use
host - Name of host for which we want to test reachability
Returns:
True if host is reachable, otherwise false
Throws:
HostException - Thrown if a ping method fails

_isReachableByMethod

public PingNotification _isReachableByMethod(java.lang.String name,
                                             HostAddress host)
                                      throws HostException
Throws:
HostException

getLocalNetworkPrefix

public static short getLocalNetworkPrefix(java.lang.String hostName)
                                   throws java.lang.Exception
This method returns a prefix for a given internet address. It will only work on the host for which the address is bound.

Throws:
java.lang.Exception

netMaskFromPrefixLength

public static byte[] netMaskFromPrefixLength(short prefix)