Skip to content

Commit 12785ff

Browse files
authored
Merge pull request #90 from oracle/Issue#89-Add-new-executeScript-method-to-ScriptRunner.java
Issue#89 add new execute script method to script runner.java
2 parents c123f11 + 2859c3c commit 12785ff

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

core/src/main/java/oracle/weblogic/deploy/util/ProcessHandler.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.io.IOException;
1111
import java.io.InputStreamReader;
1212
import java.io.OutputStreamWriter;
13+
import java.io.FileOutputStream;
1314
import java.io.PrintStream;
1415
import java.nio.charset.Charset;
1516
import 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;

core/src/main/java/oracle/weblogic/deploy/util/ScriptRunner.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,27 @@ public ScriptRunner(Map<String, String> env, String stdoutBaseFileName) {
4545
* Run the external program.
4646
*
4747
* @param scriptToRun the executable program to run
48-
* @param linesToPipeToStdout an optional list of lines to write to the external program's starndard input
48+
* @param linesToPipeToStdout an optional list of lines to write to the external program's standard input
4949
* @param args the arguments to pass to the external program on the command-line
5050
* @return the exit code of the external program
5151
* @throws ScriptRunnerException if argument validation error or error running the external program occurs
5252
*/
5353
public int executeScript(File scriptToRun, List<String> linesToPipeToStdout, String... args)
54+
throws ScriptRunnerException{
55+
return executeScript(scriptToRun,false, linesToPipeToStdout, args);
56+
}
57+
58+
/**
59+
* Run the external program.
60+
*
61+
* @param scriptToRun the executable program to run
62+
* @param appendFlag flag indicating if stdout file should be appended to
63+
* @param linesToPipeToStdout an optional list of lines to write to the external program's standard input
64+
* @param args the arguments to pass to the external program on the command-line
65+
* @return the exit code of the external program
66+
* @throws ScriptRunnerException if argument validation error or error running the external program occurs
67+
*/
68+
public int executeScript(File scriptToRun, boolean appendFlag, List<String> linesToPipeToStdout, String... args)
5469
throws ScriptRunnerException{
5570
final String METHOD = "executeScript";
5671

@@ -60,7 +75,7 @@ public int executeScript(File scriptToRun, List<String> linesToPipeToStdout, Str
6075
String[] command = getCommandStringArray(scriptToRun, args);
6176

6277
ProcessHandler processHandler = new ProcessHandler(command, cwd);
63-
processHandler.setStdoutLog(stdoutFile);
78+
processHandler.setStdoutLog(stdoutFile, appendFlag);
6479
processHandler.setBufferStdout();
6580

6681
for (Map.Entry<String, String> envEntry : env.entrySet()) {

0 commit comments

Comments
 (0)