📄 rexectask.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package org.apache.tools.ant.taskdefs.optional.net;import org.apache.commons.net.bsd.RExecClient;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.Calendar;import java.util.Enumeration;import java.util.Vector;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.Project;import org.apache.tools.ant.Task;/** * Automates the rexec protocol. * * @since Ant 1.6 */public class RExecTask extends Task { private static final int PAUSE_TIME = 250; /** * The userid to login with, if automated login is used */ private String userid = null; /** * The password to login with, if automated login is used */ private String password = null; /** * The command to execute */ private String command = null; /** * The server to connect to. */ private String server = null; /** * The tcp port to connect to. */ private int port = RExecClient.DEFAULT_PORT; /** * The list of read/write commands for this session */ private Vector rexecTasks = new Vector(); /** * If true, adds a CR to beginning of login script */ private boolean addCarriageReturn = false; /** * Default time allowed for waiting for a valid response * for all child reads. A value of 0 means no limit. */ private Integer defaultTimeout = null; /** * This class is the parent of the Read and Write tasks. * It handles the common attributes for both. */ public class RExecSubTask { // CheckStyle:VisibilityModifier OFF - bc protected String taskString = ""; // CheckStyle:VisibilityModifier ON /** * Execute the subtask. * @param rexec the client * @throws BuildException always as it is not allowed to instantiate this object */ public void execute(AntRExecClient rexec) throws BuildException { throw new BuildException("Shouldn't be able instantiate a SubTask directly"); } /** * the message as nested text * @param s the nested text */ public void addText(String s) { setString(getProject().replaceProperties(s)); } /** * the message as an attribute * @param s a <code>String</code> value */ public void setString(String s) { taskString += s; } } /** * Sends text to the connected server */ public class RExecWrite extends RExecSubTask { private boolean echoString = true; /** * Execute the write exec task. * @param rexec the task to use * @throws BuildException on error */ public void execute(AntRExecClient rexec) throws BuildException { rexec.sendString(taskString, echoString); } /** * Whether or not the message should be echoed to the log. * Defaults to <code>true</code>. * @param b a <code>boolean</code> value */ public void setEcho(boolean b) { echoString = b; } } /** * Reads the output from the connected server * until the required string is found or we time out. */ public class RExecRead extends RExecSubTask { private Integer timeout = null; /** * Execute the read exec task. * @param rexec the task to use * @throws BuildException on error */ public void execute(AntRExecClient rexec) throws BuildException { rexec.waitForString(taskString, timeout); } /** * a timeout value that overrides any task wide timeout. * @param i an <code>Integer</code> value */ public void setTimeout(Integer i) { this.timeout = i; } /** * Sets the default timeout if none has been set already * @param defaultTimeout an <code>Integer</code> value * @ant.attribute ignore="true" */ public void setDefaultTimeout(Integer defaultTimeout) { if (timeout == null) { timeout = defaultTimeout; } } } /** * This class handles the abstraction of the rexec protocol. * Currently it is a wrapper around <a * href="http://jakarta.apache.org/commons/net/index.html">Jakarta * Commons Net</a>. */ public class AntRExecClient extends RExecClient { /** * Read from the rexec session until the string we are * waiting for is found * @param s The string to wait on */ public void waitForString(String s) { waitForString(s, null); } /** * Read from the rexec session until the string we are * waiting for is found or the timeout has been reached * @param s The string to wait on * @param timeout The maximum number of seconds to wait */ public void waitForString(String s, Integer timeout) { InputStream is = this.getInputStream(); try { StringBuffer sb = new StringBuffer(); if (timeout == null || timeout.intValue() == 0) { while (sb.toString().indexOf(s) == -1) { sb.append((char) is.read()); } } else { Calendar endTime = Calendar.getInstance(); endTime.add(Calendar.SECOND, timeout.intValue()); while (sb.toString().indexOf(s) == -1) { while (Calendar.getInstance().before(endTime) && is.available() == 0) { Thread.sleep(PAUSE_TIME); } if (is.available() == 0) { throw new BuildException( "Response timed-out waiting for \"" + s + '\"', getLocation()); } sb.append((char) is.read()); } } log(sb.toString(), Project.MSG_INFO); } catch (BuildException be) { throw be; } catch (Exception e) { throw new BuildException(e, getLocation()); } } /** * Write this string to the rexec session. * @param s the string to write * @param echoString if true log the string sent */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -