23
23
24
24
import org .openqa .grid .common .GridRole ;
25
25
import org .openqa .grid .common .RegistrationRequest ;
26
- import org .openqa .grid .common .exception .GridConfigurationException ;
27
- import org .openqa .grid .internal .utils .configuration .GridHubConfiguration ;
28
26
import org .openqa .grid .internal .utils .SelfRegisteringRemote ;
27
+ import org .openqa .grid .internal .utils .configuration .GridHubConfiguration ;
29
28
import org .openqa .grid .internal .utils .configuration .GridNodeConfiguration ;
30
29
import org .openqa .grid .internal .utils .configuration .StandaloneConfiguration ;
31
30
import org .openqa .grid .shared .CliUtils ;
36
35
37
36
import java .io .File ;
38
37
import java .io .IOException ;
38
+ import java .util .Arrays ;
39
+ import java .util .function .Supplier ;
39
40
import java .util .logging .ConsoleHandler ;
40
41
import java .util .logging .FileHandler ;
41
42
import java .util .logging .Handler ;
@@ -46,52 +47,57 @@ public class GridLauncherV3 {
46
47
47
48
private static final Logger log = Logger .getLogger (GridLauncherV3 .class .getName ());
48
49
49
- public static abstract class GridItemLauncher {
50
- protected Object configuration ;
50
+ private static abstract class GridItemLauncher {
51
+ protected StandaloneConfiguration configuration ;
52
+ protected boolean helpRequested ;
51
53
abstract void setConfiguration (String [] args );
52
54
abstract void launch () throws Exception ;
53
55
void printUsage () {
54
56
new JCommander (configuration ).usage ();
55
57
}
56
58
}
57
59
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 () {
61
63
public void setConfiguration (String [] args ) {
62
64
configuration = new StandaloneConfiguration ();
63
65
new JCommander (configuration , args );
66
+ helpRequested = configuration .help ;
64
67
}
68
+
65
69
public void launch () throws Exception {
66
70
log .info ("Launching a standalone Selenium Server" );
67
- SeleniumServer server = new SeleniumServer (( StandaloneConfiguration ) configuration );
71
+ SeleniumServer server = new SeleniumServer (configuration );
68
72
server .boot ();
69
73
log .info ("Selenium Server is up and running" );
70
74
}
71
75
})
72
- .put (GridRole .HUB , new GridItemLauncher () {
76
+ .put (GridRole .HUB , () -> new GridItemLauncher () {
73
77
public void setConfiguration (String [] args ) {
74
78
configuration = new GridHubConfiguration ();
75
79
new JCommander (configuration , args );
80
+ helpRequested = configuration .help ;
76
81
}
77
82
public void launch () throws Exception {
78
83
log .info ("Launching Selenium Grid hub" );
79
- Hub h = new Hub ((GridHubConfiguration )configuration );
84
+ Hub h = new Hub ((GridHubConfiguration ) configuration );
80
85
h .start ();
81
86
log .info ("Nodes should register to " + h .getRegistrationURL ());
82
87
log .info ("Selenium Grid hub is up and running" );
83
88
}
84
89
})
85
- .put (GridRole .NODE , new GridItemLauncher () {
90
+ .put (GridRole .NODE , () -> new GridItemLauncher () {
86
91
public void setConfiguration (String [] args ) {
87
92
configuration = new GridNodeConfiguration ();
88
93
new JCommander (configuration , args );
94
+ helpRequested = configuration .help ;
89
95
}
90
96
public void launch () throws Exception {
91
97
log .info ("Launching a Selenium Grid node" );
92
- RegistrationRequest c = RegistrationRequest .build ((GridNodeConfiguration )configuration );
98
+ RegistrationRequest c = RegistrationRequest .build ((GridNodeConfiguration ) configuration );
93
99
SelfRegisteringRemote remote = new SelfRegisteringRemote (c );
94
- remote .setRemoteServer (new SeleniumServer (( StandaloneConfiguration ) configuration ));
100
+ remote .setRemoteServer (new SeleniumServer (configuration ));
95
101
remote .startRemoteServer ();
96
102
log .info ("Selenium Grid node is up and ready to register to the hub" );
97
103
remote .startRegistrationProcess ();
@@ -100,33 +106,63 @@ public void launch() throws Exception {
100
106
.build ();
101
107
102
108
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 ) {
110
111
return ;
111
112
}
112
113
113
- launchers .get (role ).setConfiguration (args );
114
-
115
- if (configuration .help ) {
116
- launchers .get (role ).printUsage ();
114
+ if (launcher .helpRequested ) {
115
+ launcher .printUsage ();
117
116
return ;
118
117
}
119
118
120
- configureLogging (configuration );
119
+ configureLogging (launcher . configuration );
121
120
122
121
try {
123
- launchers . get ( role ) .launch ();
122
+ launcher .launch ();
124
123
} catch (Exception e ) {
125
- launchers . get ( role ) .printUsage ();
124
+ launcher .printUsage ();
126
125
e .printStackTrace ();
127
126
}
128
127
}
129
128
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
+
130
166
private static void printInfoAboutRoles (String roleCommandLineArg ) {
131
167
if (roleCommandLineArg != null ) {
132
168
CliUtils .printWrappedLine (
0 commit comments