10
10
import java .io .IOException ;
11
11
import java .io .InputStreamReader ;
12
12
import java .io .OutputStreamWriter ;
13
+ import java .io .FileOutputStream ;
13
14
import java .io .PrintStream ;
14
15
import java .nio .charset .Charset ;
15
16
import java .nio .charset .StandardCharsets ;
@@ -34,6 +35,7 @@ public class ProcessHandler {
34
35
private ProcessBuilder procBuilder ;
35
36
private Process process ;
36
37
private File logFile ;
38
+ private boolean appendFlag = false ;
37
39
private PrintStream stdoutWriter ;
38
40
private int timeout = -1 ;
39
41
private WaitHandler waitHandler ;
@@ -53,12 +55,24 @@ public ProcessHandler(String[] cmd, File workingDir) {
53
55
}
54
56
55
57
/**
56
- * Set the log to which to write the standard output of the process.
58
+ * Set the file to which standard out of the process should be written .
57
59
*
58
60
* @param log the stdout log file
59
61
*/
60
62
public void setStdoutLog (File log ) {
61
- logFile = log ;
63
+ setStdoutLog (log , false );
64
+ }
65
+
66
+ /**
67
+ * Set the log that standard out of process will be written to, using
68
+ * the specified append flag. The default is to not append to <code>log</code>.
69
+ *
70
+ * @param log the stdout log file
71
+ * @param appendFlag flag indicating if file should be appended to
72
+ */
73
+ public void setStdoutLog (File log , boolean appendFlag ) {
74
+ this .logFile = log ;
75
+ this .appendFlag = appendFlag ;
62
76
}
63
77
64
78
/**
@@ -192,8 +206,7 @@ public void exec(List<String> linesToPipeToStdin) throws ScriptRunnerException {
192
206
LOGGER .entering (CLASS , METHOD );
193
207
194
208
if (waitHandler == null ) {
195
- ScriptRunnerException sre =
196
- new ScriptRunnerException ("WLSDPLY-01201" , this .toString ());
209
+ ScriptRunnerException sre = new ScriptRunnerException ("WLSDPLY-01201" , this .toString ());
197
210
LOGGER .throwing (CLASS , METHOD , sre );
198
211
throw sre ;
199
212
}
@@ -214,6 +227,7 @@ public void exec(List<String> linesToPipeToStdin) throws ScriptRunnerException {
214
227
if (linesToPipeToStdin != null && !linesToPipeToStdin .isEmpty ()) {
215
228
try (BufferedWriter writer =
216
229
new BufferedWriter (new OutputStreamWriter (process .getOutputStream (), DEFAULT_CHARSET ))) {
230
+
217
231
for (String line : linesToPipeToStdin ) {
218
232
writer .write (line );
219
233
writer .newLine ();
@@ -292,8 +306,10 @@ public void run() {
292
306
PrintStream logWriter = null ;
293
307
try (BufferedReader reader =
294
308
new BufferedReader (new InputStreamReader (process .getInputStream (), DEFAULT_CHARSET ))) {
309
+
295
310
if (logFile != null ) {
296
- logWriter = new PrintStream (logFile , StandardCharsets .UTF_8 .toString ());
311
+ FileOutputStream fos = new FileOutputStream (logFile , appendFlag );
312
+ logWriter = new PrintStream (fos , true , StandardCharsets .UTF_8 .toString ());
297
313
}
298
314
299
315
String msg ;
0 commit comments