ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From k...@apache.org
Subject svn commit: r581576 - /ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java
Date Wed, 03 Oct 2007 11:58:39 GMT
Author: kevj
Date: Wed Oct  3 04:58:38 2007
New Revision: 581576

URL: http://svn.apache.org/viewvc?rev=581576&view=rev
Log:
-open session once for a command resource not each command
-bugzilla 43437 don't make properties immutable

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?rev=581576&r1=581575&r2=581576&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 Wed Oct
 3 04:58:38 2007
@@ -150,37 +150,56 @@
         if (command == null && commandResource == null) {
             throw new BuildException("Command or commandResource is required.");
         }
-
-        /* called once */
-        if (command != null) {
-            log("cmd : " + command, Project.MSG_INFO);
-            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);
+        
+        Session session = null;
+        
+        try {
+            session = openSession();
+            /* called once */
+            if (command != null) {
+                log("cmd : " + command, Project.MSG_INFO);
+                ByteArrayOutputStream out = executeCommand(session, command);
+                if(outputProperty != null) {
+                    //#bugzilla 43437
+                    getProject().setNewProperty(outputProperty, command + " : " + out);
+                }
+            } else { // read command resource and execute for each command
+                try {
+                    BufferedReader br = new BufferedReader(
+                            new InputStreamReader(commandResource.getInputStream()));
+                    String cmd;
+                    String output = "";
+                    while ((cmd = br.readLine()) != null) {
+                        log("cmd : " + cmd, Project.MSG_INFO);
+                        ByteArrayOutputStream out = executeCommand(session, cmd);
+                        output += cmd + " : " + out + "\n";
+                    }
+                    if(outputProperty != null) {
+                        //#bugzilla 43437
+                        getProject().setNewProperty(outputProperty, output);
+                    }
+                    FileUtils.close(br);
+                } catch (IOException e) {
+                    throw new BuildException(e);
                 }
-                FileUtils.close(br);
-            } catch (IOException e) {
-                throw new BuildException(e);
+            }
+        } catch(JSchException e) {
+            throw new BuildException(e);
+        } finally {
+            if (session != null && session.isConnected()) {
+                session.disconnect();
             }
         }
     }
-
-    private void executeCommand(String cmd) throws BuildException {
+   
+    private ByteArrayOutputStream executeCommand(Session session, String cmd) throws BuildException
{
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         TeeOutputStream tee = new TeeOutputStream(out, new KeepAliveOutputStream(System.out));
 
-        Session session = null;
         try {
             final ChannelExec channel;
-            /* execute the command */
-            session = openSession();
             session.setTimeout((int) maxwait);
+            /* execute the command */
             channel = (ChannelExec) session.openChannel("exec");
             channel.setCommand(cmd);
             channel.setOutputStream(tee);
@@ -215,10 +234,7 @@
                     log(TIMEOUT_MESSAGE, Project.MSG_ERR);
                 }
             } else {
-                // completed successfully
-                if (outputProperty != null) {
-                    getProject().setProperty(outputProperty, out.toString());
-                }
+                //success
                 if (outputFile != null) {
                     writeToFile(out.toString(), append, outputFile);
                 }
@@ -258,11 +274,8 @@
             } else {
                 log("Caught exception: " + e.getMessage(), Project.MSG_ERR);
             }
-        } finally {
-            if (session != null && session.isConnected()) {
-                session.disconnect();
-            }
         }
+        return out;
     }
 
     /**



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Mime
View raw message