if (!isConnected()) {
throw new IllegalStateException("Not connected!");
}
try {
logger.debug("Opening channel");
Channel channel = connectSession.openChannel(CMD_EXEC);
((ChannelExec)channel).setCommand(command);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(channel.getInputStream()));
logger.debug("connecting channel.");
channel.connect();
// Seems like Gerrit does not like when you disconnect directly after the command has been sent.
// For instance, we have seen effects of mails not being sent out. This is the reason for
// receiving all the incoming data.
String incomingLine = null;
StringBuilder commandOutput = new StringBuilder();
while ((incomingLine = bufferedReader.readLine()) != null) {
commandOutput.append(incomingLine);
commandOutput.append('\n');
logger.trace("Incoming line: {}", incomingLine);
}
logger.trace("Closing reader.");
bufferedReader.close();
logger.trace("disconnecting channel.");
channel.disconnect();
return commandOutput.toString();
} catch (JSchException ex) {
throw new SshException(ex);
} catch (IOException ex) {