Skip to content

Commit

Permalink
[grid] Abstract away HttpClient.Factory creation
Browse files Browse the repository at this point in the history
This provides a single hook point to use when adding things like
tracing.
  • Loading branch information
shs96c committed Jan 16, 2020
1 parent 711217d commit 9c24b1e
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 27 deletions.
13 changes: 6 additions & 7 deletions java/server/src/org/openqa/selenium/grid/commands/Hub.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@

package org.openqa.selenium.grid.commands;

import com.google.auto.service.AutoService;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.ParameterException;

import com.google.auto.service.AutoService;
import io.opentracing.Tracer;
import org.openqa.selenium.BuildInfo;
import org.openqa.selenium.cli.CliCommand;
import org.openqa.selenium.events.EventBus;
Expand All @@ -36,9 +35,10 @@
import org.openqa.selenium.grid.router.Router;
import org.openqa.selenium.grid.server.BaseServerFlags;
import org.openqa.selenium.grid.server.BaseServerOptions;
import org.openqa.selenium.grid.server.EventBusOptions;
import org.openqa.selenium.grid.server.EventBusFlags;
import org.openqa.selenium.grid.server.EventBusOptions;
import org.openqa.selenium.grid.server.HelpFlags;
import org.openqa.selenium.grid.server.NetworkOptions;
import org.openqa.selenium.grid.server.Server;
import org.openqa.selenium.grid.sessionmap.SessionMap;
import org.openqa.selenium.grid.sessionmap.local.LocalSessionMap;
Expand All @@ -47,8 +47,6 @@
import org.openqa.selenium.netty.server.NettyServer;
import org.openqa.selenium.remote.http.HttpClient;

import io.opentracing.Tracer;

import java.util.logging.Logger;

@AutoService(CliCommand.class)
Expand Down Expand Up @@ -114,10 +112,11 @@ public Executable configure(String... args) {

BaseServerOptions serverOptions = new BaseServerOptions(config);

NetworkOptions networkOptions = new NetworkOptions(config);
HttpClient.Factory clientFactory = new RoutableHttpClientFactory(
serverOptions.getExternalUri().toURL(),
handler,
HttpClient.Factory.createDefault());
networkOptions.getHttpClientFactory());

