|
19 | 19 |
|
20 | 20 | import org.openqa.selenium.internal.BuildInfo; |
21 | 21 |
|
| 22 | +import java.io.BufferedReader; |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.InputStreamReader; |
22 | 25 | import java.net.InetAddress; |
| 26 | +import java.net.NetworkInterface; |
| 27 | +import java.net.SocketException; |
23 | 28 | import java.net.UnknownHostException; |
| 29 | +import java.util.Enumeration; |
24 | 30 | import java.util.HashMap; |
25 | 31 | import java.util.Map; |
| 32 | +import java.util.concurrent.TimeUnit; |
26 | 33 |
|
27 | 34 | public class WebDriverException extends RuntimeException { |
28 | 35 |
|
29 | 36 | public static final String SESSION_ID = "Session ID"; |
30 | 37 | public static final String DRIVER_INFO = "Driver info"; |
31 | 38 | protected static final String BASE_SUPPORT_URL = "http://seleniumhq.org/exceptions/"; |
32 | 39 |
|
| 40 | + private final static String HOST_NAME; |
| 41 | + private final static String HOST_ADDRESS; |
| 42 | + |
33 | 43 | private Map<String, String> extraInfo = new HashMap<>(); |
34 | 44 |
|
| 45 | + static { |
| 46 | + // Ideally, we'd use InetAddress.getLocalHost, but this does a reverse DNS lookup. On Windows |
| 47 | + // and Linux this is apparently pretty fast, so we don't get random hangs. On OS X it's |
| 48 | + // amazingly slow. That's less than ideal. Figure things out and cache. We can't rely on |
| 49 | + // Platform since that depends on this class, but fortunately there's only one place we have to |
| 50 | + // worry about slow lookups. |
| 51 | + |
| 52 | + String current = System.getProperty("os.name"); |
| 53 | + String host = System.getenv("HOSTNAME"); // Most OSs |
| 54 | + if (host == null) { |
| 55 | + host = System.getenv("COMPUTERNAME"); // Windows |
| 56 | + } |
| 57 | + if (host == null && "Mac OS X".equals(current)) { |
| 58 | + try { |
| 59 | + Process process = Runtime.getRuntime().exec("hostname"); |
| 60 | + |
| 61 | + if (!process.waitFor(2, TimeUnit.SECONDS)) { |
| 62 | + process.destroyForcibly(); |
| 63 | + } |
| 64 | + if (process.exitValue() == 0) { |
| 65 | + try (InputStreamReader isr = new InputStreamReader(process.getInputStream()); |
| 66 | + BufferedReader reader = new BufferedReader(isr)) { |
| 67 | + host = reader.readLine(); |
| 68 | + } |
| 69 | + } |
| 70 | + } catch (IOException ignored) { |
| 71 | + // Do nothing and fall through |
| 72 | + } catch (InterruptedException e) { |
| 73 | + Thread.currentThread().interrupt(); |
| 74 | + throw new RuntimeException(e); |
| 75 | + } |
| 76 | + } |
| 77 | + if (host == null) { |
| 78 | + // Give up. |
| 79 | + try { |
| 80 | + host = InetAddress.getLocalHost().getHostName(); |
| 81 | + } catch (UnknownHostException e) { |
| 82 | + host = "Unknown"; // At least we tried. |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + HOST_NAME = host; |
| 87 | + |
| 88 | + String address = null; |
| 89 | + // Now for the IP address. We're going to do silly shenanigans on OS X only. |
| 90 | + if ("Mac OS X".equals(current)) { |
| 91 | + try { |
| 92 | + NetworkInterface en0 = NetworkInterface.getByName("en0"); |
| 93 | + Enumeration<InetAddress> addresses = en0.getInetAddresses(); |
| 94 | + while (addresses.hasMoreElements()) { |
| 95 | + InetAddress inetAddress = addresses.nextElement(); |
| 96 | + address = inetAddress.getHostAddress(); |
| 97 | + break; |
| 98 | + } |
| 99 | + } catch (SocketException e) { |
| 100 | + // Fall through and go the slow way. |
| 101 | + } |
| 102 | + } |
| 103 | + if (address == null) { |
| 104 | + // Alright. I give up. |
| 105 | + try { |
| 106 | + address = InetAddress.getLocalHost().getHostAddress(); |
| 107 | + } catch (UnknownHostException e) { |
| 108 | + address = "Unknown"; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + HOST_ADDRESS = address; |
| 113 | + } |
| 114 | + |
35 | 115 | public WebDriverException() { |
36 | 116 | super(); |
37 | 117 | } |
@@ -66,21 +146,13 @@ private String createMessage(String originalMessageString) { |
66 | 146 | } |
67 | 147 |
|
68 | 148 | public String getSystemInformation() { |
69 | | - String host = "N/A"; |
70 | | - String ip = "N/A"; |
71 | | - |
72 | | - try{ |
73 | | - host = InetAddress.getLocalHost().getHostName(); |
74 | | - ip = InetAddress.getLocalHost().getHostAddress(); |
75 | | - } catch (UnknownHostException throw_away) {} |
76 | | - |
77 | 149 | return String.format("System info: host: '%s', ip: '%s', os.name: '%s', os.arch: '%s', os.version: '%s', java.version: '%s'", |
78 | | - host, |
79 | | - ip, |
80 | | - System.getProperty("os.name"), |
81 | | - System.getProperty("os.arch"), |
82 | | - System.getProperty("os.version"), |
83 | | - System.getProperty("java.version")); |
| 150 | + HOST_NAME, |
| 151 | + HOST_ADDRESS, |
| 152 | + System.getProperty("os.name"), |
| 153 | + System.getProperty("os.arch"), |
| 154 | + System.getProperty("os.version"), |
| 155 | + System.getProperty("java.version")); |
84 | 156 | } |
85 | 157 |
|
86 | 158 | public String getSupportUrl() { |
@@ -108,7 +180,7 @@ public void addInfo(String key, String value) { |
108 | 180 | } |
109 | 181 |
|
110 | 182 | public String getAdditionalInformation() { |
111 | | - if (! extraInfo.containsKey(DRIVER_INFO)) { |
| 183 | + if (!extraInfo.containsKey(DRIVER_INFO)) { |
112 | 184 | extraInfo.put(DRIVER_INFO, "driver.version: " + getDriverName(getStackTrace())); |
113 | 185 | } |
114 | 186 |
|
|
0 commit comments