Skip to content

Commit

Permalink
Code formatting and avoid using deprecated methods
Browse files Browse the repository at this point in the history
  • Loading branch information
diemol committed Oct 31, 2023
1 parent 1e5953e commit 19de74f
Showing 1 changed file with 82 additions and 87 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package com.saucelabs.ci.sauceconnect;

import com.saucelabs.saucerest.DataCenter;
import com.saucelabs.saucerest.SauceREST;
import com.saucelabs.saucerest.SauceException;
import com.saucelabs.saucerest.SauceREST;
import com.saucelabs.saucerest.api.SauceConnectEndpoint;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.NullOutputStream;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONException;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
Expand All @@ -25,6 +20,10 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.NullOutputStream;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONException;

/**
* Provides common logic for the invocation of Sauce Connect v3 and v4 processes. The class
Expand All @@ -42,11 +41,11 @@ public abstract class AbstractSauceTunnelManager implements SauceTunnelManager {
/** Should Sauce Connect output be suppressed? */
protected boolean quietMode;

protected Map<String, TunnelInformation> tunnelInformationMap = new ConcurrentHashMap<>();

/** Contains all the Sauce Connect {@link Process} instances that have been launched. */
private Map<String, List<Process>> openedProcesses = new HashMap<>();

protected Map<String, TunnelInformation> tunnelInformationMap = new ConcurrentHashMap<>();

private SauceREST sauceRest;
private SauceConnectEndpoint scEndpoint;

Expand All @@ -61,6 +60,56 @@ public AbstractSauceTunnelManager(boolean quietMode) {
this.quietMode = quietMode;
}

/**
* @param options the command line options used to launch Sauce Connect
* @param defaultValue the default value to use for the tunnel name if none specified in the
* options
* @return String representing the tunnel name
*/
public static String getTunnelName(String options, String defaultValue) {
if (options == null || options.isEmpty()) {
return defaultValue;
}

String name = null;
String[] split = options.split(" ");
for (int i = 0; i < split.length; i++) {
String option = split[i];
// Handle old tunnel-identifier option and well as new tunnel-name
if (option.equals("-i")
|| option.equals("--tunnel-name")
|| option.equals("--tunnel-identifier")) {
// next option is name
name = split[i + 1];
}
}
if (name != null) {
return name;
}

return defaultValue;
}

/**
* @param options the command line options used to launch Sauce Connect
* @return String representing the logfile location
*/
public static String getLogfile(String options) {
if (options == null || options.isEmpty()) {
return null;
}
String logFile = null;
String[] split = options.split(" ");
for (int i = 0; i < split.length; i++) {
String option = split[i];
if (option.equals("-l") || option.equals("--logfile")) {
// next option is logfile
logFile = split[i + 1];
}
}
return logFile;
}

public void setSauceRest(SauceREST sauceRest) {
this.sauceRest = sauceRest;
this.scEndpoint = sauceRest.getSauceConnectEndpoint();
Expand Down Expand Up @@ -181,56 +230,6 @@ protected void logMessage(PrintStream printStream, String message) {
julLogger.log(Level.INFO, message);
}

/**
* @param options the command line options used to launch Sauce Connect
* @param defaultValue the default value to use for the tunnel name if none specified in the
* options
* @return String representing the tunnel name
*/
public static String getTunnelName(String options, String defaultValue) {
if (options == null || options.equals("")) {
return defaultValue;
}

String name = null;
String[] split = options.split(" ");
for (int i = 0; i < split.length; i++) {
String option = split[i];
// Handle old tunnel-identifier option and well as new tunnel-name
if (option.equals("-i")
|| option.equals("--tunnel-name")
|| option.equals("--tunnel-identifier")) {
// next option is name
name = split[i + 1];
}
}
if (name != null) {
return name;
}

return defaultValue;
}

/**
* @param options the command line options used to launch Sauce Connect
* @return String representing the logfile location
*/
public static String getLogfile(String options) {
if (options == null || options.equals("")) {
return null;
}
String logFile = null;
String[] split = options.split(" ");
for (int i = 0; i < split.length; i++) {
String option = split[i];
if (option.equals("-l") || option.equals("--logfile")) {
// next option is logfile
logFile = split[i + 1];
}
}
return logFile;
}

/**
* Adds an element to an array
*
Expand Down Expand Up @@ -327,7 +326,7 @@ public Process openConnection(
return openConnection(
username,
apiKey,
"US_WEST",
DataCenter.US_WEST,
port,
sauceConnectJar,
options,
Expand Down Expand Up @@ -563,10 +562,6 @@ public SystemOutGobbler makeOutputGobbler(
"OutputGobbler", inputStream, semaphore, printStream, getSauceStartedMessage());
}

/**
* @param name
* @return
*/
private TunnelInformation getTunnelInformation(String name) {
if (name == null) {
return null;
Expand Down Expand Up @@ -636,6 +631,30 @@ public String getSauceConnectWorkingDirectory() {

public abstract File getSauceConnectLogFile(String options);

/**
* @return Text which indicates that Sauce Connect has started
*/
protected abstract String getSauceStartedMessage();

/** Base exception class which is thrown if an error occurs launching Sauce Connect. */
public static class SauceConnectException extends IOException {

public SauceConnectException(String message) {
super(message);
}

public SauceConnectException(Exception cause) {
super(cause);
}
}

/** Exception which is thrown when Sauce Connect does not start within the timeout period. */
public static class SauceConnectDidNotStartException extends SauceConnectException {
public SauceConnectDidNotStartException(String message) {
super(message);
}
}

/** Handles receiving and processing the output of an external process. */
protected abstract class StreamGobbler extends Thread {
private final PrintStream printStream;
Expand Down Expand Up @@ -746,35 +765,11 @@ public boolean isCantLockPidfile() {
}
}

/**
* @return Text which indicates that Sauce Connect has started
*/
protected abstract String getSauceStartedMessage();

/** Handles processing Sauce Connect output sent to stderr. */
public class SystemErrorGobbler extends StreamGobbler {

public SystemErrorGobbler(String name, InputStream is, PrintStream printStream) {
super(name, is, printStream);
}
}

/** Base exception class which is thrown if an error occurs launching Sauce Connect. */
public static class SauceConnectException extends IOException {

public SauceConnectException(String message) {
super(message);
}

public SauceConnectException(Exception cause) {
super(cause);
}
}

/** Exception which is thrown when Sauce Connect does not start within the timeout period. */
public static class SauceConnectDidNotStartException extends SauceConnectException {
public SauceConnectDidNotStartException(String message) {
super(message);
}
}
}

0 comments on commit 19de74f

Please sign in to comment.