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 log that standard out of process will be written to .
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 /**
@@ -193,7 +207,7 @@ public void exec(List<String> linesToPipeToStdin) throws ScriptRunnerException {
193207
194208 if (waitHandler == null ) {
195209 ScriptRunnerException sre =
196- new ScriptRunnerException ("WLSDPLY-01201" , this .toString ());
210+ new ScriptRunnerException ("WLSDPLY-01201" , this .toString ());
197211 LOGGER .throwing (CLASS , METHOD , sre );
198212 throw sre ;
199213 }
@@ -206,21 +220,21 @@ public void exec(List<String> linesToPipeToStdin) throws ScriptRunnerException {
206220 process = procBuilder .start ();
207221 } catch (IOException ioe ) {
208222 ScriptRunnerException sre =
209- new ScriptRunnerException ("WLSDPLY-01203" , ioe , this .toString (), ioe .getLocalizedMessage ());
223+ new ScriptRunnerException ("WLSDPLY-01203" , ioe , this .toString (), ioe .getLocalizedMessage ());
210224 LOGGER .throwing (CLASS , METHOD , sre );
211225 throw sre ;
212226 }
213227
214228 if (linesToPipeToStdin != null && !linesToPipeToStdin .isEmpty ()) {
215229 try (BufferedWriter writer =
216- new BufferedWriter (new OutputStreamWriter (process .getOutputStream (), DEFAULT_CHARSET ))) {
230+ new BufferedWriter (new OutputStreamWriter (process .getOutputStream (), DEFAULT_CHARSET ))) {
217231 for (String line : linesToPipeToStdin ) {
218232 writer .write (line );
219233 writer .newLine ();
220234 }
221235 } catch (IOException ioe ) {
222236 ScriptRunnerException sre =
223- new ScriptRunnerException ("WLSDPLY-01204" , ioe , this .toString (), ioe .getLocalizedMessage ());
237+ new ScriptRunnerException ("WLSDPLY-01204" , ioe , this .toString (), ioe .getLocalizedMessage ());
224238 LOGGER .throwing (CLASS , METHOD , sre );
225239 throw sre ;
226240 }
@@ -243,7 +257,7 @@ public void exec(List<String> linesToPipeToStdin) throws ScriptRunnerException {
243257 process .destroy ();
244258 }
245259 ScriptRunnerException sre =
246- new ScriptRunnerException ("WLSDPLY-01205" , this .toString (), elapsed , timeout );
260+ new ScriptRunnerException ("WLSDPLY-01205" , this .toString (), elapsed , timeout );
247261 LOGGER .throwing (CLASS , METHOD , sre );
248262 throw sre ;
249263 }
@@ -291,9 +305,10 @@ public String toString() {
291305 public void run () {
292306 PrintStream logWriter = null ;
293307 try (BufferedReader reader =
294- new BufferedReader (new InputStreamReader (process .getInputStream (), DEFAULT_CHARSET ))) {
308+ new BufferedReader (new InputStreamReader (process .getInputStream (), DEFAULT_CHARSET ))) {
295309 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 ());
297312 }
298313
299314 String msg ;
@@ -351,3 +366,4 @@ public interface WaitHandler {
351366 void processExit (Process process );
352367 }
353368}
369+
0 commit comments