source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/optional/net/TelnetTask.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 11.1 KB
Line 
1/*
2 * Copyright 2000,2002-2005 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.apache.tools.ant.taskdefs.optional.net;
19
20import org.apache.commons.net.telnet.TelnetClient;
21import java.io.IOException;
22import java.io.InputStream;
23import java.io.OutputStream;
24import java.util.Calendar;
25import java.util.Enumeration;
26import java.util.Vector;
27import org.apache.tools.ant.BuildException;
28import org.apache.tools.ant.Project;
29import org.apache.tools.ant.Task;
30
31/**
32 * Automates the telnet protocol.
33 *
34 */
35
36public class TelnetTask extends Task {
37 /**
38 * The userid to login with, if automated login is used
39 */
40 private String userid = null;
41
42 /**
43 * The password to login with, if automated login is used
44 */
45 private String password = null;
46
47 /**
48 * The server to connect to.
49 */
50 private String server = null;
51
52 /**
53 * The tcp port to connect to.
54 */
55 private int port = 23;
56
57 /**
58 * The list of read/write commands for this session
59 */
60 private Vector telnetTasks = new Vector();
61
62 /**
63 * If true, adds a CR to beginning of login script
64 */
65 private boolean addCarriageReturn = false;
66
67 /**
68 * Default time allowed for waiting for a valid response
69 * for all child reads. A value of 0 means no limit.
70 */
71 private Integer defaultTimeout = null;
72
73 /**
74 * Verify that all parameters are included.
75 * Connect and possibly login
76 * Iterate through the list of Reads and writes
77 */
78 public void execute() throws BuildException {
79 /** A server name is required to continue */
80 if (server == null) {
81 throw new BuildException("No Server Specified");
82 }
83 /** A userid and password must appear together
84 * if they appear. They are not required.
85 */
86 if (userid == null && password != null) {
87 throw new BuildException("No Userid Specified");
88 }
89 if (password == null && userid != null) {
90 throw new BuildException("No Password Specified");
91 }
92
93 /** Create the telnet client object */
94 AntTelnetClient telnet = null;
95 try {
96 telnet = new AntTelnetClient();
97 try {
98 telnet.connect(server, port);
99 } catch (IOException e) {
100 throw new BuildException("Can't connect to " + server);
101 }
102 /** Login if userid and password were specified */
103 if (userid != null && password != null) {
104 login(telnet);
105 }
106 /** Process each sub command */
107 Enumeration tasksToRun = telnetTasks.elements();
108 while (tasksToRun != null && tasksToRun.hasMoreElements()) {
109 TelnetSubTask task = (TelnetSubTask) tasksToRun.nextElement();
110 if (task instanceof TelnetRead && defaultTimeout != null) {
111 ((TelnetRead) task).setDefaultTimeout(defaultTimeout);
112 }
113 task.execute(telnet);
114 }
115 } finally {
116 if (telnet != null && telnet.isConnected()) {
117 try {
118 telnet.disconnect();
119 } catch (IOException e) {
120 throw new BuildException("Error disconnecting from "
121 + server);
122 }
123 }
124 }
125 }
126
127 /**
128 * Process a 'typical' login. If it differs, use the read
129 * and write tasks explicitely
130 */
131 private void login(AntTelnetClient telnet) {
132 if (addCarriageReturn) {
133 telnet.sendString("\n", true);
134 }
135 telnet.waitForString("ogin:");
136 telnet.sendString(userid, true);
137 telnet.waitForString("assword:");
138 telnet.sendString(password, false);
139 }
140
141 /**
142 * Set the the login id to use on the server;
143 * required if <tt>password</tt> is set.
144 */
145 public void setUserid(String u) { this.userid = u; }
146
147 /**
148 * Set the the login password to use
149 * required if <tt>userid</tt> is set.
150 */
151 public void setPassword(String p) { this.password = p; }
152
153 /**
154 * Set the hostname or address of the remote server.
155 */
156 public void setServer(String m) { this.server = m; }
157
158 /**
159 * Set the tcp port to connect to; default is 23.
160 */
161 public void setPort(int p) { this.port = p; }
162
163 /**
164 * send a carriage return after connecting; optional, defaults to false.
165 */
166 public void setInitialCR(boolean b) {
167 this.addCarriageReturn = b;
168 }
169
170 /**
171 * set a default timeout in seconds to wait for a response,
172 * zero means forever (the default)
173 */
174 public void setTimeout(Integer i) {
175 this.defaultTimeout = i;
176 }
177
178 /**
179 * A string to wait for from the server.
180 * A subTask &lt;read&gt; tag was found. Create the object,
181 * Save it in our list, and return it.
182 */
183
184 public TelnetSubTask createRead() {
185 TelnetSubTask task = (TelnetSubTask) new TelnetRead();
186 telnetTasks.addElement(task);
187 return task;
188 }
189
190 /**
191 * Add text to send to the server
192 * A subTask &lt;write&gt; tag was found. Create the object,
193 * Save it in our list, and return it.
194 */
195 public TelnetSubTask createWrite() {
196 TelnetSubTask task = (TelnetSubTask) new TelnetWrite();
197 telnetTasks.addElement(task);
198 return task;
199 }
200
201 /**
202 * This class is the parent of the Read and Write tasks.
203 * It handles the common attributes for both.
204 */
205 public class TelnetSubTask {
206 protected String taskString = "";
207 public void execute(AntTelnetClient telnet)
208 throws BuildException {
209 throw new BuildException("Shouldn't be able instantiate a SubTask directly");
210 }
211
212 /**
213 * the message as nested text
214 */
215 public void addText(String s) {
216 setString(getProject().replaceProperties(s));
217 }
218
219 /**
220 * the message as an attribute
221 */
222 public void setString(String s) {
223 taskString += s;
224 }
225 }
226
227 /**
228 * Sends text to the connected server
229 */
230 public class TelnetWrite extends TelnetSubTask {
231 private boolean echoString = true;
232 public void execute(AntTelnetClient telnet)
233 throws BuildException {
234 telnet.sendString(taskString, echoString);
235 }
236
237 /**
238 * Whether or not the message should be echoed to the log.
239 * Defaults to <code>true</code>.
240 */
241 public void setEcho(boolean b) {
242 echoString = b;
243 }
244 }
245
246 /**
247 * Reads the output from the connected server
248 * until the required string is found or we time out.
249 */
250 public class TelnetRead extends TelnetSubTask {
251 private Integer timeout = null;
252 public void execute(AntTelnetClient telnet)
253 throws BuildException {
254 telnet.waitForString(taskString, timeout);
255 }
256 /**
257 * a timeout value that overrides any task wide timeout.
258 */
259 public void setTimeout(Integer i) {
260 this.timeout = i;
261 }
262
263 /**
264 * Sets the default timeout if none has been set already
265 * @ant.attribute ignore="true"
266 */
267 public void setDefaultTimeout(Integer defaultTimeout) {
268 if (timeout == null) {
269 timeout = defaultTimeout;
270 }
271 }
272 }
273
274 /**
275 * This class handles the abstraction of the telnet protocol.
276 * Currently it is a wrapper around <a
277 * href="http://jakarta.apache.org/commons/net/index.html">Jakarta
278 * Commons Net</a>.
279 */
280 public class AntTelnetClient extends TelnetClient {
281 /**
282 * Read from the telnet session until the string we are
283 * waiting for is found
284 * @param s The string to wait on
285 */
286 public void waitForString(String s) {
287 waitForString(s, null);
288 }
289
290 /**
291 * Read from the telnet session until the string we are
292 * waiting for is found or the timeout has been reached
293 * @param s The string to wait on
294 * @param timeout The maximum number of seconds to wait
295 */
296 public void waitForString(String s, Integer timeout) {
297 InputStream is = this.getInputStream();
298 try {
299 StringBuffer sb = new StringBuffer();
300 if (timeout == null || timeout.intValue() == 0) {
301 while (sb.toString().indexOf(s) == -1) {
302 sb.append((char) is.read());
303 }
304 } else {
305 Calendar endTime = Calendar.getInstance();
306 endTime.add(Calendar.SECOND, timeout.intValue());
307 while (sb.toString().indexOf(s) == -1) {
308 while (Calendar.getInstance().before(endTime) &&
309 is.available() == 0) {
310 Thread.sleep(250);
311 }
312 if (is.available() == 0) {
313 log("Read before running into timeout: "
314 + sb.toString(), Project.MSG_DEBUG);
315 throw new BuildException(
316 "Response timed-out waiting for \"" + s + '\"',
317 getLocation());
318 }
319 sb.append((char) is.read());
320 }
321 }
322 log(sb.toString(), Project.MSG_INFO);
323 } catch (BuildException be) {
324 throw be;
325 } catch (Exception e) {
326 throw new BuildException(e, getLocation());
327 }
328 }
329
330 /**
331 * Write this string to the telnet session.
332 * @param echoString Logs string sent
333 */
334 public void sendString(String s, boolean echoString) {
335 OutputStream os = this.getOutputStream();
336 try {
337 os.write((s + "\n").getBytes());
338 if (echoString) {
339 log(s, Project.MSG_INFO);
340 }
341 os.flush();
342 } catch (Exception e) {
343 throw new BuildException(e, getLocation());
344 }
345 }
346 }
347}
Note: See TracBrowser for help on using the repository browser.