You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

HostInfo.java 8.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package org.apache.tools.ant.taskdefs;
  19. import java.net.Inet4Address;
  20. import java.net.Inet6Address;
  21. import java.net.InetAddress;
  22. import java.net.NetworkInterface;
  23. import java.util.Arrays;
  24. import java.util.Enumeration;
  25. import java.util.LinkedList;
  26. import java.util.List;
  27. import org.apache.tools.ant.BuildException;
  28. import org.apache.tools.ant.Project;
  29. import org.apache.tools.ant.Task;
  30. /**
  31. * Sets properties to the host provided, or localhost if no information is
  32. * provided. The default properties are NAME, FQDN, ADDR4, ADDR6;
  33. *
  34. * @since Ant 1.8
  35. * @ant.task category="utility"
  36. */
  37. public class HostInfo extends Task {
  38. private static final String DEF_REM_ADDR6 = "::";
  39. private static final String DEF_REM_ADDR4 = "0.0.0.0";
  40. private static final String DEF_LOCAL_ADDR6 = "::1";
  41. private static final String DEF_LOCAL_ADDR4 = "127.0.0.1";
  42. private static final String DEF_LOCAL_NAME = "localhost";
  43. private static final String DEF_DOMAIN = "localdomain";
  44. private static final String DOMAIN = "DOMAIN";
  45. private static final String NAME = "NAME";
  46. private static final String ADDR4 = "ADDR4";
  47. private static final String ADDR6 = "ADDR6";
  48. private String prefix = "";
  49. private String host;
  50. private InetAddress nameAddr;
  51. private InetAddress best6;
  52. private InetAddress best4;
  53. private List<InetAddress> inetAddrs;
  54. /**
  55. * Set a prefix for the properties. If the prefix does not end with a "."
  56. * one is automatically added.
  57. *
  58. * @param aPrefix
  59. * the prefix to use.
  60. * @since Ant 1.8
  61. */
  62. public void setPrefix(String aPrefix) {
  63. prefix = aPrefix;
  64. if (!prefix.endsWith(".")) {
  65. prefix += ".";
  66. }
  67. }
  68. /**
  69. * Set the host to be retrieved.
  70. *
  71. * @param aHost
  72. * the name or the address of the host, data for the local host
  73. * will be retrieved if omitted.
  74. * @since Ant 1.8
  75. */
  76. public void setHost(String aHost) {
  77. host = aHost;
  78. }
  79. /**
  80. * set the properties.
  81. *
  82. * @throws BuildException
  83. * on error.
  84. */
  85. public void execute() throws BuildException {
  86. if (host == null || "".equals(host)) {
  87. executeLocal();
  88. } else {
  89. executeRemote();
  90. }
  91. }
  92. private void executeLocal() {
  93. try {
  94. inetAddrs = new LinkedList<InetAddress>();
  95. Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
  96. while (interfaces.hasMoreElements()) {
  97. NetworkInterface currentif = interfaces.nextElement();
  98. Enumeration<InetAddress> addrs = currentif.getInetAddresses();
  99. while (addrs.hasMoreElements())
  100. {
  101. inetAddrs.add(addrs.nextElement());
  102. }
  103. }
  104. selectAddresses();
  105. if (nameAddr != null && hasHostName(nameAddr)) {
  106. setDomainAndName(nameAddr.getCanonicalHostName());
  107. } else {
  108. setProperty(DOMAIN, DEF_DOMAIN);
  109. setProperty(NAME, DEF_LOCAL_NAME);
  110. }
  111. if (best4 != null) {
  112. setProperty(ADDR4, best4.getHostAddress());
  113. } else {
  114. setProperty(ADDR4, DEF_LOCAL_ADDR4);
  115. }
  116. if (best6 != null) {
  117. setProperty(ADDR6, best6.getHostAddress());
  118. } else {
  119. setProperty(ADDR6, DEF_LOCAL_ADDR6);
  120. }
  121. } catch (Exception e) {
  122. log("Error retrieving local host information", e, Project.MSG_WARN);
  123. setProperty(DOMAIN, DEF_DOMAIN);
  124. setProperty(NAME, DEF_LOCAL_NAME);
  125. setProperty(ADDR4, DEF_LOCAL_ADDR4);
  126. setProperty(ADDR6, DEF_LOCAL_ADDR6);
  127. }
  128. }
  129. private boolean hasHostName(InetAddress addr)
  130. {
  131. return !addr.getHostAddress().equals(addr.getCanonicalHostName());
  132. }
  133. private void selectAddresses() {
  134. for (InetAddress current : inetAddrs) {
  135. if (!current.isMulticastAddress()) {
  136. if (current instanceof Inet4Address) {
  137. best4 = selectBestAddress(best4, current);
  138. } else if (current instanceof Inet6Address) {
  139. best6 = selectBestAddress(best6, current);
  140. }
  141. }
  142. }
  143. nameAddr = selectBestAddress(best4, best6);
  144. }
  145. private InetAddress selectBestAddress(InetAddress bestSoFar,
  146. InetAddress current) {
  147. InetAddress best = bestSoFar;
  148. if (best == null) {
  149. // none selected so far, so this one is better.
  150. best = current;
  151. } else {
  152. if (current == null || current.isLoopbackAddress()) {
  153. // definitely not better than the previously selected address.
  154. } else if (current.isLinkLocalAddress()) {
  155. // link local considered better than loopback
  156. if (best.isLoopbackAddress()) {
  157. best = current;
  158. }
  159. } else if (current.isSiteLocalAddress()) {
  160. // site local considered better than link local (and loopback)
  161. // address with hostname resolved considered better than
  162. // address without hostname
  163. if (best.isLoopbackAddress()
  164. || best.isLinkLocalAddress()
  165. || (best.isSiteLocalAddress() && !hasHostName(best))) {
  166. best = current;
  167. }
  168. } else {
  169. // current is a "Global address", considered better than
  170. // site local (and better than link local, loopback)
  171. // address with hostname resolved considered better than
  172. // address without hostname
  173. if (best.isLoopbackAddress()
  174. || best.isLinkLocalAddress()
  175. || best.isSiteLocalAddress()
  176. || !hasHostName(best)) {
  177. best = current;
  178. }
  179. }
  180. }
  181. return best;
  182. }
  183. private void executeRemote() {
  184. try {
  185. inetAddrs = Arrays.asList(InetAddress.getAllByName(host));
  186. selectAddresses();
  187. if (nameAddr != null && hasHostName(nameAddr)) {
  188. setDomainAndName(nameAddr.getCanonicalHostName());
  189. } else {
  190. setDomainAndName(host);
  191. }
  192. if (best4 != null) {
  193. setProperty(ADDR4, best4.getHostAddress());
  194. } else {
  195. setProperty(ADDR4, DEF_REM_ADDR4);
  196. }
  197. if (best6 != null) {
  198. setProperty(ADDR6, best6.getHostAddress());
  199. } else {
  200. setProperty(ADDR6, DEF_REM_ADDR6);
  201. }
  202. } catch (Exception e) {
  203. log("Error retrieving remote host information for host:" + host
  204. + ".", e, Project.MSG_WARN);
  205. setDomainAndName(host);
  206. setProperty(ADDR4, DEF_REM_ADDR4);
  207. setProperty(ADDR6, DEF_REM_ADDR6);
  208. }
  209. }
  210. private void setDomainAndName(String fqdn)
  211. {
  212. int idx = fqdn.indexOf('.');
  213. if (idx > 0) {
  214. setProperty(NAME, fqdn.substring(0, idx));
  215. setProperty(DOMAIN, fqdn.substring(idx+1));
  216. } else {
  217. setProperty(NAME, fqdn);
  218. setProperty(DOMAIN, DEF_DOMAIN);
  219. }
  220. }
  221. private void setProperty(String name, String value) {
  222. getProject().setNewProperty(prefix + name, value);
  223. }
  224. }