You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by bo...@apache.org on 2008/07/15 15:14:27 UTC

svn commit: r676906 - in /ant/core/trunk: WHATSNEW docs/manual/OptionalTasks/sshexec.html src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java

Author: bodewig
Date: Tue Jul 15 06:14:26 2008
New Revision: 676906

URL: http://svn.apache.org/viewvc?rev=676906&view=rev
Log:
Add input to sshexec.  PR 39197.  Based on patch by Robert Anderson.

Modified:
    ant/core/trunk/WHATSNEW
    ant/core/trunk/docs/manual/OptionalTasks/sshexec.html
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java

Modified: ant/core/trunk/WHATSNEW
URL: http://svn.apache.org/viewvc/ant/core/trunk/WHATSNEW?rev=676906&r1=676905&r2=676906&view=diff
==============================================================================
--- ant/core/trunk/WHATSNEW (original)
+++ ant/core/trunk/WHATSNEW Tue Jul 15 06:14:26 2008
@@ -165,6 +165,9 @@
  * 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
 =============================================
 

Modified: ant/core/trunk/docs/manual/OptionalTasks/sshexec.html
URL: http://svn.apache.org/viewvc/ant/core/trunk/docs/manual/OptionalTasks/sshexec.html?rev=676906&r1=676905&r2=676906&view=diff
==============================================================================
--- ant/core/trunk/docs/manual/OptionalTasks/sshexec.html (original)
+++ ant/core/trunk/docs/manual/OptionalTasks/sshexec.html Tue Jul 15 06:14:26 2008
@@ -139,6 +139,26 @@
       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>

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?rev=676906&r1=676905&r2=676906&view=diff
==============================================================================
--- 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 Tue Jul 15 06:14:26 2008
@@ -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 @@
 
     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;
@@ -111,6 +116,24 @@
     }
 
     /**
+     * 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
      * the file.
@@ -151,6 +174,16 @@
             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 @@
         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 @@
             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 @@
             } else {
                 log("Caught exception: " + e.getMessage(), Project.MSG_ERR);
             }
+        } finally {
+            FileUtils.close(istream);
         }
+
         return out;
     }