11package 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 .*;
74import java .util .ArrayList ;
85import java .util .HashMap ;
96import java .util .Arrays ;
1613 */
1714public 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