diff --git a/pom.xml b/pom.xml index 2569f17..ed5a1ac 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.browserstack browserstack-local-java jar - 0.2.0 + 0.3.0 browserstack-local-java Java bindings for BrowserStack Local @@ -143,8 +143,8 @@ maven-compiler-plugin 2.3.2 - 1.6 - 1.6 + 1.5 + 1.5 diff --git a/src/main/java/com/browserstack/local/Local.java b/src/main/java/com/browserstack/local/Local.java index e59d6c3..001c83a 100644 --- a/src/main/java/com/browserstack/local/Local.java +++ b/src/main/java/com/browserstack/local/Local.java @@ -1,9 +1,6 @@ package com.browserstack.local; -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.io.FileReader; -import java.io.FileWriter; +import java.io.*; import java.util.ArrayList; import java.util.HashMap; import java.util.Arrays; @@ -16,13 +13,14 @@ */ public class Local { + private static final List IGNORE_KEYS = Arrays.asList("key", "binarypath"); + List command; Map startOptions; String binaryPath; - String logFilePath; int pid = 0; - private Process proc = null; + private LocalProcess proc = null; private final Map parameters; @@ -58,19 +56,12 @@ public void start(Map options) throws Exception { binaryPath = lb.getBinaryPath(); } - logFilePath = options.get("logfile") == null ? (System.getProperty("user.dir") + "/local.log") : options.get("logfile"); makeCommand(options, "start"); if (options.get("onlyCommand") != null) return; if (proc == null) { - ProcessBuilder processBuilder = new ProcessBuilder(command); - - FileWriter fw = new FileWriter(logFilePath); - fw.write(""); - fw.close(); - - proc = processBuilder.start(); + proc = runCommand(command); BufferedReader stdoutbr = new BufferedReader(new InputStreamReader(proc.getInputStream())); BufferedReader stderrbr = new BufferedReader(new InputStreamReader(proc.getErrorStream())); String stdout="", stderr="", line; @@ -82,7 +73,7 @@ public void start(Map options) throws Exception { } int r = proc.waitFor(); - JSONObject obj = new JSONObject(stdout != "" ? stdout : stderr); + JSONObject obj = new JSONObject(!stdout.equals("") ? stdout : stderr); if(!obj.getString("state").equals("connected")){ throw new LocalException(obj.getString("message")); } @@ -100,8 +91,7 @@ public void start(Map options) throws Exception { public void stop() throws Exception { if (pid != 0) { makeCommand(startOptions, "stop"); - ProcessBuilder processBuilder = new ProcessBuilder(command); - proc = processBuilder.start(); + proc = runCommand(command); proc.waitFor(); pid = 0; } @@ -127,14 +117,11 @@ private void makeCommand(Map options, String opCode) { command.add(binaryPath); command.add("-d"); command.add(opCode); - command.add("-logFile"); - command.add(logFilePath); command.add(options.get("key")); for (Map.Entry opt : options.entrySet()) { - List ignoreKeys = Arrays.asList("key", "logfile", "binarypath"); String parameter = opt.getKey().trim(); - if (ignoreKeys.contains(parameter)) { + if (IGNORE_KEYS.contains(parameter)) { continue; } if (parameters.get(parameter) != null) { @@ -151,7 +138,7 @@ private void makeCommand(Map options, String opCode) { /** * Checks if process with pid is running * - * @param options Options supplied for the Local instance + * @param pid pid for the process to be checked. * @link http://stackoverflow.com/a/26423642/941691 */ private boolean isProcessRunning(int pid) throws Exception { @@ -170,11 +157,44 @@ private boolean isProcessRunning(int pid) throws Exception { cmd.add(String.valueOf(pid)); } - ProcessBuilder processBuilder = new ProcessBuilder(cmd); - proc = processBuilder.start(); + 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 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; + } }