1 package org.cyclopsgroup.jmxterm.io;
2
3 import org.apache.commons.lang3.Validate;
4
5 import java.io.File;
6 import java.io.FileWriter;
7 import java.io.IOException;
8 import java.io.PrintWriter;
9
10
11
12
13
14
15 public class FileCommandOutput extends CommandOutput {
16 private final PrintWriter fileWriter;
17
18 private final WriterCommandOutput output;
19
20
21
22
23
24
25 public FileCommandOutput(File file, boolean appendToOutput) throws IOException {
26 Validate.notNull(file, "File can't be NULL");
27 File af = file.getAbsoluteFile();
28 if (!af.getParentFile().isDirectory()) {
29 if (!af.getParentFile().mkdirs()) {
30 throw new IOException("Couldn't make directory " + af.getParentFile());
31 }
32 }
33 fileWriter = new PrintWriter(new FileWriter(af, appendToOutput));
34 output = new WriterCommandOutput(fileWriter, new PrintWriter(System.err, true));
35 }
36
37 @Override
38 public void close() {
39 fileWriter.flush();
40 fileWriter.close();
41 }
42
43 @Override
44 public void print(String value) {
45 output.print(value);
46 }
47
48 @Override
49 public void printError(Throwable e) {
50 output.printError(e);
51 }
52
53 @Override
54 public void printMessage(String message) {
55 output.printMessage(message);
56 }
57 }