17
17
18
18
package org .openqa .selenium .net ;
19
19
20
+ import static java .util .logging .Level .WARNING ;
21
+
20
22
import org .openqa .selenium .Platform ;
21
23
22
24
import java .io .BufferedReader ;
26
28
import java .nio .charset .Charset ;
27
29
import java .util .Enumeration ;
28
30
import java .util .concurrent .TimeUnit ;
31
+ import java .util .logging .Level ;
32
+ import java .util .logging .Logger ;
29
33
30
34
public class HostIdentifier {
31
- private static final String HOST_NAME ;
32
- private static final String HOST_ADDRESS ;
35
+ private static final Logger log = Logger .getLogger (HostIdentifier .class .getName ());
36
+
37
+ private static volatile String hostName ;
38
+ private static volatile String hostAddress ;
33
39
34
- static {
40
+ private static String resolveHostName () {
35
41
// Ideally, we'd use InetAddress.getLocalHost, but this does a reverse DNS lookup. On Windows
36
42
// and Linux this is apparently pretty fast, so we don't get random hangs. On OS X it's
37
43
// amazingly slow. That's less than ideal. Figure things out and cache.
@@ -49,29 +55,35 @@ public class HostIdentifier {
49
55
process .waitFor (2 , TimeUnit .SECONDS );
50
56
}
51
57
if (process .exitValue () == 0 ) {
52
- try (InputStreamReader isr = new InputStreamReader (process .getInputStream (), Charset .defaultCharset ());
58
+ try (InputStreamReader isr = new InputStreamReader (process .getInputStream (),
59
+ Charset .defaultCharset ());
53
60
BufferedReader reader = new BufferedReader (isr )) {
54
61
host = reader .readLine ();
55
62
}
56
63
}
57
64
} catch (InterruptedException e ) {
65
+ log .log (WARNING , "Failed to resolve host name" , e );
58
66
Thread .currentThread ().interrupt ();
59
67
throw new RuntimeException (e );
60
- } catch (Exception e ) {
68
+ } catch (Throwable e ) {
61
69
// fall through
70
+ log .log (WARNING , "Failed to resolve host name" , e );
62
71
}
63
72
}
64
73
if (host == null ) {
65
74
// Give up.
66
75
try {
67
76
host = InetAddress .getLocalHost ().getHostName ();
68
- } catch (Exception e ) {
77
+ } catch (Throwable e ) {
69
78
host = "Unknown" ; // At least we tried.
79
+ log .log (WARNING , "Failed to resolve host name" , e );
70
80
}
71
81
}
72
82
73
- HOST_NAME = host ;
83
+ return host ;
84
+ }
74
85
86
+ private static String resolveHostAddress () {
75
87
String address = null ;
76
88
// Now for the IP address. We're going to do silly shenanigans on OS X only.
77
89
if (Platform .getCurrent ().is (Platform .MAC )) {
@@ -81,27 +93,35 @@ public class HostIdentifier {
81
93
if (addresses .hasMoreElements ()) {
82
94
address = addresses .nextElement ().getHostAddress ();
83
95
}
84
- } catch (Exception e ) {
96
+ } catch (Throwable e ) {
85
97
// Fall through and go the slow way.
98
+ log .log (WARNING , "Failed to resolve host address" , e );
86
99
}
87
100
}
88
101
if (address == null ) {
89
102
// Alright. I give up.
90
103
try {
91
104
address = InetAddress .getLocalHost ().getHostAddress ();
92
- } catch (Exception e ) {
105
+ } catch (Throwable e ) {
93
106
address = "Unknown" ;
107
+ log .log (WARNING , "Failed to resolve host address" , e );
94
108
}
95
109
}
96
110
97
- HOST_ADDRESS = address ;
111
+ return address ;
98
112
}
99
113
100
- public static String getHostName () {
101
- return HOST_NAME ;
114
+ public static synchronized String getHostName () {
115
+ if (hostName == null ) {
116
+ hostName = resolveHostName ();
117
+ }
118
+ return hostName ;
102
119
}
103
120
104
- public static String getHostAddress () {
105
- return HOST_ADDRESS ;
121
+ public static synchronized String getHostAddress () {
122
+ if (hostAddress == null ) {
123
+ hostAddress = resolveHostAddress ();
124
+ }
125
+ return hostAddress ;
106
126
}
107
127
}
0 commit comments