2929import static org .openqa .selenium .remote .tracing .EventAttribute .setValue ;
3030import static org .openqa .selenium .remote .tracing .Tags .EXCEPTION ;
3131
32- import com .google .common .annotations .VisibleForTesting ;
33-
3432import org .openqa .selenium .Capabilities ;
3533import org .openqa .selenium .ImmutableCapabilities ;
3634import org .openqa .selenium .SessionNotCreatedException ;
3937import org .openqa .selenium .grid .node .ActiveSession ;
4038import org .openqa .selenium .grid .node .ProtocolConvertingSession ;
4139import org .openqa .selenium .grid .node .SessionFactory ;
40+ import org .openqa .selenium .internal .Debug ;
4241import org .openqa .selenium .internal .Either ;
4342import org .openqa .selenium .internal .Require ;
4443import org .openqa .selenium .remote .Command ;
4746import org .openqa .selenium .remote .ProtocolHandshake ;
4847import org .openqa .selenium .remote .Response ;
4948import org .openqa .selenium .remote .SessionId ;
49+ import org .openqa .selenium .remote .http .AddSeleniumUserAgent ;
50+ import org .openqa .selenium .remote .http .ClientConfig ;
51+ import org .openqa .selenium .remote .http .Contents ;
5052import org .openqa .selenium .remote .http .HttpClient ;
53+ import org .openqa .selenium .remote .http .HttpMethod ;
54+ import org .openqa .selenium .remote .http .HttpRequest ;
55+ import org .openqa .selenium .remote .http .HttpResponse ;
5156import org .openqa .selenium .remote .tracing .EventAttributeValue ;
5257import org .openqa .selenium .remote .tracing .Span ;
5358import org .openqa .selenium .remote .tracing .Status ;
6166import java .util .Map ;
6267import java .util .Objects ;
6368import java .util .Set ;
69+ import java .util .logging .Level ;
6470import java .util .logging .Logger ;
6571
6672public class RelaySessionFactory implements SessionFactory {
@@ -70,16 +76,19 @@ public class RelaySessionFactory implements SessionFactory {
7076 private final Tracer tracer ;
7177 private final HttpClient .Factory clientFactory ;
7278 private final URL serviceUrl ;
79+ private final URL serviceStatusUrl ;
7380 private final Capabilities stereotype ;
7481
7582 public RelaySessionFactory (
7683 Tracer tracer ,
7784 HttpClient .Factory clientFactory ,
7885 URI serviceUri ,
86+ URI serviceStatusUri ,
7987 Capabilities stereotype ) {
8088 this .tracer = Require .nonNull ("Tracer" , tracer );
8189 this .clientFactory = Require .nonNull ("HTTP client" , clientFactory );
82- this .serviceUrl = createServiceUrl (Require .nonNull ("Service URL" , serviceUri ));
90+ this .serviceUrl = createUrlFromUri (Require .nonNull ("Service URL" , serviceUri ));
91+ this .serviceStatusUrl = createUrlFromUri (serviceStatusUri );
8392 this .stereotype = ImmutableCapabilities
8493 .copyOf (Require .nonNull ("Stereotype" , stereotype ));
8594 }
@@ -186,14 +195,32 @@ public void stop() {
186195 }
187196 }
188197
189- @ VisibleForTesting
190- URL getServiceUrl () {
191- return serviceUrl ;
198+ public boolean isServiceUp () {
199+ if (serviceStatusUrl == null ) {
200+ // If no status endpoint was configured, we assume the server is up.
201+ return true ;
202+ }
203+ try {
204+ ClientConfig config = ClientConfig .defaultConfig ()
205+ .baseUri (serviceStatusUrl .toURI ())
206+ .filter (new AddSeleniumUserAgent ());
207+ HttpClient client = clientFactory .createClient (config );
208+ HttpResponse response = client
209+ .execute (new HttpRequest (HttpMethod .GET , serviceStatusUrl .toString ()));
210+ LOG .log (Debug .getDebugLogLevel (), Contents .string (response ));
211+ return response .getStatus () == 200 ;
212+ } catch (Exception e ) {
213+ LOG .log (Level .WARNING , "Error checking service status " + serviceStatusUrl , e );
214+ }
215+ return false ;
192216 }
193217
194- private URL createServiceUrl (URI serviceUri ) {
218+ private URL createUrlFromUri (URI uri ) {
219+ if (uri == null ) {
220+ return null ;
221+ }
195222 try {
196- return serviceUri .toURL ();
223+ return uri .toURL ();
197224 } catch (MalformedURLException e ) {
198225 throw new RuntimeException (e .getMessage (), e );
199226 }
0 commit comments