Browse Source

First of the Java1.5 extensions. Closest j2se has for detecting offline state, though I note J2ME and JavaWebStart have offline tests.

Needs tests.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@276928 13f79535-47bb-0310-9956-ffa450edef68
master
Steve Loughran 21 years ago
parent
commit
e0447a7385
2 changed files with 98 additions and 0 deletions
  1. +7
    -0
      build.xml
  2. +91
    -0
      src/main/org/apache/tools/ant/taskdefs/condition/IsPingable.java

+ 7
- 0
build.xml View File

@@ -152,6 +152,12 @@
</or>
</selector>

<selector id="needs.jdk1.5+">
<or>
<filename name="${ant.package}/taskdefs/condition/IsPingable*"/>
</or>
</selector>
<!-- classes that should be present in Sun based JVMs, but not in
Kaffe for example -->
<selector id="needs.sun.tools">
@@ -637,6 +643,7 @@
<or>
<selector refid="needs.jdk1.3+" unless="jdk1.3+"/>
<selector refid="needs.jdk1.4+" unless="jdk1.4+"/>
<selector refid="needs.jdk1.5+" unless="jdk1.5+"/>
<selector refid="needs.sun.tools" unless="sun.tools.present"/>
<selector refid="needs.sun.uue" unless="sunuue.present"/>
<selector refid="needs.sun.b64" unless="base64.present"/>


+ 91
- 0
src/main/org/apache/tools/ant/taskdefs/condition/IsPingable.java View File

@@ -0,0 +1,91 @@
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.apache.tools.ant.taskdefs.condition;

import org.apache.tools.ant.ProjectComponent;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;

import java.net.InetAddress;
import java.net.UnknownHostException;
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.
* Requires Java1.5+ to work
* @since Ant1.7
*/
public class IsPingable extends ProjectComponent implements Condition {

private String host;
public static final int DEFAULT_TIMEOUT = 30;
private int timeout=DEFAULT_TIMEOUT;
public static final String ERROR_NO_HOSTNAME = "No hostname defined";
public static final String ERROR_BAD_TIMEOUT = "Invalid timeout value";
public static final String ERROR_UNKNOWN_HOST = "Unknown host:";
public static final String ERROR_ON_NETWORK = "network error to ";

/**
* the host to ping
* @param host
*/
public void setHost(String host) {
this.host = host;
}

/**
* timeout for the reachability test -in seconds
* @param timeout
*/
public void setTimeout(int timeout) {
this.timeout = timeout;
}

/**
* Is this condition true?
*
* @return true if the condition is true
* @throws org.apache.tools.ant.BuildException
* if an error occurs
*/
public boolean eval() throws BuildException {
if(host==null && host.length()==0) {
throw new BuildException(ERROR_NO_HOSTNAME);
}
if(timeout<0) {
throw new BuildException(ERROR_BAD_TIMEOUT);
}
try {
InetAddress address=InetAddress.getByName(host);
return address.isReachable(timeout*1000);
} catch (UnknownHostException e) {
log(ERROR_UNKNOWN_HOST+host,Project.MSG_VERBOSE);
return false;
} catch (IOException e) {
log(ERROR_ON_NETWORK + host +": "+e.toString(),
Project.MSG_VERBOSE);
return false;
}
}
}

Loading…
Cancel
Save