private CommandResult waitForProcessToComplete( final Process process, final KillableObserver observer, final Killable processKillable, final Consumers.OutErrConsumers outErr, final boolean killSubprocessOnInterrupt) throws AbnormalTerminationException { log.finer("Waiting for process..."); TerminationStatus status = waitForProcess(process, killSubprocessOnInterrupt); observer.stopObserving(processKillable); log.finer(status.toString()); try { outErr.waitForCompletion(); } catch (IOException ioe) { CommandResult noOutputResult = new CommandResult(CommandResult.EMPTY_OUTPUT, CommandResult.EMPTY_OUTPUT, status); if (status.success()) { // If command was otherwise successful, throw an exception about this throw new AbnormalTerminationException(this, noOutputResult, ioe); } else { // Otherwise, throw the more important exception -- command // was not successful String message = status + "; also encountered an error while attempting to retrieve output"; throw status.exited() ? new BadExitStatusException(this, noOutputResult, message, ioe) : new AbnormalTerminationException(this, noOutputResult, message, ioe); } } CommandResult result = new CommandResult(outErr.getAccumulatedOut(), outErr.getAccumulatedErr(), status); result.logThis(); if (status.success()) { return result; } else if (status.exited()) { throw new BadExitStatusException(this, result, status.toString()); } else { throw new AbnormalTerminationException(this, result, status.toString()); } }
private static Killable observeProcess(final Process process, final KillableObserver observer) { final Killable processKillable = new ProcessKillable(process); observer.startObserving(processKillable); return processKillable; }