Browse Source

Add input to sshexec. PR 39197. Based on patch by Robert Anderson.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@676906 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 17 years ago
parent
commit
d796e53fea
3 changed files with 81 additions and 0 deletions
  1. +3
    -0
      WHATSNEW
  2. +20
    -0
      docs/manual/OptionalTasks/sshexec.html
  3. +58
    -0
      src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java

+ 3
- 0
WHATSNEW View File

@@ -165,6 +165,9 @@ Other changes:
* It is now possible to disable <ftp>'s remote verification.
Bugzilla report 35471.

* <sshexec> now supports input in a way similar to <exec>
Bugzilla report 39197.

Changes from Ant 1.7.0 TO Ant 1.7.1
=============================================



+ 20
- 0
docs/manual/OptionalTasks/sshexec.html View File

@@ -139,6 +139,26 @@ and won't work with versions of jsch earlier than
Defaults to 0 which means &quot;wait forever&quot;.</td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">input</td>
<td valign="top">A file from which the executed command's standard
input is taken. This attribute is mutually exclusive with the
inputstring attribute.<br/>
When executing more than one command via commandRessource, input
will be read for each command.
<em>since Ant 1.8.0</em></td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">inputstring</td>
<td valign="top">A string which serves as the input stream for the
executed command. This attribute is mutually exclusive with the
input attribute.<br/>
When executing more than one command via commandRessource, input
will be read for each command.
<em>since Ant 1.8.0</em></td>
<td align="center" valign="top">No</td>
</tr>
</table>

<h3>Examples</h3>


+ 58
- 0
src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java View File

@@ -19,10 +19,13 @@
package org.apache.tools.ant.taskdefs.optional.ssh;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;

@@ -58,6 +61,8 @@ public class SSHExec extends SSHBase {

private String outputProperty = null; // like <exec>
private File outputFile = null; // like <exec>
private String inputProperty = null; // like <exec>
private File inputFile = null; // like <exec>
private boolean append = false; // like <exec>

private Resource commandResource = null;
@@ -110,6 +115,24 @@ public class SSHExec extends SSHBase {
outputFile = output;
}

/**
* If used, the content of the file is piped to the remote command
*
* @param input The file which provides the input data for the remote command
*/
public void setInput(File input) {
inputFile = input;
}

/**
* If used, the content of the property is piped to the remote command
*
* @param inputProperty The property which contains the input data for the remote command.
*/
public void setInputProperty(String inputProperty) {
this.inputProperty = inputProperty;
}

/**
* Determines if the output is appended to the file given in
* <code>setOutput</code>. Default is false, that is, overwrite
@@ -151,6 +174,16 @@ public class SSHExec extends SSHBase {
throw new BuildException("Command or commandResource is required.");
}

if (inputFile != null && inputProperty != null) {
throw new BuildException("You can't specify both inputFile and"
+ " inputProperty.");
}
if (inputFile != null && !inputFile.exists()) {
throw new BuildException("The input file "
+ inputFile.getAbsolutePath()
+ " does not exist.");
}

Session session = null;

try {
@@ -197,6 +230,25 @@ public class SSHExec extends SSHBase {
ByteArrayOutputStream out = new ByteArrayOutputStream();
TeeOutputStream tee = new TeeOutputStream(out, new KeepAliveOutputStream(System.out));

InputStream istream = null ;
if (inputFile != null) {
try {
istream = new FileInputStream(inputFile) ;
} catch (IOException e) {
// because we checked the existence before, this one
// shouldn't happen What if the file exists, but there
// are no read permissions?
log("Failed to read " + inputFile + " because of: "
+ e.getMessage(), Project.MSG_WARN);
}
}
if (inputProperty != null) {
String inputData = getProject().getProperty(inputProperty) ;
if (inputData != null) {
istream = new ByteArrayInputStream(inputData.getBytes()) ;
}
}

try {
final ChannelExec channel;
session.setTimeout((int) maxwait);
@@ -205,6 +257,9 @@ public class SSHExec extends SSHBase {
channel.setCommand(cmd);
channel.setOutputStream(tee);
channel.setExtOutputStream(tee);
if (istream != null) {
channel.setInputStream(istream);
}
channel.connect();
// wait for it to finish
thread =
@@ -275,7 +330,10 @@ public class SSHExec extends SSHBase {
} else {
log("Caught exception: " + e.getMessage(), Project.MSG_ERR);
}
} finally {
FileUtils.close(istream);
}

return out;
}



Loading…
Cancel
Save