diff options
| author | Shireesh Anjal <shireesh@gluster.com> | 2011-08-20 16:44:36 +0530 |
|---|---|---|
| committer | Shireesh Anjal <shireesh@gluster.com> | 2011-08-20 16:44:36 +0530 |
| commit | 380af7d298620b3e582d1c51df3bd161c4d01856 (patch) | |
| tree | 4ae00b25c5f668ce02d8a224420639b9c27bf830 /src | |
| parent | 36df5d213037e94d4434c50d29a3e15649da49d4 (diff) | |
Modified to close streams/sessions/connections when no longer required.
Diffstat (limited to 'src')
2 files changed, 24 insertions, 8 deletions
diff --git a/src/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/ProcessUtil.java b/src/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/ProcessUtil.java index cd9d2549..87c74cf0 100644 --- a/src/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/ProcessUtil.java +++ b/src/com.gluster.storage.management.core/src/com/gluster/storage/management/core/utils/ProcessUtil.java @@ -90,6 +90,9 @@ public class ProcessUtil { output.append(line); output.append(NEWLINE); } + br.close(); + isr.close(); + is.close(); } else { output.append("Command ["); output.append(command); diff --git a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/SshUtil.java b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/SshUtil.java index 3399dc3b..a0d6db4a 100644 --- a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/SshUtil.java +++ b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/SshUtil.java @@ -255,21 +255,23 @@ public class SshUtil { } private ProcessResult executeCommand(Connection sshConnection, String command) { + Session session = null; try { - Session session = sshConnection.openSession(); + session = sshConnection.openSession(); BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(new StreamGobbler( session.getStdout()))); BufferedReader stderrReader = new BufferedReader(new InputStreamReader(new StreamGobbler( session.getStderr()))); session.execCommand(command); ProcessResult result = getResultOfExecution(session, stdoutReader, stderrReader); - session.close(); return result; } catch (IOException e) { String errMsg = "Exception while executing command [" + command + "] on [" + sshConnection.getHostname() + "]"; logger.error(errMsg, e); throw new GlusterRuntimeException(errMsg, e); + } finally { + session.close(); } } @@ -335,21 +337,32 @@ public class SshUtil { */ public ProcessResult executeRemoteWithPassword(String serverName, String command) { logger.info("Executing command [" + command + "] on server [" + serverName + "] with default password."); - Connection conn = getConnectionWithPassword(serverName); - ProcessResult result = executeCommand(conn, command); - // we don't cache password based connections. hence the connection must be closed. - conn.close(); - return result; + Connection conn = null; + try { + conn = getConnectionWithPassword(serverName); + return executeCommand(conn, command); + } finally { + // we don't cache password based connections. hence the connection must be closed. + if(conn != null) { + conn.close(); + } + } } private ProcessResult executeRemoteWithPubKey(String serverName, String command) { + Connection connection = null; try { - return executeCommand(getConnection(serverName), command); + connection = getConnection(serverName); + return executeCommand(connection, command); } catch(GlusterRuntimeException e) { Throwable cause = e.getCause(); if(cause != null && cause instanceof IOException) { + logger.info("Cached connection might have gone bad. Discarding it and trying with a fresh one.", e); // cached ssh connection might have gone bad. // remove it and try with a new one + if(connection != null) { + connection.close(); + } sshConnCache.remove(serverName); return executeCommand(getConnection(serverName), command); } else { |
