1
1
package com .browserstack .local ;
2
2
3
- import java .io .BufferedReader ;
4
- import java .io .InputStreamReader ;
5
- import java .io .FileReader ;
6
- import java .io .FileWriter ;
3
+ import java .io .*;
7
4
import java .util .ArrayList ;
8
5
import java .util .HashMap ;
9
6
import java .util .Arrays ;
16
13
*/
17
14
public class Local {
18
15
16
+ private static final List <String > IGNORE_KEYS = Arrays .asList ("key" , "binarypath" );
17
+
19
18
List <String > command ;
20
19
Map <String , String > startOptions ;
21
20
String binaryPath ;
22
- String logFilePath ;
23
21
int pid = 0 ;
24
22
25
- private Process proc = null ;
23
+ private LocalProcess proc = null ;
26
24
27
25
private final Map <String , String > parameters ;
28
26
@@ -58,19 +56,12 @@ public void start(Map<String, String> options) throws Exception {
58
56
binaryPath = lb .getBinaryPath ();
59
57
}
60
58
61
- logFilePath = options .get ("logfile" ) == null ? (System .getProperty ("user.dir" ) + "/local.log" ) : options .get ("logfile" );
62
59
makeCommand (options , "start" );
63
60
64
61
if (options .get ("onlyCommand" ) != null ) return ;
65
62
66
63
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 );
74
65
BufferedReader stdoutbr = new BufferedReader (new InputStreamReader (proc .getInputStream ()));
75
66
BufferedReader stderrbr = new BufferedReader (new InputStreamReader (proc .getErrorStream ()));
76
67
String stdout ="" , stderr ="" , line ;
@@ -82,7 +73,7 @@ public void start(Map<String, String> options) throws Exception {
82
73
}
83
74
int r = proc .waitFor ();
84
75
85
- JSONObject obj = new JSONObject (stdout != "" ? stdout : stderr );
76
+ JSONObject obj = new JSONObject (! stdout . equals ( "" ) ? stdout : stderr );
86
77
if (!obj .getString ("state" ).equals ("connected" )){
87
78
throw new LocalException (obj .getString ("message" ));
88
79
}
@@ -100,8 +91,7 @@ public void start(Map<String, String> options) throws Exception {
100
91
public void stop () throws Exception {
101
92
if (pid != 0 ) {
102
93
makeCommand (startOptions , "stop" );
103
- ProcessBuilder processBuilder = new ProcessBuilder (command );
104
- proc = processBuilder .start ();
94
+ proc = runCommand (command );
105
95
proc .waitFor ();
106
96
pid = 0 ;
107
97
}
@@ -127,14 +117,11 @@ private void makeCommand(Map<String, String> options, String opCode) {
127
117
command .add (binaryPath );
128
118
command .add ("-d" );
129
119
command .add (opCode );
130
- command .add ("-logFile" );
131
- command .add (logFilePath );
132
120
command .add (options .get ("key" ));
133
121
134
122
for (Map .Entry <String , String > opt : options .entrySet ()) {
135
- List <String > ignoreKeys = Arrays .asList ("key" , "logfile" , "binarypath" );
136
123
String parameter = opt .getKey ().trim ();
137
- if (ignoreKeys .contains (parameter )) {
124
+ if (IGNORE_KEYS .contains (parameter )) {
138
125
continue ;
139
126
}
140
127
if (parameters .get (parameter ) != null ) {
@@ -151,7 +138,7 @@ private void makeCommand(Map<String, String> options, String opCode) {
151
138
/**
152
139
* Checks if process with pid is running
153
140
*
154
- * @param options Options supplied for the Local instance
141
+ * @param pid pid for the process to be checked.
155
142
* @link http://stackoverflow.com/a/26423642/941691
156
143
*/
157
144
private boolean isProcessRunning (int pid ) throws Exception {
@@ -170,11 +157,44 @@ private boolean isProcessRunning(int pid) throws Exception {
170
157
cmd .add (String .valueOf (pid ));
171
158
}
172
159
173
- ProcessBuilder processBuilder = new ProcessBuilder (cmd );
174
- proc = processBuilder .start ();
160
+ proc = runCommand (cmd );
175
161
int exitValue = proc .waitFor ();
176
162
177
163
// 0 is the default exit code which means the process exists
178
164
return exitValue == 0 ;
179
165
}
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
+ }
180
200
}
0 commit comments