2
* The Apache Software License, Version 1.1
4
* Copyright (c) 2000 The Apache Software Foundation. All rights
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions
11
* 1. Redistributions of source code must retain the above copyright
12
* notice, this list of conditions and the following disclaimer.
14
* 2. Redistributions in binary form must reproduce the above copyright
15
* notice, this list of conditions and the following disclaimer in
16
* the documentation and/or other materials provided with the
19
* 3. The end-user documentation included with the redistribution, if
20
* any, must include the following acknowlegement:
21
* "This product includes software developed by the
22
* Apache Software Foundation (http://www.apache.org/)."
23
* Alternately, this acknowlegement may appear in the software itself,
24
* if and wherever such third-party acknowlegements normally appear.
26
* 4. The names "The Jakarta Project", "Ant", and "Apache Software
27
* Foundation" must not be used to endorse or promote products derived
28
* from this software without prior written permission. For written
29
* permission, please contact apache@apache.org.
31
* 5. Products derived from this software may not be called "Apache"
32
* nor may "Apache" appear in their names without prior written
33
* permission of the Apache Group.
35
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47
* ====================================================================
49
* This software consists of voluntary contributions made by many
50
* individuals on behalf of the Apache Software Foundation. For more
51
* information on the Apache Software Foundation, please see
52
* <http://www.apache.org/>.
55
package org.apache.tools.ant.taskdefs.optional.net;
57
import org.apache.tools.ant.BuildException;
58
import org.apache.tools.ant.Task;
59
import org.apache.tools.ant.Project;
60
import org.apache.tools.ant.DirectoryScanner;
61
import org.apache.tools.ant.taskdefs.MatchingTask;
62
import com.oroinc.net.telnet.*;
63
import org.apache.tools.ant.BuildException;
69
* Class to provide automated telnet protocol support for the Ant build tool
71
* @author Scott Carlson<a href="mailto:ScottCarlson@email.com">ScottCarlson@email.com</a>
72
* @version $Revision: 1.2 $
75
public class TelnetTask extends Task {
77
* The userid to login with, if automated login is used
79
private String userid = null;
82
* The password to login with, if automated login is used
84
private String password= null;
87
* The server to connect to.
89
private String server = null;
92
* The tcp port to connect to.
94
private int port = 23;
97
* The Object which handles the telnet session.
99
private AntTelnetClient telnet = null;
102
* The list of read/write commands for this session
104
private Vector telnetTasks = new Vector();
107
* If true, adds a CR to beginning of login script
109
private boolean addCarriageReturn = false;
112
* Default time allowed for waiting for a valid response
113
* for all child reads. A value of 0 means no limit.
115
private Integer defaultTimeout = null;
118
* Verify that all parameters are included.
119
* Connect and possibly login
120
* Iterate through the list of Reads and writes
122
public void execute() throws BuildException
124
/** A server name is required to continue */
126
throw new BuildException("No Server Specified");
127
/** A userid and password must appear together
128
* if they appear. They are not required.
130
if (userid == null && password != null)
131
throw new BuildException("No Userid Specified");
132
if (password == null && userid != null)
133
throw new BuildException("No Password Specified");
135
/** Create the telnet client object */
136
telnet = new AntTelnetClient();
138
telnet.connect(server, port);
139
} catch(IOException e) {
140
throw new BuildException("Can't connect to "+server);
142
/** Login if userid and password were specified */
143
if (userid != null && password != null)
145
/** Process each sub command */
146
Enumeration tasksToRun = telnetTasks.elements();
147
while (tasksToRun!=null && tasksToRun.hasMoreElements())
149
TelnetSubTask task = (TelnetSubTask) tasksToRun.nextElement();
150
if (task instanceof TelnetRead && defaultTimeout != null)
151
((TelnetRead)task).setDefaultTimeout(defaultTimeout);
152
task.execute(telnet);
157
* Process a 'typical' login. If it differs, use the read
158
* and write tasks explicitely
162
if (addCarriageReturn)
163
telnet.sendString("\n", true);
164
telnet.waitForString("ogin:");
165
telnet.sendString(userid, true);
166
telnet.waitForString("assword:");
167
telnet.sendString(password, false);
171
* Set the userid attribute
173
public void setUserid(String u) { this.userid = u; }
176
* Set the password attribute
178
public void setPassword(String p) { this.password = p; }
181
* Set the server address attribute
183
public void setServer(String m) { this.server = m; }
186
* Set the tcp port to connect to attribute
188
public void setPort(int p) { this.port = p; }
191
* Set the tcp port to connect to attribute
193
public void setInitialCR(boolean b)
195
this.addCarriageReturn = b;
199
* Change the default timeout to wait for
202
public void setTimeout(Integer i)
204
this.defaultTimeout = i;
208
* A subTask <read> tag was found. Create the object,
209
* Save it in our list, and return it.
212
public TelnetSubTask createRead()
214
TelnetSubTask task = (TelnetSubTask)new TelnetRead();
215
telnetTasks.addElement(task);
220
* A subTask <write> tag was found. Create the object,
221
* Save it in our list, and return it.
223
public TelnetSubTask createWrite()
225
TelnetSubTask task = (TelnetSubTask)new TelnetWrite();
226
telnetTasks.addElement(task);
231
* This class is the parent of the Read and Write tasks.
232
* It handles the common attributes for both.
234
public class TelnetSubTask
236
protected String taskString= "";
237
public void execute(AntTelnetClient telnet)
238
throws BuildException
240
throw new BuildException("Shouldn't be able instantiate a SubTask directly");
242
public void addText(String s) { setString(s);}
243
public void setString(String s)
249
* This class sends text to the connected server
251
public class TelnetWrite extends TelnetSubTask
253
private boolean echoString = true;
254
public void execute(AntTelnetClient telnet)
255
throws BuildException
257
telnet.sendString(taskString, echoString);
260
public void setEcho(boolean b)
266
* This class reads the output from the connected server
267
* until the required string is found.
269
public class TelnetRead extends TelnetSubTask
271
private Integer timeout = null;
272
public void execute(AntTelnetClient telnet)
273
throws BuildException
275
telnet.waitForString(taskString, timeout);
278
* Override any default timeouts
280
public void setTimeout(Integer i)
285
* Sets the default timeout if none has been set already
287
public void setDefaultTimeout(Integer defaultTimeout)
290
timeout = defaultTimeout;
294
* This class handles the abstraction of the telnet protocol.
295
* Currently it is a wrapper around <a href="www.oroinc.com">ORO</a>'s
298
public class AntTelnetClient extends TelnetClient
301
* Read from the telnet session until the string we are
302
* waiting for is found
303
* @parm s The string to wait on
305
public void waitForString(String s)
307
waitForString(s, null);
311
* Read from the telnet session until the string we are
312
* waiting for is found or the timeout has been reached
313
* @parm s The string to wait on
314
* @parm timeout The maximum number of seconds to wait
316
public void waitForString(String s, Integer timeout)
318
InputStream is =this.getInputStream();
320
StringBuffer sb = new StringBuffer();
321
if (timeout == null || timeout.intValue() == 0)
323
while (sb.toString().indexOf(s) == -1)
325
sb.append((char) is.read());
330
Calendar endTime = Calendar.getInstance();
331
endTime.add(Calendar.SECOND,timeout.intValue());
332
while ( sb.toString().indexOf(s) == -1)
334
while (Calendar.getInstance().before(endTime) &&
335
is.available() == 0) {
338
if (is.available() == 0)
339
throw new BuildException("Response Timed-Out", getLocation());
340
sb.append((char) is.read());
343
log(sb.toString(), Project.MSG_INFO);
344
} catch (BuildException be)
347
} catch (Exception e)
349
throw new BuildException(e, getLocation());
355
* Write this string to the telnet session.
356
* @parm echoString Logs string sent
358
public void sendString(String s, boolean echoString)
360
OutputStream os =this.getOutputStream();
362
os.write((s + "\n").getBytes());
364
log(s, Project.MSG_INFO);
366
} catch (Exception e)
368
throw new BuildException(e, getLocation());