1 18 19 package org.apache.tools.ant.taskdefs.condition; 20 21 import org.apache.tools.ant.BuildException; 22 import org.apache.tools.ant.Project; 23 import org.apache.tools.ant.ProjectComponent; 24 25 import java.lang.reflect.InvocationTargetException ; 26 import java.lang.reflect.Method ; 27 import java.net.InetAddress ; 28 import java.net.MalformedURLException ; 29 import java.net.URL ; 30 import java.net.UnknownHostException ; 31 32 54 public class IsReachable extends ProjectComponent implements Condition { 55 56 private static final int SECOND = 1000; private String host; 58 private String url; 59 60 63 public static final int DEFAULT_TIMEOUT = 30; 64 private int timeout = DEFAULT_TIMEOUT; 65 68 public static final String ERROR_NO_HOSTNAME = "No hostname defined"; 69 72 public static final String ERROR_BAD_TIMEOUT = "Invalid timeout value"; 73 76 private static final String WARN_UNKNOWN_HOST = "Unknown host: "; 77 80 public static final String ERROR_ON_NETWORK = "network error to "; 81 82 public static final String ERROR_BOTH_TARGETS 83 = "Both url and host have been specified"; 84 85 public static final String MSG_NO_REACHABLE_TEST 86 = "cannot do a proper reachability test on this Java version"; 87 88 public static final String ERROR_BAD_URL = "Bad URL "; 89 90 public static final String ERROR_NO_HOST_IN_URL = "No hostname in URL "; 91 92 public static final String METHOD_NAME = "isReachable"; 93 94 99 public void setHost(String host) { 100 this.host = host; 101 } 102 103 108 public void setUrl(String url) { 109 this.url = url; 110 } 111 112 117 public void setTimeout(int timeout) { 118 this.timeout = timeout; 119 } 120 121 128 private boolean empty(String string) { 129 return string == null || string.length() == 0; 130 } 131 132 private static Class [] parameterTypes = {Integer.TYPE}; 133 134 142 public boolean eval() throws BuildException { 143 if (empty(host) && empty(url)) { 144 throw new BuildException(ERROR_NO_HOSTNAME); 145 } 146 if (timeout < 0) { 147 throw new BuildException(ERROR_BAD_TIMEOUT); 148 } 149 String target = host; 150 if (!empty(url)) { 151 if (!empty(host)) { 152 throw new BuildException(ERROR_BOTH_TARGETS); 153 } 154 try { 155 URL realURL = new URL (url); 157 target = realURL.getHost(); 158 if (empty(target)) { 159 throw new BuildException(ERROR_NO_HOST_IN_URL + url); 160 } 161 } catch (MalformedURLException e) { 162 throw new BuildException(ERROR_BAD_URL + url, e); 163 } 164 } 165 log("Probing host " + target, Project.MSG_VERBOSE); 166 InetAddress address; 167 try { 168 address = InetAddress.getByName(target); 169 } catch (UnknownHostException e1) { 170 log(WARN_UNKNOWN_HOST + target); 171 return false; 172 } 173 log("Host address = " + address.getHostAddress(), 174 Project.MSG_VERBOSE); 175 boolean reachable; 176 Method reachableMethod = null; 178 try { 179 reachableMethod = InetAddress .class.getMethod(METHOD_NAME, 180 parameterTypes); 181 Object [] params = new Object [1]; 182 params[0] = new Integer (timeout * SECOND); 183 try { 184 reachable = ((Boolean ) reachableMethod.invoke(address, params)) 185 .booleanValue(); 186 } catch (IllegalAccessException e) { 187 throw new BuildException("When calling " + reachableMethod); 189 } catch (InvocationTargetException e) { 190 Throwable nested = e.getTargetException(); 192 log(ERROR_ON_NETWORK + target + ": " + nested.toString()); 193 reachable = false; 195 } 196 } catch (NoSuchMethodException e) { 197 log("Not found: InetAddress." + METHOD_NAME, Project.MSG_VERBOSE); 199 log(MSG_NO_REACHABLE_TEST); 200 reachable = true; 201 202 } 203 204 log("host is" + (reachable ? "" : " not") + " reachable", Project.MSG_VERBOSE); 205 return reachable; 206 } 207 } 208 | Popular Tags |