Browse Source

-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
master
Kevin Jackson 18 years ago
parent
commit
eb4293a5c6
1 changed files with 42 additions and 29 deletions
  1. +42
    -29
      src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java

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

@@ -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;
}

/**


Loading…
Cancel
Save