Repository navigation
Expand file tree
/
Copy pathHub.java
More file actions
218 lines (174 loc) · 7.06 KB
/
Copy pathHub.java
File metadata and controls
218 lines (174 loc) · 7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.openqa.grid.web;
import com.google.common.collect.Maps;
import org.openqa.grid.internal.Registry;
import org.openqa.grid.internal.utils.configuration.GridHubConfiguration;
import org.openqa.grid.web.servlet.DisplayHelpServlet;
import org.openqa.grid.web.servlet.DriverServlet;
import org.openqa.grid.web.servlet.Grid1HeartbeatServlet;
import org.openqa.grid.web.servlet.HubStatusServlet;
import org.openqa.grid.web.servlet.LifecycleServlet;
import org.openqa.grid.web.servlet.ProxyStatusServlet;
import org.openqa.grid.web.servlet.RegistrationServlet;
import org.openqa.grid.web.servlet.ResourceServlet;
import org.openqa.grid.web.servlet.TestSessionStatusServlet;
import org.openqa.grid.web.servlet.beta.ConsoleServlet;
import org.openqa.grid.web.utils.ExtraServletUtil;
import org.openqa.selenium.net.NetworkUtils;
import org.seleniumhq.jetty9.server.HttpConfiguration;
import org.seleniumhq.jetty9.server.HttpConnectionFactory;
import org.seleniumhq.jetty9.server.Server;
import org.seleniumhq.jetty9.server.ServerConnector;
import org.seleniumhq.jetty9.servlet.ServletContextHandler;
import org.seleniumhq.jetty9.util.thread.QueuedThreadPool;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.logging.Logger;
import javax.servlet.Servlet;
/**
* Jetty server. Main entry point for everything about the grid. <p> Except for unit tests, this
* should be a singleton.
*/
public class Hub {
private static final Logger log = Logger.getLogger(Hub.class.getName());
private GridHubConfiguration config;
private final Registry registry;
private final Map<String, Class<? extends Servlet>> extraServlet = Maps.newHashMap();
private Server server;
private void addServlet(String key, Class<? extends Servlet> s) {
extraServlet.put(key, s);
}
/**
* get the registry backing up the hub state.
*
* @return The registry
*/
public Registry getRegistry() {
return registry;
}
public Hub(GridHubConfiguration gridHubConfiguration) {
registry = Registry.newInstance(this, gridHubConfiguration);
config = gridHubConfiguration;
if (config.host == null) {
NetworkUtils utils = new NetworkUtils();
config.host = utils.getIp4NonLoopbackAddressOfThisMachine().getHostAddress();
}
if (config.port == null) {
config.port = 4444;
}
if (config.servlets != null) {
for (String s : config.servlets) {
Class<? extends Servlet> servletClass = ExtraServletUtil.createServlet(s);
if (servletClass != null) {
String path = "/grid/admin/" + servletClass.getSimpleName() + "/*";
log.info("binding " + servletClass.getCanonicalName() + " to " + path);
addServlet(path, servletClass);
}
}
}
}
private void addDefaultServlets(ServletContextHandler handler) {
// add mandatory default servlets
handler.addServlet(RegistrationServlet.class.getName(), "/grid/register/*");
handler.addServlet(DriverServlet.class.getName(), "/wd/hub/*");
handler.addServlet(DriverServlet.class.getName(), "/selenium-server/driver/*");
handler.addServlet(ProxyStatusServlet.class.getName(), "/grid/api/proxy/*");
handler.addServlet(HubStatusServlet.class.getName(), "/grid/api/hub/*");
handler.addServlet(TestSessionStatusServlet.class.getName(), "/grid/api/testsession/*");
// add optional default servlets
if (!config.isWithOutServlet(ResourceServlet.class)) {
handler.addServlet(ResourceServlet.class.getName(), "/grid/resources/*");
}
if (!config.isWithOutServlet(DisplayHelpServlet.class)) {
handler.addServlet(DisplayHelpServlet.class.getName(), "/*");
handler.setInitParameter(DisplayHelpServlet.HELPER_TYPE_PARAMETER, config.role);
}
if (!config.isWithOutServlet(ConsoleServlet.class)) {
handler.addServlet(ConsoleServlet.class.getName(), "/grid/console/*");
handler.setInitParameter(ConsoleServlet.CONSOLE_PATH_PARAMETER, "/grid/console");
}
if (!config.isWithOutServlet(LifecycleServlet.class)) {
handler.addServlet(LifecycleServlet.class.getName(), "/lifecycle-manager/*");
}
if (!config.isWithOutServlet(Grid1HeartbeatServlet.class)) {
handler.addServlet(Grid1HeartbeatServlet.class.getName(), "/heartbeat");
}
}
private void initServer() {
try {
if (config.jettyMaxThreads != null && config.jettyMaxThreads > 0) {
QueuedThreadPool pool = new QueuedThreadPool();
pool.setMaxThreads(config.jettyMaxThreads);
server = new Server(pool);
} else {
server = new Server();
}
HttpConfiguration httpConfig = new HttpConfiguration();
httpConfig.setSecureScheme("https");
httpConfig.setSecurePort(config.port);
log.info("Will listen on " + config.port);
ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(httpConfig));
http.setPort(config.port);
server.addConnector(http);
ServletContextHandler root = new ServletContextHandler(ServletContextHandler.SESSIONS);
root.setContextPath("/");
server.setHandler(root);
root.setAttribute(Registry.KEY, registry);
addDefaultServlets(root);
// Load any additional servlets provided by the user.
for (Map.Entry<String, Class<? extends Servlet>> entry : extraServlet.entrySet()) {
root.addServlet(entry.getValue().getName(), entry.getKey());
}
} catch (Throwable e) {
throw new RuntimeException("Error initializing the hub " + e.getMessage(), e);
}
}
public GridHubConfiguration getConfiguration() {
return config;
}
public void start() throws Exception {
initServer();
server.start();
}
public void stop() throws Exception {
server.stop();
}
public URL getUrl() {
return getUrl("");
}
public URL getUrl(String path) {
try {
return new URL("http://" + config.host + ":" + config.port + path);
} catch (MalformedURLException e) {
throw new RuntimeException(e.getMessage());
}
}
public URL getRegistrationURL() {
return getUrl("/grid/register/");
}
/**
* @return URL one would use to request a new WebDriver session on this hub.
*/
public URL getWebDriverHubRequestURL() {
return getUrl("/wd/hub");
}
public URL getConsoleURL() {
return getUrl("/grid/console");
}
}