From f1414a37f28a85a5b7fab5e4019b723c5a3546d2 Mon Sep 17 00:00:00 2001 From: Steve Loughran Date: Thu, 10 Mar 2005 14:09:39 +0000 Subject: [PATCH] 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 --- docs/manual/CoreTasks/conditions.html | 68 ++++++++++++++ .../optional/condition/IsPingable.java | 91 ++++++++++++++++--- .../tools/ant/types/defaults.properties | 2 +- 3 files changed, 147 insertions(+), 14 deletions(-) diff --git a/docs/manual/CoreTasks/conditions.html b/docs/manual/CoreTasks/conditions.html index 6e6dfeeac..ce5e3b480 100644 --- a/docs/manual/CoreTasks/conditions.html +++ b/docs/manual/CoreTasks/conditions.html @@ -533,6 +533,74 @@ Check for XML Schema support. Check for Xerces-specific definition of the location of the no namespace schema. +

isreachable

+ +

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 + +

+

+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. +

+

+ 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 detected as reachable with ICMP, but +not reachable on other ports (i.e. port 80), because of firewalls. +

+

+ +This condition was added in Apache Ant 1.7.

+ + + + + + + + + + + + + + + + + + + + + + +
AttributeDescriptionRequired
hosthost to check forone of url or host
urlURL containing hostnameone of url or host
timeouttimeout in secondsno, default is 30s
+ +
+<condition property="offline">
+  <isreachable url="http://ibiblio.org/maven/" />
+</condition>
+
+ +

+Probe for the maven repository being reachable. +

+ +
+<condition property="offline">
+  <isreachable host="ibiblio.org" timeout="10" />
+</condition>
+
+ +

+Probe for the maven repository being reachable using the hostname, ten second timeout.. +


Copyright © 2001-2005 Apache Software Foundation. All rights Reserved.

diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/condition/IsPingable.java b/src/main/org/apache/tools/ant/taskdefs/optional/condition/IsPingable.java index 6ec451b3d..ae6ee00a3 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/condition/IsPingable.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/condition/IsPingable.java @@ -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. - * + *

* 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. + *

+ * 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 detected as reachable with ICMP, but + * not reachable on other ports (i.e. port 80), because of firewalls. + *

* 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; } } diff --git a/src/main/org/apache/tools/ant/types/defaults.properties b/src/main/org/apache/tools/ant/types/defaults.properties index e3077660b..97021fb2f 100644 --- a/src/main/org/apache/tools/ant/types/defaults.properties +++ b/src/main/org/apache/tools/ant/types/defaults.properties @@ -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