Platform: getHostName()
parent
2ea5215d7f
commit
0d3ef91654
|
@ -1,5 +1,9 @@
|
|||
package org.hwo.platform;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
public class Platform {
|
||||
|
||||
public enum OsType { UNKNOWN, LINUX, OSX, WINDOWS};
|
||||
|
@ -50,4 +54,34 @@ public class Platform {
|
|||
);
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
return hostname;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue