Fixed OSX hostname retrieval

master
haraldwolff 2016-09-09 00:55:41 +02:00
parent 9ba896427d
commit 723e515970
1 changed files with 41 additions and 21 deletions

View File

@ -1,8 +1,12 @@
package org.hwo.platform;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.xml.stream.util.StreamReaderDelegate;
public class Platform {
@ -35,6 +39,8 @@ public class Platform {
return Bitness.B64;
} else if (arch.equals("amd64")){
return Bitness.B64;
} else if (arch.equals("x86_64")){
return Bitness.B64;
}
return null;
}
@ -53,34 +59,48 @@ public class Platform {
"XX"
);
}
static private String getHostNameFromFile(){
String hostname = System.getenv("HOSTNAME");
if (hostname == null){
try {
FileReader fr = new FileReader("/etc/hostname");
char[] b = new char[2048];
fr.read(b, 0, 2048);
fr.close();
hostname = new String(b).trim();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return hostname;
}
public static String getHostName(){
switch (getOperatingSystem()){
case WINDOWS:
return System.getenv("COMPUTERNAME");
case LINUX:
case OSX:
default:
String hostname = System.getenv("HOSTNAME");
if (hostname == null){
try {
FileReader fr = new FileReader("/etc/hostname");
char[] b = new char[2048];
fr.read(b, 0, 2048);
fr.close();
hostname = new String(b).trim();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ProcessBuilder pb = new ProcessBuilder();
pb.command("/bin/hostname");
try {
Process p = pb.start();
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String hostname = reader.readLine().trim();
return hostname;
} catch (IOException e1) {
} catch (InterruptedException e) {
}
return hostname;
default:
return getHostNameFromFile();
}
}