Browse Source

documented, and renamed the test to isreachable, because there may be more than just ping going on here (it goes through our firewall where http doesnt)

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@277874 13f79535-47bb-0310-9956-ffa450edef68
master
Steve Loughran 20 years ago
parent
commit
f1414a37f2
3 changed files with 147 additions and 14 deletions
  1. +68
    -0
      docs/manual/CoreTasks/conditions.html
  2. +78
    -13
      src/main/org/apache/tools/ant/taskdefs/optional/condition/IsPingable.java
  3. +1
    -1
      src/main/org/apache/tools/ant/types/defaults.properties

+ 68
- 0
docs/manual/CoreTasks/conditions.html View File

@@ -533,6 +533,74 @@ Check for XML Schema support.

Check for Xerces-specific definition of the location of the no namespace schema.

<h4>isreachable</h4>

<p>Uses Java1.5+ networking APIs to probe for a (remote) system being
reachable. Exactly what probe mechanisms are used is an implementation
feature of the JVM. They may include ICMP "ping" packets

</p>
<p>
This condition turns unknown host exceptions into false conditions. This is
because on a laptop, DNS is one of the first services when the network goes; you
are implicitly offline.
</p>
<p>
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.
</p>
<p>
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 detected as reachable with ICMP, but
not reachable on other ports (i.e. port 80), because of firewalls.
</p>
<p>

This condition was added in Apache Ant 1.7.</p>

<table border="1" cellpadding="2" cellspacing="0">
<tr>
<td valign="top"><b>Attribute</b></td>
<td valign="top"><b>Description</b></td>
<td align="center" valign="top"><b>Required</b></td>
</tr>
<tr>
<td valign="top">host</td>
<td valign="top">host to check for</td>
<td valign="top" align="center">one of url or host</td>
</tr>
<tr>
<td valign="top">url</td>
<td valign="top">URL containing hostname</td>
<td valign="top" align="center">one of url or host</td>
</tr>
<tr>
<td valign="top">timeout</td>
<td valign="top">timeout in seconds</td>
<td valign="top" align="center">no, default is 30s</td>
</tr>
</table>

<pre>
&lt;condition property="offline"&gt;
&lt;isreachable url="http://ibiblio.org/maven/" /&gt;
&lt;/condition&gt;
</pre>

<p>
Probe for the maven repository being reachable.
</p>

<pre>
&lt;condition property="offline"&gt;
&lt;isreachable host="ibiblio.org" timeout="10" /&gt;
&lt;/condition&gt;
</pre>

<p>
Probe for the maven repository being reachable using the hostname, ten second timeout..
</p>
<hr>
<p align="center">Copyright &copy; 2001-2005 Apache Software
Foundation. All rights Reserved.</p>


+ 78
- 13
src/main/org/apache/tools/ant/taskdefs/optional/condition/IsPingable.java View File

@@ -24,50 +24,97 @@ import org.apache.tools.ant.taskdefs.condition.Condition;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.IOException;

/**
* Test for a host being reachable using ICMP "ping" packets.
* Ping packets are very reliable for assessing reachability in a LAN or WAN,
* but they do not get through any well-configured firewall.
*
* <p/>
* This condition turns unknown host exceptions into false conditions. This is
* because on a laptop, DNS is one of the first services when the network goes; you
* are implicitly offline.
* <p/>
* 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.
* <p/>
* 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 detected as reachable with ICMP, but
* not reachable on other ports (i.e. port 80), because of firewalls.
* <p/>
* Requires Java1.5+ to work
*
* @ant.condition name="isreachable"
* @since Ant1.7
*/
public class IsPingable extends ProjectComponent implements Condition {
public class IsPingable extends ProjectComponent implements Condition {

private String host;
/** The default timeout. */
private String url;

/**
* The default timeout.
*/
public static final int DEFAULT_TIMEOUT = 30;
private int timeout = DEFAULT_TIMEOUT;
/** Error when no hostname is defined */
/**
* Error when no hostname is defined
*/
public static final String ERROR_NO_HOSTNAME = "No hostname defined";
/** Error when invalid timeout value is defined */
/**
* Error when invalid timeout value is defined
*/
public static final String ERROR_BAD_TIMEOUT = "Invalid timeout value";
/** Unknown host message is seen. */
/**
* Unknown host message is seen.
*/
public static final String ERROR_UNKNOWN_HOST = "Unknown host:";
/** Network error message is seen. */
/**
* Network error message is seen.
*/
public static final String ERROR_ON_NETWORK = "network error to ";
public static final String ERROR_BOTH_TARGETS = "Both url and host have been specified";

/**
* The host to ping.
*
* @param host the host to ping.
*/
public void setHost(String host) {
this.host = host;
}

/**
* A URL to extract the hostname from
*
* @param url
*/
public void setUrl(String url) {
this.url = url;
}

/**
* Timeout for the reachability test -in seconds.
*
* @param timeout the timeout in seconds.
*/
public void setTimeout(int timeout) {
this.timeout = timeout;
}

/**
* emptyness test
*
* @param string param to check
* @return true if it is empty
*/
private boolean empty(String string) {
return string == null || string.length() == 0;
}

/**
* Is this condition true?
*
@@ -76,21 +123,39 @@ public class IsPingable extends ProjectComponent implements Condition {
* if an error occurs
*/
public boolean eval() throws BuildException {
if (host == null || host.length() == 0) {
if (empty(host) && empty(url)) {
throw new BuildException(ERROR_NO_HOSTNAME);
}
if (timeout < 0) {
throw new BuildException(ERROR_BAD_TIMEOUT);
}
String target = host;
if (!empty(url)) {
if (!empty(host)) {
throw new BuildException(ERROR_BOTH_TARGETS);
}
try {
//get the host of a url
URL realURL = new URL(url);
target = realURL.getHost();
} catch (MalformedURLException e) {
throw new BuildException("Bad URL " + url, e);
}
}
try {
InetAddress address = InetAddress.getByName(host);
return address.isReachable(timeout * 1000);
log("Probing host " + target, Project.MSG_VERBOSE);
InetAddress address = InetAddress.getByName(target);
log("Host address =" + address.getHostAddress(),
Project.MSG_VERBOSE);
final boolean reachable = address.isReachable(timeout * 1000);
log("host is " + (reachable ? "" : "not") + " reachable",
Project.MSG_VERBOSE);
return reachable;
} catch (UnknownHostException e) {
log(ERROR_UNKNOWN_HOST + host, Project.MSG_VERBOSE);
log(ERROR_UNKNOWN_HOST + target);
return false;
} catch (IOException e) {
log(ERROR_ON_NETWORK + host + ": " + e.toString(),
Project.MSG_VERBOSE);
log(ERROR_ON_NETWORK + target + ": " + e.toString());
return false;
}
}


+ 1
- 1
src/main/org/apache/tools/ant/types/defaults.properties View File

@@ -37,7 +37,7 @@ assertions=org.apache.tools.ant.types.Assertions
concatfilter=org.apache.tools.ant.filters.ConcatFilter
issigned=org.apache.tools.ant.taskdefs.condition.IsSigned
isfileselected=org.apache.tools.ant.taskdefs.condition.IsFileSelected
ispingable=org.apache.tools.ant.taskdefs.optional.condition.IsPingable
isreachable=org.apache.tools.ant.taskdefs.optional.condition.IsPingable
mavenrepository=org.apache.tools.ant.taskdefs.repository.MavenRepository
scriptselector=org.apache.tools.ant.types.optional.ScriptSelector
scriptcondition=org.apache.tools.ant.types.optional.ScriptCondition


Loading…
Cancel
Save