-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathLocal.java
More file actions
245 lines (216 loc) · 7.73 KB
/
Local.java
File metadata and controls
245 lines (216 loc) · 7.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package com.browserstack.local;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.json.*;
/**
* Creates and manages a secure tunnel connection to BrowserStack.
*/
public class Local {
private static final List<String> IGNORE_KEYS = Arrays.asList("key", "binarypath");
List<String> command;
Map<String, String> startOptions;
String binaryPath;
int pid = 0;
private LocalProcess proc = null;
// Current version of binding package, used for --source option of binary
private static final String packageVersion = "1.1.6";
private final Map<String, String> parameters;
private final Map<String, String> avoidValueParameters;
public Local() {
avoidValueParameters = new HashMap<String, String>();
avoidValueParameters.put("v", "-vvv");
avoidValueParameters.put("force", "-force");
avoidValueParameters.put("forcelocal", "-forcelocal");
avoidValueParameters.put("onlyAutomate", "-onlyAutomate");
avoidValueParameters.put("forceproxy", "-forceproxy");
parameters = new HashMap<String, String>();
parameters.put("f", "-f");
parameters.put("only", "-only");
parameters.put("localIdentifier", "-localIdentifier");
parameters.put("proxyHost", "-proxyHost");
parameters.put("proxyPort", "-proxyPort");
parameters.put("proxyUser", "-proxyUser");
parameters.put("proxyPass", "-proxyPass");
}
/**
* Starts Local instance with options
*
* @param options Options for the Local instance
* @throws Exception
*/
public void start(Map<String, String> options) throws Exception {
startOptions = options;
LocalBinary lb;
if (options.get("binarypath") != null) {
lb = new LocalBinary(options.get("binarypath"), options.get("key"));
} else {
lb = new LocalBinary("", options.get("key"));
}
binaryPath = lb.getBinaryPath();
makeCommand(options, "start");
if (options.get("onlyCommand") != null) return;
if (proc == null) {
proc = runCommand(command);
BufferedReader stdoutbr = new BufferedReader(new InputStreamReader(proc.getInputStream()));
BufferedReader stderrbr = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
String stdout="", stderr="", line;
while ((line = stdoutbr.readLine()) != null) {
stdout += line;
}
while ((line = stderrbr.readLine()) != null) {
stderr += line;
}
int r = proc.waitFor();
JSONObject obj = new JSONObject(!stdout.equals("") ? stdout : stderr);
if(!obj.getString("state").equals("connected")){
throw new LocalException(obj.getJSONObject("message").getString("message"));
}
else {
pid = obj.getInt("pid");
}
}
}
/**
* Stops the Local instance
*
* @throws InterruptedException
*/
public void stop() throws Exception {
if (pid != 0) {
makeCommand(startOptions, "stop");
proc = runCommand(command);
proc.waitFor();
pid = 0;
}
}
/**
* Stops the Local instance specified by the given identifier
* @param options Options supplied for the Local instance
**/
public void stop(Map<String, String> options) throws Exception {
LocalBinary lb;
if (options.get("binarypath") != null) {
lb = new LocalBinary(options.get("binarypath"), options.get("key"));
} else {
lb = new LocalBinary("", options.get("key"));
}
binaryPath = lb.getBinaryPath();
makeCommand(options, "stop");
proc = runCommand(command);
proc.waitFor();
pid = 0;
}
/**
* Checks if Local instance is running
*
* @return true if Local instance is running, else false
*/
public boolean isRunning() throws Exception {
if (pid == 0) return false;
return isProcessRunning(pid);
}
/**
* Returns the package version
*
* @return {String} package version
*/
public static String getPackageVersion() {
return packageVersion;
}
/**
* Creates a list of command-line arguments for the Local instance
*
* @param options Options supplied for the Local instance
*/
private void makeCommand(Map<String, String> options, String opCode) {
command = new ArrayList<String>();
command.add(binaryPath);
command.add("-d");
command.add(opCode);
command.add("--key");
command.add(options.get("key"));
command.add("--source");
command.add("java-" + packageVersion);
for (Map.Entry<String, String> opt : options.entrySet()) {
String parameter = opt.getKey().trim();
if (IGNORE_KEYS.contains(parameter)) {
continue;
}
if (avoidValueParameters.get(parameter) != null && opt.getValue().trim().toLowerCase() != "false") {
command.add(avoidValueParameters.get(parameter));
} else {
if (parameters.get(parameter) != null) {
command.add(parameters.get(parameter));
} else {
command.add("-" + parameter);
}
if (opt.getValue() != null) {
command.add(opt.getValue().trim());
}
}
}
}
/**
* Checks if process with pid is running
*
* @param pid pid for the process to be checked.
* @link http://stackoverflow.com/a/26423642/941691
*/
private boolean isProcessRunning(int pid) throws Exception {
ArrayList<String> cmd = new ArrayList<String>();
if (System.getProperty("os.name").toLowerCase().contains("windows")) {
//tasklist exit code is always 0. Parse output
//findstr exit code 0 if found pid, 1 if it doesn't
cmd.add("cmd");
cmd.add("/c");
cmd.add("\"tasklist /FI \"PID eq " + pid + "\" | findstr " + pid + "\"");
}
else {
//ps exit code 0 if process exists, 1 if it doesn't
cmd.add("/bin/sh");
cmd.add("-c");
cmd.add("ps");
cmd.add("-o");
cmd.add("pid=");
cmd.add("|");
cmd.add("grep");
cmd.add("-w");
cmd.add(String.valueOf(pid));
}
proc = runCommand(cmd);
int exitValue = proc.waitFor();
// 0 is the default exit code which means the process exists
return exitValue == 0;
}
/**
* Executes the supplied command on the shell.
*
* @param command Command to be executed on the shell.
* @return {@link LocalProcess} for managing the launched process.
* @throws IOException
*/
protected LocalProcess runCommand(List<String> command) throws IOException {
ProcessBuilder processBuilder = new ProcessBuilder(command);
final Process process = processBuilder.start();
return new LocalProcess() {
public InputStream getInputStream() {
return process.getInputStream();
}
public InputStream getErrorStream() {
return process.getErrorStream();
}
public int waitFor() throws Exception {
return process.waitFor();
}
};
}
public interface LocalProcess {
InputStream getInputStream();
InputStream getErrorStream();
int waitFor() throws Exception;
}
}