From fdae4b3eaec0822c0ab1138b9ef8c4de28b2b0b8 Mon Sep 17 00:00:00 2001 From: Kevin Jackson Date: Sun, 18 Feb 2007 04:58:23 +0000 Subject: [PATCH] -provide support for a command file to pass to SSHExec - implemented with a FileResource git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@508865 13f79535-47bb-0310-9956-ffa450edef68 --- .../ant/taskdefs/optional/ssh/SSHExec.java | 61 ++++++++++++++----- 1 file changed, 47 insertions(+), 14 deletions(-) diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java b/src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java index e6feeb345..bbaddb497 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java @@ -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 @@ public class SSHExec extends SSHBase { 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."; @@ -71,6 +78,15 @@ public class SSHExec extends SSHBase { this.command = command; } + /** + * 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 @@ -128,24 +144,43 @@ public class SSHExec extends SSHBase { && 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 @@ public class SSHExec extends SSHBase { } } - /** * Writes a string to a file. If destination file exists, it may be * overwritten depending on the "append" value. @@ -257,5 +291,4 @@ public class SSHExec extends SSHBase { } } } - -} +} \ No newline at end of file