Skip to content

Commit af3ea26

Browse files
committed
Inject the NewSessionPipeline far higher
1 parent b129262 commit af3ea26

File tree

4 files changed

+50
-40
lines changed

4 files changed

+50
-40
lines changed

java/server/src/org/openqa/selenium/remote/server/AllHandlers.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,13 @@
5252
class AllHandlers {
5353

5454
private final Json json;
55+
private final NewSessionPipeline pipeline;
5556
private final ActiveSessions allSessions;
5657

5758
private final Map<HttpMethod, ImmutableList<Function<String, CommandHandler>>> additionalHandlers;
5859

59-
public AllHandlers(ActiveSessions allSessions) {
60+
public AllHandlers(NewSessionPipeline pipeline, ActiveSessions allSessions) {
61+
this.pipeline = pipeline;
6062
this.allSessions = Objects.requireNonNull(allSessions);
6163
this.json = new Json();
6264

@@ -119,6 +121,7 @@ private <H extends CommandHandler> Function<String, CommandHandler> handler(
119121
}
120122

121123
ImmutableSet.Builder<Object> args = ImmutableSet.builder();
124+
args.add(pipeline);
122125
args.add(allSessions);
123126
args.add(json);
124127
if (match.getParameters().containsKey("sessionId")) {

java/server/src/org/openqa/selenium/remote/server/WebDriverServlet.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,20 @@
3434
import org.openqa.selenium.remote.server.log.PerSessionLogHandler;
3535
import org.openqa.selenium.remote.server.xdrpc.CrossDomainRpc;
3636
import org.openqa.selenium.remote.server.xdrpc.CrossDomainRpcLoader;
37+
import org.openqa.selenium.remote.service.DriverService;
3738

3839
import java.io.ByteArrayInputStream;
3940
import java.io.IOException;
4041
import java.util.List;
42+
import java.util.Optional;
4143
import java.util.concurrent.ExecutionException;
4244
import java.util.concurrent.ExecutorService;
4345
import java.util.concurrent.Executors;
4446
import java.util.concurrent.Future;
4547
import java.util.concurrent.TimeoutException;
4648
import java.util.logging.Handler;
4749
import java.util.logging.Logger;
50+
import java.util.stream.Stream;
4851

4952
import javax.servlet.ServletException;
5053
import javax.servlet.ServletInputStream;
@@ -57,6 +60,7 @@ public class WebDriverServlet extends HttpServlet {
5760

5861
private static final Logger LOG = Logger.getLogger(WebDriverServlet.class.getName());
5962
public static final String ACTIVE_SESSIONS_KEY = WebDriverServlet.class.getName() + ".sessions";
63+
public static final String NEW_SESSION_PIPELINE_KEY = WebDriverServlet.class.getName() + ".pipeline";
6064

6165
private static final String CROSS_DOMAIN_RPC_PATH = "/xdrpc";
6266

@@ -81,7 +85,42 @@ public void init() throws ServletException {
8185
getServletContext().setAttribute(ACTIVE_SESSIONS_KEY, allSessions);
8286
}
8387

84-
handlers = new AllHandlers(allSessions);
88+
NewSessionPipeline pipeline =
89+
(NewSessionPipeline) getServletContext().getAttribute(NEW_SESSION_PIPELINE_KEY);
90+
if (pipeline == null) {
91+
// Set up the pipeline to inject
92+
SessionFactory fallback = Stream.of(
93+
"org.openqa.selenium.chrome.ChromeDriverService",
94+
"org.openqa.selenium.firefox.GeckoDriverService",
95+
"org.openqa.selenium.edge.EdgeDriverService",
96+
"org.openqa.selenium.ie.InternetExplorerDriverService",
97+
"org.openqa.selenium.safari.SafariDriverService")
98+
.filter(name -> {
99+
try {
100+
Class.forName(name).asSubclass(DriverService.class);
101+
return true;
102+
} catch (ReflectiveOperationException e) {
103+
return false;
104+
}
105+
})
106+
.findFirst()
107+
.map(serviceName -> {
108+
SessionFactory factory = new ServicedSession.Factory(serviceName);
109+
return (SessionFactory) (dialects, caps) -> {
110+
LOG.info("Using default factory: " + serviceName);
111+
return factory.apply(dialects, caps);
112+
};
113+
})
114+
.orElse((dialects, caps) -> Optional.empty());
115+
116+
pipeline = NewSessionPipeline.builder()
117+
.add(new ActiveSessionFactory())
118+
.fallback(fallback)
119+
.create();
120+
getServletContext().setAttribute(NEW_SESSION_PIPELINE_KEY, pipeline);
121+
}
122+
123+
handlers = new AllHandlers(pipeline, allSessions);
85124
}
86125

87126
private synchronized Logger configureLogging() {

java/server/src/org/openqa/selenium/remote/server/commandhandler/BeginSession.java

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,17 @@
3131
import org.openqa.selenium.remote.http.HttpRequest;
3232
import org.openqa.selenium.remote.http.HttpResponse;
3333
import org.openqa.selenium.remote.server.ActiveSession;
34-
import org.openqa.selenium.remote.server.ActiveSessionFactory;
3534
import org.openqa.selenium.remote.server.ActiveSessions;
3635
import org.openqa.selenium.remote.server.CommandHandler;
3736
import org.openqa.selenium.remote.server.NewSessionPayload;
3837
import org.openqa.selenium.remote.server.NewSessionPipeline;
39-
import org.openqa.selenium.remote.server.ServicedSession;
40-
import org.openqa.selenium.remote.server.SessionFactory;
4138
import org.openqa.selenium.remote.server.log.LoggingManager;
42-
import org.openqa.selenium.remote.service.DriverService;
4339

4440
import java.io.IOException;
4541
import java.io.InputStreamReader;
4642
import java.io.Reader;
47-
import java.util.Optional;
4843
import java.util.logging.Level;
4944
import java.util.logging.Logger;
50-
import java.util.stream.Stream;
5145

5246
public class BeginSession implements CommandHandler {
5347

@@ -57,38 +51,10 @@ public class BeginSession implements CommandHandler {
5751
private final ActiveSessions allSessions;
5852
private final Json json;
5953

60-
public BeginSession(ActiveSessions allSessions, Json json) {
61-
this.json = json;
62-
63-
SessionFactory fallback = Stream.of(
64-
"org.openqa.selenium.chrome.ChromeDriverService",
65-
"org.openqa.selenium.firefox.GeckoDriverService",
66-
"org.openqa.selenium.edge.EdgeDriverService",
67-
"org.openqa.selenium.ie.InternetExplorerDriverService",
68-
"org.openqa.selenium.safari.SafariDriverService")
69-
.filter(name -> {
70-
try {
71-
Class.forName(name).asSubclass(DriverService.class);
72-
return true;
73-
} catch (ReflectiveOperationException e) {
74-
return false;
75-
}
76-
})
77-
.findFirst()
78-
.map(serviceName -> {
79-
SessionFactory factory = new ServicedSession.Factory(serviceName);
80-
return (SessionFactory) (dialects, caps) -> {
81-
LOG.info("Using default factory: " + serviceName);
82-
return factory.apply(dialects, caps);
83-
};
84-
})
85-
.orElse((dialects, caps) -> Optional.empty());
86-
54+
public BeginSession(NewSessionPipeline pipeline, ActiveSessions allSessions, Json json) {
55+
this.pipeline = pipeline;
8756
this.allSessions = allSessions;
88-
this.pipeline = NewSessionPipeline.builder()
89-
.add(new ActiveSessionFactory())
90-
.fallback(fallback)
91-
.create();
57+
this.json = json;
9258
}
9359

9460
@Override

java/server/test/org/openqa/selenium/remote/server/AllHandlersTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
import java.util.concurrent.TimeUnit;
3535

3636
public class AllHandlersTest {
37-
private AllHandlers allHandlers = new AllHandlers(new ActiveSessions(120, SECONDS));
37+
private AllHandlers allHandlers = new AllHandlers(
38+
NewSessionPipeline.builder().add(new ActiveSessionFactory()).create(),
39+
new ActiveSessions(120, SECONDS));
3840

3941
static class NoArgs implements CommandHandler {
4042
@Override

0 commit comments

Comments
 (0)