106 lines
2.5 KiB
Java
Executable File
106 lines
2.5 KiB
Java
Executable File
package bootstrap.platform;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.FileReader;
|
|
import java.io.IOException;
|
|
import java.io.InputStreamReader;
|
|
|
|
public class Platform {
|
|
|
|
public enum OsType { UNKNOWN, LINUX, OSX, WINDOWS};
|
|
public enum Bitness { B32, B64 };
|
|
|
|
public static OsType getOperatingSystem()
|
|
{
|
|
if (System.getProperty("os.name").equals("Mac OS X"))
|
|
return OsType.OSX;
|
|
if (System.getProperty("os.name").equals("Linux"))
|
|
return OsType.LINUX;
|
|
if (System.getProperty("os.name").startsWith("Windows"))
|
|
return OsType.WINDOWS;
|
|
|
|
return OsType.UNKNOWN;
|
|
}
|
|
|
|
public static Bitness getBitness(){
|
|
String arch = System.getProperty("os.arch");
|
|
if (arch == null) {
|
|
return null;
|
|
}
|
|
|
|
if (arch.equals("x86")){
|
|
return Bitness.B32;
|
|
} else if (arch.equals("i386")){
|
|
return Bitness.B32;
|
|
} else if (arch.equals("xmd64")){
|
|
return Bitness.B64;
|
|
} else if (arch.equals("amd64")){
|
|
return Bitness.B64;
|
|
} else if (arch.equals("x86_64")){
|
|
return Bitness.B64;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static String PlatformName(){
|
|
OsType ost = getOperatingSystem();
|
|
Bitness bit = getBitness();
|
|
|
|
return String.format("%s%s",
|
|
(ost == OsType.LINUX) ? "linux" :
|
|
(ost == OsType.OSX) ? "osx" :
|
|
(ost == OsType.WINDOWS) ? "mswin" :
|
|
"unknown",
|
|
(bit == Bitness.B32) ? "32" :
|
|
(bit == Bitness.B64) ? "64" :
|
|
"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:
|
|
|
|
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) {
|
|
}
|
|
default:
|
|
return getHostNameFromFile();
|
|
}
|
|
}
|
|
|
|
}
|