From 24fe82e4334d79bd0b4a683110ca036f56bc8125 Mon Sep 17 00:00:00 2001
From: Jacobus Martinus Kruithof
Date: Sat, 31 Jan 2009 14:54:54 +0000
Subject: [PATCH] Adding task to provide host info, requests in bug reports
31164 and 45861.
git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@739567 13f79535-47bb-0310-9956-ffa450edef68
---
WHATSNEW | 3 +
docs/manual/coretasklist.html | 1 +
docs/manual/tasksoverview.html | 7 +
.../apache/tools/ant/taskdefs/HostInfo.java | 247 ++++++++++++++++++
.../tools/ant/taskdefs/defaults.properties | 1 +
src/tests/antunit/taskdefs/hostinfo-test.xml | 94 +++++++
6 files changed, 353 insertions(+)
create mode 100644 src/main/org/apache/tools/ant/taskdefs/HostInfo.java
create mode 100644 src/tests/antunit/taskdefs/hostinfo-test.xml
diff --git a/WHATSNEW b/WHATSNEW
index ecc54180c..942ab5c17 100644
--- a/WHATSNEW
+++ b/WHATSNEW
@@ -340,6 +340,9 @@ Fixed bugs:
Other changes:
--------------
+ * A HostInfo task was added performing information on hosts, including info on
+ the host ant is running on.
+ Bugzilla reports 45861 and 31164.
* There is now a FileProvider interface for resources that act as a source
of filenames. This should be used by tasks that require resources
diff --git a/docs/manual/coretasklist.html b/docs/manual/coretasklist.html
index a3951c7a3..4ff3e2b14 100644
--- a/docs/manual/coretasklist.html
+++ b/docs/manual/coretasklist.html
@@ -74,6 +74,7 @@
FixCRLF
GenKey
Get
+Hostinfo
GUnzip
GZip
Import
diff --git a/docs/manual/tasksoverview.html b/docs/manual/tasksoverview.html
index 07d269d88..06569b3b7 100644
--- a/docs/manual/tasksoverview.html
+++ b/docs/manual/tasksoverview.html
@@ -749,6 +749,13 @@ documentation.
GenKey |
Generates a key in keystore. |
+
+
+
+ HostInfo |
+ Sets properties related to the provided host, or to
+ the host the process is run on. |
+
Input |
diff --git a/src/main/org/apache/tools/ant/taskdefs/HostInfo.java b/src/main/org/apache/tools/ant/taskdefs/HostInfo.java
new file mode 100644
index 000000000..3aca2f748
--- /dev/null
+++ b/src/main/org/apache/tools/ant/taskdefs/HostInfo.java
@@ -0,0 +1,247 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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;
+
+import java.net.Inet4Address;
+import java.net.Inet6Address;
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.util.Arrays;
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.Task;
+
+/**
+ * Sets properties to the host provided, or localhost if no information is
+ * provided. The default properties are NAME, FQDN, ADDR4, ADDR6;
+ *
+ * @since Ant 1.8
+ * @ant.task category="utility"
+ */
+
+
+public class HostInfo extends Task {
+ private static final String DEF_REM_ADDR6 = "::";
+
+ private static final String DEF_REM_ADDR4 = "0.0.0.0";
+
+ private static final String DEF_LOCAL_ADDR6 = "::1";
+
+ private static final String DEF_LOCAL_ADDR4 = "127.0.0.1";
+
+ private static final String DEF_LOCAL_NAME = "localhost";
+ private static final String DEF_DOMAIN = "localdomain";
+
+ private static final String DOMAIN = "DOMAIN";
+
+ private static final String NAME = "NAME";
+
+ private static final String ADDR4 = "ADDR4";
+
+ private static final String ADDR6 = "ADDR6";
+
+ private String prefix = "";
+
+ private String host;
+
+ private InetAddress nameAddr;
+
+ private InetAddress best6;
+
+ private InetAddress best4;
+
+ private List inetAddrs;
+
+ /**
+ * Set a prefix for the properties. If the prefix does not end with a "."
+ * one is automatically added.
+ *
+ * @param aPrefix
+ * the prefix to use.
+ * @since Ant 1.8
+ */
+ public void setPrefix(String aPrefix) {
+ prefix = aPrefix;
+ if (!prefix.endsWith(".")) {
+ prefix += ".";
+ }
+ }
+
+ /**
+ * Set the host to be retrieved.
+ *
+ * @param aHost
+ * the name or the address of the host, data for the local host
+ * will be retrieved if ommited.
+ * @since Ant 1.8
+ */
+ public void setHost(String aHost) {
+ host = aHost;
+ }
+
+ /**
+ * set the properties.
+ *
+ * @throws BuildException
+ * on error.
+ */
+ public void execute() throws BuildException {
+ if (host == null || "".equals(host)) {
+ executeLocal();
+ } else {
+ executeRemote();
+ }
+ }
+
+ private void executeLocal() {
+ try {
+ Enumeration interfaces = NetworkInterface.getNetworkInterfaces();
+ inetAddrs = new LinkedList();
+ while (interfaces.hasMoreElements()) {
+ NetworkInterface currentif = (NetworkInterface) interfaces
+ .nextElement();
+ Enumeration addrs = currentif.getInetAddresses();
+ while (addrs.hasMoreElements())
+ {
+ inetAddrs.add(addrs.nextElement());
+ }
+ }
+ selectAddresses();
+
+ if (nameAddr != null) {
+ setDomainAndName(nameAddr.getCanonicalHostName());
+ } else {
+ setProperty(DOMAIN, DEF_DOMAIN);
+ setProperty(NAME, DEF_LOCAL_NAME);
+ }
+ if (best4 != null) {
+ setProperty(ADDR4, best4.getHostAddress());
+ } else {
+ setProperty(ADDR4, DEF_LOCAL_ADDR4);
+ }
+ if (best6 != null) {
+ setProperty(ADDR6, best6.getHostAddress());
+ } else {
+ setProperty(ADDR6, DEF_LOCAL_ADDR6);
+ }
+ } catch (Exception e) {
+ log("Error retrieving local host information", e, Project.MSG_WARN);
+ setProperty(DOMAIN, DEF_DOMAIN);
+ setProperty(NAME, DEF_LOCAL_NAME);
+ setProperty(ADDR4, DEF_LOCAL_ADDR4);
+ setProperty(ADDR6, DEF_LOCAL_ADDR6);
+ }
+ }
+
+ private void selectAddresses() {
+ Iterator i = inetAddrs.iterator();
+ while (i.hasNext()) {
+ InetAddress current = (InetAddress) i.next();
+ if (!current.isMulticastAddress()) {
+ if (current instanceof Inet4Address) {
+ best4 = selectBestAddress(best4, current);
+ } else if (current instanceof Inet6Address) {
+ best6 = selectBestAddress(best6, current);
+ }
+ }
+ }
+
+ nameAddr = selectBestAddress(best6, best4);
+ }
+
+ private InetAddress selectBestAddress(InetAddress bestSoFar,
+ InetAddress current) {
+ InetAddress best = bestSoFar;
+ if (best == null) {
+ // none selected so far, so this one is better.
+ best = current;
+ } else {
+ if (current.isLoopbackAddress()) {
+ // definitely not better than the previously selected address.
+ } else if (current.isLinkLocalAddress()) {
+ // link local considered better than loopback
+ if (best.isLoopbackAddress()) {
+ best = current;
+ }
+ } else if (current.isSiteLocalAddress()) {
+ // site local considered better than link local (and loopback)
+ if (best.isLoopbackAddress() || best.isLinkLocalAddress()) {
+ best = current;
+ }
+ } else {
+ // current is a global address, and therefore best (at least
+ // equally well)
+ best = current;
+ }
+ }
+ return best;
+ }
+
+ private void executeRemote() {
+ try {
+ inetAddrs = Arrays.asList(InetAddress.getAllByName(host));
+
+ selectAddresses();
+
+ if (nameAddr != null) {
+ setDomainAndName(nameAddr.getCanonicalHostName());
+ } else {
+ setDomainAndName(host);
+ }
+ if (best4 != null) {
+ setProperty(ADDR4, best4.getHostAddress());
+ } else {
+ setProperty(ADDR4, DEF_REM_ADDR4);
+ }
+ if (best6 != null) {
+ setProperty(ADDR6, best6.getHostAddress());
+ } else {
+ setProperty(ADDR6, DEF_REM_ADDR6);
+ }
+ } catch (Exception e) {
+ log("Error retrieving remote host information for host:" + host
+ + ".", e, Project.MSG_WARN);
+ setDomainAndName(host);
+ setProperty(ADDR4, DEF_REM_ADDR4);
+ setProperty(ADDR6, DEF_REM_ADDR6);
+ }
+ }
+
+ private void setDomainAndName(String fqdn)
+ {
+ int idx = fqdn.indexOf('.');
+ if (idx > 0) {
+ setProperty(NAME, fqdn.substring(0, idx));
+ setProperty(DOMAIN, fqdn.substring(idx+1));
+ } else {
+ setProperty(NAME, fqdn);
+ setProperty(DOMAIN, DEF_DOMAIN);
+ }
+ }
+
+ private void setProperty(String name, String value) {
+ getProject().setNewProperty(prefix + name, value);
+ }
+
+}
diff --git a/src/main/org/apache/tools/ant/taskdefs/defaults.properties b/src/main/org/apache/tools/ant/taskdefs/defaults.properties
index 122fe5cf7..ef114d881 100644
--- a/src/main/org/apache/tools/ant/taskdefs/defaults.properties
+++ b/src/main/org/apache/tools/ant/taskdefs/defaults.properties
@@ -55,6 +55,7 @@ genkey=org.apache.tools.ant.taskdefs.GenerateKey
get=org.apache.tools.ant.taskdefs.Get
gunzip=org.apache.tools.ant.taskdefs.GUnzip
gzip=org.apache.tools.ant.taskdefs.GZip
+hostinfo=org.apache.tools.ant.taskdefs.HostInfo
import=org.apache.tools.ant.taskdefs.ImportTask
include=org.apache.tools.ant.taskdefs.ImportTask
input=org.apache.tools.ant.taskdefs.Input
diff --git a/src/tests/antunit/taskdefs/hostinfo-test.xml b/src/tests/antunit/taskdefs/hostinfo-test.xml
new file mode 100644
index 000000000..43fc7b5a4
--- /dev/null
+++ b/src/tests/antunit/taskdefs/hostinfo-test.xml
@@ -0,0 +1,94 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+