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 log that standard out of process will be written to .
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
/**
@@ -193,7 +207,7 @@ public void exec(List<String> linesToPipeToStdin) throws ScriptRunnerException {
193
207
194
208
if (waitHandler == null ) {
195
209
ScriptRunnerException sre =
196
- new ScriptRunnerException ("WLSDPLY-01201" , this .toString ());
210
+ new ScriptRunnerException ("WLSDPLY-01201" , this .toString ());
197
211
LOGGER .throwing (CLASS , METHOD , sre );
198
212
throw sre ;
199
213
}
@@ -206,21 +220,21 @@ public void exec(List<String> linesToPipeToStdin) throws ScriptRunnerException {
206
220
process = procBuilder .start ();
207
221
} catch (IOException ioe ) {
208
222
ScriptRunnerException sre =
209
- new ScriptRunnerException ("WLSDPLY-01203" , ioe , this .toString (), ioe .getLocalizedMessage ());
223
+ new ScriptRunnerException ("WLSDPLY-01203" , ioe , this .toString (), ioe .getLocalizedMessage ());
210
224
LOGGER .throwing (CLASS , METHOD , sre );
211
225
throw sre ;
212
226
}
213
227
214
228
if (linesToPipeToStdin != null && !linesToPipeToStdin .isEmpty ()) {
215
229
try (BufferedWriter writer =
216
- new BufferedWriter (new OutputStreamWriter (process .getOutputStream (), DEFAULT_CHARSET ))) {
230
+ new BufferedWriter (new OutputStreamWriter (process .getOutputStream (), DEFAULT_CHARSET ))) {
217
231
for (String line : linesToPipeToStdin ) {
218
232
writer .write (line );
219
233
writer .newLine ();
220
234
}
221
235
} catch (IOException ioe ) {
222
236
ScriptRunnerException sre =
223
- new ScriptRunnerException ("WLSDPLY-01204" , ioe , this .toString (), ioe .getLocalizedMessage ());
237
+ new ScriptRunnerException ("WLSDPLY-01204" , ioe , this .toString (), ioe .getLocalizedMessage ());
224
238
LOGGER .throwing (CLASS , METHOD , sre );
225
239
throw sre ;
226
240
}
@@ -243,7 +257,7 @@ public void exec(List<String> linesToPipeToStdin) throws ScriptRunnerException {
243
257
process .destroy ();
244
258
}
245
259
ScriptRunnerException sre =
246
- new ScriptRunnerException ("WLSDPLY-01205" , this .toString (), elapsed , timeout );
260
+ new ScriptRunnerException ("WLSDPLY-01205" , this .toString (), elapsed , timeout );
247
261
LOGGER .throwing (CLASS , METHOD , sre );
248
262
throw sre ;
249
263
}
@@ -291,9 +305,10 @@ public String toString() {
291
305
public void run () {
292
306
PrintStream logWriter = null ;
293
307
try (BufferedReader reader =
294
- new BufferedReader (new InputStreamReader (process .getInputStream (), DEFAULT_CHARSET ))) {
308
+ new BufferedReader (new InputStreamReader (process .getInputStream (), DEFAULT_CHARSET ))) {
295
309
if (logFile != null ) {
296
- logWriter = new PrintStream (logFile , StandardCharsets .UTF_8 .toString ());
310
+ FileOutputStream fos = new FileOutputStream (logFile , appendFlag );
311
+ logWriter = new PrintStream (fos , true , StandardCharsets .UTF_8 .toString ());
297
312
}
298
313
299
314
String msg ;
@@ -351,3 +366,4 @@ public interface WaitHandler {
351
366
void processExit (Process process );
352
367
}
353
368
}
369
+
0 commit comments