diff --git a/java/server/src/org/openqa/selenium/grid/BUILD.bazel b/java/server/src/org/openqa/selenium/grid/BUILD.bazel index 08411be7d7517..62265a0ed66f8 100644 --- a/java/server/src/org/openqa/selenium/grid/BUILD.bazel +++ b/java/server/src/org/openqa/selenium/grid/BUILD.bazel @@ -3,9 +3,28 @@ load("//:copy_file.bzl", "copy_file") load("//java:defs.bzl", "java_dist_zip", "java_export", "javadoc") load("//java:version.bzl", "SE_VERSION") +BASE_COMMAND_SRCS = [ + "TemplateGridCommand.java", +] + +java_library( + name = "base-command", + srcs = BASE_COMMAND_SRCS, + visibility = [ + "//java/server/src/org/openqa/selenium/grid:__subpackages__", + ], + deps = [ + "//java/server/src/org/openqa/selenium/cli", + "//java/server/src/org/openqa/selenium/grid/config", + "//java/server/src/org/openqa/selenium/grid/log", + "//java/server/src/org/openqa/selenium/grid/server", + artifact("com.beust:jcommander"), + ], +) + java_export( name = "grid", - srcs = glob(["*.java"]), + srcs = glob(["*.java"], exclude = BASE_COMMAND_SRCS), maven_coordinates = "org.seleniumhq.selenium:selenium-grid:%s" % SE_VERSION, pom_template = "//java/client/src/org/openqa/selenium:template-pom", visibility = [ diff --git a/java/server/src/org/openqa/selenium/grid/TemplateGridCommand.java b/java/server/src/org/openqa/selenium/grid/TemplateGridCommand.java new file mode 100644 index 0000000000000..90cf00ceaca84 --- /dev/null +++ b/java/server/src/org/openqa/selenium/grid/TemplateGridCommand.java @@ -0,0 +1,91 @@ +// 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; + +import com.beust.jcommander.JCommander; +import com.beust.jcommander.ParameterException; +import org.openqa.selenium.cli.CliCommand; +import org.openqa.selenium.grid.config.AnnotatedConfig; +import org.openqa.selenium.grid.config.CompoundConfig; +import org.openqa.selenium.grid.config.ConcatenatingConfig; +import org.openqa.selenium.grid.config.Config; +import org.openqa.selenium.grid.config.ConfigFlags; +import org.openqa.selenium.grid.config.EnvConfig; +import org.openqa.selenium.grid.log.LoggingOptions; +import org.openqa.selenium.grid.server.HelpFlags; + +import java.util.LinkedHashSet; +import java.util.Set; + +public abstract class TemplateGridCommand implements CliCommand { + + @Override + public final Executable configure(String... args) { + Set allFlags = getFlagObjects(); + + HelpFlags helpFlags = new HelpFlags(); + ConfigFlags configFlags = new ConfigFlags(); + + JCommander.Builder builder = JCommander.newBuilder() + .programName(getName()) + .addObject(configFlags) + .addObject(helpFlags); + allFlags.forEach(builder::addObject); + JCommander commander = builder.build(); + + return () -> { + try { + commander.parse(args); + } catch (ParameterException e) { + System.err.println(e.getMessage()); + commander.usage(); + return; + } + + if (helpFlags.displayHelp(commander, System.out)) { + return; + } + + Set allConfigs = new LinkedHashSet<>(); + allConfigs.add(new EnvConfig()); + allConfigs.add(new ConcatenatingConfig(getSystemPropertiesConfigPrefix(), '.', System.getProperties())); + allFlags.forEach(flags -> allConfigs.add(new AnnotatedConfig(flags))); + allConfigs.add(configFlags.readConfigFiles()); + allConfigs.add(getDefaultConfig()); + + Config config = new CompoundConfig(allConfigs.toArray(Config[]::new)); + + if (helpFlags.dumpConfig(config, System.out)) { + return; + } + + LoggingOptions loggingOptions = new LoggingOptions(config); + loggingOptions.configureLogging(); + + execute(config); + }; + } + + protected abstract Set getFlagObjects(); + + protected abstract String getSystemPropertiesConfigPrefix(); + + protected abstract Config getDefaultConfig(); + + protected abstract void execute(Config config) throws Exception; +} diff --git a/java/server/src/org/openqa/selenium/grid/commands/BUILD.bazel b/java/server/src/org/openqa/selenium/grid/commands/BUILD.bazel index 550f4e022bae2..b199e93767740 100644 --- a/java/server/src/org/openqa/selenium/grid/commands/BUILD.bazel +++ b/java/server/src/org/openqa/selenium/grid/commands/BUILD.bazel @@ -13,9 +13,11 @@ java_library( deps = [ "//java:auto-service", "//java/client/src/org/openqa/selenium:core", + "//java/client/src/org/openqa/selenium/json", "//java/client/src/org/openqa/selenium/remote", "//java/server/src/org/openqa/selenium/cli", "//java/server/src/org/openqa/selenium/events", + "//java/server/src/org/openqa/selenium/grid:base-command", "//java/server/src/org/openqa/selenium/grid/config", "//java/server/src/org/openqa/selenium/grid/distributor", "//java/server/src/org/openqa/selenium/grid/distributor/local", diff --git a/java/server/src/org/openqa/selenium/grid/commands/Hub.java b/java/server/src/org/openqa/selenium/grid/commands/Hub.java index fbb680c26801e..cce21958521dc 100644 --- a/java/server/src/org/openqa/selenium/grid/commands/Hub.java +++ b/java/server/src/org/openqa/selenium/grid/commands/Hub.java @@ -17,19 +17,14 @@ package org.openqa.selenium.grid.commands; -import com.beust.jcommander.JCommander; -import com.beust.jcommander.ParameterException; import com.google.auto.service.AutoService; +import com.google.common.collect.ImmutableSet; import io.opentelemetry.trace.Tracer; import org.openqa.selenium.BuildInfo; import org.openqa.selenium.cli.CliCommand; import org.openqa.selenium.events.EventBus; -import org.openqa.selenium.grid.config.AnnotatedConfig; -import org.openqa.selenium.grid.config.CompoundConfig; -import org.openqa.selenium.grid.config.ConcatenatingConfig; +import org.openqa.selenium.grid.TemplateGridCommand; import org.openqa.selenium.grid.config.Config; -import org.openqa.selenium.grid.config.ConfigFlags; -import org.openqa.selenium.grid.config.EnvConfig; import org.openqa.selenium.grid.distributor.Distributor; import org.openqa.selenium.grid.distributor.local.LocalDistributor; import org.openqa.selenium.grid.log.LoggingOptions; @@ -38,7 +33,6 @@ import org.openqa.selenium.grid.server.BaseServerOptions; 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; @@ -48,10 +42,11 @@ import org.openqa.selenium.netty.server.NettyServer; import org.openqa.selenium.remote.http.HttpClient; +import java.util.Set; import java.util.logging.Logger; @AutoService(CliCommand.class) -public class Hub implements CliCommand { +public class Hub extends TemplateGridCommand { private static final Logger LOG = Logger.getLogger(Hub.class.getName()); @@ -66,83 +61,60 @@ public String getDescription() { } @Override - public Executable configure(String... args) { - HelpFlags help = new HelpFlags(); - ConfigFlags configFlags = new ConfigFlags(); - BaseServerFlags baseFlags = new BaseServerFlags(); - EventBusFlags eventBusFlags = new EventBusFlags(); - JCommander commander = JCommander.newBuilder() - .programName("standalone") - .addObject(baseFlags) - .addObject(configFlags) - .addObject(eventBusFlags) - .addObject(help) - .build(); - - return () -> { - try { - commander.parse(args); - } catch (ParameterException e) { - System.err.println(e.getMessage()); - commander.usage(); - return; - } - - if (help.displayHelp(commander, System.out)) { - return; - } - - Config config = new CompoundConfig( - new EnvConfig(), - new ConcatenatingConfig("selenium", '.', System.getProperties()), - new AnnotatedConfig(help), - new AnnotatedConfig(eventBusFlags), - new AnnotatedConfig(baseFlags), - configFlags.readConfigFiles(), - new DefaultHubConfig()); - - if (help.dumpConfig(config, System.out)) { - return; - } - - LoggingOptions loggingOptions = new LoggingOptions(config); - loggingOptions.configureLogging(); - Tracer tracer = loggingOptions.getTracer(); - - EventBusOptions events = new EventBusOptions(config); - EventBus bus = events.getEventBus(); - - CombinedHandler handler = new CombinedHandler(); - - SessionMap sessions = new LocalSessionMap(tracer, bus); - handler.addHandler(sessions); - - BaseServerOptions serverOptions = new BaseServerOptions(config); - - NetworkOptions networkOptions = new NetworkOptions(config); - HttpClient.Factory clientFactory = new RoutableHttpClientFactory( - serverOptions.getExternalUri().toURL(), - handler, - networkOptions.getHttpClientFactory(tracer)); - - Distributor distributor = new LocalDistributor( - tracer, - bus, - clientFactory, - sessions, - null); - handler.addHandler(distributor); - Router router = new Router(tracer, clientFactory, sessions, distributor); - - Server server = new NettyServer(serverOptions, router); - server.start(); - - BuildInfo info = new BuildInfo(); - LOG.info(String.format( - "Started Selenium hub %s (revision %s): %s", - info.getReleaseLabel(), - info.getBuildRevision(), - server.getUrl())); - }; + protected Set getFlagObjects() { + return ImmutableSet.of( + new BaseServerFlags(), + new EventBusFlags()); + } + + @Override + protected String getSystemPropertiesConfigPrefix() { + return "selenium"; + } + + @Override + protected Config getDefaultConfig() { + return new DefaultHubConfig(); + } + + @Override + protected void execute(Config config) throws Exception { + LoggingOptions loggingOptions = new LoggingOptions(config); + Tracer tracer = loggingOptions.getTracer(); + + EventBusOptions events = new EventBusOptions(config); + EventBus bus = events.getEventBus(); + + CombinedHandler handler = new CombinedHandler(); + + SessionMap sessions = new LocalSessionMap(tracer, bus); + handler.addHandler(sessions); + + BaseServerOptions serverOptions = new BaseServerOptions(config); + + NetworkOptions networkOptions = new NetworkOptions(config); + HttpClient.Factory clientFactory = new RoutableHttpClientFactory( + serverOptions.getExternalUri().toURL(), + handler, + networkOptions.getHttpClientFactory(tracer)); + + Distributor distributor = new LocalDistributor( + tracer, + bus, + clientFactory, + sessions, + null); + handler.addHandler(distributor); + Router router = new Router(tracer, clientFactory, sessions, distributor); + + Server server = new NettyServer(serverOptions, router); + server.start(); + + BuildInfo info = new BuildInfo(); + LOG.info(String.format( + "Started Selenium hub %s (revision %s): %s", + info.getReleaseLabel(), + info.getBuildRevision(), + server.getUrl())); } } diff --git a/java/server/src/org/openqa/selenium/grid/commands/MessageBusCommand.java b/java/server/src/org/openqa/selenium/grid/commands/MessageBusCommand.java index c73ff69abb3aa..44b7e44ef32cf 100644 --- a/java/server/src/org/openqa/selenium/grid/commands/MessageBusCommand.java +++ b/java/server/src/org/openqa/selenium/grid/commands/MessageBusCommand.java @@ -17,36 +17,32 @@ package org.openqa.selenium.grid.commands; -import com.beust.jcommander.JCommander; -import com.beust.jcommander.ParameterException; import com.google.auto.service.AutoService; import com.google.common.collect.ImmutableMap; -import com.google.common.net.MediaType; +import com.google.common.collect.ImmutableSet; import org.openqa.selenium.BuildInfo; import org.openqa.selenium.cli.CliCommand; import org.openqa.selenium.events.EventBus; -import org.openqa.selenium.grid.config.AnnotatedConfig; -import org.openqa.selenium.grid.config.CompoundConfig; -import org.openqa.selenium.grid.config.ConcatenatingConfig; +import org.openqa.selenium.grid.TemplateGridCommand; import org.openqa.selenium.grid.config.Config; -import org.openqa.selenium.grid.config.EnvConfig; import org.openqa.selenium.grid.config.MapConfig; -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.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.netty.server.NettyServer; import org.openqa.selenium.remote.http.Contents; import org.openqa.selenium.remote.http.HttpResponse; import org.openqa.selenium.remote.http.Route; +import java.util.Set; import java.util.logging.Logger; +import static org.openqa.selenium.json.Json.JSON_UTF_8; + @AutoService(CliCommand.class) -public class MessageBusCommand implements CliCommand { +public class MessageBusCommand extends TemplateGridCommand { private static final Logger LOG = Logger.getLogger(MessageBusCommand.class.getName()); @Override @@ -65,73 +61,55 @@ public boolean isShown() { } @Override - public Executable configure(String... args) { - HelpFlags help = new HelpFlags(); - BaseServerFlags baseFlags = new BaseServerFlags(); - EventBusFlags eventBusFlags = new EventBusFlags(); - - JCommander commander = JCommander.newBuilder() - .programName("standalone") - .addObject(baseFlags) - .addObject(eventBusFlags) - .addObject(help) - .build(); - - return () -> { - try { - commander.parse(args); - } catch (ParameterException e) { - System.err.println(e.getMessage()); - commander.usage(); - return; - } - - if (help.displayHelp(commander, System.out)) { - return; - } - - Config config = new CompoundConfig( - new EnvConfig(), - new ConcatenatingConfig("message-bus", '.', System.getProperties()), - new AnnotatedConfig(help), - new AnnotatedConfig(eventBusFlags), - new AnnotatedConfig(baseFlags), - new MapConfig(ImmutableMap.of( - "events", ImmutableMap.of( - "bind", true, - "publish", "tcp://*:4442", - "subscribe", "tcp://*:4443"), - "server", ImmutableMap.of( - "port", 5557)))); - - if (help.dumpConfig(config, System.out)) { - return; - } - - LoggingOptions loggingOptions = new LoggingOptions(config); - loggingOptions.configureLogging(); - - EventBusOptions events = new EventBusOptions(config); - // We need this reference to stop the bus being garbage collected. Which would be less than ideal. - EventBus bus = events.getEventBus(); - - BaseServerOptions serverOptions = new BaseServerOptions(config); - - Server server = new NettyServer( - serverOptions, - Route.get("/status").to(() -> req -> - new HttpResponse() - .addHeader("Content-Type", MediaType.JSON_UTF_8.toString()) - .setContent(Contents.asJson(ImmutableMap.of("ready", true, "message", "Event bus running"))))); - server.start(); - - BuildInfo info = new BuildInfo(); - LOG.info(String.format("Started Selenium message bus %s (revision %s)", info.getReleaseLabel(), info.getBuildRevision())); - - // If we exit, the bus goes out of scope, and it's closed - Thread.currentThread().join(); - - LOG.info("Shutting down: " + bus); - }; + protected Set getFlagObjects() { + return ImmutableSet.of( + new BaseServerFlags(), + new EventBusFlags()); + + } + + @Override + protected String getSystemPropertiesConfigPrefix() { + return "selenium"; + } + + @Override + protected Config getDefaultConfig() { + return new MapConfig(ImmutableMap.of( + "events", ImmutableMap.of( + "bind", true, + "publish", "tcp://*:4442", + "subscribe", "tcp://*:4443"), + "server", ImmutableMap.of( + "port", 5557))); + } + + @Override + protected void execute(Config config) throws Exception { + EventBusOptions events = new EventBusOptions(config); + // We need this reference to stop the bus being garbage collected. Which would be less than ideal. + EventBus bus = events.getEventBus(); + + BaseServerOptions serverOptions = new BaseServerOptions(config); + + Server server = new NettyServer( + serverOptions, + Route.get("/status").to(() -> req -> + new HttpResponse() + .addHeader("Content-Type", JSON_UTF_8) + .setContent(Contents.asJson(ImmutableMap.of("ready", true, "message", "Event bus running"))))); + server.start(); + + BuildInfo info = new BuildInfo(); + LOG.info(String.format( + "Started Selenium message bus %s (revision %s): %s", + info.getReleaseLabel(), + info.getBuildRevision(), + server.getUrl())); + + // If we exit, the bus goes out of scope, and it's closed + Thread.currentThread().join(); + + LOG.info("Shutting down: " + bus); } } diff --git a/java/server/src/org/openqa/selenium/grid/commands/Standalone.java b/java/server/src/org/openqa/selenium/grid/commands/Standalone.java index 25bd17905e13b..7a7c34ac4911a 100644 --- a/java/server/src/org/openqa/selenium/grid/commands/Standalone.java +++ b/java/server/src/org/openqa/selenium/grid/commands/Standalone.java @@ -17,20 +17,15 @@ package org.openqa.selenium.grid.commands; -import com.beust.jcommander.JCommander; -import com.beust.jcommander.ParameterException; import com.google.auto.service.AutoService; +import com.google.common.collect.ImmutableSet; import io.opentelemetry.trace.Tracer; import org.openqa.selenium.BuildInfo; import org.openqa.selenium.WebDriverException; import org.openqa.selenium.cli.CliCommand; +import org.openqa.selenium.grid.TemplateGridCommand; import org.openqa.selenium.events.EventBus; -import org.openqa.selenium.grid.config.AnnotatedConfig; -import org.openqa.selenium.grid.config.CompoundConfig; -import org.openqa.selenium.grid.config.ConcatenatingConfig; import org.openqa.selenium.grid.config.Config; -import org.openqa.selenium.grid.config.ConfigFlags; -import org.openqa.selenium.grid.config.EnvConfig; import org.openqa.selenium.grid.distributor.Distributor; import org.openqa.selenium.grid.distributor.local.LocalDistributor; import org.openqa.selenium.grid.docker.DockerFlags; @@ -44,7 +39,6 @@ import org.openqa.selenium.grid.server.BaseServerOptions; 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; @@ -57,10 +51,11 @@ import java.net.URI; import java.net.URISyntaxException; +import java.util.Set; import java.util.logging.Logger; @AutoService(CliCommand.class) -public class Standalone implements CliCommand { +public class Standalone extends TemplateGridCommand { private static final Logger LOG = Logger.getLogger("selenium"); @@ -75,112 +70,84 @@ public String getDescription() { } @Override - public Executable configure(String... args) { - HelpFlags help = new HelpFlags(); - ConfigFlags configFlags = new ConfigFlags(); - BaseServerFlags baseFlags = new BaseServerFlags(); - EventBusFlags eventFlags = new EventBusFlags(); - DockerFlags dockerFlags = new DockerFlags(); - StandaloneFlags standaloneFlags = new StandaloneFlags(); - - JCommander commander = JCommander.newBuilder() - .programName("standalone") - .addObject(baseFlags) - .addObject(configFlags) - .addObject(dockerFlags) - .addObject(eventFlags) - .addObject(help) - .addObject(standaloneFlags) - .build(); - - return () -> { - try { - commander.parse(args); - } catch (ParameterException e) { - System.err.println(e.getMessage()); - commander.usage(); - return; - } - - if (help.displayHelp(commander, System.out)) { - return; - } - - Config config = new CompoundConfig( - new EnvConfig(), - new ConcatenatingConfig("selenium", '.', System.getProperties()), - new AnnotatedConfig(help), - new AnnotatedConfig(baseFlags), - new AnnotatedConfig(dockerFlags), - new AnnotatedConfig(eventFlags), - configFlags.readConfigFiles(), - new AnnotatedConfig(standaloneFlags), - new DefaultStandaloneConfig()); - - if (help.dumpConfig(config, System.out)) { - return; - } - - LoggingOptions loggingOptions = new LoggingOptions(config); - loggingOptions.configureLogging(); - Tracer tracer = loggingOptions.getTracer(); - - EventBusOptions events = new EventBusOptions(config); - EventBus bus = events.getEventBus(); - - String hostName; - try { - hostName = new NetworkUtils().getNonLoopbackAddressOfThisMachine(); - } catch (WebDriverException e) { - hostName = "localhost"; - } - - int port = config.getInt("server", "port") - .orElseThrow(() -> new IllegalArgumentException("No port to use configured")); - URI localhost = null; - try { - localhost = new URI("http", null, hostName, port, null, null, null); - } catch (URISyntaxException e) { - throw new RuntimeException(e); - } - - NetworkOptions networkOptions = new NetworkOptions(config); - CombinedHandler combinedHandler = new CombinedHandler(); - HttpClient.Factory clientFactory = new RoutableHttpClientFactory( - localhost.toURL(), - combinedHandler, - networkOptions.getHttpClientFactory(tracer)); - - SessionMap sessions = new LocalSessionMap(tracer, bus); - combinedHandler.addHandler(sessions); - Distributor distributor = new LocalDistributor(tracer, bus, clientFactory, sessions, null); - combinedHandler.addHandler(distributor); - Router router = new Router(tracer, clientFactory, sessions, distributor); - - LocalNode.Builder nodeBuilder = LocalNode.builder( - tracer, - bus, - clientFactory, - localhost, - null) - .maximumConcurrentSessions(Runtime.getRuntime().availableProcessors() * 3); - - new NodeOptions(config).configure(tracer, clientFactory, nodeBuilder); - new DockerOptions(config).configure(tracer, clientFactory, nodeBuilder); - - Node node = nodeBuilder.build(); - combinedHandler.addHandler(node); - distributor.add(node); - - Server server = new NettyServer(new BaseServerOptions(config), router); - server.start(); - - BuildInfo info = new BuildInfo(); - LOG.info(String.format( - "Started Selenium standalone %s (revision %s): %s", - info.getReleaseLabel(), - info.getBuildRevision(), - server.getUrl())); - }; + protected Set getFlagObjects() { + return ImmutableSet.of( + new BaseServerFlags(), + new DockerFlags(), + new EventBusFlags(), + new StandaloneFlags()); + } + + @Override + protected String getSystemPropertiesConfigPrefix() { + return "selenium"; + } + + @Override + protected Config getDefaultConfig() { + return new DefaultStandaloneConfig(); + } + + @Override + protected void execute(Config config) throws Exception { + LoggingOptions loggingOptions = new LoggingOptions(config); + Tracer tracer = loggingOptions.getTracer(); + + EventBusOptions events = new EventBusOptions(config); + EventBus bus = events.getEventBus(); + + String hostName; + try { + hostName = new NetworkUtils().getNonLoopbackAddressOfThisMachine(); + } catch (WebDriverException e) { + hostName = "localhost"; + } + + int port = config.getInt("server", "port") + .orElseThrow(() -> new IllegalArgumentException("No port to use configured")); + URI localhost = null; + try { + localhost = new URI("http", null, hostName, port, null, null, null); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + + NetworkOptions networkOptions = new NetworkOptions(config); + CombinedHandler combinedHandler = new CombinedHandler(); + HttpClient.Factory clientFactory = new RoutableHttpClientFactory( + localhost.toURL(), + combinedHandler, + networkOptions.getHttpClientFactory(tracer)); + + SessionMap sessions = new LocalSessionMap(tracer, bus); + combinedHandler.addHandler(sessions); + Distributor distributor = new LocalDistributor(tracer, bus, clientFactory, sessions, null); + combinedHandler.addHandler(distributor); + Router router = new Router(tracer, clientFactory, sessions, distributor); + + LocalNode.Builder nodeBuilder = LocalNode.builder( + tracer, + bus, + clientFactory, + localhost, + null) + .maximumConcurrentSessions(Runtime.getRuntime().availableProcessors() * 3); + + new NodeOptions(config).configure(tracer, clientFactory, nodeBuilder); + new DockerOptions(config).configure(tracer, clientFactory, nodeBuilder); + + Node node = nodeBuilder.build(); + combinedHandler.addHandler(node); + distributor.add(node); + + Server server = new NettyServer(new BaseServerOptions(config), router); + server.start(); + + BuildInfo info = new BuildInfo(); + LOG.info(String.format( + "Started Selenium standalone %s (revision %s): %s", + info.getReleaseLabel(), + info.getBuildRevision(), + server.getUrl())); } } diff --git a/java/server/src/org/openqa/selenium/grid/distributor/httpd/BUILD.bazel b/java/server/src/org/openqa/selenium/grid/distributor/httpd/BUILD.bazel index c9e0d03d801d2..67bdf69e76995 100644 --- a/java/server/src/org/openqa/selenium/grid/distributor/httpd/BUILD.bazel +++ b/java/server/src/org/openqa/selenium/grid/distributor/httpd/BUILD.bazel @@ -11,6 +11,7 @@ java_library( "//java/client/src/org/openqa/selenium/remote", "//java/server/src/org/openqa/selenium/cli", "//java/server/src/org/openqa/selenium/events", + "//java/server/src/org/openqa/selenium/grid:base-command", "//java/server/src/org/openqa/selenium/grid/config", "//java/server/src/org/openqa/selenium/grid/distributor", "//java/server/src/org/openqa/selenium/grid/distributor/local", diff --git a/java/server/src/org/openqa/selenium/grid/distributor/httpd/DistributorServer.java b/java/server/src/org/openqa/selenium/grid/distributor/httpd/DistributorServer.java index e4f37b0f3673c..52263bb35ba5f 100644 --- a/java/server/src/org/openqa/selenium/grid/distributor/httpd/DistributorServer.java +++ b/java/server/src/org/openqa/selenium/grid/distributor/httpd/DistributorServer.java @@ -17,19 +17,14 @@ package org.openqa.selenium.grid.distributor.httpd; -import com.beust.jcommander.JCommander; -import com.beust.jcommander.ParameterException; import com.google.auto.service.AutoService; +import com.google.common.collect.ImmutableSet; import io.opentelemetry.trace.Tracer; import org.openqa.selenium.BuildInfo; import org.openqa.selenium.cli.CliCommand; import org.openqa.selenium.events.EventBus; -import org.openqa.selenium.grid.config.AnnotatedConfig; -import org.openqa.selenium.grid.config.CompoundConfig; -import org.openqa.selenium.grid.config.ConcatenatingConfig; +import org.openqa.selenium.grid.TemplateGridCommand; import org.openqa.selenium.grid.config.Config; -import org.openqa.selenium.grid.config.ConfigFlags; -import org.openqa.selenium.grid.config.EnvConfig; import org.openqa.selenium.grid.distributor.Distributor; import org.openqa.selenium.grid.distributor.local.LocalDistributor; import org.openqa.selenium.grid.log.LoggingOptions; @@ -37,7 +32,6 @@ import org.openqa.selenium.grid.server.BaseServerOptions; 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; @@ -46,12 +40,12 @@ import org.openqa.selenium.netty.server.NettyServer; import org.openqa.selenium.remote.http.HttpClient; +import java.util.Set; import java.util.logging.Logger; @AutoService(CliCommand.class) -public class -DistributorServer implements CliCommand { +public class DistributorServer extends TemplateGridCommand { private static final Logger LOG = Logger.getLogger(DistributorServer.class.getName()); @@ -66,80 +60,53 @@ public String getDescription() { } @Override - public Executable configure(String... args) { - - HelpFlags help = new HelpFlags(); - ConfigFlags configFlags = new ConfigFlags(); - BaseServerFlags serverFlags = new BaseServerFlags(); - SessionMapFlags sessionMapFlags = new SessionMapFlags(); - EventBusFlags eventBusFlags = new EventBusFlags(); - - JCommander commander = JCommander.newBuilder() - .programName(getName()) - .addObject(configFlags) - .addObject(eventBusFlags) - .addObject(help) - .addObject(serverFlags) - .addObject(sessionMapFlags) - .build(); - - return () -> { - try { - commander.parse(args); - } catch (ParameterException e) { - System.err.println(e.getMessage()); - commander.usage(); - return; - } - - if (help.displayHelp(commander, System.out)) { - return; - } - - Config config = new CompoundConfig( - new EnvConfig(), - new ConcatenatingConfig("distributor", '.', System.getProperties()), - new AnnotatedConfig(help), - new AnnotatedConfig(eventBusFlags), - new AnnotatedConfig(serverFlags), - new AnnotatedConfig(sessionMapFlags), - configFlags.readConfigFiles(), - new DefaultDistributorConfig()); - - if (help.dumpConfig(config, System.out)) { - return; - } - - LoggingOptions loggingOptions = new LoggingOptions(config); - loggingOptions.configureLogging(); - 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); - - Distributor distributor = new LocalDistributor( - tracer, - bus, - clientFactory, - sessions, - serverOptions.getRegistrationSecret()); - - Server server = new NettyServer(serverOptions, distributor); - server.start(); - - BuildInfo info = new BuildInfo(); - LOG.info(String.format( - "Started Selenium distributor %s (revision %s): %s", - info.getReleaseLabel(), - info.getBuildRevision(), - server.getUrl())); - }; + protected Set getFlagObjects() { + return ImmutableSet.of( + new BaseServerFlags(), + new SessionMapFlags(), + new EventBusFlags()); + } + + @Override + protected String getSystemPropertiesConfigPrefix() { + return "distributor"; + } + + @Override + protected Config getDefaultConfig() { + return new DefaultDistributorConfig(); + } + + @Override + protected void execute(Config config) throws Exception { + 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); + + Distributor distributor = new LocalDistributor( + tracer, + bus, + clientFactory, + sessions, + serverOptions.getRegistrationSecret()); + + Server server = new NettyServer(serverOptions, distributor); + server.start(); + + BuildInfo info = new BuildInfo(); + LOG.info(String.format( + "Started Selenium distributor %s (revision %s): %s", + info.getReleaseLabel(), + info.getBuildRevision(), + server.getUrl())); } } diff --git a/java/server/src/org/openqa/selenium/grid/node/httpd/BUILD.bazel b/java/server/src/org/openqa/selenium/grid/node/httpd/BUILD.bazel index 249742d7929de..7c1b2a4b93fff 100644 --- a/java/server/src/org/openqa/selenium/grid/node/httpd/BUILD.bazel +++ b/java/server/src/org/openqa/selenium/grid/node/httpd/BUILD.bazel @@ -12,6 +12,7 @@ java_library( "//java/server/src/org/openqa/selenium/cli", "//java/server/src/org/openqa/selenium/concurrent", "//java/server/src/org/openqa/selenium/events", + "//java/server/src/org/openqa/selenium/grid:base-command", "//java/server/src/org/openqa/selenium/grid/component", "//java/server/src/org/openqa/selenium/grid/config", "//java/server/src/org/openqa/selenium/grid/data", diff --git a/java/server/src/org/openqa/selenium/grid/node/httpd/NodeServer.java b/java/server/src/org/openqa/selenium/grid/node/httpd/NodeServer.java index 1df521b3b7baf..8404570b99acc 100644 --- a/java/server/src/org/openqa/selenium/grid/node/httpd/NodeServer.java +++ b/java/server/src/org/openqa/selenium/grid/node/httpd/NodeServer.java @@ -17,21 +17,16 @@ package org.openqa.selenium.grid.node.httpd; -import com.beust.jcommander.JCommander; -import com.beust.jcommander.ParameterException; import com.google.auto.service.AutoService; +import com.google.common.collect.ImmutableSet; import io.opentelemetry.trace.Tracer; import org.openqa.selenium.BuildInfo; import org.openqa.selenium.cli.CliCommand; import org.openqa.selenium.concurrent.Regularly; import org.openqa.selenium.events.EventBus; +import org.openqa.selenium.grid.TemplateGridCommand; import org.openqa.selenium.grid.component.HealthCheck; -import org.openqa.selenium.grid.config.AnnotatedConfig; -import org.openqa.selenium.grid.config.CompoundConfig; -import org.openqa.selenium.grid.config.ConcatenatingConfig; import org.openqa.selenium.grid.config.Config; -import org.openqa.selenium.grid.config.ConfigFlags; -import org.openqa.selenium.grid.config.EnvConfig; import org.openqa.selenium.grid.data.NodeStatusEvent; import org.openqa.selenium.grid.docker.DockerFlags; import org.openqa.selenium.grid.docker.DockerOptions; @@ -42,17 +37,17 @@ import org.openqa.selenium.grid.server.BaseServerOptions; 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 java.time.Duration; +import java.util.Set; import java.util.logging.Logger; @AutoService(CliCommand.class) -public class NodeServer implements CliCommand { +public class NodeServer extends TemplateGridCommand { private static final Logger LOG = Logger.getLogger(NodeServer.class.getName()); @@ -67,104 +62,75 @@ public String getDescription() { } @Override - public Executable configure(String... args) { - - HelpFlags help = new HelpFlags(); - ConfigFlags configFlags = new ConfigFlags(); - BaseServerFlags serverFlags = new BaseServerFlags(); - EventBusFlags eventBusFlags = new EventBusFlags(); - NodeFlags nodeFlags = new NodeFlags(); - DockerFlags dockerFlags = new DockerFlags(); - - JCommander commander = JCommander.newBuilder() - .programName(getName()) - .addObject(configFlags) - .addObject(dockerFlags) - .addObject(eventBusFlags) - .addObject(help) - .addObject(nodeFlags) - .addObject(serverFlags) - .build(); - - return () -> { - try { - commander.parse(args); - } catch (ParameterException e) { - System.err.println(e.getMessage()); - commander.usage(); - return; - } - - if (help.displayHelp(commander, System.out)) { - return; - } - - Config config = new CompoundConfig( - new EnvConfig(), - new ConcatenatingConfig("node", '.', System.getProperties()), - new AnnotatedConfig(help), - new AnnotatedConfig(serverFlags), - new AnnotatedConfig(eventBusFlags), - new AnnotatedConfig(nodeFlags), - new AnnotatedConfig(dockerFlags), - configFlags.readConfigFiles(), - new DefaultNodeConfig()); - - if (help.dumpConfig(config, System.out)) { - return; - } - - LoggingOptions loggingOptions = new LoggingOptions(config); - loggingOptions.configureLogging(); - Tracer tracer = loggingOptions.getTracer(); - - EventBusOptions events = new EventBusOptions(config); - EventBus bus = events.getEventBus(); - - NetworkOptions networkOptions = new NetworkOptions(config); - HttpClient.Factory clientFactory = networkOptions.getHttpClientFactory(tracer); - - BaseServerOptions serverOptions = new BaseServerOptions(config); - - LOG.info("Reporting self as: " + serverOptions.getExternalUri()); - - LocalNode.Builder builder = LocalNode.builder( - tracer, - bus, - clientFactory, - serverOptions.getExternalUri(), - serverOptions.getRegistrationSecret()); - - new NodeOptions(config).configure(tracer, clientFactory, builder); - new DockerOptions(config).configure(tracer, clientFactory, builder); - - LocalNode node = builder.build(); - - Server server = new NettyServer(serverOptions, node); - server.start(); - - BuildInfo info = new BuildInfo(); - LOG.info(String.format( - "Started Selenium node %s (revision %s): %s", - info.getReleaseLabel(), - info.getBuildRevision(), - server.getUrl())); - - Regularly regularly = new Regularly("Register Node with Distributor"); - - regularly.submit( - () -> { - HealthCheck.Result check = node.getHealthCheck().check(); - if (!check.isAlive()) { - LOG.severe("Node is not alive: " + check.getMessage()); - // Throw an exception to force another check sooner. - throw new UnsupportedOperationException("Node cannot be registered"); - } - - bus.fire(new NodeStatusEvent(node.getStatus())); - }, - Duration.ofMinutes(5), - Duration.ofSeconds(30)); - }; + protected Set getFlagObjects() { + return ImmutableSet.of( + new BaseServerFlags(), + new EventBusFlags(), + new NodeFlags(), + new DockerFlags()); + } + + @Override + protected String getSystemPropertiesConfigPrefix() { + return "node"; + } + + @Override + protected Config getDefaultConfig() { + return new DefaultNodeConfig(); + } + + @Override + protected void execute(Config config) throws Exception { + 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); + + BaseServerOptions serverOptions = new BaseServerOptions(config); + + LOG.info("Reporting self as: " + serverOptions.getExternalUri()); + + LocalNode.Builder builder = LocalNode.builder( + tracer, + bus, + clientFactory, + serverOptions.getExternalUri(), + serverOptions.getRegistrationSecret()); + + new NodeOptions(config).configure(tracer, clientFactory, builder); + new DockerOptions(config).configure(tracer, clientFactory, builder); + + LocalNode node = builder.build(); + + Server server = new NettyServer(serverOptions, node); + server.start(); + + BuildInfo info = new BuildInfo(); + LOG.info(String.format( + "Started Selenium node %s (revision %s): %s", + info.getReleaseLabel(), + info.getBuildRevision(), + server.getUrl())); + + Regularly regularly = new Regularly("Register Node with Distributor"); + + regularly.submit( + () -> { + HealthCheck.Result check = node.getHealthCheck().check(); + if (!check.isAlive()) { + LOG.severe("Node is not alive: " + check.getMessage()); + // Throw an exception to force another check sooner. + throw new UnsupportedOperationException("Node cannot be registered"); + } + + bus.fire(new NodeStatusEvent(node.getStatus())); + }, + Duration.ofMinutes(5), + Duration.ofSeconds(30)); } } diff --git a/java/server/src/org/openqa/selenium/grid/router/httpd/BUILD.bazel b/java/server/src/org/openqa/selenium/grid/router/httpd/BUILD.bazel index 983c712b4d067..bdb80413e2a2c 100644 --- a/java/server/src/org/openqa/selenium/grid/router/httpd/BUILD.bazel +++ b/java/server/src/org/openqa/selenium/grid/router/httpd/BUILD.bazel @@ -10,6 +10,7 @@ java_library( "//java:auto-service", "//java/client/src/org/openqa/selenium/remote", "//java/server/src/org/openqa/selenium/cli", + "//java/server/src/org/openqa/selenium/grid:base-command", "//java/server/src/org/openqa/selenium/grid/config", "//java/server/src/org/openqa/selenium/grid/distributor", "//java/server/src/org/openqa/selenium/grid/distributor/config", diff --git a/java/server/src/org/openqa/selenium/grid/router/httpd/RouterServer.java b/java/server/src/org/openqa/selenium/grid/router/httpd/RouterServer.java index e3fad14d9bf56..2a9d9daf9826b 100644 --- a/java/server/src/org/openqa/selenium/grid/router/httpd/RouterServer.java +++ b/java/server/src/org/openqa/selenium/grid/router/httpd/RouterServer.java @@ -17,19 +17,14 @@ package org.openqa.selenium.grid.router.httpd; -import com.beust.jcommander.JCommander; -import com.beust.jcommander.ParameterException; import com.google.auto.service.AutoService; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; import io.opentelemetry.trace.Tracer; import org.openqa.selenium.BuildInfo; import org.openqa.selenium.cli.CliCommand; -import org.openqa.selenium.grid.config.AnnotatedConfig; -import org.openqa.selenium.grid.config.CompoundConfig; -import org.openqa.selenium.grid.config.ConcatenatingConfig; +import org.openqa.selenium.grid.TemplateGridCommand; import org.openqa.selenium.grid.config.Config; -import org.openqa.selenium.grid.config.ConfigFlags; -import org.openqa.selenium.grid.config.EnvConfig; import org.openqa.selenium.grid.config.MapConfig; import org.openqa.selenium.grid.distributor.Distributor; import org.openqa.selenium.grid.distributor.config.DistributorFlags; @@ -38,7 +33,6 @@ 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.HelpFlags; import org.openqa.selenium.grid.server.NetworkOptions; import org.openqa.selenium.grid.server.Server; import org.openqa.selenium.grid.sessionmap.SessionMap; @@ -47,10 +41,11 @@ import org.openqa.selenium.netty.server.NettyServer; import org.openqa.selenium.remote.http.HttpClient; +import java.util.Set; import java.util.logging.Logger; @AutoService(CliCommand.class) -public class RouterServer implements CliCommand { +public class RouterServer extends TemplateGridCommand { private static final Logger LOG = Logger.getLogger(RouterServer.class.getName()); @@ -65,76 +60,49 @@ public String getDescription() { } @Override - public Executable configure(String... args) { - - HelpFlags help = new HelpFlags(); - ConfigFlags configFlags = new ConfigFlags(); - BaseServerFlags serverFlags = new BaseServerFlags(); - SessionMapFlags sessionMapFlags = new SessionMapFlags(); - DistributorFlags distributorFlags = new DistributorFlags(); - - JCommander commander = JCommander.newBuilder() - .programName(getName()) - .addObject(configFlags) - .addObject(distributorFlags) - .addObject(help) - .addObject(serverFlags) - .addObject(sessionMapFlags) - .build(); - - return () -> { - try { - commander.parse(args); - } catch (ParameterException e) { - System.err.println(e.getMessage()); - commander.usage(); - return; - } - - if (help.displayHelp(commander, System.out)) { - return; - } - - Config config = new CompoundConfig( - new EnvConfig(), - new ConcatenatingConfig("router", '.', System.getProperties()), - new AnnotatedConfig(help), - new AnnotatedConfig(serverFlags), - new AnnotatedConfig(sessionMapFlags), - new AnnotatedConfig(distributorFlags), - configFlags.readConfigFiles(), - new MapConfig(ImmutableMap.of("server", ImmutableMap.of("port", 4444)))); - - if (help.dumpConfig(config, System.out)) { - return; - } - - LoggingOptions loggingOptions = new LoggingOptions(config); - loggingOptions.configureLogging(); - Tracer tracer = loggingOptions.getTracer(); - - NetworkOptions networkOptions = new NetworkOptions(config); - HttpClient.Factory clientFactory = networkOptions.getHttpClientFactory(tracer); - - SessionMapOptions sessionsOptions = new SessionMapOptions(config); - SessionMap sessions = sessionsOptions.getSessionMap(); - - BaseServerOptions serverOptions = new BaseServerOptions(config); - - DistributorOptions distributorOptions = new DistributorOptions(config); - Distributor distributor = distributorOptions.getDistributor(tracer, clientFactory); - - Router router = new Router(tracer, clientFactory, sessions, distributor); - - Server server = new NettyServer(serverOptions, router); - server.start(); - - BuildInfo info = new BuildInfo(); - LOG.info(String.format( - "Started Selenium router %s (revision %s): %s", - info.getReleaseLabel(), - info.getBuildRevision(), - server.getUrl())); - }; + protected Set getFlagObjects() { + return ImmutableSet.of( + new BaseServerFlags(), + new SessionMapFlags(), + new DistributorFlags()); + } + + @Override + protected String getSystemPropertiesConfigPrefix() { + return "router"; + } + + @Override + protected Config getDefaultConfig() { + return new MapConfig(ImmutableMap.of("server", ImmutableMap.of("port", 4444))); + } + + @Override + protected void execute(Config config) throws Exception { + LoggingOptions loggingOptions = new LoggingOptions(config); + Tracer tracer = loggingOptions.getTracer(); + + NetworkOptions networkOptions = new NetworkOptions(config); + HttpClient.Factory clientFactory = networkOptions.getHttpClientFactory(tracer); + + SessionMapOptions sessionsOptions = new SessionMapOptions(config); + SessionMap sessions = sessionsOptions.getSessionMap(); + + BaseServerOptions serverOptions = new BaseServerOptions(config); + + DistributorOptions distributorOptions = new DistributorOptions(config); + Distributor distributor = distributorOptions.getDistributor(tracer, clientFactory); + + Router router = new Router(tracer, clientFactory, sessions, distributor); + + Server server = new NettyServer(serverOptions, router); + server.start(); + + BuildInfo info = new BuildInfo(); + LOG.info(String.format( + "Started Selenium router %s (revision %s): %s", + info.getReleaseLabel(), + info.getBuildRevision(), + server.getUrl())); } } diff --git a/java/server/src/org/openqa/selenium/grid/sessionmap/httpd/BUILD.bazel b/java/server/src/org/openqa/selenium/grid/sessionmap/httpd/BUILD.bazel index 549219931bb12..f81c36b1a57f7 100644 --- a/java/server/src/org/openqa/selenium/grid/sessionmap/httpd/BUILD.bazel +++ b/java/server/src/org/openqa/selenium/grid/sessionmap/httpd/BUILD.bazel @@ -8,9 +8,11 @@ java_library( ], deps = [ "//java:auto-service", + "//java/client/src/org/openqa/selenium/json", "//java/client/src/org/openqa/selenium/remote", "//java/server/src/org/openqa/selenium/cli", "//java/server/src/org/openqa/selenium/events", + "//java/server/src/org/openqa/selenium/grid:base-command", "//java/server/src/org/openqa/selenium/grid/config", "//java/server/src/org/openqa/selenium/grid/log", "//java/server/src/org/openqa/selenium/grid/server", diff --git a/java/server/src/org/openqa/selenium/grid/sessionmap/httpd/SessionMapServer.java b/java/server/src/org/openqa/selenium/grid/sessionmap/httpd/SessionMapServer.java index 620073dca0ce8..31f5ab7893e61 100644 --- a/java/server/src/org/openqa/selenium/grid/sessionmap/httpd/SessionMapServer.java +++ b/java/server/src/org/openqa/selenium/grid/sessionmap/httpd/SessionMapServer.java @@ -17,42 +17,32 @@ package org.openqa.selenium.grid.sessionmap.httpd; -import com.beust.jcommander.JCommander; -import com.beust.jcommander.ParameterException; import com.google.auto.service.AutoService; import com.google.common.collect.ImmutableMap; -import com.google.common.net.MediaType; -import io.opentelemetry.trace.Tracer; +import com.google.common.collect.ImmutableSet; import org.openqa.selenium.BuildInfo; import org.openqa.selenium.cli.CliCommand; -import org.openqa.selenium.events.EventBus; -import org.openqa.selenium.grid.config.AnnotatedConfig; -import org.openqa.selenium.grid.config.CompoundConfig; -import org.openqa.selenium.grid.config.ConcatenatingConfig; +import org.openqa.selenium.grid.TemplateGridCommand; import org.openqa.selenium.grid.config.Config; -import org.openqa.selenium.grid.config.ConfigFlags; -import org.openqa.selenium.grid.config.EnvConfig; -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.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; import org.openqa.selenium.grid.sessionmap.config.SessionMapOptions; -import org.openqa.selenium.grid.sessionmap.local.LocalSessionMap; import org.openqa.selenium.netty.server.NettyServer; import org.openqa.selenium.remote.http.Contents; import org.openqa.selenium.remote.http.HttpResponse; import org.openqa.selenium.remote.http.Route; +import java.util.Set; import java.util.logging.Logger; +import static org.openqa.selenium.json.Json.JSON_UTF_8; import static org.openqa.selenium.remote.http.Route.get; @AutoService(CliCommand.class) -public class SessionMapServer implements CliCommand { +public class SessionMapServer extends TemplateGridCommand { private static final Logger LOG = Logger.getLogger(SessionMapServer.class.getName()); @@ -67,73 +57,42 @@ public String getDescription() { } @Override - public Executable configure(String... args) { - - HelpFlags help = new HelpFlags(); - ConfigFlags configFlags = new ConfigFlags(); - BaseServerFlags serverFlags = new BaseServerFlags(); - EventBusFlags eventBusFlags = new EventBusFlags(); - - JCommander commander = JCommander.newBuilder() - .programName(getName()) - .addObject(configFlags) - .addObject(eventBusFlags) - .addObject(help) - .addObject(serverFlags) - .build(); - - return () -> { - try { - commander.parse(args); - } catch (ParameterException e) { - System.err.println(e.getMessage()); - commander.usage(); - return; - } - - if (help.displayHelp(commander, System.out)) { - return; - } - - Config config = new CompoundConfig( - new EnvConfig(), - new ConcatenatingConfig("sessions", '.', System.getProperties()), - new AnnotatedConfig(help), - new AnnotatedConfig(serverFlags), - new AnnotatedConfig(eventBusFlags), - configFlags.readConfigFiles(), - new DefaultSessionMapConfig()); - - if (help.dumpConfig(config, System.out)) { - return; - } - - LoggingOptions loggingOptions = new LoggingOptions(config); - loggingOptions.configureLogging(); - Tracer tracer = loggingOptions.getTracer(); - - EventBusOptions events = new EventBusOptions(config); - EventBus bus = events.getEventBus(); - - SessionMapOptions sessionMapOptions = new SessionMapOptions(config); - SessionMap sessions = sessionMapOptions.getSessionMap(); + protected Set getFlagObjects() { + return ImmutableSet.of( + new BaseServerFlags(), + new EventBusFlags()); + } - BaseServerOptions serverOptions = new BaseServerOptions(config); + @Override + protected String getSystemPropertiesConfigPrefix() { + return "sessions"; + } - Server server = new NettyServer(serverOptions, Route.combine( - sessions, - get("/status").to(() -> req -> - new HttpResponse() - .addHeader("Content-Type", MediaType.JSON_UTF_8.toString()) - .setContent(Contents.asJson(ImmutableMap.of("ready", true, "message", "Session map is ready.")))))); - server.start(); + @Override + protected Config getDefaultConfig() { + return new DefaultSessionMapConfig(); + } - BuildInfo info = new BuildInfo(); - LOG.info(String.format( - "Started Selenium session map %s (revision %s): %s", - info.getReleaseLabel(), - info.getBuildRevision(), - server.getUrl())); - }; + @Override + protected void execute(Config config) throws Exception { + SessionMapOptions sessionMapOptions = new SessionMapOptions(config); + SessionMap sessions = sessionMapOptions.getSessionMap(); + + BaseServerOptions serverOptions = new BaseServerOptions(config); + + Server server = new NettyServer(serverOptions, Route.combine( + sessions, + get("/status").to(() -> req -> + new HttpResponse() + .addHeader("Content-Type", JSON_UTF_8) + .setContent(Contents.asJson(ImmutableMap.of("ready", true, "message", "Session map is ready.")))))); + server.start(); + + BuildInfo info = new BuildInfo(); + LOG.info(String.format( + "Started Selenium session map %s (revision %s): %s", + info.getReleaseLabel(), + info.getBuildRevision(), + server.getUrl())); } }