package com.bugsnag.callbacks;

import com.bugsnag.Report;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Locale;

public class DeviceCallback implements Callback {
    private static final Supplier<String> hostnameCache =
        Suppliers.memoize(new Supplier<String>() {

            public String get() {
                // Windows always sets COMPUTERNAME
                if (System.getProperty("os.name").startsWith("Windows")) {
                    return System.getenv("COMPUTERNAME");
                }

                // Try the HOSTNAME env variable (most unix systems)
                String hostname = System.getenv("HOSTNAME");
                if (hostname != null) {
                    return hostname;
                }

                // Resort to dns hostname lookup
                try {
                    return InetAddress.getLocalHost().getHostName();
                } catch (UnknownHostException ex) {
                    // Give up
                }
                return null;
            }
        });

    public static void initializeCache() {
        hostnameCache.get();
    }

    @Override
    public void beforeNotify(Report report) {
        report
                .setDeviceInfo("hostname", hostnameCache.get())
                .setDeviceInfo("osName", System.getProperty("os.name"))
                .setDeviceInfo("osVersion", System.getProperty("os.version"))
                .setDeviceInfo("osArch", System.getProperty("os.arch"))
                .setDeviceInfo("runtimeName", System.getProperty("java.runtime.name"))
                .setDeviceInfo("runtimeVersion", System.getProperty("java.runtime.version"))
                .setDeviceInfo("locale", Locale.getDefault());
    }
}

@@ -57,90 +57,8 @@
 t;%0A%0A
-import com.google.common.base.Supplier;%0Aimport com.google.common.base.Suppliers;%0A%0A
 impo
@@ -196,16 +196,17 @@
 lback %7B%0A
+%0A
     priv
@@ -220,141 +220,605 @@
 tic 
-final Supplier%3CString%3E hostnameCache =%0A        Suppliers.memoize(new Supplier%3CString%3E() %7B%0A%0A            public String get() %7B%0A        
+volatile String hostname;%0A    private static transient volatile boolean hostnameInitialised;%0A    private static final Object lock = new Object();%0A%0A    /**%0A     * Memoises the hostname, as lookup can be expensive%0A     */%0A    private static String getHostnameValue() %7B%0A        if (!hostnameInitialised) %7B%0A            synchronized (lock) %7B%0A                if (!hostnameInitialised) %7B%0A                    hostname = lookupHostname();%0A                    hostnameInitialised = true;%0A                %7D%0A            %7D%0A        %7D%0A        return hostname;%0A    %7D%0A%0A    private static String lookupHostname() %7B%0A
     
@@ -857,24 +857,16 @@
 TERNAME%0A
-        
         
@@ -932,32 +932,24 @@
             
-    
-    
 return Syste
@@ -982,35 +982,19 @@
         
-       
- 
 %7D%0A%0A
-        
         
@@ -1054,24 +1054,16 @@
         
-  
-      
 String h
@@ -1099,24 +1099,16 @@
 NAME%22);%0A
-        
         
@@ -1139,32 +1139,24 @@
             
-       
- 
 return hostn
@@ -1160,24 +1160,16 @@
 stname;%0A
-        
         
@@ -1179,24 +1179,16 @@
         
-  
-      
 // Resor
@@ -1224,30 +1224,14 @@
     
-     
-   
 try %7B%0A
-        
     
@@ -1283,32 +1283,24 @@
 HostName();%0A
-        
         %7D ca
@@ -1343,24 +1343,16 @@
         
-  
-      
 // Give 
@@ -1366,33 +1366,18 @@
     
-     
-   %7D%0A       
+%7D%0A
         
  ret
@@ -1368,25 +1368,24 @@
   %7D%0A        
- 
 return null;
@@ -1393,29 +1393,9 @@
     
-        %7D%0A        %7D);
+%7D
 %0A%0A  
@@ -1443,33 +1443,32 @@
         
-h
+getH
 ostname
-Cache.get
+Value
 ();%0A    
@@ -1592,25 +1592,24 @@
 e%22, 
-h
+getH
 ostname
-Cache.get
+Value
 ())%0A

remove dependency on guava suppliers