From: <jrb...@us...> - 2012-01-20 19:12:09
|
Revision: 1301 http://cishell.svn.sourceforge.net/cishell/?rev=1301&view=rev Author: jrbibers Date: 2012-01-20 19:12:00 +0000 (Fri, 20 Jan 2012) Log Message: ----------- Added a "commands" field to ProcessResult that stores the commands used to run the Process that resulted in this ProcessResult. This is more convenient for reporting Process outcomes. Since this changed the semantics of ProcessResult.equals(), this class has been renamed to the now-more-suitable ProcessReport. Reviewed by Thomas. Modified Paths: -------------- trunk/core/org.cishell.utilities/src/org/cishell/utilities/process/OutputGobblingProcessRunner.java Added Paths: ----------- trunk/core/org.cishell.utilities/src/org/cishell/utilities/process/ProcessReport.java Removed Paths: ------------- trunk/core/org.cishell.utilities/src/org/cishell/utilities/process/ProcessResult.java Modified: trunk/core/org.cishell.utilities/src/org/cishell/utilities/process/OutputGobblingProcessRunner.java =================================================================== --- trunk/core/org.cishell.utilities/src/org/cishell/utilities/process/OutputGobblingProcessRunner.java 2012-01-10 23:51:20 UTC (rev 1300) +++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/process/OutputGobblingProcessRunner.java 2012-01-20 19:12:00 UTC (rev 1301) @@ -24,7 +24,7 @@ this.charsetName = charsetName; } - public ProcessResult run() throws IOException, InterruptedException { + public ProcessReport run() throws IOException, InterruptedException { // Start the process Process process = processBuilder.start(); @@ -51,7 +51,7 @@ String stdoutMessage = stdoutStream.toString(charsetName).trim(); String stderrMessage = stderrStream.toString(charsetName).trim(); - return new ProcessResult(exitValue, stdoutMessage, stderrMessage); + return ProcessReport.of(processBuilder.command(), exitValue, stdoutMessage, stderrMessage); } @Override Copied: trunk/core/org.cishell.utilities/src/org/cishell/utilities/process/ProcessReport.java (from rev 1300, trunk/core/org.cishell.utilities/src/org/cishell/utilities/process/ProcessResult.java) =================================================================== --- trunk/core/org.cishell.utilities/src/org/cishell/utilities/process/ProcessReport.java (rev 0) +++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/process/ProcessReport.java 2012-01-20 19:12:00 UTC (rev 1301) @@ -0,0 +1,107 @@ +package org.cishell.utilities.process; + +import java.util.List; + +import com.google.common.base.Joiner; +import com.google.common.base.Objects; +import com.google.common.collect.ImmutableList; + +/** + * Value class for the result of {@link OutputGobblingProcessRunner#run()}. + * Represents a process's commands, exit value, and messages to standard out and standard errors. + */ +public class ProcessReport { + private final ImmutableList<String> commands; + private final int exitValue; + private final String stdoutMessage; + private final String stderrMessage; + + /** + * @param commands Commands used to launch the corresponding {@link Process}. + * @param exitValue A completed {@link Process}'s exit value. + * @param stdoutMessage A completed {@link Process}'s messages to standard output as one String. + * @param stderrMessage A completed {@link Process}'s messages to standard error as one String. + */ + public static ProcessReport of( + List<String> commands, int exitValue, String stdoutMessage, String stderrMessage) { + return new ProcessReport(commands, exitValue, stdoutMessage, stderrMessage); + } + private ProcessReport( + List<String> commands, int exitValue, String stdoutMessage, String stderrMessage) { + this.commands = ImmutableList.copyOf(commands); + this.exitValue = exitValue; + this.stdoutMessage = stdoutMessage; + this.stderrMessage = stderrMessage; + } + + + public ImmutableList<String> getCommands() { + return commands; + } + + public int getExitValue() { + return exitValue; + } + + /** + * @return The message to standard output. + */ + public String getStdoutMessage() { + return stdoutMessage; + } + + /** + * @return The message to standard error. + */ + public String getStderrMessage() { + return stderrMessage; + } + + /** + * @return True if and only if the exit value is zero. + */ + public boolean isExitNormal() { + return (exitValue == 0); + } + + @Override + public boolean equals(Object thatObject) { + if (!(thatObject instanceof ProcessReport)) { + return false; + } + + ProcessReport that = (ProcessReport) thatObject; + + return (Objects.equal(this.commands, + that.commands) + && Objects.equal(this.exitValue, + that.exitValue) + && Objects.equal(this.stdoutMessage, + that.stdoutMessage) + && Objects.equal(this.stderrMessage, + that.stderrMessage)); + } + @Override + public int hashCode() { + return Objects.hashCode(commands, exitValue, stdoutMessage, stderrMessage); + } + + /** + * @return A human-readable report of all values. + */ + @Override + public String toString() { + return Joiner.on(" ").join( + String.format("The commands %s resulted in exit value %d.", commands, exitValue), + reportStreamContents("standard output", stdoutMessage), + reportStreamContents("standard error", stderrMessage)); + } + + private static String reportStreamContents(String streamName, String contents) { + if (contents.isEmpty()) { + return String.format("No messages to %s.", streamName); + } else { + return String.format("Message to %s: [[%s]].", streamName, contents); + } + } +} \ No newline at end of file Deleted: trunk/core/org.cishell.utilities/src/org/cishell/utilities/process/ProcessResult.java =================================================================== --- trunk/core/org.cishell.utilities/src/org/cishell/utilities/process/ProcessResult.java 2012-01-10 23:51:20 UTC (rev 1300) +++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/process/ProcessResult.java 2012-01-20 19:12:00 UTC (rev 1301) @@ -1,98 +0,0 @@ -package org.cishell.utilities.process; - -import com.google.common.base.Joiner; -import com.google.common.base.Objects; - -/** - * Value class for the result of {@link OutputGobblingProcessRunner#run()}. - * Represents a process's exit value and messages to standard out and standard error, - * both as a single String. - */ -public class ProcessResult { - private int exitValue; - private String stdoutMessage; - private String stderrMessage; - - /** - * @param exitValue A completed {@link Process}'s exit value. - * @param stdoutMessage A completed {@link Process}'s messages to standard output as one String. - * @param stderrMessage A completed {@link Process}'s messages to standard error as one String. - */ - public ProcessResult(int exitValue, String stdoutMessage, String stderrMessage) { - this.exitValue = exitValue; - this.stdoutMessage = stdoutMessage; - this.stderrMessage = stderrMessage; - } - - public int getExitValue() { - return exitValue; - } - - /** - * @return The message to standard output. - */ - public String getStdoutMessage() { - return stdoutMessage; - } - - /** - * @return The message to standard error. - */ - public String getStderrMessage() { - return stderrMessage; - } - - /** - * @return True if and only if the exit value is zero. - */ - public boolean isExitNormal() { - return (exitValue == 0); - } - - /** - * @return A plain-text report of the exit value and standard output/error messages. - */ - public String report() { - return Joiner.on(" ").join( - String.format("The program returned exit value %d.", exitValue), - reportStreamContents("standard output", stdoutMessage), - reportStreamContents("standard error", stderrMessage)); - } - - private static String reportStreamContents(String streamName, String contents) { - if (contents.isEmpty()) { - return String.format("No messages to %s.", streamName); - } else { - return String.format("Message to %s: [[%s]].", streamName, contents); - } - } - - @Override - public boolean equals(Object thatObject) { - if (!(thatObject instanceof ProcessResult)) { - return false; - } - - ProcessResult that = (ProcessResult) thatObject; - - return (Objects.equal(this.exitValue, - that.exitValue) - && Objects.equal(this.stdoutMessage, - that.stdoutMessage) - && Objects.equal(this.stderrMessage, - that.stderrMessage)); - } - @Override - public int hashCode() { - return Objects.hashCode(exitValue, stdoutMessage, stderrMessage); - } - - @Override - public String toString() { - return Objects.toStringHelper(this) - .add("exitValue", exitValue) - .add("stdoutMessage", stdoutMessage) - .add("stderrMessage", stderrMessage) - .toString(); - } -} \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |