diff --git a/java-scpclient/src/main/java/cz/it4i/fiji/scpclient/SshCommandClient.java b/java-scpclient/src/main/java/cz/it4i/fiji/scpclient/SshCommandClient.java index b4a097ef177fff3d668f87cfceffc33d952fafad..fcfb5ee9a46655cad14e533a6e56a6fbe6970d36 100644 --- a/java-scpclient/src/main/java/cz/it4i/fiji/scpclient/SshCommandClient.java +++ b/java-scpclient/src/main/java/cz/it4i/fiji/scpclient/SshCommandClient.java @@ -6,6 +6,7 @@ import com.jcraft.jsch.Identity; import com.jcraft.jsch.JSchException; import java.io.BufferedReader; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.LinkedList; @@ -37,20 +38,29 @@ public class SshCommandClient extends AbstractBaseSshClient { super(hostName, userName, keyFile, pass); } - public List<String> executeCommand(String command) { - List<String> result = new LinkedList<>(); + public SshExecutionSession openSshExecutionSession(String command) { try { ChannelExec channelExec = (ChannelExec) getConnectedSession().openChannel( "exec"); - InputStream in = channelExec.getInputStream(); - channelExec.setCommand(command); channelExec.connect(); + return new P_SshExecutionSession(channelExec); + } + catch (Exception e) { + log.error("Error: ", e); + throw new RuntimeException(e); + } + } + + public List<String> executeCommand(String command) { + List<String> result = new LinkedList<>(); + try (SshExecutionSession session = openSshExecutionSession(command)) { - BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + BufferedReader reader = new BufferedReader(new InputStreamReader(session + .getStdout())); BufferedReader errReader = new BufferedReader(new InputStreamReader( - channelExec.getErrStream())); + session.getStderr())); String line; while ((line = reader.readLine()) != null) { @@ -61,8 +71,7 @@ public class SshCommandClient extends AbstractBaseSshClient { errors.add(line); } - int exitStatus = channelExec.getExitStatus(); - channelExec.disconnect(); + int exitStatus = session.getExitStatus(); if (exitStatus < 0) { log.debug("Done, but exit status not set!"); @@ -92,4 +101,34 @@ public class SshCommandClient extends AbstractBaseSshClient { } return true; } + + private class P_SshExecutionSession implements SshExecutionSession { + + private ChannelExec channel; + + public P_SshExecutionSession(ChannelExec channel) { + this.channel = channel; + } + + @Override + public InputStream getStdout() throws IOException { + return channel.getInputStream(); + } + + @Override + public InputStream getStderr() throws IOException { + return channel.getErrStream(); + } + + @Override + public int getExitStatus() { + return channel.getExitStatus(); + } + + @Override + public void close() { + channel.disconnect(); + } + + } } diff --git a/java-scpclient/src/main/java/cz/it4i/fiji/scpclient/SshExecutionSession.java b/java-scpclient/src/main/java/cz/it4i/fiji/scpclient/SshExecutionSession.java new file mode 100644 index 0000000000000000000000000000000000000000..3cb9240672097526d33b60dc35551fc06c6f90b8 --- /dev/null +++ b/java-scpclient/src/main/java/cz/it4i/fiji/scpclient/SshExecutionSession.java @@ -0,0 +1,18 @@ + +package cz.it4i.fiji.scpclient; + +import java.io.Closeable; +import java.io.IOException; +import java.io.InputStream; + +public interface SshExecutionSession extends Closeable { + + InputStream getStdout() throws IOException; + + InputStream getStderr() throws IOException; + + int getExitStatus(); + + @Override + void close(); +}