Return-Path: Delivered-To: apmail-ant-dev-archive@www.apache.org Received: (qmail 12108 invoked from network); 18 Feb 2007 04:58:45 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 18 Feb 2007 04:58:45 -0000 Received: (qmail 87985 invoked by uid 500); 18 Feb 2007 04:58:53 -0000 Delivered-To: apmail-ant-dev-archive@ant.apache.org Received: (qmail 87678 invoked by uid 500); 18 Feb 2007 04:58:52 -0000 Mailing-List: contact dev-help@ant.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Ant Developers List" Reply-To: "Ant Developers List" Delivered-To: mailing list dev@ant.apache.org Received: (qmail 87667 invoked by uid 500); 18 Feb 2007 04:58:52 -0000 Received: (qmail 87664 invoked by uid 99); 18 Feb 2007 04:58:52 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 17 Feb 2007 20:58:52 -0800 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 17 Feb 2007 20:58:44 -0800 Received: by eris.apache.org (Postfix, from userid 65534) id DC6F61A981A; Sat, 17 Feb 2007 20:58:23 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r508865 - /ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java Date: Sun, 18 Feb 2007 04:58:23 -0000 To: ant-cvs@apache.org From: kevj@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20070218045823.DC6F61A981A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: kevj Date: Sat Feb 17 20:58:23 2007 New Revision: 508865 URL: http://svn.apache.org/viewvc?view=rev&rev=508865 Log: -provide support for a command file to pass to SSHExec - implemented with a FileResource Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java?view=diff&rev=508865&r1=508864&r2=508865 ============================================================================== --- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java (original) +++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java Sat Feb 17 20:58:23 2007 @@ -18,17 +18,22 @@ package org.apache.tools.ant.taskdefs.optional.ssh; -import org.apache.tools.ant.BuildException; -import org.apache.tools.ant.Project; -import org.apache.tools.ant.util.TeeOutputStream; -import org.apache.tools.ant.util.KeepAliveOutputStream; - +import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileWriter; import java.io.IOException; +import java.io.InputStreamReader; import java.io.StringReader; +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.types.Resource; +import org.apache.tools.ant.types.resources.FileResource; +import org.apache.tools.ant.util.FileUtils; +import org.apache.tools.ant.util.KeepAliveOutputStream; +import org.apache.tools.ant.util.TeeOutputStream; + import com.jcraft.jsch.ChannelExec; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; @@ -51,6 +56,8 @@ private String outputProperty = null; // like private File outputFile = null; // like private boolean append = false; // like + + private Resource commandResource = null; private static final String TIMEOUT_MESSAGE = "Timeout period exceeded, connection dropped."; @@ -72,6 +79,15 @@ } /** + * Sets a commandResource from a file + * @param f + * @since Ant 1.7.1 + */ + public void setCommandResource(String f) { + this.commandResource = new FileResource(new File(f)); + } + + /** * The connection can be dropped after a specified number of * milliseconds. This is sometimes useful when a connection may be * flaky. Default is 0, which means "wait forever". @@ -128,24 +144,43 @@ && getUserInfo().getPassword() == null) { throw new BuildException("Password or Keyfile is required."); } - if (command == null) { - throw new BuildException("Command is required."); + if (command == null && commandResource == null) { + throw new BuildException("Command or commandFile is required."); } + /* called once */ + if (command != null) { + executeCommand(command); + } else { // read command resource and execute for each command + try { + BufferedReader br = new BufferedReader(new InputStreamReader(commandResource.getInputStream())); + String cmd; + while((cmd = br.readLine()) != null) { + log("cmd : "+cmd, Project.MSG_INFO); + executeCommand(cmd); + } + FileUtils.close(br); + } catch (IOException e) { + throw new BuildException(e); + } + } + } + + private void executeCommand(String cmd) throws BuildException { ByteArrayOutputStream out = new ByteArrayOutputStream(); TeeOutputStream tee = new TeeOutputStream(out, new KeepAliveOutputStream(System.out)); Session session = null; try { - // execute the command + final ChannelExec channel; + /* execute the command */ session = openSession(); session.setTimeout((int) maxwait); - final ChannelExec channel = (ChannelExec) session.openChannel("exec"); - channel.setCommand(command); + channel = (ChannelExec) session.openChannel("exec"); + channel.setCommand(cmd); channel.setOutputStream(tee); channel.setExtOutputStream(tee); channel.connect(); - // wait for it to finish thread = new Thread() { @@ -225,7 +260,6 @@ } } - /** * Writes a string to a file. If destination file exists, it may be * overwritten depending on the "append" value. @@ -257,5 +291,4 @@ } } } - -} +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org For additional commands, e-mail: dev-help@ant.apache.org