From eb4293a5c60d218c2410b4a03bfb90b29fd7cf52 Mon Sep 17 00:00:00 2001 From: Kevin Jackson Date: Wed, 3 Oct 2007 11:58:38 +0000 Subject: [PATCH] -open session once for a command resource not each command -bugzilla 43437 don't make properties immutable git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@581576 13f79535-47bb-0310-9956-ffa450edef68 --- .../ant/taskdefs/optional/ssh/SSHExec.java | 71 +++++++++++-------- 1 file changed, 42 insertions(+), 29 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 499d4b1aa..a4af4da7b 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 @@ -150,37 +150,56 @@ public class SSHExec extends SSHBase { 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 @@ public class SSHExec extends SSHBase { 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 @@ public class SSHExec extends SSHBase { } else { log("Caught exception: " + e.getMessage(), Project.MSG_ERR); } - } finally { - if (session != null && session.isConnected()) { - session.disconnect(); - } } + return out; } /**