Skip to content

Commit

Permalink
[java] Replacing reflection-based injector by pure java8-based implem…
Browse files Browse the repository at this point in the history
…entation
  • Loading branch information
barancev committed Apr 4, 2019
1 parent e06c592 commit c4c41fb
Show file tree
Hide file tree
Showing 37 changed files with 141 additions and 690 deletions.
2 changes: 1 addition & 1 deletion java/server/src/org/openqa/selenium/grid/commands/Hub.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public Executable configure(String... args) {

Server<?> server = new BaseServer<>(
serverOptions);
server.addRoute(Routes.matching(router).using(router).decorateWith(W3CCommandHandler.class));
server.addRoute(Routes.matching(router).using(router).decorateWith(W3CCommandHandler::new));
server.start();
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public Executable configure(String... args) {
distributor.add(node);

Server<?> server = new BaseServer<>(new BaseServerOptions(config));
server.addRoute(Routes.matching(router).using(router).decorateWith(W3CCommandHandler.class));
server.addRoute(Routes.matching(router).using(router).decorateWith(W3CCommandHandler::new));
server.start();
};
}
Expand Down
1 change: 0 additions & 1 deletion java/server/src/org/openqa/selenium/grid/distributor/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ java_library(
"//java/server/src/org/openqa/selenium/grid/node/remote:remote",
"//java/server/src/org/openqa/selenium/grid/server:server",
"//java/server/src/org/openqa/selenium/grid/web:web",
"//java/server/src/org/openqa/selenium/injector:injector",
"//third_party/java/guava:guava",
],
visibility = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,14 @@
import org.openqa.selenium.grid.web.CommandHandler;
import org.openqa.selenium.grid.web.HandlerNotFoundException;
import org.openqa.selenium.grid.web.Routes;
import org.openqa.selenium.injector.Injector;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.remote.NewSessionPayload;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;
import org.openqa.selenium.remote.tracing.DistributedTracer;

import java.io.IOException;
import java.io.Reader;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
Expand Down Expand Up @@ -78,29 +76,27 @@
public abstract class Distributor implements Predicate<HttpRequest>, CommandHandler {

private final Routes routes;
private final Injector injector;

protected Distributor(DistributedTracer tracer, HttpClient.Factory httpClientFactory) {
Objects.requireNonNull(tracer);
Objects.requireNonNull(httpClientFactory);

injector = Injector.builder()
.register(this)
.register(tracer)
.register(new Json())
.register(httpClientFactory)
.build();

Json json = new Json();
routes = Routes.combine(
post("/session").using((req, res) -> {
CreateSessionResponse sessionResponse = newSession(req);
res.setContent(sessionResponse.getDownstreamEncodedResponse());
}),
post("/se/grid/distributor/session").using(CreateSession.class),
post("/se/grid/distributor/node").using(AddNode.class),
delete("/se/grid/distributor/node/{nodeId}").using(RemoveNode.class)
post("/se/grid/distributor/session")
.using(() -> new CreateSession(json, this)),
post("/se/grid/distributor/node")
.using(() -> new AddNode(tracer, this, json, httpClientFactory)),
delete("/se/grid/distributor/node/{nodeId}")
.using((Map<String,String> params) -> new RemoveNode(this, UUID.fromString(params.get("nodeId"))))
.map("nodeId", UUID::fromString),
get("/se/grid/distributor/status").using(GetDistributorStatus.class)).build();
get("/se/grid/distributor/status")
.using(() -> new GetDistributorStatus(json, this)))
.build();
}

public abstract CreateSessionResponse newSession(HttpRequest request)
Expand All @@ -114,12 +110,12 @@ public abstract CreateSessionResponse newSession(HttpRequest request)

@Override
public boolean test(HttpRequest req) {
return routes.match(injector, req).isPresent();
return routes.match(req).isPresent();
}

