You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by st...@apache.org on 2005/03/10 15:09:39 UTC

cvs commit: ant/src/main/org/apache/tools/ant/taskdefs/optional/condition IsPingable.java

stevel      2005/03/10 06:09:39

  Modified:    docs/manual/CoreTasks conditions.html
               src/main/org/apache/tools/ant/types defaults.properties
               src/main/org/apache/tools/ant/taskdefs/optional/condition
                        IsPingable.java
  Log:
  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)
  
  Revision  Changes    Path
  1.32      +68 -0     ant/docs/manual/CoreTasks/conditions.html
  
  Index: conditions.html
  ===================================================================
  RCS file: /home/cvs/ant/docs/manual/CoreTasks/conditions.html,v
  retrieving revision 1.31
  retrieving revision 1.32
  diff -u -r1.31 -r1.32
  --- conditions.html	7 Mar 2005 18:09:09 -0000	1.31
  +++ conditions.html	10 Mar 2005 14:09:39 -0000	1.32
  @@ -533,6 +533,74 @@
   
   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>
  
  
  
  1.38      +1 -1      ant/src/main/org/apache/tools/ant/types/defaults.properties
  
  Index: defaults.properties
  ===================================================================
  RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/types/defaults.properties,v
  retrieving revision 1.37
  retrieving revision 1.38
  diff -u -r1.37 -r1.38
  --- defaults.properties	25 Feb 2005 11:32:35 -0000	1.37
  +++ defaults.properties	10 Mar 2005 14:09:39 -0000	1.38
  @@ -37,7 +37,7 @@
   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
  
  
  
  1.5       +78 -13    ant/src/main/org/apache/tools/ant/taskdefs/optional/condition/IsPingable.java
  
  Index: IsPingable.java
  ===================================================================
  RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/optional/condition/IsPingable.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- IsPingable.java	28 Jan 2005 11:29:16 -0000	1.4
  +++ IsPingable.java	10 Mar 2005 14:09:39 -0000	1.5
  @@ -24,36 +24,63 @@
   
   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) {
  @@ -61,7 +88,17 @@
       }
   
       /**
  +     * 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) {
  @@ -69,6 +106,16 @@
       }
   
       /**
  +     * 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?
        *
        * @return true if the condition is true.
  @@ -76,21 +123,39 @@
        *          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;
           }
       }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org