Do not try to connect to ssh if already connected

This commit is contained in:
Ignasi Barrera 2016-08-12 00:38:51 +02:00
parent 449c6d8d84
commit 7dc73ee0bf
6 changed files with 28 additions and 1 deletions

View File

@ -71,7 +71,9 @@ public class SudoAwareInitManager {
public ExecResponse refreshAndRunAction(String action) {
checkState(ssh != null, "please call init() before invoking call");
try {
ssh.connect();
if (!ssh.isConnected()) {
ssh.connect();
}
return runAction(action);
} finally {
if (ssh != null)

View File

@ -61,6 +61,8 @@ public interface SshClient {
void disconnect();
boolean isConnected();
void put(String path, String contents);
}

View File

@ -339,11 +339,17 @@ public class JschSshClient implements SshClient {
return toString;
}
@Override
@PreDestroy
public void disconnect() {
sessionConnection.clear();
}
@Override
public boolean isConnected() {
return sessionConnection.getSession().isConnected();
}
protected ConnectionWithStreams<ChannelExec> execConnection(final String command) {
checkNotNull(command, "command");
return new ConnectionWithStreams<ChannelExec>() {

View File

@ -86,6 +86,10 @@ public class JschSshClientLiveTest {
public void disconnect() {
}
public boolean isConnected() {
return false;
}
public Payload get(String path) {
if (path.equals("/etc/passwd")) {
return Payloads.newStringPayload("root");

View File

@ -410,6 +410,15 @@ public class SshjSshClient implements SshClient {
}
}
@Override
public boolean isConnected() {
try {
return sshClientConnection.getSSHClient().isConnected();
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
protected Connection<Session> execConnection() {
return new Connection<Session>() {

View File

@ -75,6 +75,10 @@ public class SshjSshClientLiveTest {
public void disconnect() {
}
public boolean isConnected() {
return false;
}
public Payload get(String path) {
if (path.equals("/etc/passwd")) {
return Payloads.newStringPayload("root");