FileDocCategorySizeDatePackage
TelnetTask.javaAPI DocApache Ant 1.7012462Wed Dec 13 06:16:22 GMT 2006org.apache.tools.ant.taskdefs.optional.net

TelnetTask

public class TelnetTask extends org.apache.tools.ant.Task
Automates the telnet protocol.

Fields Summary
private String
userid
The userid to login with, if automated login is used
private String
password
The password to login with, if automated login is used
private String
server
The server to connect to.
private int
port
The tcp port to connect to.
private Vector
telnetTasks
The list of read/write commands for this session
private boolean
addCarriageReturn
If true, adds a CR to beginning of login script
private Integer
defaultTimeout
Default time allowed for waiting for a valid response for all child reads. A value of 0 means no limit.
Constructors Summary
Methods Summary
public org.apache.tools.ant.taskdefs.optional.net.TelnetTask$TelnetSubTaskcreateRead()
A string to wait for from the server. A subTask <read> tag was found. Create the object, Save it in our list, and return it.

return
a read telnet sub task

        TelnetSubTask task = (TelnetSubTask) new TelnetRead();
        telnetTasks.addElement(task);
        return task;
    
public org.apache.tools.ant.taskdefs.optional.net.TelnetTask$TelnetSubTaskcreateWrite()
Add text to send to the server A subTask <write> tag was found. Create the object, Save it in our list, and return it.

return
a write telnet sub task

        TelnetSubTask task = (TelnetSubTask) new TelnetWrite();
        telnetTasks.addElement(task);
        return task;
    
public voidexecute()
Verify that all parameters are included. Connect and possibly login Iterate through the list of Reads and writes

throws
BuildException on error


                                  
         
       /**  A server name is required to continue */
       if (server == null) {
           throw new BuildException("No Server Specified");
       }
       /**  A userid and password must appear together
        *   if they appear.  They are not required.
        */
       if (userid == null && password != null) {
           throw new BuildException("No Userid Specified");
       }
       if (password == null && userid != null) {
           throw new BuildException("No Password Specified");
       }

       /**  Create the telnet client object */
       AntTelnetClient telnet = null;
       try {
           telnet = new AntTelnetClient();
           try {
               telnet.connect(server, port);
           } catch (IOException e) {
               throw new BuildException("Can't connect to " + server);
           }
           /**  Login if userid and password were specified */
           if (userid != null && password != null) {
               login(telnet);
           }
           /**  Process each sub command */
           Enumeration tasksToRun = telnetTasks.elements();
           while (tasksToRun != null && tasksToRun.hasMoreElements()) {
               TelnetSubTask task = (TelnetSubTask) tasksToRun.nextElement();
               if (task instanceof TelnetRead && defaultTimeout != null) {
                   ((TelnetRead) task).setDefaultTimeout(defaultTimeout);
               }
               task.execute(telnet);
           }
       } finally {
           if (telnet != null && telnet.isConnected()) {
               try {
                   telnet.disconnect();
               } catch (IOException e) {
                   throw new BuildException("Error disconnecting from "
                                            + server);
               }
           }
       }
    
private voidlogin(org.apache.tools.ant.taskdefs.optional.net.TelnetTask$AntTelnetClient telnet)
Process a 'typical' login. If it differs, use the read and write tasks explicitely

       if (addCarriageReturn) {
          telnet.sendString("\n", true);
       }
       telnet.waitForString("ogin:");
       telnet.sendString(userid, true);
       telnet.waitForString("assword:");
       telnet.sendString(password, false);
    
public voidsetInitialCR(boolean b)
send a carriage return after connecting; optional, defaults to false.

param
b a boolean value

       this.addCarriageReturn = b;
    
public voidsetPassword(java.lang.String p)
Set the the login password to use required if userid is set.

param
p a String value

        this.password = p;
    
public voidsetPort(int p)
Set the tcp port to connect to; default is 23.

param
p an int value

        this.port = p;
    
public voidsetServer(java.lang.String m)
Set the hostname or address of the remote server.

param
m a String value

        this.server = m;
    
public voidsetTimeout(java.lang.Integer i)
set a default timeout in seconds to wait for a response, zero means forever (the default)

param
i an Integer value

       this.defaultTimeout = i;
    
public voidsetUserid(java.lang.String u)
Set the the login id to use on the server; required if password is set.

param
u a String value

        this.userid = u;