Skip to content

Commit 01d9bc7

Browse files
committed
Rework the V3 launcher.
This resolves two issues: 1) The map of possible roles and configs wasn't thread safe. It is now. 2) The server could only be started in standalone mode, and didn't allow node or hub configs to be started from the main.
1 parent 529ea7c commit 01d9bc7

File tree

1 file changed

+63
-27
lines changed

1 file changed

+63
-27
lines changed

java/server/src/org/openqa/grid/selenium/GridLauncherV3.java

Lines changed: 63 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@
2323

2424
import org.openqa.grid.common.GridRole;
2525
import org.openqa.grid.common.RegistrationRequest;
26-
import org.openqa.grid.common.exception.GridConfigurationException;
27-
import org.openqa.grid.internal.utils.configuration.GridHubConfiguration;
2826
import org.openqa.grid.internal.utils.SelfRegisteringRemote;
27+
import org.openqa.grid.internal.utils.configuration.GridHubConfiguration;
2928
import org.openqa.grid.internal.utils.configuration.GridNodeConfiguration;
3029
import org.openqa.grid.internal.utils.configuration.StandaloneConfiguration;
3130
import org.openqa.grid.shared.CliUtils;
@@ -36,6 +35,8 @@
3635

3736
import java.io.File;
3837
import java.io.IOException;
38+
import java.util.Arrays;
39+
import java.util.function.Supplier;
3940
import java.util.logging.ConsoleHandler;
4041
import java.util.logging.FileHandler;
4142
import java.util.logging.Handler;
@@ -46,52 +47,57 @@ public class GridLauncherV3 {
4647

4748
private static final Logger log = Logger.getLogger(GridLauncherV3.class.getName());
4849

49-
public static abstract class GridItemLauncher {
50-
protected Object configuration;
50+
private static abstract class GridItemLauncher {
51+
protected StandaloneConfiguration configuration;
52+
protected boolean helpRequested;
5153
abstract void setConfiguration(String[] args);
5254
abstract void launch() throws Exception;
5355
void printUsage() {
5456
new JCommander(configuration).usage();
5557
}
5658
}
5759

58-
private static ImmutableMap<GridRole, GridItemLauncher> launchers
59-
= new ImmutableMap.Builder<GridRole, GridItemLauncher>()
60-
.put(GridRole.NOT_GRID, new GridItemLauncher() {
60+
private static ImmutableMap<GridRole, Supplier<GridItemLauncher>> LAUNCHERS =
61+
ImmutableMap.<GridRole, Supplier<GridItemLauncher>>builder()
62+
.put(GridRole.NOT_GRID, () -> new GridItemLauncher() {
6163
public void setConfiguration(String[] args) {
6264
configuration = new StandaloneConfiguration();
6365
new JCommander(configuration, args);
66+
helpRequested = configuration.help;
6467
}
68+
6569
public void launch() throws Exception {
6670
log.info("Launching a standalone Selenium Server");
67-
SeleniumServer server = new SeleniumServer((StandaloneConfiguration)configuration);
71+
SeleniumServer server = new SeleniumServer(configuration);
6872
server.boot();
6973
log.info("Selenium Server is up and running");
7074
}
7175
})
72-
.put(GridRole.HUB, new GridItemLauncher() {
76+
.put(GridRole.HUB, () -> new GridItemLauncher() {
7377
public void setConfiguration(String[] args) {
7478
configuration = new GridHubConfiguration();
7579
new JCommander(configuration, args);
80+
helpRequested = configuration.help;
7681
}
7782
public void launch() throws Exception {
7883
log.info("Launching Selenium Grid hub");
79-
Hub h = new Hub((GridHubConfiguration)configuration);
84+
Hub h = new Hub((GridHubConfiguration) configuration);
8085
h.start();
8186
log.info("Nodes should register to " + h.getRegistrationURL());
8287
log.info("Selenium Grid hub is up and running");
8388
}
8489
})
85-
.put(GridRole.NODE, new GridItemLauncher() {
90+
.put(GridRole.NODE, () -> new GridItemLauncher() {
8691
public void setConfiguration(String[] args) {
8792
configuration = new GridNodeConfiguration();
8893
new JCommander(configuration, args);
94+
helpRequested = configuration.help;
8995
}
9096
public void launch() throws Exception {
9197
log.info("Launching a Selenium Grid node");
92-
RegistrationRequest c = RegistrationRequest.build((GridNodeConfiguration)configuration);
98+
RegistrationRequest c = RegistrationRequest.build((GridNodeConfiguration) configuration);
9399
SelfRegisteringRemote remote = new SelfRegisteringRemote(c);
94-
remote.setRemoteServer(new SeleniumServer((StandaloneConfiguration)configuration));
100+
remote.setRemoteServer(new SeleniumServer(configuration));
95101
remote.startRemoteServer();
96102
log.info("Selenium Grid node is up and ready to register to the hub");
97103
remote.startRegistrationProcess();
@@ -100,33 +106,63 @@ public void launch() throws Exception {
100106
.build();
101107

102108
public static void main(String[] args) throws Exception {
103-
StandaloneConfiguration configuration = new StandaloneConfiguration();
104-
new JCommander(configuration, args);
105-
106-
GridRole role = GridRole.get(configuration.role);
107-
108-
if (role == null) {
109-
printInfoAboutRoles(configuration.role);
109+
GridItemLauncher launcher = buildLauncher(args);
110+
if (launcher == null) {
110111
return;
111112
}
112113

113-
launchers.get(role).setConfiguration(args);
114-
115-
if (configuration.help) {
116-
launchers.get(role).printUsage();
114+
if (launcher.helpRequested) {
115+
launcher.printUsage();
117116
return;
118117
}
119118

120-
configureLogging(configuration);
119+
configureLogging(launcher.configuration);
121120

122121
try {
123-
launchers.get(role).launch();
122+
launcher.launch();
124123
} catch (Exception e) {
125-
launchers.get(role).printUsage();
124+
launcher.printUsage();
126125
e.printStackTrace();
127126
}
128127
}
129128

129+
/**
130+
* From the {@code args}, builds a new {@link GridItemLauncher} and populates it properly.
131+
*
132+
* @return null if no role is found, or a properly populated {@link GridItemLauncher}.
133+
*/
134+
private static GridItemLauncher buildLauncher(String[] args) {
135+
String role = "standalone";
136+
137+
for (int i = 0; i < args.length; i++) {
138+
if (args[i].startsWith("-role=")) {
139+
role = args[i].substring("-role=".length());
140+
} else if (args[i].equals("-role")) {
141+
i++; // Increment, because we're going to need this.
142+
if (i < args.length) {
143+
role = args[i];
144+
} else {
145+
role = null; // Will cause us to print the usage information.
146+
}
147+
}
148+
}
149+
150+
GridRole gridRole = GridRole.get(role);
151+
if (gridRole == null) {
152+
printInfoAboutRoles(role);
153+
return null;
154+
}
155+
156+
Supplier<GridItemLauncher> supplier = LAUNCHERS.get(gridRole);
157+
if (supplier == null) {
158+
System.err.println("Unknown role: " + gridRole);
159+
return null;
160+
}
161+
GridItemLauncher toReturn = supplier.get();
162+
toReturn.setConfiguration(args);
163+
return toReturn;
164+
}
165+
130166
private static void printInfoAboutRoles(String roleCommandLineArg) {
131167
if (roleCommandLineArg != null) {
132168
CliUtils.printWrappedLine(

0 commit comments

Comments
 (0)