Skip to content

Commit

Permalink
[server] Mapping grid roles to launchers as enum values without conve…
Browse files Browse the repository at this point in the history
…rsion to strings
  • Loading branch information
barancev committed Oct 11, 2018
1 parent e6c9404 commit acfc1c5
Showing 1 changed file with 100 additions and 111 deletions.
211 changes: 100 additions & 111 deletions java/server/src/org/openqa/grid/selenium/GridLauncherV3.java
Expand Up @@ -66,7 +66,7 @@ private interface GridItemLauncher {
Stoppable launch(PrintStream out);
}

private static Map<String, Function<String[], GridItemLauncher>> LAUNCHERS = buildLaunchers();
private static Map<GridRole, Function<String[], GridItemLauncher>> LAUNCHERS = buildLaunchers();

public static void main(String[] args) {
new GridLauncherV3().launch(args);
Expand Down Expand Up @@ -118,12 +118,12 @@ private GridItemLauncher buildLauncher(String[] args) {
}

GridRole gridRole = GridRole.get(role);
if (gridRole == null || LAUNCHERS.get(gridRole.toString()) == null) {
if (gridRole == null || LAUNCHERS.get(gridRole) == null) {
printInfoAboutRoles(role);
return null;
}

return LAUNCHERS.get(gridRole.toString()).apply(args);
return LAUNCHERS.get(gridRole).apply(args);
}

private void printInfoAboutRoles(String roleCommandLineArg) {
Expand Down Expand Up @@ -208,7 +208,7 @@ private static void configureLogging(String log, boolean debug) {
}
}

private static Map<String, Function<String[], GridItemLauncher>> buildLaunchers() {
private static Map<GridRole, Function<String[], GridItemLauncher>> buildLaunchers() {
BiConsumer<JCommander, PrintStream> usage = (commander, out) -> {
StringBuilder toPrint = new StringBuilder();
commander.usage(toPrint);
Expand All @@ -222,112 +222,101 @@ private static Map<String, Function<String[], GridItemLauncher>> buildLaunchers(
buildInfo.getBuildRevision()));
};

ImmutableMap.Builder<String, Function<String[], GridItemLauncher>> launchers =
ImmutableMap.<String, Function<String[], GridItemLauncher>>builder()
.put(GridRole.NOT_GRID.toString(), (args) -> new GridItemLauncher() {
@Override
public Stoppable launch(PrintStream out) {
StandaloneCliOptions options = new StandaloneCliOptions();
JCommander commander = options.parse(args);

if (options.getVersion()) {
version.accept(out);
return ()->{};
}

if (options.getHelp()) {
usage.accept(commander, out);
return ()->{};
}

configureLogging(options.getLog(), options.getDebug());

log.info(String.format(
"Selenium build info: version: '%s', revision: '%s'",
buildInfo.getReleaseLabel(),
buildInfo.getBuildRevision()));

StandaloneConfiguration configuration = new StandaloneConfiguration(options);
log.info(String.format(
"Launching a standalone Selenium Server on port %s", configuration.port));
SeleniumServer server = new SeleniumServer(configuration);
server.boot();
return server;
}
})
.put(GridRole.HUB.toString(), (args) -> new GridItemLauncher() {

@Override
public Stoppable launch(PrintStream out) {
GridHubCliOptions options = new GridHubCliOptions();
JCommander commander = options.parse(args);

if (options.getVersion()) {
version.accept(out);
return ()->{};
}

if (options.getHelp()) {
usage.accept(commander, out);
return ()->{};
}

configureLogging(options.getLog(), options.getDebug());

log.info(String.format(
"Selenium build info: version: '%s', revision: '%s'",
buildInfo.getReleaseLabel(),
buildInfo.getBuildRevision()));

GridHubConfiguration configuration = new GridHubConfiguration(options);
log.info(String.format(
"Launching Selenium Grid hub on port %s", configuration.port));
Hub hub = new Hub(configuration);
hub.start();
return hub;
}
})
.put(GridRole.NODE.toString(), (args) -> new GridItemLauncher() {

@Override
public Stoppable launch(PrintStream out) {
GridNodeCliOptions options = new GridNodeCliOptions();
JCommander commander = options.parse(args);

if (options.getVersion()) {
version.accept(out);
return ()->{};
}

if (options.getHelp()) {
usage.accept(commander, out);
return ()->{};
}

configureLogging(options.getLog(), options.getDebug());

log.info(String.format(
"Selenium build info: version: '%s', revision: '%s'",
buildInfo.getReleaseLabel(),
buildInfo.getBuildRevision()));

GridNodeConfiguration configuration = new GridNodeConfiguration(options);
if (configuration.port == null || configuration.port == -1) {
configuration.port = PortProber.findFreePort();
}
log.info(String.format(
"Launching a Selenium Grid node on port %s", configuration.port));
SelfRegisteringRemote remote = new SelfRegisteringRemote(configuration);
SeleniumServer server = new SeleniumServer(remote.getConfiguration());
remote.setRemoteServer(server);
if (remote.startRemoteServer()) {
log.info("Selenium Grid node is up and ready to register to the hub");
remote.startRegistrationProcess();
}
return server;
}
});

return launchers.build();
return ImmutableMap.<GridRole, Function<String[], GridItemLauncher>>builder()
.put(GridRole.NOT_GRID, (args) -> out -> {
StandaloneCliOptions options = new StandaloneCliOptions();
JCommander commander = options.parse(args);

if (options.getVersion()) {
version.accept(out);
return ()->{};
}

if (options.getHelp()) {
usage.accept(commander, out);
return ()->{};
}

configureLogging(options.getLog(), options.getDebug());

log.info(String.format(
"Selenium build info: version: '%s', revision: '%s'",
buildInfo.getReleaseLabel(),
buildInfo.getBuildRevision()));

StandaloneConfiguration configuration = new StandaloneConfiguration(options);
log.info(String.format(
"Launching a standalone Selenium Server on port %s", configuration.port));
SeleniumServer server = new SeleniumServer(configuration);
server.boot();
return server;
})

.put(GridRole.HUB, (args) -> out -> {
GridHubCliOptions options = new GridHubCliOptions();
JCommander commander = options.parse(args);

if (options.getVersion()) {
version.accept(out);
return ()->{};
}

if (options.getHelp()) {
usage.accept(commander, out);
return ()->{};
}

configureLogging(options.getLog(), options.getDebug());

log.info(String.format(
"Selenium build info: version: '%s', revision: '%s'",
buildInfo.getReleaseLabel(),
buildInfo.getBuildRevision()));

GridHubConfiguration configuration = new GridHubConfiguration(options);
log.info(String.format(
"Launching Selenium Grid hub on port %s", configuration.port));
Hub hub = new Hub(configuration);
hub.start();
return hub;
})

.put(GridRole.NODE, (args) -> out -> {
GridNodeCliOptions options = new GridNodeCliOptions();
JCommander commander = options.parse(args);

if (options.getVersion()) {
version.accept(out);
return ()->{};
}

if (options.getHelp()) {
usage.accept(commander, out);
return ()->{};
}

configureLogging(options.getLog(), options.getDebug());

log.info(String.format(
"Selenium build info: version: '%s', revision: '%s'",
buildInfo.getReleaseLabel(),
buildInfo.getBuildRevision()));

GridNodeConfiguration configuration = new GridNodeConfiguration(options);
if (configuration.port == null || configuration.port == -1) {
configuration.port = PortProber.findFreePort();
}
log.info(String.format(
"Launching a Selenium Grid node on port %s", configuration.port));
SelfRegisteringRemote remote = new SelfRegisteringRemote(configuration);
SeleniumServer server = new SeleniumServer(remote.getConfiguration());
remote.setRemoteServer(server);
if (remote.startRemoteServer()) {
log.info("Selenium Grid node is up and ready to register to the hub");
remote.startRegistrationProcess();
}
return server;
})
.build();
}
}

0 comments on commit acfc1c5

Please sign in to comment.