diff --git a/WHATSNEW b/WHATSNEW index 2cd2c1de9..258886b37 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -151,7 +151,7 @@ Fixed bugs: * now has (local|remote)tofile attributes to rename files on the fly. Bugzilla Report 26758. -* didn't close the session. Bugzilla Report 25935. +* and didn't close the session. Bugzilla Report 25935. Other changes: -------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/net/RExecTask.java b/src/main/org/apache/tools/ant/taskdefs/optional/net/RExecTask.java index bcc2c3986..2958fc840 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/net/RExecTask.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/net/RExecTask.java @@ -61,11 +61,6 @@ public class RExecTask extends Task { */ private int port = RExecClient.DEFAULT_PORT; - /** - * The Object which handles the rexec session. - */ - private AntRExecClient rexec = null; - /** * The list of read/write commands for this session */ @@ -322,34 +317,46 @@ public class RExecTask extends Task { } /** Create the telnet client object */ - rexec = new AntRExecClient(); + AntRExecClient rexec = null; try { - rexec.connect(server, port); - } catch (IOException e) { - throw new BuildException("Can't connect to " + server); - } - /** Login if userid and password were specified */ - if (userid != null && password != null) { - login(); - } - /** Process each sub command */ - Enumeration tasksToRun = rexecTasks.elements(); - while (tasksToRun != null && tasksToRun.hasMoreElements()) { - RExecSubTask task = (RExecSubTask) tasksToRun.nextElement(); - if (task instanceof RExecRead && defaultTimeout != null) { - ((RExecRead) task).setDefaultTimeout(defaultTimeout); + rexec = new AntRExecClient(); + try { + rexec.connect(server, port); + } catch (IOException e) { + throw new BuildException("Can't connect to " + server); + } + /** Login if userid and password were specified */ + if (userid != null && password != null) { + login(rexec); + } + /** Process each sub command */ + Enumeration tasksToRun = rexecTasks.elements(); + while (tasksToRun != null && tasksToRun.hasMoreElements()) { + RExecSubTask task = (RExecSubTask) tasksToRun.nextElement(); + if (task instanceof RExecRead && defaultTimeout != null) { + ((RExecRead) task).setDefaultTimeout(defaultTimeout); + } + task.execute(rexec); } - task.execute(rexec); - } - /** Keep reading input stream until end of it or time-out */ - rexec.waitForEOF(defaultTimeout); + /** Keep reading input stream until end of it or time-out */ + rexec.waitForEOF(defaultTimeout); + } finally { + if (rexec != null) { + try { + rexec.disconnect(); + } catch (IOException e) { + throw new BuildException("Error disconnecting from " + + server); + } + } + } } /** * Process a 'typical' login. If it differs, use the read * and write tasks explicitely */ - private void login() { + private void login(AntRExecClient rexec) { if (addCarriageReturn) { rexec.sendString("\n", true); }