From 47a7778fe42ad9e82ee8621efdfdc4b745dff08f Mon Sep 17 00:00:00 2001 From: kstyrc Date: Sun, 25 Jan 2015 22:50:01 +0100 Subject: [PATCH 1/2] OS detector based on StackOverflow collected know-how --- .../java/redis/embedded/util/OSDetector.java | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 src/main/java/redis/embedded/util/OSDetector.java diff --git a/src/main/java/redis/embedded/util/OSDetector.java b/src/main/java/redis/embedded/util/OSDetector.java new file mode 100644 index 00000000..de9e9894 --- /dev/null +++ b/src/main/java/redis/embedded/util/OSDetector.java @@ -0,0 +1,113 @@ +package redis.embedded.util; + +import java.io.BufferedReader; +import java.io.InputStreamReader; + +public class OSDetector { + + public static enum OS { + WINDOWS, + UNIX, + MACOSX + } + + public static enum Architecture { + x86, + x86_64 + } + + public static OS getOS() { + String osName = System.getProperty("os.name").toLowerCase(); + + if (osName.contains("win")) { + return OS.WINDOWS; + } else if (osName.contains("nix") || osName.contains("nux") || osName.contains("aix")) { + return OS.UNIX; + } else if ("Mac OS X".equalsIgnoreCase(osName)) { + return OS.MACOSX; + } else { + throw new RuntimeException("Unrecognized OS: " + osName); + } + } + + public static Architecture getArchitecture() { + OS os; + switch (os = getOS()) { + case WINDOWS: + return getWindowsArchitecture(); + case UNIX: + return getUnixArchitecture(); + case MACOSX: + return getMacOSXArchitecture(); + default: + throw new RuntimeException("Unsupported OS: " + os); + } + } + + private static Architecture getWindowsArchitecture() { + String arch = System.getenv("PROCESSOR_ARCHITECTURE"); + String wow64Arch = System.getenv("PROCESSOR_ARCHITEW6432"); + + if (arch.endsWith("64") || wow64Arch != null && wow64Arch.endsWith("64")) { + return Architecture.x86_64; + } else { + return Architecture.x86; + } + } + + private static Architecture getUnixArchitecture() { + BufferedReader input = null; + try { + String line; + Process proc = Runtime.getRuntime().exec("uname -m"); + input = new BufferedReader(new InputStreamReader(proc.getInputStream())); + while ((line = input.readLine()) != null) { + if (line.length() > 0) { + if (line.contains("64")) { + return Architecture.x86_64; + } + } + } + } catch (Exception e) { + throw new RuntimeException(e); + } finally { + try { + if (input != null) { + input.close(); + } + } catch (Exception ignored) { + // ignore + } + } + + return Architecture.x86; + } + + private static Architecture getMacOSXArchitecture() { + BufferedReader input = null; + try { + String line; + Process proc = Runtime.getRuntime().exec("sysctl hw"); + input = new BufferedReader(new InputStreamReader(proc.getInputStream())); + while ((line = input.readLine()) != null) { + if (line.length() > 0) { + if ((line.contains("cpu64bit_capable")) && (line.trim().endsWith("1"))) { + return Architecture.x86_64; + } + } + } + } catch (Exception e) { + throw new RuntimeException(e); + } finally { + try { + if (input != null) { + input.close(); + } + } catch (Exception ignored) { + // ignore + } + } + + return Architecture.x86; + } +} From e3e5fdeeb35569f4b44e9cf45499d416e961b39f Mon Sep 17 00:00:00 2001 From: kstyrc Date: Mon, 16 Feb 2015 20:53:47 +0100 Subject: [PATCH 2/2] Middle work --- src/main/java/redis/embedded/RedisServer.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/redis/embedded/RedisServer.java b/src/main/java/redis/embedded/RedisServer.java index 87aaf725..1593ce23 100644 --- a/src/main/java/redis/embedded/RedisServer.java +++ b/src/main/java/redis/embedded/RedisServer.java @@ -3,6 +3,7 @@ import com.google.common.base.Strings; import com.google.common.io.Files; import redis.embedded.util.JarUtil; +import redis.embedded.util.OSDetector; import java.io.BufferedReader; import java.io.File; @@ -29,11 +30,14 @@ private RedisRunScriptEnum(String runScript) { } public static String getRedisRunScript() { - String osName = System.getProperty("os.name").toLowerCase(); - String osArch = System.getProperty("os.arch").toLowerCase(); + OSDetector.OS os = OSDetector.getOS(); + OSDetector.Architecture arch = OSDetector.getArchitecture(); - if (osName.contains("win")) { - if (osArch.contains("64")) { + switch (os) { + case + } + if (os == OSDetector.OS.WINDOWS) { + if (arch == OSDetector.Architecture.x86_64) { return WINDOWS_64.runScript; } else { return WINDOWS_32.runScript;