Skip to content

Commit

Permalink
Refactoring GridLauncher: replacing big switch with a map of simple l…
Browse files Browse the repository at this point in the history
…aunchers; it's a step toward decomposition of the GridLauncher and dynamic discovery of the elements that can be launched.
  • Loading branch information
barancev committed Oct 15, 2015
1 parent 606cd04 commit 1127a11
Showing 1 changed file with 69 additions and 35 deletions.
104 changes: 69 additions & 35 deletions java/server/src/org/openqa/grid/selenium/GridLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.openqa.grid.selenium;

import com.google.common.collect.ImmutableMap;

import org.openqa.grid.common.CommandLineOptionHelper;
import org.openqa.grid.common.GridDocHelper;
import org.openqa.grid.common.GridRole;
Expand All @@ -43,6 +45,64 @@ public class GridLauncher {

private static final Logger log = Logger.getLogger(GridLauncher.class.getName());

public interface GridItemLauncher {
void launch(String[] args, Logger log) throws Exception;
void printUsage();
}

private static ImmutableMap<GridRole, GridItemLauncher> launchers
= new ImmutableMap.Builder<GridRole, GridItemLauncher>()
.put(GridRole.NOT_GRID, new GridItemLauncher() {
@Override
public void launch(String[] args, Logger log) throws Exception {
log.info("Launching a standalone Selenium Server");
SeleniumServer.main(args);
log.info("Selenium Server is up and running");
}

@Override
public void printUsage() {
String separator = "\n-------------------------------\n";
RemoteControlLauncher.usage(separator + "Running as a standalone server:" + separator);
}
})
.put(GridRole.HUB, new GridItemLauncher() {
@Override
public void launch(String[] args, Logger log) throws Exception {
log.info("Launching Selenium Grid hub");
GridHubConfiguration c = GridHubConfiguration.build(args);
Hub h = new Hub(c);
h.start();
log.info("Nodes should register to " + h.getRegistrationURL());
log.info("Selenium Grid hub is up and running");
}

@Override
public void printUsage() {
String separator = "\n-------------------------------\n";
GridDocHelper.printHubHelp(separator + "Running as a grid hub:" + separator, false);
}
})
.put(GridRole.NODE, new GridItemLauncher() {
@Override
public void launch(String[] args, Logger log) throws Exception {
log.info("Launching a Selenium Grid node");
RegistrationRequest c = RegistrationRequest.build(args);
SelfRegisteringRemote remote = new SelfRegisteringRemote(c);
remote.setRemoteServer(new SeleniumServer(c.getConfiguration()));
remote.startRemoteServer();
log.info("Selenium Grid node is up and ready to register to the hub");
remote.startRegistrationProcess();
}

@Override
public void printUsage() {
String separator = "\n-------------------------------\n";
GridDocHelper.printNodeHelp(separator + "Running as a grid node:" + separator, false);
}
})
.build();

public static void main(String[] args) throws Exception {
CommandLineOptionHelper helper = new CommandLineOptionHelper(args);
GridRole role = GridRole.find(args);
Expand All @@ -59,41 +119,15 @@ public static void main(String[] args) throws Exception {

configureLogging(helper);

switch (role) {
case NOT_GRID:
log.info("Launching a standalone Selenium Server");
SeleniumServer.main(args);
log.info("Selenium Server is up and running");
break;
case HUB:
log.info("Launching Selenium Grid hub");
try {
GridHubConfiguration c = GridHubConfiguration.build(args);
Hub h = new Hub(c);
h.start();
log.info("Nodes should register to " + h.getRegistrationURL());
log.info("Selenium Grid hub is up and running");
} catch (GridConfigurationException e) {
GridDocHelper.printHubHelp(e.getMessage());
e.printStackTrace();
}
break;
case NODE:
log.info("Launching a Selenium Grid node");
try {
RegistrationRequest c = RegistrationRequest.build(args);
SelfRegisteringRemote remote = new SelfRegisteringRemote(c);
remote.setRemoteServer(new SeleniumServer(c.getConfiguration()));
remote.startRemoteServer();
log.info("Selenium Grid node is up and ready to register to the hub");
remote.startRegistrationProcess();
} catch (GridConfigurationException e) {
GridDocHelper.printNodeHelp(e.getMessage());
e.printStackTrace();
}
break;
default:
throw new GridConfigurationException("Unknown role: " + role);
if (launchers.containsKey(role)) {
try {
launchers.get(role).launch(args, log);
} catch (Exception e) {
launchers.get(role).printUsage();
e.printStackTrace();
}
} else {
throw new GridConfigurationException("Unknown role: " + role);
}
}

Expand Down

0 comments on commit 1127a11

Please sign in to comment.