@Override
public void execute(HttpRequest req, HttpResponse resp) throws IOException {
Optional<CommandHandler> handler = routes.match(injector, req);
Optional<CommandHandler> handler = routes.match(req);
if (!handler.isPresent()) {
throw new HandlerNotFoundException(req);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ public Executable configure(String... args) {

Server<?> server = new BaseServer<>(serverOptions);
server.addRoute(
Routes.matching(distributor)
.using(distributor)
.decorateWith(W3CCommandHandler.class));
Routes.matching(distributor).using(distributor).decorateWith(W3CCommandHandler::new));
server.start();
};
}
Expand Down
1 change: 0 additions & 1 deletion java/server/src/org/openqa/selenium/grid/module-info.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ module org.openqa.selenium.grid {
exports org.openqa.selenium.grid.sessionmap.local;
exports org.openqa.selenium.grid.sessionmap.remote;
exports org.openqa.selenium.grid.web;
exports org.openqa.selenium.injector;
exports org.openqa.selenium.remote.tracing;

provides org.openqa.selenium.cli.CliCommand with
Expand Down
1 change: 0 additions & 1 deletion java/server/src/org/openqa/selenium/grid/node/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ java_library(
"//java/server/src/org/openqa/selenium/grid/server:server",
"//java/server/src/org/openqa/selenium/grid/sessionmap:sessionmap",
"//java/server/src/org/openqa/selenium/grid/web:web",
"//java/server/src/org/openqa/selenium/injector:injector",
"//third_party/java/guava:guava",
],
visibility = [
Expand Down
34 changes: 12 additions & 22 deletions java/server/src/org/openqa/selenium/grid/node/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.openqa.selenium.grid.web.CommandHandler;
import org.openqa.selenium.grid.web.HandlerNotFoundException;
import org.openqa.selenium.grid.web.Routes;
import org.openqa.selenium.injector.Injector;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.remote.SessionId;
import org.openqa.selenium.remote.http.HttpRequest;
Expand Down Expand Up @@ -101,7 +100,6 @@ public abstract class Node implements Predicate<HttpRequest>, CommandHandler {
protected final DistributedTracer tracer;
private final UUID id;
private final URI uri;
private final Injector injector;
private final Routes routes;

protected Node(DistributedTracer tracer, UUID id, URI uri) {
Expand All @@ -110,27 +108,19 @@ protected Node(DistributedTracer tracer, UUID id, URI uri) {
this.uri = Objects.requireNonNull(uri);

Json json = new Json();
injector = Injector.builder()
.register(this)
.register(json)
.register(tracer)
.build();

routes = combine(
get("/se/grid/node/owner/{sessionId}").using(IsSessionOwner.class)
.map("sessionId", SessionId::new),
delete("/se/grid/node/session/{sessionId}").using(StopNodeSession.class)
.map("sessionId", SessionId::new),
get("/se/grid/node/session/{sessionId}").using(GetNodeSession.class)
.map("sessionId", SessionId::new),
post("/se/grid/node/session").using(NewNodeSession.class),
get("/se/grid/node/owner/{sessionId}")
.using((params) -> new IsSessionOwner(this, json, new SessionId(params.get("sessionId")))),
delete("/se/grid/node/session/{sessionId}")
.using((params) -> new StopNodeSession(this, new SessionId(params.get("sessionId")))),
get("/se/grid/node/session/{sessionId}")
.using((params) -> new GetNodeSession(this, json, new SessionId(params.get("sessionId")))),
post("/se/grid/node/session").using(() -> new NewNodeSession(this, json)),
get("/se/grid/node/status")
.using((req, res) -> {
res.setContent(json.toJson(getStatus()).getBytes(UTF_8));
}),
get("/status").using(StatusHandler.class),
.using((req, res) -> res.setContent(json.toJson(getStatus()).getBytes(UTF_8))),
get("/status").using(() -> new StatusHandler(this, json)),
matching(req -> getSessionId(req).map(this::isSessionOwner).orElse(false))
.using(ForwardWebDriverCommand.class)
.using(() -> new ForwardWebDriverCommand(this))
).build();
}

Expand Down Expand Up @@ -160,12 +150,12 @@ public URI getUri() {

@Override
public boolean test(HttpRequest req) {
return routes.match(injector, req).isPresent();
return routes.match(req).isPresent();
}

@Override
public void execute(HttpRequest req, HttpResponse resp) throws IOException {
Optional<CommandHandler> handler = routes.match(injector, req);
Optional<CommandHandler> handler = routes.match(req);
if (!handler.isPresent()) {
throw new HandlerNotFoundException(req);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public Executable configure(String... args) {
LocalNode node = builder.build();

Server<?> server = new BaseServer<>(serverOptions);
server.addRoute(Routes.matching(node).using(node).decorateWith(W3CCommandHandler.class));
server.addRoute(Routes.matching(node).using(node).decorateWith(W3CCommandHandler::new));
server.start();

Regularly regularly = new Regularly("Register Node with Distributor");
Expand Down
1 change: 0 additions & 1 deletion java/server/src/org/openqa/selenium/grid/router/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ java_library(
srcs = glob(["*.java"]),
deps = [
"//java/client/src/org/openqa/selenium/remote/tracing:tracing",
"//java/server/src/org/openqa/selenium/injector:injector",
"//java/server/src/org/openqa/selenium/grid/distributor:distributor",
"//java/server/src/org/openqa/selenium/grid/sessionmap:sessionmap",
"//java/server/src/org/openqa/selenium/grid/server:server",
Expand Down
24 changes: 9 additions & 15 deletions java/server/src/org/openqa/selenium/grid/router/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.openqa.selenium.grid.web.CommandHandler;
import org.openqa.selenium.grid.web.HandlerNotFoundException;
import org.openqa.selenium.grid.web.Routes;
import org.openqa.selenium.injector.Injector;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpRequest;
Expand All @@ -43,38 +42,33 @@
*/
public class Router implements Predicate<HttpRequest>, CommandHandler {

private final Injector injector;
private final Routes routes;

public Router(
DistributedTracer tracer,
HttpClient.Factory clientFactory,
SessionMap sessions,
Distributor distributor) {
injector = Injector.builder()
.register(tracer)
.register(clientFactory)
.register(sessions)
.register(distributor)
.register(new Json())
.build();

Distributor distributor)
{
routes = combine(
get("/status").using(GridStatusHandler.class).decorateWith(W3CCommandHandler.class),
get("/status")
.using(() -> new GridStatusHandler(new Json(), clientFactory, distributor))
.decorateWith(W3CCommandHandler::new),
matching(sessions).using(sessions),
matching(distributor).using(distributor),
matching(req -> req.getUri().startsWith("/session/")).using(HandleSession.class))
matching(req -> req.getUri().startsWith("/session/"))
.using(() -> new HandleSession(tracer, clientFactory, sessions)))
.build();
}

@Override
public boolean test(HttpRequest req) {
return routes.match(injector, req).isPresent();
return routes.match(req).isPresent();
}

@Override
public void execute(HttpRequest req, HttpResponse resp) throws IOException {
Optional<CommandHandler> handler = routes.match(injector, req);
Optional<CommandHandler> handler = routes.match(req);
if (!handler.isPresent()) {
throw new HandlerNotFoundException(req);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public Executable configure(String... args) {
Router router = new Router(tracer, clientFactory, sessions, distributor);

Server<?> server = new BaseServer<>(serverOptions);
server.addRoute(Routes.matching(router).using(router).decorateWith(W3CCommandHandler.class));
server.addRoute(Routes.matching(router).using(router).decorateWith(W3CCommandHandler::new));
server.start();
};
}
Expand Down
1 change: 0 additions & 1 deletion java/server/src/org/openqa/selenium/grid/server/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ java_library(
"//java/client/src/org/openqa/selenium/remote:remote",
"//java/client/src/org/openqa/selenium/remote/tracing:tracing",
"//java/server/src/org/openqa/selenium/events/zeromq:zeromq",
"//java/server/src/org/openqa/selenium/injector:injector",
"//java/server/src/org/openqa/selenium/grid/config:config",
"//third_party/java/beust:jcommander",
"//third_party/java/guava:guava",
Expand Down
19 changes: 1 addition & 18 deletions java/server/src/org/openqa/selenium/grid/server/BaseServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,10 @@
import com.google.common.net.MediaType;

import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.grid.web.CommandHandler;
import org.openqa.selenium.grid.web.Routes;
import org.openqa.selenium.injector.Injector;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.net.NetworkUtils;
import org.openqa.selenium.net.PortProber;
import org.openqa.selenium.remote.http.HttpRequest;
import org.seleniumhq.jetty9.security.ConstraintMapping;
import org.seleniumhq.jetty9.security.ConstraintSecurityHandler;
import org.seleniumhq.jetty9.server.Connector;
Expand All @@ -50,12 +47,8 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Predicate;
import java.util.logging.Logger;

import javax.servlet.Servlet;
Expand All @@ -66,11 +59,8 @@ public class BaseServer<T extends BaseServer> implements Server<T> {
private static final int MAX_SHUTDOWN_RETRIES = 8;

private final org.seleniumhq.jetty9.server.Server server;
private final Map<Predicate<HttpRequest>, BiFunction<Injector, HttpRequest, CommandHandler>>
handlers;
private final List<Routes> routes = new ArrayList<>();
private final ServletContextHandler servletContextHandler;
private final Injector injector;
private final URL url;

public BaseServer(BaseServerOptions options) {
Expand All @@ -94,14 +84,7 @@ public BaseServer(BaseServerOptions options) {
this.server = new org.seleniumhq.jetty9.server.Server(
new QueuedThreadPool(options.getMaxServerThreads()));

// Insertion order may matter
this.handlers = new LinkedHashMap<>();

Json json = new Json();
this.injector = Injector.builder()
.register(json)
.build();

addRoute(
Routes.get("/status").using(
(in, out) -> {
Expand Down Expand Up @@ -198,7 +181,7 @@ public T start() {
}
Routes first = routes.remove(0);
Routes routes = Routes.combine(first, this.routes.toArray(new Routes[0]))
.decorateWith(W3CCommandHandler.class)
.decorateWith(W3CCommandHandler::new)
.build();

addServlet(new CommandHandlerServlet(routes), "/*");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import org.openqa.selenium.UnsupportedCommandException;
import org.openqa.selenium.grid.web.CommandHandler;
import org.openqa.selenium.grid.web.Routes;
import org.openqa.selenium.injector.Injector;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;

Expand All @@ -38,7 +36,6 @@
class CommandHandlerServlet extends HttpServlet {

private final Routes routes;
private final Injector injector;

public CommandHandlerServlet(Routes routes) {
Objects.requireNonNull(routes);
Expand All @@ -49,15 +46,14 @@ public CommandHandlerServlet(Routes routes) {
throw new UnsupportedCommandException(String.format(
"Unknown command: (%s) %s", req.getMethod(), req.getUri()));
})).build();
this.injector = Injector.builder().register(new Json()).build();
}

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
HttpRequest request = new ServletRequestWrappingHttpRequest(req);
HttpResponse response = new ServletResponseWrappingHttpResponse(resp);

Optional<CommandHandler> possibleMatch = routes.match(injector, request);
Optional<CommandHandler> possibleMatch = routes.match(request);
if (possibleMatch.isPresent()) {
possibleMatch.get().execute(request, response);
} else {
Expand Down
1 change: 0 additions & 1 deletion java/server/src/org/openqa/selenium/grid/sessionmap/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ java_library(
"//java/server/src/org/openqa/selenium/grid/config:config",
"//java/server/src/org/openqa/selenium/grid/data:data",
"//java/server/src/org/openqa/selenium/grid/web:web",
"//java/server/src/org/openqa/selenium/injector:injector",
"//third_party/java/guava:guava",
],
visibility = [
Expand Down

0 comments on commit c4c41fb

Please sign in to comment.