Skip to content

Commit d0b7253

Browse files
committed
Making GridItemLaunchers immutable
1 parent 79d6a05 commit d0b7253

File tree

1 file changed

+32
-40
lines changed

1 file changed

+32
-40
lines changed

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

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
import java.lang.reflect.Method;
4242
import java.util.HashMap;
4343
import java.util.Map;
44-
import java.util.function.Supplier;
44+
import java.util.function.Function;
4545
import java.util.logging.ConsoleHandler;
4646
import java.util.logging.FileHandler;
4747
import java.util.logging.Handler;
@@ -58,13 +58,12 @@ public class GridLauncherV3 {
5858
private static final BuildInfo buildInfo = new BuildInfo();
5959

6060
private interface GridItemLauncher {
61-
void setConfiguration(String[] args);
6261
StandaloneConfiguration getConfiguration();
6362
void launch() throws Exception;
6463
default void printUsage() { new JCommander(getConfiguration()).usage(); }
6564
}
6665

67-
private static ImmutableMap<String, Supplier<GridItemLauncher>> LAUNCHERS = buildLaunchers();
66+
private static Map<String, Function<String[], GridItemLauncher>> LAUNCHERS = buildLaunchers();
6867

6968
public static void main(String[] args) throws Exception {
7069
GridItemLauncher launcher = buildLauncher(args);
@@ -96,7 +95,7 @@ private static GridItemLauncher buildLauncher(String[] args) {
9695

9796
for (int i = 0; i < args.length; i++) {
9897
if (args[i].equals("-htmlSuite")) {
99-
Supplier<GridItemLauncher> launcherSupplier = LAUNCHERS.get("corerunner");
98+
Function<String[], GridItemLauncher> launcherSupplier = LAUNCHERS.get("corerunner");
10099
if (launcherSupplier == null) {
101100
System.err.println(Joiner.on("\n").join(
102101
"Unable to find the HTML runner. This is normally because you have not downloaded",
@@ -107,9 +106,7 @@ private static GridItemLauncher buildLauncher(String[] args) {
107106
"running your HTML suite."));
108107
return null;
109108
}
110-
GridItemLauncher launcher = launcherSupplier.get();
111-
launcher.setConfiguration(args);
112-
return launcher;
109+
return launcherSupplier.apply(args);
113110
}
114111
if (args[i].startsWith("-role=")) {
115112
role = args[i].substring("-role=".length());
@@ -129,13 +126,12 @@ private static GridItemLauncher buildLauncher(String[] args) {
129126
return null;
130127
}
131128

132-
Supplier<GridItemLauncher> supplier = LAUNCHERS.get(gridRole.toString());
129+
Function<String[], GridItemLauncher> supplier = LAUNCHERS.get(gridRole.toString());
133130
if (supplier == null) {
134131
System.err.println("Unknown role: " + gridRole);
135132
return null;
136133
}
137-
GridItemLauncher toReturn = supplier.get();
138-
toReturn.setConfiguration(args);
134+
GridItemLauncher toReturn = supplier.apply(args);
139135

140136
if (toReturn.getConfiguration().help) {
141137
toReturn.printUsage();
@@ -239,18 +235,17 @@ private static void configureLogging(StandaloneConfiguration configuration) {
239235
}
240236
}
241237

242-
private static ImmutableMap<String, Supplier<GridItemLauncher>> buildLaunchers() {
243-
ImmutableMap.Builder<String, Supplier<GridItemLauncher>> launchers =
244-
ImmutableMap.<String, Supplier<GridItemLauncher>>builder()
245-
.put(GridRole.NOT_GRID.toString(), () -> new GridItemLauncher() {
246-
StandaloneConfiguration configuration;
247-
public StandaloneConfiguration getConfiguration() {
248-
return configuration;
238+
private static Map<String, Function<String[], GridItemLauncher>> buildLaunchers() {
239+
ImmutableMap.Builder<String, Function<String[], GridItemLauncher>> launchers =
240+
ImmutableMap.<String, Function<String[], GridItemLauncher>>builder()
241+
.put(GridRole.NOT_GRID.toString(), (args) -> new GridItemLauncher() {
242+
StandaloneConfiguration configuration = new StandaloneConfiguration();
243+
{
244+
JCommander.newBuilder().addObject(configuration).build().parse(args);
249245
}
250246

251-
public void setConfiguration(String[] args) {
252-
configuration = new StandaloneConfiguration();
253-
JCommander.newBuilder().addObject(configuration).build().parse(args);
247+
public StandaloneConfiguration getConfiguration() {
248+
return configuration;
254249
}
255250

256251
public void launch() throws Exception {
@@ -263,13 +258,9 @@ public void launch() throws Exception {
263258
server.boot();
264259
}
265260
})
266-
.put(GridRole.HUB.toString(), () -> new GridItemLauncher() {
261+
.put(GridRole.HUB.toString(), (args) -> new GridItemLauncher() {
267262
GridHubConfiguration configuration;
268-
public StandaloneConfiguration getConfiguration() {
269-
return configuration;
270-
}
271-
272-
public void setConfiguration(String[] args) {
263+
{
273264
GridHubConfiguration pending = new GridHubConfiguration();
274265
JCommander.newBuilder().addObject(pending).build().parse(args);
275266
configuration = pending;
@@ -281,20 +272,20 @@ public void setConfiguration(String[] args) {
281272
}
282273
}
283274

275+
public StandaloneConfiguration getConfiguration() {
276+
return configuration;
277+
}
278+
284279
public void launch() throws Exception {
285280
log.info(String.format(
286281
"Launching Selenium Grid hub on port %s", configuration.port));
287282
Hub h = new Hub(configuration);
288283
h.start();
289284
}
290285
})
291-
.put(GridRole.NODE.toString(), () -> new GridItemLauncher() {
286+
.put(GridRole.NODE.toString(), (args) -> new GridItemLauncher() {
292287
GridNodeConfiguration configuration;
293-
public StandaloneConfiguration getConfiguration() {
294-
return configuration;
295-
}
296-
297-
public void setConfiguration(String[] args) {
288+
{
298289
GridNodeConfiguration pending = new GridNodeConfiguration();
299290
JCommander.newBuilder().addObject(pending).build().parse(args);
300291
configuration = pending;
@@ -309,6 +300,10 @@ public void setConfiguration(String[] args) {
309300
}
310301
}
311302

303+
public StandaloneConfiguration getConfiguration() {
304+
return configuration;
305+
}
306+
312307
public void launch() throws Exception {
313308
log.info(String.format(
314309
"Launching a Selenium Grid node on port %s", configuration.port));
@@ -324,18 +319,15 @@ public void launch() throws Exception {
324319
try {
325320
Class.forName(CORE_RUNNER_CLASS, false, GridLauncherV3.class.getClassLoader());
326321

327-
launchers.put("corerunner", () -> new GridItemLauncher() {
328-
CoreRunnerConfiguration configuration;
322+
launchers.put("corerunner", (args) -> new GridItemLauncher() {
323+
CoreRunnerConfiguration configuration = new CoreRunnerConfiguration();
324+
{
325+
JCommander.newBuilder().addObject(configuration).build().parse(args);
326+
}
329327
public StandaloneConfiguration getConfiguration() {
330328
return configuration;
331329
}
332330

333-
@Override
334-
public void setConfiguration(String[] args) {
335-
configuration = new CoreRunnerConfiguration();
336-
JCommander.newBuilder().addObject(configuration).build().parse(args);
337-
}
338-
339331
@Override
340332
public void launch() throws Exception {
341333
Class<?> coreRunnerClass = Class.forName(CORE_RUNNER_CLASS);

0 commit comments

Comments
 (0)