Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code refactor #8426

Merged
merged 11 commits into from
Jun 16, 2020
28 changes: 28 additions & 0 deletions java/server/src/org/openqa/selenium/grid/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.openqa.selenium.grid.config;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand All @@ -40,4 +42,30 @@ default Optional<Integer> getInt(String section, String option) {
default Optional<Boolean> getBool(String section, String option) {
return get(section, option).map(Boolean::parseBoolean);
}

default <X> Object getClass(String section, String option, Class<X> typeOfClass, String defaultClazz) {
String clazz = get(section, option).orElse(defaultClazz);

try {
Class<?> ClassClazz = Class.forName(clazz);
Method create = ClassClazz.getMethod("create", Config.class);

if (!Modifier.isStatic(create.getModifiers())) {
throw new IllegalArgumentException(String.format(
"Class %s's `create(Config)` method must be static", clazz));
}

if (!typeOfClass.isAssignableFrom(create.getReturnType())) {
throw new IllegalArgumentException(String.format(
"Class %s's `create(Config)` method must be static", clazz));
}

return create.invoke(null, this);
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException(String.format(
"Class %s must have a static `create(Config)` method", clazz));
} catch (ReflectiveOperationException e) {
throw new IllegalArgumentException("Unable to find class: " + clazz, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ java_library(
"//java/client/src/org/openqa/selenium/remote",
"//java/server/src/org/openqa/selenium/grid/config",
"//java/server/src/org/openqa/selenium/grid/distributor",
"//java/server/src/org/openqa/selenium/grid/distributor/remote",
artifact("com.beust:jcommander"),
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,16 @@
import org.openqa.selenium.grid.config.Config;
import org.openqa.selenium.grid.config.ConfigException;
import org.openqa.selenium.grid.distributor.Distributor;
import org.openqa.selenium.grid.distributor.remote.RemoteDistributor;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.tracing.Tracer;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Optional;

import static org.openqa.selenium.net.Urls.fromUri;
import java.util.logging.Logger;

public class DistributorOptions {

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

private final Config config;

Expand Down Expand Up @@ -78,11 +74,7 @@ public URI getDistributorUri() {
}
}

public Distributor getDistributor(Tracer tracer, HttpClient.Factory clientFactory) {
URL distributorUrl = fromUri(getDistributorUri());
return new RemoteDistributor(
tracer,
clientFactory,
distributorUrl);
public Distributor getDistributor(String defaultClass) {
return (Distributor) config.getClass(DISTRIBUTOR_SECTION, "implementation", Distributor.class, defaultClass);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ java_library(
"//java/server/src/org/openqa/selenium/grid/sessionmap/config",
"//java/server/src/org/openqa/selenium/grid/web",
"//java/server/src/org/openqa/selenium/netty/server",
"//java/server/src/org/openqa/selenium/grid/distributor/config",
artifact("com.beust:jcommander"),
artifact("com.google.guava:guava"),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,19 @@
import com.google.common.net.MediaType;
import org.openqa.selenium.BuildInfo;
import org.openqa.selenium.cli.CliCommand;
import org.openqa.selenium.events.EventBus;
import org.openqa.selenium.grid.TemplateGridCommand;
import org.openqa.selenium.grid.config.Config;
import org.openqa.selenium.grid.config.Role;
import org.openqa.selenium.grid.data.DistributorStatus;
import org.openqa.selenium.grid.distributor.Distributor;
import org.openqa.selenium.grid.distributor.local.LocalDistributor;
import org.openqa.selenium.grid.log.LoggingOptions;
import org.openqa.selenium.grid.distributor.config.DistributorOptions;
import org.openqa.selenium.grid.server.BaseServerOptions;
import org.openqa.selenium.grid.server.EventBusOptions;
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.SessionMapOptions;
import org.openqa.selenium.netty.server.NettyServer;
import org.openqa.selenium.remote.http.Contents;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpHandler;
import org.openqa.selenium.remote.http.HttpResponse;
import org.openqa.selenium.remote.http.Route;
import org.openqa.selenium.remote.tracing.Tracer;

import java.util.Collections;
import java.util.Set;
Expand All @@ -61,6 +53,7 @@
public class DistributorServer extends TemplateGridCommand {

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

@Override
public String getName() {
Expand Down Expand Up @@ -94,25 +87,11 @@ protected Config getDefaultConfig() {

@Override
protected void execute(Config config) {
LoggingOptions loggingOptions = new LoggingOptions(config);
Tracer tracer = loggingOptions.getTracer();

EventBusOptions events = new EventBusOptions(config);
EventBus bus = events.getEventBus();

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

SessionMap sessions = new SessionMapOptions(config).getSessionMap();

BaseServerOptions serverOptions = new BaseServerOptions(config);
DistributorOptions distributorOptions = new DistributorOptions(config);

Distributor distributor = distributorOptions.getDistributor(LOCAL_DISTRIBUTOR_SERVER);

Distributor distributor = new LocalDistributor(
tracer,
bus,
clientFactory,
sessions,
serverOptions.getRegistrationSecret());
HttpHandler readinessCheck = req -> {
DistributorStatus status = distributor.getStatus();
if (status.hasCapacity()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ java_library(
"//java/server/src/org/openqa/selenium/grid/node",
"//java/server/src/org/openqa/selenium/grid/node/remote",
"//java/server/src/org/openqa/selenium/grid/sessionmap",
"//java/server/src/org/openqa/selenium/grid/config",
"//java/server/src/org/openqa/selenium/grid/log",
"//java/server/src/org/openqa/selenium/grid/server",
"//java/server/src/org/openqa/selenium/grid/sessionmap/config",

artifact("com.google.guava:guava"),
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.openqa.selenium.SessionNotCreatedException;
import org.openqa.selenium.concurrent.Regularly;
import org.openqa.selenium.events.EventBus;
import org.openqa.selenium.grid.config.Config;
import org.openqa.selenium.grid.data.CreateSessionRequest;
import org.openqa.selenium.grid.data.CreateSessionResponse;
import org.openqa.selenium.grid.data.DistributorStatus;
Expand All @@ -33,9 +34,14 @@
import org.openqa.selenium.grid.data.NodeRemovedEvent;
import org.openqa.selenium.grid.data.NodeStatus;
import org.openqa.selenium.grid.distributor.Distributor;
import org.openqa.selenium.grid.log.LoggingOptions;
import org.openqa.selenium.grid.node.Node;
import org.openqa.selenium.grid.node.remote.RemoteNode;
import org.openqa.selenium.grid.server.BaseServerOptions;
import org.openqa.selenium.grid.server.EventBusOptions;
import org.openqa.selenium.grid.server.NetworkOptions;
import org.openqa.selenium.grid.sessionmap.SessionMap;
import org.openqa.selenium.grid.sessionmap.config.SessionMapOptions;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.json.JsonOutput;
Expand Down Expand Up @@ -109,6 +115,16 @@ public LocalDistributor(
bus.addListener(NODE_STATUS, event -> refresh(event.getData(NodeStatus.class)));
}

public static Distributor create(Config config) {
Tracer tracer = new LoggingOptions(config).getTracer();
EventBus bus = new EventBusOptions(config).getEventBus();
HttpClient.Factory clientFactory = new NetworkOptions(config).getHttpClientFactory(tracer);
SessionMap sessions = new SessionMapOptions(config).getSessionMap();
BaseServerOptions serverOptions = new BaseServerOptions(config);

return new LocalDistributor(tracer, bus, clientFactory, sessions, serverOptions.getRegistrationSecret());
}

@Override
public CreateSessionResponse newSession(HttpRequest request)
throws SessionNotCreatedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ java_library(
"//java/server/src/org/openqa/selenium/grid/distributor",
"//java/server/src/org/openqa/selenium/grid/node",
"//java/server/src/org/openqa/selenium/grid/web",
"//java/server/src/org/openqa/selenium/grid/config",
"//java/server/src/org/openqa/selenium/grid/log",
"//java/server/src/org/openqa/selenium/grid/server"
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.UUID;
import java.util.logging.Logger;

import static org.openqa.selenium.net.Urls.fromUri;
import static org.openqa.selenium.remote.http.Contents.asJson;
import static org.openqa.selenium.remote.http.HttpMethod.DELETE;
import static org.openqa.selenium.remote.http.HttpMethod.GET;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ java_library(
"//java/server/src/org/openqa/selenium/grid/data",
"//java/server/src/org/openqa/selenium/grid/distributor",
"//java/server/src/org/openqa/selenium/grid/distributor/config",
"//java/server/src/org/openqa/selenium/grid/distributor/remote",
"//java/server/src/org/openqa/selenium/grid/graphql",
"//java/server/src/org/openqa/selenium/grid/log",
"//java/server/src/org/openqa/selenium/grid/node",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.openqa.selenium.grid.config.Role;
import org.openqa.selenium.grid.distributor.Distributor;
import org.openqa.selenium.grid.distributor.config.DistributorOptions;
import org.openqa.selenium.grid.distributor.remote.RemoteDistributor;
import org.openqa.selenium.grid.graphql.GraphqlHandler;
import org.openqa.selenium.grid.log.LoggingOptions;
import org.openqa.selenium.grid.router.ProxyCdpIntoGrid;
Expand All @@ -43,6 +44,8 @@
import org.openqa.selenium.remote.http.Route;
import org.openqa.selenium.remote.tracing.Tracer;

import java.net.URI;
import java.net.URL;
import java.util.Collections;
import java.util.Set;
import java.util.logging.Logger;
Expand All @@ -51,6 +54,7 @@
import static org.openqa.selenium.grid.config.StandardGridRoles.DISTRIBUTOR_ROLE;
import static org.openqa.selenium.grid.config.StandardGridRoles.HTTPD_ROLE;
import static org.openqa.selenium.grid.config.StandardGridRoles.SESSION_MAP_ROLE;
import static org.openqa.selenium.net.Urls.fromUri;
import static org.openqa.selenium.remote.http.Route.get;

@AutoService(CliCommand.class)
Expand Down Expand Up @@ -102,7 +106,12 @@ protected void execute(Config config) {
BaseServerOptions serverOptions = new BaseServerOptions(config);

DistributorOptions distributorOptions = new DistributorOptions(config);
Distributor distributor = distributorOptions.getDistributor(tracer, clientFactory);
URL distributorUrl = fromUri(distributorOptions.getDistributorUri());
Distributor distributor = new RemoteDistributor(
tracer,
clientFactory,
distributorUrl
);

GraphqlHandler graphqlHandler = new GraphqlHandler(distributor, serverOptions.getExternalUri().toString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,6 @@ public EventBus getEventBus() {
}

private EventBus createBus() {
String clazzName = config.get("events", "implementation").orElse(DEFAULT_CLASS);
LOG.info("Creating event bus: " + clazzName);
try {
Class<?> busClazz = Class.forName(clazzName);
Method create = busClazz.getMethod("create", Config.class);

if (!Modifier.isStatic(create.getModifiers())) {
throw new IllegalArgumentException(String.format(
"Event bus class %s's `create(Config)` method must be static", clazzName));
}

if (!EventBus.class.isAssignableFrom(create.getReturnType())) {
throw new IllegalArgumentException(String.format(
"Event bus class %s's `create(Config)` method must return an EventBus", clazzName));
}

return (EventBus) create.invoke(null, config);
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException(String.format(
"Event bus class %s must have a static `create(Config)` method", clazzName));
} catch (ReflectiveOperationException e) {
throw new IllegalArgumentException("Unable to find event bus class: " + clazzName, e);
}
return (EventBus) config.getClass("events", "implementation", EventBus.class, DEFAULT_CLASS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import org.openqa.selenium.grid.config.ConfigException;
import org.openqa.selenium.grid.sessionmap.SessionMap;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Optional;
Expand Down Expand Up @@ -78,28 +76,6 @@ public URI getSessionMapUri() {
}

public SessionMap getSessionMap() {
String clazz = config.get(SESSIONS_SECTION, "implementation").orElse(DEFAULT_SESSION_MAP);
LOG.info("Creating session map: " + clazz);
try {
Class<?> busClazz = Class.forName(clazz);
Method create = busClazz.getMethod("create", Config.class);

if (!Modifier.isStatic(create.getModifiers())) {
throw new IllegalArgumentException(String.format(
"Session map class %s's `create(Config)` method must be static", clazz));
}

if (!SessionMap.class.isAssignableFrom(create.getReturnType())) {
throw new IllegalArgumentException(String.format(
"Session map class %s's `create(Config)` method must return a SessionMap", clazz));
}

return (SessionMap) create.invoke(null, config);
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException(String.format(
"Session map class %s must have a static `create(Config)` method", clazz));
} catch (ReflectiveOperationException e) {
throw new IllegalArgumentException("Unable to find event bus class: " + clazz, e);
}
return (SessionMap) config.getClass(SESSIONS_SECTION, "implementation", SessionMap.class, DEFAULT_SESSION_MAP);
}
}