1 package org.cyclopsgroup.jmxterm.io;
2
3 import org.apache.commons.io.output.NullWriter;
4 import org.apache.commons.lang3.Validate;
5
6 import java.io.IOException;
7 import java.io.PrintWriter;
8 import java.io.Writer;
9
10
11
12
13
14
15 public class WriterCommandOutput extends CommandOutput {
16 private final Writer messageOutput;
17
18 private final Writer resultOutput;
19
20
21
22
23 public WriterCommandOutput(Writer output) {
24 this(output, output);
25 }
26
27
28
29
30
31 public WriterCommandOutput(Writer resultOutput, Writer messageOutput) {
32 Validate.notNull(resultOutput, "Result output can't be NULL");
33 this.resultOutput = resultOutput;
34 this.messageOutput = messageOutput == null ? new NullWriter() : messageOutput;
35 }
36
37 @Override
38 public void print(String output) {
39 if (output == null) {
40 return;
41 }
42 try {
43 resultOutput.write(output);
44 } catch (IOException e) {
45 throw new RuntimeIOException("Can't print out result", e);
46 }
47 }
48
49 @Override
50 public void printError(Throwable e) {
51 e.printStackTrace(new PrintWriter(messageOutput, true));
52 }
53
54 @Override
55 public void printMessage(String message) {
56 try {
57 messageOutput.write(message);
58 } catch (IOException e) {
59 throw new RuntimeIOException("Can't print out message", e);
60 }
61 }
62 }