Skip to content

Commit

Permalink
Start using DI for the WebDriverServlet
Browse files Browse the repository at this point in the history
One day, this will be a command handler
  • Loading branch information
Simon Stewart committed Aug 9, 2018
1 parent 9a30412 commit 132e075
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.openqa.selenium.remote.server;

import static java.util.concurrent.TimeUnit.SECONDS;
import static org.openqa.selenium.remote.server.WebDriverServlet.ACTIVE_SESSIONS_KEY;
import static org.openqa.selenium.remote.server.WebDriverServlet.NEW_SESSION_PIPELINE_KEY;

Expand All @@ -43,11 +44,13 @@
import org.seleniumhq.jetty9.server.ServerConnector;
import org.seleniumhq.jetty9.server.handler.ContextHandler;
import org.seleniumhq.jetty9.servlet.ServletContextHandler;
import org.seleniumhq.jetty9.servlet.ServletHolder;
import org.seleniumhq.jetty9.util.security.Constraint;
import org.seleniumhq.jetty9.util.thread.QueuedThreadPool;

import java.net.BindException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -127,24 +130,19 @@ public boolean boot() {

ServletContextHandler handler = new ServletContextHandler(ServletContextHandler.SECURITY);

if (configuration.browserTimeout != null && configuration.browserTimeout >= 0) {
handler.setInitParameter(WebDriverServlet.BROWSER_TIMEOUT_PARAMETER,
String.valueOf(configuration.browserTimeout));
}

long inactiveSessionTimeoutSeconds = configuration.timeout == null ?
Long.MAX_VALUE / 1000 : configuration.timeout;
if (configuration.timeout != null && configuration.timeout >= 0) {
handler.setInitParameter(WebDriverServlet.SESSION_TIMEOUT_PARAMETER,
String.valueOf(inactiveSessionTimeoutSeconds));
}

NewSessionPipeline pipeline = createPipeline(configuration);
handler.setAttribute(NEW_SESSION_PIPELINE_KEY, pipeline);

handler.setContextPath("/");
handler.addServlet(WebDriverServlet.class, "/wd/hub/*");
handler.addServlet(WebDriverServlet.class, "/webdriver/*");

ActiveSessions allSessions = new ActiveSessions(inactiveSessionTimeoutSeconds, SECONDS);
ServletHolder driverServlet = new ServletHolder(new WebDriverServlet(allSessions, pipeline));

handler.addServlet(driverServlet, "/wd/hub/*");
handler.addServlet(driverServlet, "/webdriver/*");
handler.setInitParameter(ConsoleServlet.CONSOLE_PATH_PARAMETER, "/wd/hub");

handler.setInitParameter(DisplayHelpServlet.HELPER_TYPE_PARAMETER, configuration.role);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@

package org.openqa.selenium.remote.server;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.openqa.selenium.remote.CapabilityType.BROWSER_NAME;

import com.google.common.base.Splitter;
import com.google.common.net.HttpHeaders;
import com.google.common.net.MediaType;

import org.openqa.selenium.grid.web.CommandHandler;
import org.openqa.selenium.grid.server.ServletRequestWrappingHttpRequest;
import org.openqa.selenium.grid.server.ServletResponseWrappingHttpResponse;
import org.openqa.selenium.grid.web.CommandHandler;
import org.openqa.selenium.logging.LoggingHandler;
import org.openqa.selenium.remote.SessionId;
import org.openqa.selenium.remote.server.commandhandler.ExceptionHandler;
Expand All @@ -40,7 +38,7 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand All @@ -49,6 +47,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
Expand All @@ -60,43 +59,28 @@

public class WebDriverServlet extends HttpServlet {

public static final String SESSION_TIMEOUT_PARAMETER = "webdriver.server.session.timeout";
public static final String BROWSER_TIMEOUT_PARAMETER = "webdriver.server.browser.timeout";
private static final Logger LOG = Logger.getLogger(WebDriverServlet.class.getName());
public static final String ACTIVE_SESSIONS_KEY = WebDriverServlet.class.getName() + ".sessions";
public static final String NEW_SESSION_PIPELINE_KEY = WebDriverServlet.class.getName() + ".pipeline";

private static final String CROSS_DOMAIN_RPC_PATH = "/xdrpc";

private final Logger logger;
private final StaticResourceHandler staticResourceHandler = new StaticResourceHandler();
private final ExecutorService executor = Executors.newCachedThreadPool();
private final ScheduledExecutorService scheduled = Executors.newSingleThreadScheduledExecutor();
private ActiveSessions allSessions;
private final ActiveSessions allSessions;
private AllHandlers handlers;

@Override
public void init() {
configureLogging();
log("Initialising WebDriverServlet");

long inactiveSessionTimeout = Optional.ofNullable(getServletContext().getInitParameter(SESSION_TIMEOUT_PARAMETER))
.map(value -> SECONDS.toMillis(Long.parseLong(value)))
.filter(value -> value > 0)
.orElse(Long.MAX_VALUE);

allSessions = (ActiveSessions) getServletContext().getAttribute(ACTIVE_SESSIONS_KEY);
if (allSessions == null) {
allSessions = new ActiveSessions(inactiveSessionTimeout, MILLISECONDS);
getServletContext().setAttribute(ACTIVE_SESSIONS_KEY, allSessions);
}
scheduled.scheduleWithFixedDelay(() -> allSessions.cleanUp(), 5, 5, TimeUnit.SECONDS);
public WebDriverServlet(
ActiveSessions allSessions,
NewSessionPipeline pipeline) {
logger = configureLogging();
logger.info("Initialising WebDriverServlet");

NewSessionPipeline pipeline =
(NewSessionPipeline) getServletContext().getAttribute(NEW_SESSION_PIPELINE_KEY);
if (pipeline == null) {
pipeline = DefaultPipeline.createDefaultPipeline().create();
getServletContext().setAttribute(NEW_SESSION_PIPELINE_KEY, pipeline);
}
this.allSessions = Objects.requireNonNull(allSessions);

scheduled.scheduleWithFixedDelay(allSessions::cleanUp, 5, 5, TimeUnit.SECONDS);

handlers = new AllHandlers(pipeline, allSessions);
}
Expand Down Expand Up @@ -267,7 +251,7 @@ private void handle(HttpServletRequest req, HttpServletResponse resp) {
new ServletRequestWrappingHttpRequest(req),
new ServletResponseWrappingHttpResponse(resp));
} catch (InterruptedException e) {
log("Unexpectedly interrupted: " + e.getMessage(), e);
logger.log(Level.WARNING, "Unexpectedly interrupted: " + e.getMessage(), e);
invalidateSession = true;

Thread.currentThread().interrupt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.verify;
import static org.openqa.selenium.remote.server.WebDriverServlet.ACTIVE_SESSIONS_KEY;
import static org.openqa.selenium.remote.server.WebDriverServlet.NEW_SESSION_PIPELINE_KEY;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
Expand All @@ -46,11 +44,9 @@
import org.openqa.testing.FakeHttpServletRequest;
import org.openqa.testing.FakeHttpServletResponse;
import org.openqa.testing.UrlInfo;
import org.seleniumhq.jetty9.server.handler.ContextHandler;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

Expand Down Expand Up @@ -91,33 +87,8 @@ public WebDriver newInstance(Capabilities capabilities) {
.add(factory)
.create();

ContextHandler.Context context = new ContextHandler().getServletContext();
context.setAttribute(ACTIVE_SESSIONS_KEY, testSessions);
context.setAttribute(NEW_SESSION_PIPELINE_KEY, pipeline);
context.setInitParameter("webdriver.server.session.timeout", "18");
context.setInitParameter("webdriver.server.browser.timeout", "2");

// Override log methods for testing.
driverServlet = new WebDriverServlet() {
@Override
public void log(String msg) {
}

@Override
public void log(String message, Throwable t) {
}

@Override
public ServletContext getServletContext() {
return context;
}

@Override
public String getInitParameter(String name) {
return context.getInitParameter(name);
}
};
driverServlet.init();
driverServlet = new WebDriverServlet(testSessions, pipeline);
}

@Test
Expand Down

0 comments on commit 132e075

Please sign in to comment.