| 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 |
|
|---|
| 31 | /**
|
|---|
| 32 | * @author Christian Illy
|
|---|
| 33 | */
|
|---|
| 34 | public enum Platform {
|
|---|
| 35 | /**
|
|---|
| 36 | * Running Windows
|
|---|
| 37 | */
|
|---|
| 38 | WIN,
|
|---|
| 39 | /**
|
|---|
| 40 | * Running MacOS
|
|---|
| 41 | */
|
|---|
| 42 | MACOS,
|
|---|
| 43 | /**
|
|---|
| 44 | * Running a Linux
|
|---|
| 45 | */
|
|---|
| 46 | LINUX,
|
|---|
| 47 | /**
|
|---|
| 48 | * Unknown OS
|
|---|
| 49 | */
|
|---|
| 50 | UNKNOWN
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | private static Platform platform = null;
|
|---|
| 54 | private static Architecture architecture = null;
|
|---|
| 55 |
|
|---|
| 56 | /**
|
|---|
| 57 | * @return Processor architecture
|
|---|
| 58 | */
|
|---|
| 59 | public static Architecture getArchitecture() {
|
|---|
| 60 | if (architecture == null) {
|
|---|
| 61 | switch (getPlatform()) {
|
|---|
| 62 | case WIN:
|
|---|
| 63 | String arch = System.getenv("PROCESSOR_ARCHITECTURE")
|
|---|
| 64 | .toLowerCase();
|
|---|
| 65 | if (arch.startsWith("x86"))
|
|---|
| 66 | architecture = Architecture.X86;
|
|---|
| 67 | architecture = Architecture.AMD64;
|
|---|
| 68 | break;
|
|---|
| 69 | case MACOS:
|
|---|
| 70 | case LINUX:
|
|---|
| 71 | Vector<String> params = new Vector<String>();
|
|---|
| 72 | params.add("LONG_BIT");
|
|---|
| 73 | ApplicationInvocationResult res = null;
|
|---|
| 74 | try {
|
|---|
| 75 | res = ApplicationInvoker.executeAndWait(
|
|---|
| 76 | EExeType.OSBINARY, null, new File("getconf"),
|
|---|
| 77 | params, false);
|
|---|
| 78 | } catch (IOException e) {
|
|---|
| 79 | e.printStackTrace();
|
|---|
| 80 | } catch (ERuntimeNotInstalledException e) {
|
|---|
| 81 | e.printStackTrace();
|
|---|
| 82 | }
|
|---|
| 83 | architecture = Architecture.X86;
|
|---|
| 84 | if (res != null) {
|
|---|
| 85 | if (res.output.get(0).equals("64"))
|
|---|
| 86 | architecture = Architecture.AMD64;
|
|---|
| 87 | }
|
|---|
| 88 | break;
|
|---|
| 89 | default:
|
|---|
| 90 | architecture = null;
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 | return architecture;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | /**
|
|---|
| 97 | * @return The operating system running on
|
|---|
| 98 | */
|
|---|
| 99 | public static Platform getPlatform() {
|
|---|
| 100 | if (platform == null) {
|
|---|
| 101 | String os = System.getProperty("os.name").toLowerCase();
|
|---|
| 102 | if (os.startsWith("win"))
|
|---|
| 103 | platform = Platform.WIN;
|
|---|
| 104 | else if (os.startsWith("linux"))
|
|---|
| 105 | platform = Platform.LINUX;
|
|---|
| 106 | else if (os.startsWith("mac"))
|
|---|
| 107 | platform = Platform.MACOS;
|
|---|
| 108 | else
|
|---|
| 109 | platform = Platform.UNKNOWN;
|
|---|
| 110 | }
|
|---|
| 111 | return platform;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | }
|
|---|