}
public Streams executeCommand( String command, boolean ignoreFailures )
throws CommandExecutionException
{
ChannelExec channel = null;
BufferedReader stdoutReader = null;
BufferedReader stderrReader = null;
try
{
channel = (ChannelExec) session.openChannel( EXEC_CHANNEL );
channel.setCommand( command + "\n" );
InputStream stdout = channel.getInputStream();
InputStream stderr = channel.getErrStream();
channel.connect();
stdoutReader = new BufferedReader( new InputStreamReader( stdout ) );
stderrReader = new BufferedReader( new InputStreamReader( stderr ) );
Streams streams = CommandExecutorStreamProcessor.processStreams( stderrReader, stdoutReader );
if ( streams.getErr().length() > 0 && !ignoreFailures )
{
int exitCode = channel.getExitStatus();
throw new CommandExecutionException( "Exit code: " + exitCode + " - " + streams.getErr() );
}
return streams;
}
catch ( IOException e )
{
throw new CommandExecutionException( "Cannot execute remote command: " + command, e );
}
catch ( JSchException e )
{
throw new CommandExecutionException( "Cannot execute remote command: " + command, e );
}
finally
{
IOUtil.close( stdoutReader );
IOUtil.close( stderrReader );
if ( channel != null )
{
channel.disconnect();
}
}
}