Skip to content

Commit f99e013

Browse files
committed
Enables overriding execution specifics of commands
1 parent 7290373 commit f99e013

File tree

1 file changed

+42
-12
lines changed

1 file changed

+42
-12
lines changed

src/main/java/com/browserstack/local/Local.java

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.browserstack.local;
22

3-
import java.io.BufferedReader;
4-
import java.io.InputStreamReader;
5-
import java.io.FileReader;
6-
import java.io.FileWriter;
3+
import java.io.*;
74
import java.util.ArrayList;
85
import java.util.HashMap;
96
import java.util.Arrays;
@@ -22,7 +19,7 @@ public class Local {
2219
String logFilePath;
2320
int pid = 0;
2421

25-
private Process proc = null;
22+
private LocalProcess proc = null;
2623

2724
private final Map<String, String> parameters;
2825

@@ -64,13 +61,11 @@ public void start(Map<String, String> options) throws Exception {
6461
if (options.get("onlyCommand") != null) return;
6562

6663
if (proc == null) {
67-
ProcessBuilder processBuilder = new ProcessBuilder(command);
68-
6964
FileWriter fw = new FileWriter(logFilePath);
7065
fw.write("");
7166
fw.close();
7267

73-
proc = processBuilder.start();
68+
proc = runCommand(command);
7469
BufferedReader stdoutbr = new BufferedReader(new InputStreamReader(proc.getInputStream()));
7570
BufferedReader stderrbr = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
7671
String stdout="", stderr="", line;
@@ -100,8 +95,7 @@ public void start(Map<String, String> options) throws Exception {
10095
public void stop() throws Exception {
10196
if (pid != 0) {
10297
makeCommand(startOptions, "stop");
103-
ProcessBuilder processBuilder = new ProcessBuilder(command);
104-
proc = processBuilder.start();
98+
proc = runCommand(command);
10599
proc.waitFor();
106100
pid = 0;
107101
}
@@ -170,11 +164,47 @@ private boolean isProcessRunning(int pid) throws Exception {
170164
cmd.add(String.valueOf(pid));
171165
}
172166

173-
ProcessBuilder processBuilder = new ProcessBuilder(cmd);
174-
proc = processBuilder.start();
167+
proc = runCommand(cmd);
175168
int exitValue = proc.waitFor();
176169

177170
// 0 is the default exit code which means the process exists
178171
return exitValue == 0;
179172
}
173+
174+
/**
175+
* Executes the supplied command on the shell.
176+
*
177+
* @param command Command to be executed on the shell.
178+
* @return {@link LocalProcess} for managing the launched process.
179+
* @throws IOException
180+
*/
181+
protected LocalProcess runCommand(List<String> command) throws IOException {
182+
ProcessBuilder processBuilder = new ProcessBuilder(command);
183+
final Process process = processBuilder.start();
184+
185+
return new LocalProcess() {
186+
@Override
187+
public InputStream getInputStream() {
188+
return process.getInputStream();
189+
}
190+
191+
@Override
192+
public InputStream getErrorStream() {
193+
return process.getErrorStream();
194+
}
195+
196+
@Override
197+
public int waitFor() throws InterruptedException {
198+
return process.waitFor();
199+
}
200+
};
201+
}
202+
203+
public interface LocalProcess {
204+
InputStream getInputStream();
205+
206+
InputStream getErrorStream();
207+
208+
int waitFor() throws InterruptedException;
209+
}
180210
}

0 commit comments

Comments
 (0)