Skip to content

Commit

Permalink
[java] Small refactoring of debug server
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Sep 30, 2018
1 parent 0ca0810 commit c55f5d8
Showing 1 changed file with 11 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import static com.google.common.net.MediaType.JSON_UTF_8;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Collections.singletonMap;
import static org.openqa.selenium.net.PortProber.findFreePort;
import static org.openqa.selenium.testing.InProject.locate;

import com.google.common.collect.ImmutableList;
Expand All @@ -30,6 +29,7 @@
import org.openqa.selenium.io.TemporaryFilesystem;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.net.NetworkUtils;
import org.openqa.selenium.net.PortProber;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpMethod;
import org.openqa.selenium.remote.http.HttpRequest;
Expand Down Expand Up @@ -58,6 +58,7 @@
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;

import javax.servlet.Servlet;

Expand Down Expand Up @@ -118,8 +119,7 @@ public JettyAppServer(String hostName, int httpPort, int httpsPort) {

TemporaryFilesystem tempFs = TemporaryFilesystem.getDefaultTmpFS();
tempPageDir = tempFs.createTempDir("pages", "test");
ServletContextHandler tempContext = addResourceHandler(
TEMP_SRC_CONTEXT_PATH, tempPageDir.toPath());
addResourceHandler(TEMP_SRC_CONTEXT_PATH, tempPageDir.toPath());
defaultContext.setInitParameter("tempPageDir", tempPageDir.getAbsolutePath());
defaultContext.setInitParameter("hostname", hostName);
defaultContext.setInitParameter("port", ""+port);
Expand All @@ -144,14 +144,16 @@ public JettyAppServer(String hostName, int httpPort, int httpsPort) {
addServlet(defaultContext, "/createPage", CreatePageServlet.class);
}

private static Optional<Integer> getEnvValue(String key) {
return Optional.ofNullable(System.getenv(key)).map(Integer::parseInt);
}

private static int getHttpPort() {
String port = System.getenv(FIXED_HTTP_PORT_ENV_NAME);
return port == null ? findFreePort() : Integer.parseInt(port);
return getEnvValue(FIXED_HTTP_PORT_ENV_NAME).orElseGet(PortProber::findFreePort);
}

private static int getHttpsPort() {
String port = System.getenv(FIXED_HTTPS_PORT_ENV_NAME);
return port == null ? findFreePort() : Integer.parseInt(port);
return getEnvValue(FIXED_HTTPS_PORT_ENV_NAME).orElseGet(PortProber::findFreePort);
}

@Override
Expand Down Expand Up @@ -308,13 +310,11 @@ protected ServletContextHandler addResourceHandler(String contextPath, Path reso
}

protected static int getHttpPortFromEnv() {
String port = System.getenv(FIXED_HTTP_PORT_ENV_NAME);
return port == null ? DEFAULT_HTTP_PORT : Integer.parseInt(port);
return getEnvValue(FIXED_HTTP_PORT_ENV_NAME).orElse(DEFAULT_HTTP_PORT);
}

protected static int getHttpsPortFromEnv() {
String port = System.getenv(FIXED_HTTPS_PORT_ENV_NAME);
return port == null ? DEFAULT_HTTPS_PORT : Integer.parseInt(port);
return getEnvValue(FIXED_HTTPS_PORT_ENV_NAME).orElse(DEFAULT_HTTPS_PORT);
}

public static void main(String[] args) {
Expand Down

0 comments on commit c55f5d8

Please sign in to comment.