Distributor distributor = new LocalDistributor(
tracer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@
import org.openqa.selenium.grid.router.Router;
import org.openqa.selenium.grid.server.BaseServerFlags;
import org.openqa.selenium.grid.server.BaseServerOptions;
import org.openqa.selenium.grid.server.EventBusOptions;
import org.openqa.selenium.grid.server.EventBusFlags;
import org.openqa.selenium.grid.server.EventBusOptions;
import org.openqa.selenium.grid.server.HelpFlags;
import org.openqa.selenium.grid.server.NetworkOptions;
import org.openqa.selenium.grid.server.Server;
import org.openqa.selenium.grid.sessionmap.SessionMap;
import org.openqa.selenium.grid.sessionmap.local.LocalSessionMap;
Expand All @@ -52,7 +53,6 @@
import org.openqa.selenium.net.NetworkUtils;
import org.openqa.selenium.netty.server.NettyServer;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.tracing.TracedHttpClient;

import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -136,13 +136,12 @@ public Executable configure(String... args) {
throw new RuntimeException(e);
}

NetworkOptions networkOptions = new NetworkOptions(config);
CombinedHandler combinedHandler = new CombinedHandler();
HttpClient.Factory clientFactory = new TracedHttpClient.Factory(
tracer,
new RoutableHttpClientFactory(
HttpClient.Factory clientFactory = new RoutableHttpClientFactory(
localhost.toURL(),
combinedHandler,
HttpClient.Factory.createDefault()));
networkOptions.getHttpClientFactory());

SessionMap sessions = new LocalSessionMap(tracer, bus);
combinedHandler.addHandler(sessions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@
import org.openqa.selenium.grid.log.LoggingOptions;
import org.openqa.selenium.grid.server.BaseServerFlags;
import org.openqa.selenium.grid.server.BaseServerOptions;
import org.openqa.selenium.grid.server.EventBusOptions;
import org.openqa.selenium.grid.server.EventBusFlags;
import org.openqa.selenium.grid.server.EventBusOptions;
import org.openqa.selenium.grid.server.HelpFlags;
import org.openqa.selenium.grid.server.NetworkOptions;
import org.openqa.selenium.grid.server.Server;
import org.openqa.selenium.grid.sessionmap.SessionMap;
import org.openqa.selenium.grid.sessionmap.config.SessionMapFlags;
import org.openqa.selenium.grid.sessionmap.config.SessionMapOptions;
import org.openqa.selenium.netty.server.NettyServer;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.tracing.TracedHttpClient;

import java.util.logging.Logger;

Expand Down Expand Up @@ -109,7 +109,8 @@ public Executable configure(String... args) {
EventBusOptions events = new EventBusOptions(config);
EventBus bus = events.getEventBus();

HttpClient.Factory clientFactory = new TracedHttpClient.Factory(tracer, HttpClient.Factory.createDefault());
NetworkOptions networkOptions = new NetworkOptions(config);
HttpClient.Factory clientFactory = networkOptions.getHttpClientFactory();

SessionMap sessions = new SessionMapOptions(config).getSessionMap(tracer, clientFactory);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@

package org.openqa.selenium.grid.docker;

import static org.openqa.selenium.docker.ContainerInfo.image;
import static org.openqa.selenium.remote.Dialect.W3C;
import static org.openqa.selenium.remote.http.Contents.string;
import static org.openqa.selenium.remote.http.HttpMethod.GET;

import io.opentracing.Tracer;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
Expand Down Expand Up @@ -58,6 +53,11 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import static org.openqa.selenium.docker.ContainerInfo.image;
import static org.openqa.selenium.remote.Dialect.W3C;
import static org.openqa.selenium.remote.http.Contents.string;
import static org.openqa.selenium.remote.http.HttpMethod.GET;

public class DockerSessionFactory implements SessionFactory {

private static final Logger LOG = Logger.getLogger(DockerSessionFactory.class.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@
import org.openqa.selenium.grid.node.local.LocalNode;
import org.openqa.selenium.grid.server.BaseServerFlags;
import org.openqa.selenium.grid.server.BaseServerOptions;
import org.openqa.selenium.grid.server.EventBusOptions;
import org.openqa.selenium.grid.server.EventBusFlags;
import org.openqa.selenium.grid.server.EventBusOptions;
import org.openqa.selenium.grid.server.HelpFlags;
import org.openqa.selenium.grid.server.NetworkOptions;
import org.openqa.selenium.grid.server.Server;
import org.openqa.selenium.netty.server.NettyServer;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.tracing.TracedHttpClient;

import java.time.Duration;
import java.util.logging.Logger;
Expand Down Expand Up @@ -113,7 +113,8 @@ public Executable configure(String... args) {
EventBusOptions events = new EventBusOptions(config);
EventBus bus = events.getEventBus();

HttpClient.Factory clientFactory = new TracedHttpClient.Factory(tracer, HttpClient.Factory.createDefault());
NetworkOptions networkOptions = new NetworkOptions(config);
HttpClient.Factory clientFactory = networkOptions.getHttpClientFactory();

BaseServerOptions serverOptions = new BaseServerOptions(config);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
import org.openqa.selenium.grid.server.BaseServerFlags;
import org.openqa.selenium.grid.server.BaseServerOptions;
import org.openqa.selenium.grid.server.HelpFlags;
import org.openqa.selenium.grid.server.NetworkOptions;
import org.openqa.selenium.grid.server.Server;
import org.openqa.selenium.grid.sessionmap.SessionMap;
import org.openqa.selenium.grid.sessionmap.config.SessionMapFlags;
import org.openqa.selenium.grid.sessionmap.config.SessionMapOptions;
import org.openqa.selenium.netty.server.NettyServer;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.tracing.TracedHttpClient;

import java.util.logging.Logger;

Expand Down Expand Up @@ -102,7 +102,8 @@ public Executable configure(String... args) {
loggingOptions.configureLogging();
Tracer tracer = loggingOptions.getTracer();

HttpClient.Factory clientFactory = new TracedHttpClient.Factory(tracer, HttpClient.Factory.createDefault());
NetworkOptions networkOptions = new NetworkOptions(config);
HttpClient.Factory clientFactory = networkOptions.getHttpClientFactory();

SessionMapOptions sessionsOptions = new SessionMapOptions(config);
SessionMap sessions = sessionsOptions.getSessionMap(tracer, clientFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ java_library(
"//java/server/src/org/openqa/selenium/events",
"//java/server/src/org/openqa/selenium/grid/component",
"//java/server/src/org/openqa/selenium/grid/config",
"//java/server/src/org/openqa/selenium/grid/log",
"//java/server/src/org/openqa/selenium/grid/web",
artifact("com.beust:jcommander"),
artifact("com.google.guava:guava"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.grid.server;

import io.opentracing.Tracer;
import org.openqa.selenium.grid.config.Config;
import org.openqa.selenium.grid.log.LoggingOptions;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.tracing.TracedHttpClient;

import java.util.Objects;

public class NetworkOptions {

private final Config config;

public NetworkOptions(Config config) {
this.config = Objects.requireNonNull(config, "Config to use must be set.");
}

public HttpClient.Factory getHttpClientFactory() {
Tracer tracer = new LoggingOptions(config).getTracer();
return new TracedHttpClient.Factory(tracer, HttpClient.Factory.createDefault());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
import org.openqa.selenium.grid.log.LoggingOptions;
import org.openqa.selenium.grid.server.BaseServerFlags;
import org.openqa.selenium.grid.server.BaseServerOptions;
import org.openqa.selenium.grid.server.EventBusOptions;
import org.openqa.selenium.grid.server.EventBusFlags;
import org.openqa.selenium.grid.server.EventBusOptions;
import org.openqa.selenium.grid.server.HelpFlags;
import org.openqa.selenium.grid.server.Server;
import org.openqa.selenium.grid.sessionmap.SessionMap;
Expand Down

0 comments on commit 9c24b1e

Please sign in to comment.