Skip to content

Commit 6a73b28

Browse files
Add getDistributor based on implementaion required (#8426)
* Add getDistributor based on implementaion required * Resolve deps for new getDistributor method * Update deps * Change variable class name * Add Remote Distributor as default class * Add getClass method * Remove unused imports * Update getClass method * Use getClass for EventBus implementation * Create RemoteDistributor inline for RouterServer Co-authored-by: David Burns <david.burns@theautomatedtester.co.uk>
1 parent ec4ce14 commit 6a73b28

13 files changed

Lines changed: 76 additions & 88 deletions

File tree

java/server/src/org/openqa/selenium/grid/config/Config.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.openqa.selenium.grid.config;
1919

20+
import java.lang.reflect.Method;
21+
import java.lang.reflect.Modifier;
2022
import java.util.List;
2123
import java.util.Optional;
2224
import java.util.Set;
@@ -40,4 +42,30 @@ default Optional<Integer> getInt(String section, String option) {
4042
default Optional<Boolean> getBool(String section, String option) {
4143
return get(section, option).map(Boolean::parseBoolean);
4244
}
45+
46+
default <X> Object getClass(String section, String option, Class<X> typeOfClass, String defaultClazz) {
47+
String clazz = get(section, option).orElse(defaultClazz);
48+
49+
try {
50+
Class<?> ClassClazz = Class.forName(clazz);
51+
Method create = ClassClazz.getMethod("create", Config.class);
52+
53+
if (!Modifier.isStatic(create.getModifiers())) {
54+
throw new IllegalArgumentException(String.format(
55+
"Class %s's `create(Config)` method must be static", clazz));
56+
}
57+
58+
if (!typeOfClass.isAssignableFrom(create.getReturnType())) {
59+
throw new IllegalArgumentException(String.format(
60+
"Class %s's `create(Config)` method must be static", clazz));
61+
}
62+
63+
return create.invoke(null, this);
64+
} catch (NoSuchMethodException e) {
65+
throw new IllegalArgumentException(String.format(
66+
"Class %s must have a static `create(Config)` method", clazz));
67+
} catch (ReflectiveOperationException e) {
68+
throw new IllegalArgumentException("Unable to find class: " + clazz, e);
69+
}
70+
}
4371
}

java/server/src/org/openqa/selenium/grid/distributor/config/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ java_library(
1111
"//java/client/src/org/openqa/selenium/remote",
1212
"//java/server/src/org/openqa/selenium/grid/config",
1313
"//java/server/src/org/openqa/selenium/grid/distributor",
14-
"//java/server/src/org/openqa/selenium/grid/distributor/remote",
1514
artifact("com.beust:jcommander"),
1615
],
1716
)

java/server/src/org/openqa/selenium/grid/distributor/config/DistributorOptions.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,16 @@
2020
import org.openqa.selenium.grid.config.Config;
2121
import org.openqa.selenium.grid.config.ConfigException;
2222
import org.openqa.selenium.grid.distributor.Distributor;
23-
import org.openqa.selenium.grid.distributor.remote.RemoteDistributor;
24-
import org.openqa.selenium.remote.http.HttpClient;
25-
import org.openqa.selenium.remote.tracing.Tracer;
2623

2724
import java.net.URI;
2825
import java.net.URISyntaxException;
29-
import java.net.URL;
3026
import java.util.Optional;
31-
32-
import static org.openqa.selenium.net.Urls.fromUri;
27+
import java.util.logging.Logger;
3328

3429
public class DistributorOptions {
3530

3631
private static final String DISTRIBUTOR_SECTION = "distributor";
32+
private static final Logger LOG = Logger.getLogger(DistributorOptions.class.getName());
3733

3834
private final Config config;
3935

@@ -78,11 +74,7 @@ public URI getDistributorUri() {
7874
}
7975
}
8076

81-
public Distributor getDistributor(Tracer tracer, HttpClient.Factory clientFactory) {
82-
URL distributorUrl = fromUri(getDistributorUri());
83-
return new RemoteDistributor(
84-
tracer,
85-
clientFactory,
86-
distributorUrl);
77+
public Distributor getDistributor(String defaultClass) {
78+
return (Distributor) config.getClass(DISTRIBUTOR_SECTION, "implementation", Distributor.class, defaultClass);
8779
}
8880
}

