|
2 | 2 |
|
3 | 3 | import com.bugsnag.Report; |
4 | 4 |
|
| 5 | +import com.google.common.base.Supplier; |
| 6 | +import com.google.common.base.Suppliers; |
| 7 | + |
5 | 8 | import java.net.InetAddress; |
6 | 9 | import java.net.UnknownHostException; |
7 | 10 | import java.util.Locale; |
8 | 11 |
|
9 | 12 | public class DeviceCallback implements Callback { |
| 13 | + private static final Supplier<String> hostnameCache = |
| 14 | + Suppliers.memoize(new Supplier<String>() { |
| 15 | + |
| 16 | + public String get() { |
| 17 | + // Windows always sets COMPUTERNAME |
| 18 | + if (System.getProperty("os.name").startsWith("Windows")) { |
| 19 | + return System.getenv("COMPUTERNAME"); |
| 20 | + } |
| 21 | + |
| 22 | + // Try the HOSTNAME env variable (most unix systems) |
| 23 | + String hostname = System.getenv("HOSTNAME"); |
| 24 | + if (hostname != null) { |
| 25 | + return hostname; |
| 26 | + } |
| 27 | + |
| 28 | + // Resort to dns hostname lookup |
| 29 | + try { |
| 30 | + return InetAddress.getLocalHost().getHostName(); |
| 31 | + } catch (UnknownHostException ex) { |
| 32 | + // Give up |
| 33 | + } |
| 34 | + return null; |
| 35 | + } |
| 36 | + }); |
| 37 | + |
10 | 38 | @Override |
11 | 39 | public void beforeNotify(Report report) { |
12 | 40 | report |
13 | | - .setDeviceInfo("hostname", getHostname()) |
| 41 | + .setDeviceInfo("hostname", hostnameCache.get()) |
14 | 42 | .setDeviceInfo("osName", System.getProperty("os.name")) |
15 | 43 | .setDeviceInfo("osVersion", System.getProperty("os.version")) |
16 | 44 | .setDeviceInfo("osArch", System.getProperty("os.arch")) |
17 | 45 | .setDeviceInfo("runtimeName", System.getProperty("java.runtime.name")) |
18 | 46 | .setDeviceInfo("runtimeVersion", System.getProperty("java.runtime.version")) |
19 | 47 | .setDeviceInfo("locale", Locale.getDefault()); |
20 | 48 | } |
21 | | - |
22 | | - private String getHostname() { |
23 | | - // Windows always sets COMPUTERNAME |
24 | | - if (System.getProperty("os.name").startsWith("Windows")) { |
25 | | - return System.getenv("COMPUTERNAME"); |
26 | | - } |
27 | | - |
28 | | - // Try the HOSTNAME env variable (most unix systems) |
29 | | - String hostname = System.getenv("HOSTNAME"); |
30 | | - if (hostname != null) { |
31 | | - return hostname; |
32 | | - } |
33 | | - |
34 | | - // Resort to dns hostname lookup |
35 | | - try { |
36 | | - return InetAddress.getLocalHost().getHostName(); |
37 | | - } catch (UnknownHostException ex) { |
38 | | - // Give up |
39 | | - } |
40 | | - |
41 | | - return null; |
42 | | - } |
43 | 49 | } |
0 commit comments