private LocalCommandActionResult executeProcess(Process process, LocalCommandActionDefinition definition)
throws Exception
{
// Start process thread
ProcessExecutionThread processExecutionThread = new ProcessExecutionThread(process);
processExecutionThread.start();
// Start standard output copier
ProcessOutputThread processStandardOutputThread = new ProcessOutputThread(
process.getInputStream(), maxCaptureSize, definition.isSkipStdOutput());
processStandardOutputThread.start();
// Start error output copier
ProcessOutputThread processStandardErrorThread = new ProcessOutputThread(
process.getErrorStream(), maxCaptureSize, definition.isSkipStdError());
processStandardErrorThread.start();
// If timeout is set start watchdog thread. It terminates process thread after timeout exceeds
if (definition.getTimeoutMs() > 0) {
ProcessTimeoutThread processTimeoutThread = new ProcessTimeoutThread(
processExecutionThread, definition.getTimeoutMs());
processTimeoutThread.start();
}
// Wait while process thread exits
processExecutionThread.join();
// Release all resources - even the process is not alive (http://kylecartmell.com/?p=9)
IOUtils.closeQuietly(process.getErrorStream());
IOUtils.closeQuietly(process.getInputStream());
IOUtils.closeQuietly(process.getOutputStream());
process.destroy();
// Wait while output copiers exit
processStandardOutputThread.shutdownCapture();
processStandardErrorThread.shutdownCapture();
// Compose the result
LocalCommandActionResult result = new LocalCommandActionResult();
result.setStatus(processExecutionThread.getStatus());
result.setExitCode(processExecutionThread.getExitCode());
result.setStdout(processStandardOutputThread.composeActionOutput());
result.setStderr(processStandardErrorThread.composeActionOutput());
// In case of the process termination change "success" status to "terminated"
if (result.getStatus() == LocalCommandActionResultStatus.TIMEOUT) {