java/server/src/org/openqa/selenium/grid/distributor/httpd/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ java_library(
2323
"//java/server/src/org/openqa/selenium/grid/sessionmap/config",
2424
"//java/server/src/org/openqa/selenium/grid/web",
2525
"//java/server/src/org/openqa/selenium/netty/server",
26+
"//java/server/src/org/openqa/selenium/grid/distributor/config",
2627
artifact("com.beust:jcommander"),
2728
artifact("com.google.guava:guava"),
2829
],

java/server/src/org/openqa/selenium/grid/distributor/httpd/DistributorServer.java

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,19 @@
2323
import com.google.common.net.MediaType;
2424
import org.openqa.selenium.BuildInfo;
2525
import org.openqa.selenium.cli.CliCommand;
26-
import org.openqa.selenium.events.EventBus;
2726
import org.openqa.selenium.grid.TemplateGridCommand;
2827
import org.openqa.selenium.grid.config.Config;
2928
import org.openqa.selenium.grid.config.Role;
3029
import org.openqa.selenium.grid.data.DistributorStatus;
3130
import org.openqa.selenium.grid.distributor.Distributor;
32-
import org.openqa.selenium.grid.distributor.local.LocalDistributor;
33-
import org.openqa.selenium.grid.log.LoggingOptions;
31+
import org.openqa.selenium.grid.distributor.config.DistributorOptions;
3432
import org.openqa.selenium.grid.server.BaseServerOptions;
35-
import org.openqa.selenium.grid.server.EventBusOptions;
36-
import org.openqa.selenium.grid.server.NetworkOptions;
3733
import org.openqa.selenium.grid.server.Server;
38-
import org.openqa.selenium.grid.sessionmap.SessionMap;
39-
import org.openqa.selenium.grid.sessionmap.config.SessionMapOptions;
4034
import org.openqa.selenium.netty.server.NettyServer;
4135
import org.openqa.selenium.remote.http.Contents;
42-
import org.openqa.selenium.remote.http.HttpClient;
4336
import org.openqa.selenium.remote.http.HttpHandler;
4437
import org.openqa.selenium.remote.http.HttpResponse;
4538
import org.openqa.selenium.remote.http.Route;
46-
import org.openqa.selenium.remote.tracing.Tracer;
4739

