processBuilder.directory(new File(baseDirPath));
process = processBuilder.start();
process.getOutputStream().close();
} catch (IOException e1) {
throw new AlgorithmExecutionException(e1.getMessage(), e1);
}
//monitor the process, printing its stdout and stderr to console
monitor.start(ProgressMonitor.CANCELLABLE, -1);
InputStream in = process.getInputStream();
StringBuffer inBuffer = new StringBuffer();
InputStream err = process.getErrorStream();
StringBuffer errBuffer = new StringBuffer();
Integer exitValue = null;
boolean killedOnPurpose = false;
//while the process is still running...
while (!killedOnPurpose && exitValue == null) {
//print its output, and watch to see if it has finished/died.
inBuffer = logStream(LogService.LOG_INFO, in, inBuffer);
errBuffer = logStream(LogService.LOG_ERROR, err, errBuffer);
if (monitor.isCanceled()) {
killedOnPurpose = true;
process.destroy();
}
try {
int value = process.exitValue();
exitValue = new Integer(value);
} catch (IllegalThreadStateException e) {
// thrown if the process isn't done.
// kinda nasty, but there looks to be no other option.
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// possibly normal operation
}
}
monitor.done();
// if the process failed unexpectedly...
if (process.exitValue() != 0 && !killedOnPurpose) {
throw new AlgorithmExecutionException(
"Algorithm exited unexpectedly (exit value: "
+ process.exitValue()
+ "). Please check the console window for any error messages.");
}