Skip to content

Commit 67c5283

Browse files
committed
Merge pull request #13 from shirish87/extend
Minor changes for reusing Local
2 parents 7290373 + 8710d23 commit 67c5283

File tree

2 files changed

+47
-27
lines changed

2 files changed

+47
-27
lines changed

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>com.browserstack</groupId>
55
<artifactId>browserstack-local-java</artifactId>
66
<packaging>jar</packaging>
7-
<version>0.2.0</version>
7+
<version>0.3.0</version>
88

99
<name>browserstack-local-java</name>
1010
<description>Java bindings for BrowserStack Local</description>
@@ -143,8 +143,8 @@
143143
<artifactId>maven-compiler-plugin</artifactId>
144144
<version>2.3.2</version>
145145
<configuration>
146-
<source>1.6</source>
147-
<target>1.6</target>
146+
<source>1.5</source>
147+
<target>1.5</target>
148148
</configuration>
149149
</plugin>
150150
<plugin>

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

Lines changed: 44 additions & 24 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;
@@ -16,13 +13,14 @@
1613
*/
1714
public class Local {
1815

16+
private static final List<String> IGNORE_KEYS = Arrays.asList("key", "binarypath");
17+
1918
List<String> command;
2019
Map<String, String> startOptions;
2120
String binaryPath;
22-
String logFilePath;
2321
int pid = 0;
2422

25-
private Process proc = null;
23+
private LocalProcess proc = null;
2624

2725
private final Map<String, String> parameters;
2826

@@ -58,19 +56,12 @@ public void start(Map<String, String> options) throws Exception {
5856
binaryPath = lb.getBinaryPath();
5957
}
6058

61-
logFilePath = options.get("logfile") == null ? (System.getProperty("user.dir") + "/local.log") : options.get("logfile");
6259
makeCommand(options, "start");
6360

6461
if (options.get("onlyCommand") != null) return;
6562

6663
if (proc == null) {
67-
ProcessBuilder processBuilder = new ProcessBuilder(command);
68-
69-
FileWriter fw = new FileWriter(logFilePath);
70-
fw.write("");
71-
fw.close();
72-
73-
proc = processBuilder.start();
64+
proc = runCommand(command);
7465
BufferedReader stdoutbr = new BufferedReader(new InputStreamReader(proc.getInputStream()));
7566
BufferedReader stderrbr = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
7667
String stdout="", stderr="", line;
@@ -82,7 +73,7 @@ public void start(Map<String, String> options) throws Exception {
8273
}
8374
int r = proc.waitFor();
8475

85-
JSONObject obj = new JSONObject(stdout != "" ? stdout : stderr);
76+
JSONObject obj = new JSONObject(!stdout.equals("") ? stdout : stderr);
8677
if(!obj.getString("state").equals("connected")){
8778
throw new LocalException(obj.getString("message"));
8879
}
@@ -100,8 +91,7 @@ public void start(Map<String, String> options) throws Exception {
10091
public void stop() throws Exception {
10192
if (pid != 0) {
10293
makeCommand(startOptions, "stop");
103-
ProcessBuilder processBuilder = new ProcessBuilder(command);
104-
proc = processBuilder.start();
94+
proc = runCommand(command);
10595
proc.waitFor();
10696
pid = 0;
10797
}
@@ -127,14 +117,11 @@ private void makeCommand(Map<String, String> options, String opCode) {
127117
command.add(binaryPath);
128118
command.add("-d");
129119
command.add(opCode);
130-
command.add("-logFile");
131-
command.add(logFilePath);
132120
command.add(options.get("key"));
133121

134122
for (Map.Entry<String, String> opt : options.entrySet()) {
135-
List<String> ignoreKeys = Arrays.asList("key", "logfile", "binarypath");
136123
String parameter = opt.getKey().trim();
137-
if (ignoreKeys.contains(parameter)) {
124+
if (IGNORE_KEYS.contains(parameter)) {
138125
continue;
139126
}
140127
if (parameters.get(parameter) != null) {
@@ -151,7 +138,7 @@ private void makeCommand(Map<String, String> options, String opCode) {
151138
/**
152139
* Checks if process with pid is running
153140
*
154-
* @param options Options supplied for the Local instance
141+
* @param pid pid for the process to be checked.
155142
* @link http://stackoverflow.com/a/26423642/941691
156143
*/
157144
private boolean isProcessRunning(int pid) throws Exception {
@@ -170,11 +157,44 @@ private boolean isProcessRunning(int pid) throws Exception {
170157
cmd.add(String.valueOf(pid));
171158
}
172159

173-
ProcessBuilder processBuilder = new ProcessBuilder(cmd);
174-
proc = processBuilder.start();
160+
proc = runCommand(cmd);
175161
int exitValue = proc.waitFor();
176162

177163
// 0 is the default exit code which means the process exists
178164
return exitValue == 0;
179165
}
166+
167+
/**
168+
* Executes the supplied command on the shell.
169+
*
170+
* @param command Command to be executed on the shell.
171+
* @return {@link LocalProcess} for managing the launched process.
172+
* @throws IOException
173+
*/
174+
protected LocalProcess runCommand(List<String> command) throws IOException {
175+
ProcessBuilder processBuilder = new ProcessBuilder(command);
176+
final Process process = processBuilder.start();
177+
178+
return new LocalProcess() {
179+
public InputStream getInputStream() {
180+
return process.getInputStream();
181+
}
182+
183+
public InputStream getErrorStream() {
184+
return process.getErrorStream();
185+
}
186+
187+
public int waitFor() throws Exception {
188+
return process.waitFor();
189+
}
190+
};
191+
}
192+
193+
public interface LocalProcess {
194+
InputStream getInputStream();
195+
196+
InputStream getErrorStream();
197+
198+
int waitFor() throws Exception;
199+
}
180200
}

0 commit comments

Comments
 (0)