| 1 |
package net.oni2.platformtools; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.IOException; |
| 5 |
import java.util.Vector; |
| 6 |
|
| 7 |
import net.oni2.platformtools.applicationinvoker.ApplicationInvoker; |
| 8 |
import net.oni2.platformtools.applicationinvoker.ApplicationInvocationResult; |
| 9 |
import net.oni2.platformtools.applicationinvoker.EExeType; |
| 10 |
import net.oni2.platformtools.applicationinvoker.ERuntimeNotInstalledException; |
| 11 |
|
| 12 |
/** |
| 13 |
* @author Christian Illy |
| 14 |
*/ |
| 15 |
public class PlatformInformation { |
| 16 |
|
| 17 |
/** |
| 18 |
* @author Christian Illy |
| 19 |
*/ |
| 20 |
public enum Architecture { |
| 21 |
/** |
| 22 |
* 32 bit |
| 23 |
*/ |
| 24 |
X86, |
| 25 |
/** |
| 26 |
* 64 bit |
| 27 |
*/ |
| 28 |
AMD64, |
| 29 |
/** |
| 30 |
* PPC |
| 31 |
*/ |
| 32 |
PPC |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* @author Christian Illy |
| 37 |
*/ |
| 38 |
public enum Platform { |
| 39 |
/** |
| 40 |
* Running Windows |
| 41 |
*/ |
| 42 |
WIN, |
| 43 |
/** |
| 44 |
* Running MacOS |
| 45 |
*/ |
| 46 |
MACOS, |
| 47 |
/** |
| 48 |
* Running a Linux |
| 49 |
*/ |
| 50 |
LINUX, |
| 51 |
/** |
| 52 |
* Unknown OS |
| 53 |
*/ |
| 54 |
UNKNOWN |
| 55 |
} |
| 56 |
|
| 57 |
private static Platform platform = null; |
| 58 |
private static Architecture architecture = null; |
| 59 |
|
| 60 |
/** |
| 61 |
* @return Processor architecture |
| 62 |
*/ |
| 63 |
public static Architecture getArchitecture() { |
| 64 |
if (architecture == null) { |
| 65 |
Vector<String> params = new Vector<String>(); |
| 66 |
ApplicationInvocationResult res = null; |
| 67 |
|
| 68 |
switch (getPlatform()) { |
| 69 |
case WIN: |
| 70 |
String arch = System.getenv("PROCESSOR_ARCHITECTURE") |
| 71 |
.toLowerCase(); |
| 72 |
if (arch.startsWith("x86")) |
| 73 |
architecture = Architecture.X86; |
| 74 |
architecture = Architecture.AMD64; |
| 75 |
break; |
| 76 |
case MACOS: |
| 77 |
params.add("-m"); |
| 78 |
try { |
| 79 |
res = ApplicationInvoker.executeAndWait( |
| 80 |
EExeType.OSBINARY, null, new File("uname"), |
| 81 |
params, false); |
| 82 |
} catch (IOException e) { |
| 83 |
e.printStackTrace(); |
| 84 |
} catch (ERuntimeNotInstalledException e) { |
| 85 |
e.printStackTrace(); |
| 86 |
} |
| 87 |
architecture = Architecture.AMD64; |
| 88 |
if (res != null) { |
| 89 |
if (res.output.get(0).toLowerCase().contains("power macintosh")) |
| 90 |
architecture = Architecture.PPC; |
| 91 |
} |
| 92 |
break; |
| 93 |
case LINUX: |
| 94 |
params.add("LONG_BIT"); |
| 95 |
try { |
| 96 |
res = ApplicationInvoker.executeAndWait( |
| 97 |
EExeType.OSBINARY, null, new File("getconf"), |
| 98 |
params, false); |
| 99 |
} catch (IOException e) { |
| 100 |
e.printStackTrace(); |
| 101 |
} catch (ERuntimeNotInstalledException e) { |
| 102 |
e.printStackTrace(); |
| 103 |
} |
| 104 |
architecture = Architecture.X86; |
| 105 |
if (res != null) { |
| 106 |
if (res.output.get(0).equals("64")) |
| 107 |
architecture = Architecture.AMD64; |
| 108 |
} |
| 109 |
break; |
| 110 |
default: |
| 111 |
architecture = null; |
| 112 |
} |
| 113 |
} |
| 114 |
return architecture; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* @return The operating system running on |
| 119 |
*/ |
| 120 |
public static Platform getPlatform() { |
| 121 |
if (platform == null) { |
| 122 |
String os = System.getProperty("os.name").toLowerCase(); |
| 123 |
if (os.startsWith("win")) |
| 124 |
platform = Platform.WIN; |
| 125 |
else if (os.startsWith("linux")) |
| 126 |
platform = Platform.LINUX; |
| 127 |
else if (os.startsWith("mac") || os.startsWith("darwin")) |
| 128 |
platform = Platform.MACOS; |
| 129 |
else |
| 130 |
platform = Platform.UNKNOWN; |
| 131 |
} |
| 132 |
return platform; |
| 133 |
} |
| 134 |
|
| 135 |
} |