package za.redbridge.experiment;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by jamie on 2014/11/24.
 */
public final class Utils {

    private static final Logger log = LoggerFactory.getLogger(Utils.class);

    /** Check if a String is either null or empty. */
    public static boolean isBlank(String s) {
        return s == null || s.isEmpty();
    }

    /**
     * Reads a serialized object instance from a file.
     * @param filepath the String representation of the path to the file
     * @return the deserialized object, or null if the object could not be deserialised
     */
    public static Object readObjectFromFile(String filepath) {
        Path path = Paths.get(filepath);
        Object object = null;
        try (ObjectInputStream in = new ObjectInputStream(Files.newInputStream(path))) {
            object = in.readObject();
        } catch (IOException | ClassNotFoundException e) {
            log.error("Unable to load object from file", e);
        }
        return object;
    }

    public static void saveObjectToFile(Serializable object, String filepath) {
        Path path = Paths.get(filepath);
        saveObjectToFile(object, path);
    }

    public static void saveObjectToFile(Serializable object, Path path) {
        try (ObjectOutputStream out = new ObjectOutputStream(Files.newOutputStream(path))) {
            out.writeObject(object);
        } catch (IOException e) {
            log.error("Unable to save object to file", e);
        }
    }

    public static Path getLoggingDirectory() {
        String hostname = getHostname();
        if (hostname == null) {
            hostname = "unknown";
        }

        String date = new SimpleDateFormat("yyyyMMdd'T'HHmm").format(new Date());

        return Paths.get("results", hostname + "-" + date);
    }

    public static String getHostname() {
        Process process;
        try {
            process = Runtime.getRuntime().exec("hostname");
        } catch (IOException e) {
            log.error("Unable to request hostname", e);
            return null;
        }

        String hostname = null;
        try (BufferedReader reader =
                     new BufferedReader(new InputStreamReader(process.getInputStream()))) {
            hostname = reader.readLine();
        } catch (IOException e) {
            log.error("Failed to read stdout when requesting hostname", e);
        }

        return hostname;
    }

}

@@ -104,84 +104,19 @@
 .io.
-BufferedReader;%0Aimport java.io.IOException;%0Aimport java.io.InputStreamReader
+IOException
 ;%0Aim
@@ -211,16 +211,45 @@
 izable;%0A
+import java.net.InetAddress;%0A
 import j
@@ -1885,29 +1885,34 @@
 stname = get
+Local
 Host
-n
+N
 ame();%0A     
@@ -2165,13 +2165,18 @@
  get
+Local
 Host
-n
+N
 ame(
@@ -2191,425 +2191,71 @@
     
-Process process;%0A        try %7B%0A            process = Runtime.getRuntime().exec(%22hostname%22);%0A        %7D catch (IOException e) %7B%0A            log.error(%22Unable to request hostname%22, e);%0A            return null;%0A        %7D%0A%0A        String hostname = null;%0A        try (BufferedReader reader =%0A                     new BufferedReader(new InputStreamReader(process.getInputStream()))) %7B%0A            hostn
+try %7B%0A            return InetAddress.getLocalHost().getHostN
 ame
- = reader.readLine
 ();%0A
@@ -2315,50 +2315,29 @@
 or(%22
-Fai
+Unab
 le
-d
  to 
-read stdout when requesting
+query
  host
+ 
 name
@@ -2345,33 +2345,32 @@
 , e);%0A        %7D%0A
-%0A
         return h
@@ -2368,24 +2368,20 @@
  return 
-hostname
+null
 ;%0A    %7D%0A

Use InetAddress to get hostname like logback does