4840
import java.util.Collections;
4941
import java.util.Set;
@@ -61,6 +53,7 @@
6153
public class DistributorServer extends TemplateGridCommand {
6254

6355
private static final Logger LOG = Logger.getLogger(DistributorServer.class.getName());
56+
private static final String LOCAL_DISTRIBUTOR_SERVER = "org.openqa.selenium.grid.distributor.local.LocalDistributor";
6457

6558
@Override
6659
public String getName() {
@@ -94,25 +87,11 @@ protected Config getDefaultConfig() {
9487

9588
@Override
9689
protected void execute(Config config) {
97-
LoggingOptions loggingOptions = new LoggingOptions(config);
98-
Tracer tracer = loggingOptions.getTracer();
99-
100-
EventBusOptions events = new EventBusOptions(config);
101-
EventBus bus = events.getEventBus();
102-
103-
NetworkOptions networkOptions = new NetworkOptions(config);
104-
HttpClient.Factory clientFactory = networkOptions.getHttpClientFactory(tracer);
105-
106-
SessionMap sessions = new SessionMapOptions(config).getSessionMap();
107-
10890
BaseServerOptions serverOptions = new BaseServerOptions(config);
91+
DistributorOptions distributorOptions = new DistributorOptions(config);
92+
93+
Distributor distributor = distributorOptions.getDistributor(LOCAL_DISTRIBUTOR_SERVER);
10994

110-
Distributor distributor = new LocalDistributor(
111-
tracer,
112-
bus,
113-
clientFactory,
114-
sessions,
115-
serverOptions.getRegistrationSecret());
11695
HttpHandler readinessCheck = req -> {
11796
DistributorStatus status = distributor.getStatus();
11897
if (status.hasCapacity()) {

java/server/src/org/openqa/selenium/grid/distributor/local/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ java_library(
1919
"//java/server/src/org/openqa/selenium/grid/node",
2020
"//java/server/src/org/openqa/selenium/grid/node/remote",
2121
"//java/server/src/org/openqa/selenium/grid/sessionmap",
22+
"//java/server/src/org/openqa/selenium/grid/config",
23+
"//java/server/src/org/openqa/selenium/grid/log",
24+
"//java/server/src/org/openqa/selenium/grid/server",
25+
"//java/server/src/org/openqa/selenium/grid/sessionmap/config",
26+
2227
artifact("com.google.guava:guava"),
2328
],
2429
)

java/server/src/org/openqa/selenium/grid/distributor/local/LocalDistributor.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.openqa.selenium.SessionNotCreatedException;
2626
import org.openqa.selenium.concurrent.Regularly;
2727
import org.openqa.selenium.events.EventBus;
28+
import org.openqa.selenium.grid.config.Config;
2829
import org.openqa.selenium.grid.data.CreateSessionRequest;
2930
import org.openqa.selenium.grid.data.CreateSessionResponse;
3031
import org.openqa.selenium.grid.data.DistributorStatus;
@@ -33,9 +34,14 @@
3334
import org.openqa.selenium.grid.data.NodeRemovedEvent;
3435
import org.openqa.selenium.grid.data.NodeStatus;
3536
import org.openqa.selenium.grid.distributor.Distributor;
37+
import org.openqa.selenium.grid.log.LoggingOptions;
3638
import org.openqa.selenium.grid.node.Node;
3739
import org.openqa.selenium.grid.node.remote.RemoteNode;
40+
import org.openqa.selenium.grid.server.BaseServerOptions;
41+
import org.openqa.selenium.grid.server.EventBusOptions;
42+
import org.openqa.selenium.grid.server.NetworkOptions;
3843
import org.openqa.selenium.grid.sessionmap.SessionMap;
44+
import org.openqa.selenium.grid.sessionmap.config.SessionMapOptions;
3945
import org.openqa.selenium.internal.Require;
4046
import org.openqa.selenium.json.Json;
4147
import org.openqa.selenium.json.JsonOutput;
@@ -109,6 +115,16 @@ public LocalDistributor(
109115
bus.addListener(NODE_STATUS, event -> refresh(event.getData(NodeStatus.class)));
110116
}
111117

118+
public static Distributor create(Config config) {
119+
Tracer tracer = new LoggingOptions(config).getTracer();
120+
EventBus bus = new EventBusOptions(config).getEventBus();
121+
HttpClient.Factory clientFactory = new NetworkOptions(config).getHttpClientFactory(tracer);
122+
SessionMap sessions = new SessionMapOptions(config).getSessionMap();
123+
BaseServerOptions serverOptions = new BaseServerOptions(config);
124+
125+
return new LocalDistributor(tracer, bus, clientFactory, sessions, serverOptions.getRegistrationSecret());
126+
}
127+
112128
@Override
113129
public CreateSessionResponse newSession(HttpRequest request)
114130
throws SessionNotCreatedException {

java/server/src/org/openqa/selenium/grid/distributor/remote/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@ java_library(
1414
"//java/server/src/org/openqa/selenium/grid/distributor",
1515
"//java/server/src/org/openqa/selenium/grid/node",
1616
"//java/server/src/org/openqa/selenium/grid/web",
17+
"//java/server/src/org/openqa/selenium/grid/config",
18+
"//java/server/src/org/openqa/selenium/grid/log",
19+
"//java/server/src/org/openqa/selenium/grid/server"
1720
],
1821
)

java/server/src/org/openqa/selenium/grid/distributor/remote/RemoteDistributor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.UUID;
3636
import java.util.logging.Logger;
3737

38+
import static org.openqa.selenium.net.Urls.fromUri;
3839
import static org.openqa.selenium.remote.http.Contents.asJson;
3940
import static org.openqa.selenium.remote.http.HttpMethod.DELETE;
4041
import static org.openqa.selenium.remote.http.HttpMethod.GET;

java/server/src/org/openqa/selenium/grid/router/httpd/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ java_library(
1616
"//java/server/src/org/openqa/selenium/grid/data",
1717
"//java/server/src/org/openqa/selenium/grid/distributor",
1818
"//java/server/src/org/openqa/selenium/grid/distributor/config",
19+
"//java/server/src/org/openqa/selenium/grid/distributor/remote",
1920
"//java/server/src/org/openqa/selenium/grid/graphql",
2021
"//java/server/src/org/openqa/selenium/grid/log",
2122
"//java/server/src/org/openqa/selenium/grid/node",

0 commit comments

Comments
 (0)