|
| 1 | +package com.browserstack.local; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.FileReader; |
| 5 | +import java.io.FileWriter; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +/** |
| 12 | + * Creates and manages a secure tunnel connection to BrowserStack. |
| 13 | + */ |
| 14 | +public class Local { |
| 15 | + |
| 16 | + List<String> command; |
| 17 | + |
| 18 | + private Process proc = null; |
| 19 | + |
| 20 | + private final Map<String, String> parameters; |
| 21 | + |
| 22 | + public Local() throws Exception { |
| 23 | + parameters = new HashMap<String, String>(); |
| 24 | + parameters.put("v", "-vvv"); |
| 25 | + parameters.put("f", "-f"); |
| 26 | + parameters.put("h", "-h"); |
| 27 | + parameters.put("version", "-version"); |
| 28 | + parameters.put("force", "-force"); |
| 29 | + parameters.put("only", "-only"); |
| 30 | + parameters.put("forcelocal", "-forcelocal"); |
| 31 | + parameters.put("localIdentifier", "-localIdentifier"); |
| 32 | + parameters.put("onlyAutomate", "-onlyAutomate"); |
| 33 | + parameters.put("proxyHost", "-proxyHost"); |
| 34 | + parameters.put("proxyPort", "-proxyPort"); |
| 35 | + parameters.put("proxyUser", "-proxyUser"); |
| 36 | + parameters.put("proxyPass", "-proxyPass"); |
| 37 | + parameters.put("hosts", "-hosts"); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Starts Local instance with options |
| 42 | + * |
| 43 | + * @param options Options for the Local instance |
| 44 | + * @throws Exception |
| 45 | + */ |
| 46 | + public void start(Map<String, String> options) throws Exception { |
| 47 | + command = new ArrayList<String>(); |
| 48 | + |
| 49 | + if (options.get("binarypath") != null) { |
| 50 | + command.add(options.get("binarypath")); |
| 51 | + } else { |
| 52 | + LocalBinary lb = new LocalBinary(); |
| 53 | + command.add(lb.getBinaryPath()); |
| 54 | + } |
| 55 | + |
| 56 | + String logFilePath = options.get("logfile") == null ? |
| 57 | + (System.getProperty("user.dir") + "/local.log") : options.get("logfile"); |
| 58 | + command.add("-logFile"); |
| 59 | + command.add(logFilePath); |
| 60 | + |
| 61 | + command.add(options.get("key")); |
| 62 | + makeCommand(options); |
| 63 | + |
| 64 | + if (options.get("onlyCommand") != null) return; |
| 65 | + |
| 66 | + 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(); |
| 74 | + FileReader f = new FileReader(logFilePath); |
| 75 | + BufferedReader reader = new BufferedReader(f); |
| 76 | + String string; |
| 77 | + |
| 78 | + while (true) { |
| 79 | + string = reader.readLine(); |
| 80 | + if (string == null) continue; |
| 81 | + |
| 82 | + if (string.equalsIgnoreCase("Press Ctrl-C to exit")) { |
| 83 | + f.close(); |
| 84 | + break; |
| 85 | + } |
| 86 | + |
| 87 | + if (string.contains("*** Error")) { |
| 88 | + f.close(); |
| 89 | + stop(); |
| 90 | + throw new LocalException(string); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Stops the Local instance |
| 99 | + * |
| 100 | + * @throws InterruptedException |
| 101 | + */ |
| 102 | + public void stop() throws InterruptedException { |
| 103 | + if (proc != null) { |
| 104 | + proc.destroy(); |
| 105 | + while (isRunning()) { |
| 106 | + Thread.sleep(1000); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Checks if Local instance is running |
| 113 | + * |
| 114 | + * @return true if Local instance is running, else false |
| 115 | + */ |
| 116 | + public boolean isRunning() { |
| 117 | + if (proc == null) return false; |
| 118 | + |
| 119 | + try { |
| 120 | + proc.exitValue(); |
| 121 | + return false; |
| 122 | + } catch (IllegalThreadStateException e) { |
| 123 | + return true; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Creates a list of command-line arguments for the Local instance |
| 129 | + * |
| 130 | + * @param options Options supplied for the Local instance |
| 131 | + */ |
| 132 | + private void makeCommand(Map<String, String> options) { |
| 133 | + for (Map.Entry<String, String> opt : options.entrySet()) { |
| 134 | + String parameter = opt.getKey().trim(); |
| 135 | + if (parameters.get(parameter) != null) { |
| 136 | + command.add(parameters.get(parameter)); |
| 137 | + |
| 138 | + if (opt.getValue() != null) { |
| 139 | + command.add(opt.getValue().trim()); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments