Author: jkf Date: Sat Jan 31 14:54:54 2009 New Revision: 739567 URL: http://svn.apache.org/viewvc?rev=739567&view=rev Log: Adding task to provide host info, requests in bug reports 31164 and 45861.
Added: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/HostInfo.java ant/core/trunk/src/tests/antunit/taskdefs/hostinfo-test.xml Modified: ant/core/trunk/WHATSNEW ant/core/trunk/docs/manual/coretasklist.html ant/core/trunk/docs/manual/tasksoverview.html ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/defaults.properties Modified: ant/core/trunk/WHATSNEW URL: http://svn.apache.org/viewvc/ant/core/trunk/WHATSNEW?rev=739567&r1=739566&r2=739567&view=diff ============================================================================== --- ant/core/trunk/WHATSNEW (original) +++ ant/core/trunk/WHATSNEW Sat Jan 31 14:54:54 2009 @@ -340,6 +340,9 @@ 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 Modified: ant/core/trunk/docs/manual/coretasklist.html URL: http://svn.apache.org/viewvc/ant/core/trunk/docs/manual/coretasklist.html?rev=739567&r1=739566&r2=739567&view=diff ============================================================================== --- ant/core/trunk/docs/manual/coretasklist.html (original) +++ ant/core/trunk/docs/manual/coretasklist.html Sat Jan 31 14:54:54 2009 @@ -74,6 +74,7 @@ <a href="CoreTasks/fixcrlf.html">FixCRLF</a><br/> <a href="CoreTasks/genkey.html">GenKey</a><br/> <a href="CoreTasks/get.html">Get</a><br/> +<a href="CoreTasks/hostinfo.html">Hostinfo</a><br/> <a href="CoreTasks/unpack.html">GUnzip</a><br/> <a href="CoreTasks/pack.html">GZip</a><br/> <a href="CoreTasks/import.html">Import</a><br/> Modified: ant/core/trunk/docs/manual/tasksoverview.html URL: http://svn.apache.org/viewvc/ant/core/trunk/docs/manual/tasksoverview.html?rev=739567&r1=739566&r2=739567&view=diff ============================================================================== --- ant/core/trunk/docs/manual/tasksoverview.html (original) +++ ant/core/trunk/docs/manual/tasksoverview.html Sat Jan 31 14:54:54 2009 @@ -749,6 +749,13 @@ <td nowrap><a href="CoreTasks/genkey.html">GenKey</a></td> <td><p>Generates a key in keystore.</p></td> </tr> + + + <tr valign="top"> + <td nowrap><a href="CoreTasks/hostinfo.html">HostInfo</a></td> + <td><p>Sets properties related to the provided host, or to + the host the process is run on.</p></td> + </tr> <tr valign="top"> <td nowrap><a href="CoreTasks/input.html">Input</a></td> Added: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/HostInfo.java URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/HostInfo.java?rev=739567&view=auto ============================================================================== --- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/HostInfo.java (added) +++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/HostInfo.java Sat Jan 31 14:54:54 2009 @@ -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); + } + +} Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/defaults.properties URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/defaults.properties?rev=739567&r1=739566&r2=739567&view=diff ============================================================================== --- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/defaults.properties (original) +++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/defaults.properties Sat Jan 31 14:54:54 2009 @@ -55,6 +55,7 @@ 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 Added: ant/core/trunk/src/tests/antunit/taskdefs/hostinfo-test.xml URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/antunit/taskdefs/hostinfo-test.xml?rev=739567&view=auto ============================================================================== --- ant/core/trunk/src/tests/antunit/taskdefs/hostinfo-test.xml (added) +++ ant/core/trunk/src/tests/antunit/taskdefs/hostinfo-test.xml Sat Jan 31 14:54:54 2009 @@ -0,0 +1,94 @@ +<?xml version="1.0"?> +<!-- + 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. +--> +<project name="hostinfo-test" default="antunit" + xmlns:au="antlib:org.apache.ant.antunit"> + + <import file="../antunit-base.xml" /> + + <property name="undef-name" value="nonexistenthost.nonexistentdomain" /> + <property name="undef-hostname" value="nonexistenthost" /> + <property name="undef-domainname" value="nonexistentdomain" /> + + <property name="undef-ip4" value="0.0.0.0" /> + <property name="undef-ip6" value="::" /> + + <property name="apache-hostname" value="www.apache.org" /> + <property name="apache-domain" value="apache.org" /> + <property name="apache-realhost" value="eos" /> + <property name="apache-ip4" value="140.211.11.130" /> + + <property name="xs4all-hostname" value="www.xs4all.nl" /> + <property name="xs4all-domain" value="xs4all.nl" /> + <property name="xs4all-realhost" value="www" /> + <property name="xs4all-ip4" value="194.109.6.92" /> + + <target name="setUp"> + </target> + + <target name="testLocal" depends="setUp"> + <hostinfo prefix="local" /> + <!-- Do not know what to expect here, machine dependent --> + </target> + + <target name="testApache" depends="setUp"> + <hostinfo prefix="apache" host="${apache-hostname}"/> + <au:assertTrue> + <and> + <equals arg1="${apache.NAME}" arg2="${apache-realhost}" /> + <equals arg1="${apache.DOMAIN}" arg2="${apache-domain}" /> + <equals arg1="${apache.ADDR4}" arg2="${apache-ip4}" /> + </and> + </au:assertTrue> + </target> + + <target name="testApacheNoPrefix" depends="setUp"> + <hostinfo host="${apache-hostname}"/> + <au:assertTrue> + <and> + <equals arg1="${NAME}" arg2="${apache-realhost}" /> + <equals arg1="${DOMAIN}" arg2="${apache-domain}" /> + <equals arg1="${ADDR4}" arg2="${apache-ip4}" /> + </and> + </au:assertTrue> + </target> + + <target name="testReverse" depends="setUp"> + <hostinfo prefix="reverse" host="${xs4all-ip4}"/> + <au:assertTrue> + <and> + <equals arg1="${reverse.NAME}" arg2="${xs4all-realhost}" /> + <equals arg1="${reverse.DOMAIN}" arg2="${xs4all-domain}" /> + <equals arg1="${reverse.ADDR4}" arg2="${xs4all-ip4}" /> + </and> + </au:assertTrue> + </target> + + + <target name="testUndef" depends="setUp"> + <hostinfo prefix="undef" host="${undef-name}"/> + <au:assertTrue> + <and> + <equals arg1="${undef.NAME}" arg2="${undef-hostname}" /> + <equals arg1="${undef.DOMAIN}" arg2="${undef-domainname}" /> + <equals arg1="${undef.ADDR4}" arg2="${undef-ip4}" /> + <equals arg1="${undef.ADDR6}" arg2="${undef-ip6}" /> + </and> + </au:assertTrue> + </target> + +</project>