Skip to content

Commit

Permalink
[grid] Remove some duplication from Grid cli commands
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Mar 30, 2020
1 parent 6371c31 commit 7b0e22b
Show file tree
Hide file tree
Showing 14 changed files with 529 additions and 635 deletions.
21 changes: 20 additions & 1 deletion java/server/src/org/openqa/selenium/grid/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
91 changes: 91 additions & 0 deletions java/server/src/org/openqa/selenium/grid/TemplateGridCommand.java
Original file line number Diff line number Diff line change
@@ -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<Object> 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<Config> 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<Object> getFlagObjects();

protected abstract String getSystemPropertiesConfigPrefix();

protected abstract Config getDefaultConfig();

protected abstract void execute(Config config) throws Exception;
}
2 changes: 2 additions & 0 deletions java/server/src/org/openqa/selenium/grid/commands/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
146 changes: 59 additions & 87 deletions java/server/src/org/openqa/selenium/grid/commands/Hub.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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());

Expand All @@ -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<Object> 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()));
}
}
Loading

0 comments on commit 7b0e22b

Please sign in to comment.