org.apache.tools.ant.taskdefs.condition

Class IsReachable

public class IsReachable extends ProjectComponent implements Condition

Test for a host being reachable using ICMP "ping" packets & echo operations. Ping packets are very reliable for assessing reachability in a LAN or WAN, but they do not get through any well-configured firewall. Echo (port 7) may.

This condition turns unknown host exceptions into false conditions. This is because on a laptop, DNS is one of the first services lost when the network goes; you are implicitly offline.

If a URL is supplied instead of a host, the hostname is extracted and used in the test--all other parts of the URL are discarded.

The test may not work through firewalls; that is, something may be reachable using a protocol such as HTTP, while the lower level ICMP packets get dropped on the floor. Similarly, a host may be detected as reachable with ICMP, but not reachable on other ports (i.e. port 80), because of firewalls.

Requires Java 5+ to work properly. On Java 1.4, if a hostname can be resolved, the destination is assumed to be reachable.

Since: Ant 1.7

Field Summary
static intDEFAULT_TIMEOUT
The default timeout.
static StringERROR_BAD_TIMEOUT
Error when invalid timeout value is defined
static StringERROR_BAD_URL
Error message when an invalid url is used.
static StringERROR_BOTH_TARGETS
Error message when url and host are specified.
static StringERROR_NO_HOSTNAME
Error when no hostname is defined
static StringERROR_NO_HOST_IN_URL
Error message when no hostname in url.
static StringERROR_ON_NETWORK
Network error message is seen.
static StringMETHOD_NAME
The method name to look for in InetAddress
static StringMSG_NO_REACHABLE_TEST
Error message when no reachably test avail.
Method Summary
booleaneval()
Evaluate the condition.
voidsetHost(String host)
Set the host to ping.
voidsetTimeout(int timeout)
Set the timeout for the reachability test in seconds.
voidsetUrl(String url)
Set the URL from which to extract the hostname.

Field Detail

DEFAULT_TIMEOUT

public static final int DEFAULT_TIMEOUT
The default timeout.

ERROR_BAD_TIMEOUT

public static final String ERROR_BAD_TIMEOUT
Error when invalid timeout value is defined

ERROR_BAD_URL

public static final String ERROR_BAD_URL
Error message when an invalid url is used.

ERROR_BOTH_TARGETS

public static final String ERROR_BOTH_TARGETS
Error message when url and host are specified.

ERROR_NO_HOSTNAME

public static final String ERROR_NO_HOSTNAME
Error when no hostname is defined

ERROR_NO_HOST_IN_URL

public static final String ERROR_NO_HOST_IN_URL
Error message when no hostname in url.

ERROR_ON_NETWORK

public static final String ERROR_ON_NETWORK
Network error message is seen.

METHOD_NAME

public static final String METHOD_NAME
The method name to look for in InetAddress

MSG_NO_REACHABLE_TEST

public static final String MSG_NO_REACHABLE_TEST
Error message when no reachably test avail.

Method Detail

eval

public boolean eval()
Evaluate the condition.

Returns: true if the condition is true.

Throws: org.apache.tools.ant.BuildException if an error occurs

setHost

public void setHost(String host)
Set the host to ping.

Parameters: host the host to ping.

setTimeout

public void setTimeout(int timeout)
Set the timeout for the reachability test in seconds.

Parameters: timeout the timeout in seconds.

setUrl

public void setUrl(String url)
Set the URL from which to extract the hostname.

Parameters: url a URL object.