View Javadoc
1   package org.cyclopsgroup.jmxterm.io;
2   
3   import org.apache.commons.lang3.Validate;
4   
5   import java.io.PrintStream;
6   
7   /**
8    * Implementation of CommandOutput where output is written in given PrintStream objects
9    * 
10   * @author <a href="mailto:jiaqi.guo@gmail.com">Jiaqi Guo</a>
11   */
12  public class PrintStreamCommandOutput extends CommandOutput {
13    private final PrintStream messageOutput;
14  
15    private final PrintStream resultOutput;
16  
17    /**
18     * Default constructor that uses system standard output and err output
19     */
20    public PrintStreamCommandOutput() {
21      this(System.out);
22    }
23  
24    /**
25     * Constructor with given result output and system error as message output
26     * 
27     * @param output Output for result
28     */
29    public PrintStreamCommandOutput(PrintStream output) {
30      this(output, System.err);
31    }
32  
33    /**
34     * @param resultOutput PrintStream where result is written to
35     * @param messageOutput PrintStream where message is written to
36     */
37    public PrintStreamCommandOutput(PrintStream resultOutput, PrintStream messageOutput) {
38      Validate.notNull(resultOutput, "Result output can't be NULL");
39      Validate.notNull(messageOutput, "Message output can't be NULL");
40      this.resultOutput = resultOutput;
41      this.messageOutput = messageOutput;
42    }
43  
44    @Override
45    public void print(String output) {
46      resultOutput.print(output);
47    }
48  
49    @Override
50    public void printError(Throwable e) {
51      e.printStackTrace(messageOutput);
52    }
53  
54    @Override
55    public void printMessage(String message) {
56      messageOutput.println(message);
57    }
58  }