1010import java .io .IOException ;
1111import java .io .InputStreamReader ;
1212import java .io .OutputStreamWriter ;
13+ import java .io .FileOutputStream ;
1314import java .io .PrintStream ;
1415import java .nio .charset .Charset ;
1516import java .nio .charset .StandardCharsets ;
@@ -34,6 +35,7 @@ public class ProcessHandler {
3435 private ProcessBuilder procBuilder ;
3536 private Process process ;
3637 private File logFile ;
38+ private boolean appendFlag = false ;
3739 private PrintStream stdoutWriter ;
3840 private int timeout = -1 ;
3941 private WaitHandler waitHandler ;
@@ -53,12 +55,24 @@ public ProcessHandler(String[] cmd, File workingDir) {
5355 }
5456
5557 /**
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 .
5759 *
5860 * @param log the stdout log file
5961 */
6062 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 ;
6276 }
6377
6478 /**
@@ -192,8 +206,7 @@ public void exec(List<String> linesToPipeToStdin) throws ScriptRunnerException {
192206 LOGGER .entering (CLASS , METHOD );
193207
194208 if (waitHandler == null ) {
195- ScriptRunnerException sre =
196- new ScriptRunnerException ("WLSDPLY-01201" , this .toString ());
209+ ScriptRunnerException sre = new ScriptRunnerException ("WLSDPLY-01201" , this .toString ());
197210 LOGGER .throwing (CLASS , METHOD , sre );
198211 throw sre ;
199212 }
@@ -214,6 +227,7 @@ public void exec(List<String> linesToPipeToStdin) throws ScriptRunnerException {
214227 if (linesToPipeToStdin != null && !linesToPipeToStdin .isEmpty ()) {
215228 try (BufferedWriter writer =
216229 new BufferedWriter (new OutputStreamWriter (process .getOutputStream (), DEFAULT_CHARSET ))) {
230+
217231 for (String line : linesToPipeToStdin ) {
218232 writer .write (line );
219233 writer .newLine ();
@@ -292,8 +306,10 @@ public void run() {
292306 PrintStream logWriter = null ;
293307 try (BufferedReader reader =
294308 new BufferedReader (new InputStreamReader (process .getInputStream (), DEFAULT_CHARSET ))) {
309+
295310 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 ());
297313 }
298314
299315 String msg ;
0 commit comments