Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.browserstack</groupId>
<artifactId>browserstack-local-java</artifactId>
<packaging>jar</packaging>
<version>0.2.0</version>
<version>0.3.0</version>

<name>browserstack-local-java</name>
<description>Java bindings for BrowserStack Local</description>
Expand Down Expand Up @@ -143,8 +143,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
Expand Down
68 changes: 44 additions & 24 deletions src/main/java/com/browserstack/local/Local.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,13 +13,14 @@
*/
public class Local {

private static final List<String> IGNORE_KEYS = Arrays.asList("key", "binarypath");

List<String> command;
Map<String, String> startOptions;
String binaryPath;
String logFilePath;
int pid = 0;

private Process proc = null;
private LocalProcess proc = null;

private final Map<String, String> parameters;

Expand Down Expand Up @@ -58,19 +56,12 @@ public void start(Map<String, String> 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;
Expand All @@ -82,7 +73,7 @@ public void start(Map<String, String> 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"));
}
Expand All @@ -100,8 +91,7 @@ public void start(Map<String, String> 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;
}
Expand All @@ -127,14 +117,11 @@ private void makeCommand(Map<String, String> 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<String, String> opt : options.entrySet()) {
List<String> 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) {
Expand All @@ -151,7 +138,7 @@ private void makeCommand(Map<String, String> 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 {
Expand All @@ -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<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;
}
}