⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rexectask.java

📁 Use the links below to download a source distribution of Ant from one of our mirrors. It is good pra
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        public void sendString(String s, boolean echoString) {            OutputStream os = this.getOutputStream();            try {                os.write((s + "\n").getBytes());                if (echoString) {                    log(s, Project.MSG_INFO);                }                os.flush();            } catch (Exception e) {                throw new BuildException(e, getLocation());            }        }        /**         * Read from the rexec session until the EOF is found or         * the timeout has been reached         * @param timeout The maximum number of seconds to wait         */        public void waitForEOF(Integer timeout) {            InputStream is = this.getInputStream();            try {                StringBuffer sb = new StringBuffer();                if (timeout == null || timeout.intValue() == 0) {                int read;                    while ((read = is.read()) != -1) {                        char c = (char) read;                        sb.append(c);                        if (c == '\n') {                        log(sb.toString(), Project.MSG_INFO);                        sb.delete(0, sb.length());                        }                    }                } else {                    Calendar endTime = Calendar.getInstance();                    endTime.add(Calendar.SECOND, timeout.intValue());                int read = 0;                    while (read != -1) {                        while (Calendar.getInstance().before(endTime) && is.available() == 0) {                            Thread.sleep(PAUSE_TIME);                        }                        if (is.available() == 0) {                        log(sb.toString(), Project.MSG_INFO);                            throw new BuildException(                                                     "Response timed-out waiting for EOF",                                                     getLocation());                        }                        read =  is.read();                        if (read != -1) {                        char c = (char) read;                        sb.append(c);                        if (c == '\n') {                                log(sb.toString(), Project.MSG_INFO);                                sb.delete(0, sb.length());                        }                        }                    }                }                if (sb.length() > 0) {                log(sb.toString(), Project.MSG_INFO);                }            } catch (BuildException be) {                throw be;            } catch (Exception e) {                throw new BuildException(e, getLocation());            }        }    }    /**     *  A string to wait for from the server.     *  A subTask &lt;read&gt; tag was found.  Create the object,     *  Save it in our list, and return it.     * @return a read sub task     */    public RExecSubTask createRead() {        RExecSubTask task = (RExecSubTask) new RExecRead();        rexecTasks.addElement(task);        return task;    }    /**     *  Add text to send to the server     *  A subTask &lt;write&gt; tag was found.  Create the object,     *  Save it in our list, and return it.     * @return a write sub task     */    public RExecSubTask createWrite() {        RExecSubTask task = (RExecSubTask) new RExecWrite();        rexecTasks.addElement(task);        return task;    }    /**     *  Verify that all parameters are included.     *  Connect and possibly login.     *  Iterate through the list of Reads and writes.     * @throws BuildException on error     */    public void execute() throws BuildException {        /**  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);                }            }        }    }    /**     *  Process a 'typical' login.  If it differs, use the read     *  and write tasks explicitely     */    private void login(AntRExecClient rexec) {        if (addCarriageReturn) {            rexec.sendString("\n", true);        }        rexec.waitForString("ogin:");        rexec.sendString(userid, true);        rexec.waitForString("assword:");        rexec.sendString(password, false);    }    /**     * Set the the comand to execute on the server;     * @param c a <code>String</code> value     */    public void setCommand(String c) {        this.command = c;    }    /**     *  send a carriage return after connecting; optional, defaults to false.     * @param b a <code>boolean</code> value     */    public void setInitialCR(boolean b) {        this.addCarriageReturn = b;    }    /**     *  Set the the login password to use     * required if <tt>userid</tt> is set.     * @param p a <code>String</code> value     */    public void setPassword(String p) {        this.password = p;    }    /**     *  Set the tcp port to connect to; default is 23.     * @param p an <code>int</code> value     */    public void setPort(int p) {        this.port = p;    }    /**     *  Set the hostname or address of the remote server.     * @param m a <code>String</code> value     */    public void setServer(String m) {        this.server = m;    }    /**     * set a default timeout in seconds to wait for a response,     * zero means forever (the default)     * @param i an <code>Integer</code> value     */    public void setTimeout(Integer i) {        this.defaultTimeout = i;    }    /**     * Set the the login id to use on the server;     * required if <tt>password</tt> is set.     * @param u a <code>String</code> value     */    public void setUserid(String u) {        this.userid = u;    }    /**     * Deals with multiple read/write calls.     *     * @since Ant 1.6.3     */    private void handleMultipleTasks(AntRExecClient rexec) {        /**  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);        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -