FileDocCategorySizeDatePackage
RExecTask.javaAPI DocApache Ant 1.7015576Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.taskdefs.optional.net

RExecTask

public class RExecTask extends org.apache.tools.ant.Task
Automates the rexec protocol.
since
Ant 1.6

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
command
The command to execute
private String
server
The server to connect to.
private int
port
The tcp port to connect to.
private Vector
rexecTasks
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.RExecTask$RExecSubTaskcreateRead()
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 sub task

        RExecSubTask task = (RExecSubTask) new RExecRead();
        rexecTasks.addElement(task);
        return task;
    
public org.apache.tools.ant.taskdefs.optional.net.RExecTask$RExecSubTaskcreateWrite()
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 sub task

        RExecSubTask task = (RExecSubTask) new RExecWrite();
        rexecTasks.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 */
        AntRExecClient rexec = null;
        try {
            rexec = new AntRExecClient();
            try {
                rexec.connect(server, port);
            } catch (IOException e) {
                throw new BuildException("Can't connect to " + server);
            }
            if (userid != null && password != null && command != null
                && rexecTasks.size() == 0) {
                // simple one-shot execution
                rexec.rexec(userid, password, command);
            } else {
                // need nested read/write elements
                handleMultipleTasks(rexec);
            }

            /** Keep reading input stream until end of it or time-out */
            rexec.waitForEOF(defaultTimeout);
        } catch (IOException e) {
            throw new BuildException("Error r-executing command", e);
        } finally {
            if (rexec != null && rexec.isConnected()) {
                try {
                    rexec.disconnect();
                } catch (IOException e) {
                    throw new BuildException("Error disconnecting from "
                                             + server);
                }
            }
        }
    
private voidhandleMultipleTasks(org.apache.tools.ant.taskdefs.optional.net.RExecTask$AntRExecClient rexec)
Deals with multiple read/write calls.

since
Ant 1.6.3


        /**  Login if userid and password were specified */
        if (userid != null && password != null) {
            login(rexec);
        }
        /**  Process each sub command */
        Enumeration tasksToRun = rexecTasks.elements();
        while (tasksToRun != null && tasksToRun.hasMoreElements()) {
            RExecSubTask task = (RExecSubTask) tasksToRun.nextElement();
            if (task instanceof RExecRead && defaultTimeout != null) {
                ((RExecRead) task).setDefaultTimeout(defaultTimeout);
            }
            task.execute(rexec);
        }
    
private voidlogin(org.apache.tools.ant.taskdefs.optional.net.RExecTask$AntRExecClient rexec)
Process a 'typical' login. If it differs, use the read and write tasks explicitely

        if (addCarriageReturn) {
            rexec.sendString("\n", true);
        }
        rexec.waitForString("ogin:");
        rexec.sendString(userid, true);
        rexec.waitForString("assword:");
        rexec.sendString(password, false);
    
public voidsetCommand(java.lang.String c)
Set the the comand to execute on the server;

param
c a String value

        this.command = c;
    
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;