source: release-kits/wirk3/ant-scripts/tasks/antelope/src/ise/antelope/tasks/HostnameTask.java@ 15023

Last change on this file since 15023 was 15023, checked in by oranfry, 16 years ago

did the bulk of the work on wirk3

File size: 5.3 KB
Line 
1package ise.antelope.tasks;
2
3import java.io.IOException;
4import java.net.*;
5import java.util.Enumeration;
6import org.apache.tools.ant.BuildException;
7import org.apache.tools.ant.Task;
8
9/**
10 * A task to put hostname or IP in a property. The default property is
11 * 'hostname'. <p>
12 *
13 * I updated this task in Apr 2005 to be able to show all hostnames and IP's for
14 * the local machine, which is sometimes handy for machines with dual cards,
15 * like maybe an ethernet card and a wireless card. <p>
16 *
17 * Dale Anson, May 2001
18 *
19 * @version $Revision: 1.2 $
20 */
21public class HostnameTask extends Task {
22
23 private String property = "hostname";
24 private boolean useIp = false;
25 private String host = null;
26 private String nIC = null;
27 private boolean failOnError = false;
28 private boolean showAll = false;
29 private boolean canonical = true;
30 private int outputType = 0;
31 public static final int HOST = 0;
32 public static final int IP = 1;
33 public static final int CANON = 2;
34
35 /**
36 * @param p The name of the property to hold the hostname or IP address.
37 */
38 public void setProperty(String p) {
39 property = p;
40 }
41
42 /**
43 * Should this task get the IP address of the local host instead of the
44 * hostname? Default is no, get the hostname, not the IP address.
45 *
46 * @param b The new ip value
47 */
48 public void setShowip(boolean b) {
49 useIp = b;
50 outputType = IP;
51 }
52
53 /**
54 * Should the build fail if this task fails? Default is no.
55 *
56 * @param b The new failonerror value
57 */
58 public void setFailonerror(boolean b) {
59 failOnError = b;
60 }
61
62 /**
63 * Set a specific network interface to get info for.
64 *
65 * @param n The new nic value
66 */
67 public void setNic(String n) {
68 nIC = n;
69 }
70
71 /**
72 * Show all network interfaces, hostnames and IPs for the local host.
73 * Default is false. If used, output will be something like:<br>
74 * lo:127.0.0.1, eth0:mycomputer.somewhere.com, eth1:wireless.somwhere.com
75 *
76 * @param b if true, show all hostnames and IPs
77 */
78 public void setShowall(boolean b) {
79 showAll = b;
80 }
81
82 public void setShowcanonical(boolean b) {
83 canonical = b;
84 outputType = CANON;
85 }
86
87 /** Description of the Method */
88 public void execute() {
89 try {
90 if (showAll) {
91 StringBuffer hostnames = new StringBuffer();
92 Enumeration nics = NetworkInterface.getNetworkInterfaces();
93 while (nics.hasMoreElements()) {
94 NetworkInterface nic = (NetworkInterface) nics.nextElement();
95 hostnames.append(nic.getName() + ":");
96 Enumeration addrs = nic.getInetAddresses();
97 while (addrs.hasMoreElements()) {
98 InetAddress addr = (InetAddress) addrs.nextElement();
99 String hostname = getAddress(addr);
100 if (hostname != null && hostname.trim().length() > 0)
101 hostnames.append(hostname);
102 if (addrs.hasMoreElements())
103 hostnames.append(", ");
104 }
105 if (nics.hasMoreElements())
106 hostnames.append(", ");
107 }
108 getProject().setProperty(property, hostnames.toString());
109 }
110 else if (nIC != null) {
111 StringBuffer hostnames = new StringBuffer();
112 Enumeration nics = NetworkInterface.getNetworkInterfaces();
113 while (nics.hasMoreElements()) {
114 NetworkInterface nic = (NetworkInterface) nics.nextElement();
115 if (nIC.equals(nic.getName())) {
116 hostnames.append(nic.getName() + ":");
117 Enumeration addrs = nic.getInetAddresses();
118 while (addrs.hasMoreElements()) {
119 InetAddress addr = (InetAddress) addrs.nextElement();
120 String hostname = getAddress(addr);
121 if (hostname != null && hostname.trim().length() > 0)
122 hostnames.append(hostname);
123 if (addrs.hasMoreElements())
124 hostnames.append(", ");
125 }
126 }
127 }
128 getProject().setProperty(property, hostnames.toString());
129 }
130 else {
131 InetAddress addr = InetAddress.getLocalHost();
132 String hostname = getAddress(addr);
133 getProject().setProperty(property, hostname);
134 }
135 }
136 catch (IOException e) {
137 if (failOnError)
138 throw new BuildException(e.getMessage());
139 else
140 log(e.getMessage());
141 }
142 }
143
144 private String getAddress(InetAddress addr) {
145 String hostname = "";
146 switch (outputType) {
147 case IP:
148 hostname = addr.getHostAddress();
149 break;
150 case CANON:
151 hostname = addr.getCanonicalHostName();
152 break;
153 default:
154 hostname = addr.getHostName();
155 }
156 return hostname;
157 }
158}
159
Note: See TracBrowser for help on using the repository browser.