Fields Summary |
---|
private static final int | SECOND |
private String | host |
private String | url |
public static final int | DEFAULT_TIMEOUTThe default timeout. |
private int | timeout |
public static final String | ERROR_NO_HOSTNAMEError when no hostname is defined |
public static final String | ERROR_BAD_TIMEOUTError when invalid timeout value is defined |
private static final String | WARN_UNKNOWN_HOSTUnknown host message is seen. |
public static final String | ERROR_ON_NETWORKNetwork error message is seen. |
public static final String | ERROR_BOTH_TARGETSError message when url and host are specified. |
public static final String | MSG_NO_REACHABLE_TESTError message when no reachably test avail. |
public static final String | ERROR_BAD_URLError message when an invalid url is used. |
public static final String | ERROR_NO_HOST_IN_URLError message when no hostname in url. |
public static final String | METHOD_NAMEThe method name to look for in InetAddress |
private static Class[] | parameterTypes |
Methods Summary |
---|
private boolean | empty(java.lang.String string)emptyness test
return string == null || string.length() == 0;
|
public boolean | eval()Evaluate the condition.
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();
if (empty(target)) {
throw new BuildException(ERROR_NO_HOST_IN_URL + url);
}
} catch (MalformedURLException e) {
throw new BuildException(ERROR_BAD_URL + url, e);
}
}
log("Probing host " + target, Project.MSG_VERBOSE);
InetAddress address;
try {
address = InetAddress.getByName(target);
} catch (UnknownHostException e1) {
log(WARN_UNKNOWN_HOST + target);
return false;
}
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);
Object[] params = new Object[1];
params[0] = new Integer(timeout * SECOND);
try {
reachable = ((Boolean) reachableMethod.invoke(address, params))
.booleanValue();
} catch (IllegalAccessException e) {
//utterly implausible, but catered for anyway
throw new BuildException("When calling " + reachableMethod);
} catch (InvocationTargetException e) {
//assume this is an IOexception about un readability
Throwable nested = e.getTargetException();
log(ERROR_ON_NETWORK + target + ": " + nested.toString());
//any kind of fault: not reachable.
reachable = false;
}
} catch (NoSuchMethodException e) {
//java1.4 or earlier
log("Not found: InetAddress." + METHOD_NAME, Project.MSG_VERBOSE);
log(MSG_NO_REACHABLE_TEST);
reachable = true;
}
log("host is" + (reachable ? "" : " not") + " reachable", Project.MSG_VERBOSE);
return reachable;
|
public void | setHost(java.lang.String host)Set the host to ping.
this.host = host;
|
public void | setTimeout(int timeout)Set the timeout for the reachability test in seconds.
this.timeout = timeout;
|
public void | setUrl(java.lang.String url)Set the URL from which to extract the hostname.
this.url = url;
|