You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by ja...@apache.org on 2019/02/26 03:07:32 UTC

[ant] branch 1.9.x updated: Call InetAddress.isReachable instead of using reflection, now that we require Java 5 runtime for Ant

This is an automated email from the ASF dual-hosted git repository.

jaikiran pushed a commit to branch 1.9.x
in repository https://gitbox.apache.org/repos/asf/ant.git


The following commit(s) were added to refs/heads/1.9.x by this push:
     new f04cc8b  Call InetAddress.isReachable instead of using reflection, now that we require Java 5 runtime for Ant
f04cc8b is described below

commit f04cc8bb19723314e3370300c3b9d1741888b834
Author: Jaikiran Pai <ja...@apache.org>
AuthorDate: Tue Feb 26 08:35:11 2019 +0530

    Call InetAddress.isReachable instead of using reflection, now that we require Java 5 runtime for Ant
---
 manual/Tasks/conditions.html                       |  5 +--
 .../tools/ant/taskdefs/condition/IsReachable.java  | 41 ++++++----------------
 2 files changed, 11 insertions(+), 35 deletions(-)

diff --git a/manual/Tasks/conditions.html b/manual/Tasks/conditions.html
index 35216cd..2af7b30 100644
--- a/manual/Tasks/conditions.html
+++ b/manual/Tasks/conditions.html
@@ -642,10 +642,7 @@ Check for Xerces-specific definition of the location of the no namespace schema.
 <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, UDP or TCP connections
-to port 7 "echo service" or other means. On Java1.4 and earlier, being able
-to resolve the hostname is considered success. This means that if DNS is not
-working or a URL/hostname is bad, the test will fail, but otherwise succeed
-even if the remote host is actually absent.
+to port 7 "echo service" or other means.
 
 </p>
 <p>
diff --git a/src/main/org/apache/tools/ant/taskdefs/condition/IsReachable.java b/src/main/org/apache/tools/ant/taskdefs/condition/IsReachable.java
index 818393b..d83fcbe 100644
--- a/src/main/org/apache/tools/ant/taskdefs/condition/IsReachable.java
+++ b/src/main/org/apache/tools/ant/taskdefs/condition/IsReachable.java
@@ -18,6 +18,7 @@
 
 package org.apache.tools.ant.taskdefs.condition;
 
+import java.io.IOException;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.net.InetAddress;
@@ -46,9 +47,6 @@ import org.apache.tools.ant.ProjectComponent;
  * 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.</p>
  *
- * <p>Requires Java 5+ to work properly. On Java 1.4, if a hostname
- * can be resolved, the destination is assumed to be reachable.</p>
- *
  * @since Ant 1.7
  */
 public class IsReachable extends ProjectComponent implements Condition {
@@ -88,7 +86,11 @@ public class IsReachable extends ProjectComponent implements Condition {
     public static final String ERROR_BAD_URL = "Bad URL ";
     /** Error message when no hostname in url. */
     public static final String ERROR_NO_HOST_IN_URL = "No hostname in URL ";
-    /** The method name to look for in InetAddress */
+    /**
+     * The method name to look for in InetAddress
+     * @deprecated Since 1.9.14
+     */
+    @Deprecated
     public static final String METHOD_NAME = "isReachable";
 
     /**
@@ -129,8 +131,6 @@ public class IsReachable extends ProjectComponent implements Condition {
         return string == null || string.length() == 0;
     }
 
-    private static Class[] parameterTypes = {Integer.TYPE};
-
     /**
      * Evaluate the condition.
      *
@@ -173,32 +173,11 @@ public class IsReachable extends ProjectComponent implements Condition {
         log("Host address = " + address.getHostAddress(),
                 Project.MSG_VERBOSE);
         boolean reachable;
-        //Java1.5: reachable = address.isReachable(timeout * 1000);
-        Method reachableMethod = null;
         try {
-            reachableMethod = InetAddress.class.getMethod(METHOD_NAME,
-                    parameterTypes);
-            final Object[] params = new Object[1];
-            params[0] = new Integer(timeout * SECOND);
-            try {
-                reachable = ((Boolean) reachableMethod.invoke(address, params))
-                        .booleanValue();
-            } catch (final IllegalAccessException e) {
-                //utterly implausible, but catered for anyway
-                throw new BuildException("When calling " + reachableMethod);
-            } catch (final InvocationTargetException e) {
-                //assume this is an IOException about un readability
-                final Throwable nested = e.getTargetException();
-                log(ERROR_ON_NETWORK + target + ": " + nested.toString());
-                //any kind of fault: not reachable.
-                reachable = false;
-            }
-        } catch (final NoSuchMethodException e) {
-            //java1.4
-            log("Not found: InetAddress." + METHOD_NAME, Project.MSG_VERBOSE);
-            log(MSG_NO_REACHABLE_TEST);
-            reachable = true;
-
+            reachable = address.isReachable(timeout * SECOND);
+        } catch (final IOException ioe) {
+            reachable = false;
+            log(ERROR_ON_NETWORK + target + ": " + ioe.toString());
         }
 
         log("host is" + (reachable ? "" : " not") + " reachable", Project.MSG_VERBOSE);