Permalink
Browse files

Always use english locale when converting case for OS/CPU detection

  • Loading branch information...
1 parent da59b1f commit f2f314025f9e835cd7e0c471d3a88a2686072ec6 Wayne Meissner committed Aug 2, 2012
Showing with 46 additions and 30 deletions.
  1. +23 −16 src/main/java/com/kenai/jffi/Platform.java
  2. +23 −14 src/main/java/com/kenai/jffi/internal/StubLoader.java
@@ -36,6 +36,7 @@
* Convenience class to interrogate the system about various platform-specific details.
*/
public abstract class Platform {
+ private static final java.util.Locale LOCALE = java.util.Locale.ENGLISH;
private final OS os;
private final int javaVersionMajor;
@@ -69,7 +70,7 @@
UNKNOWN;
@Override
- public String toString() { return name().toLowerCase(); }
+ public String toString() { return name().toLowerCase(LOCALE); }
}
/**
@@ -106,7 +107,7 @@
public final int dataModel;
public final long addressMask;
@Override
- public String toString() { return name().toLowerCase(); }
+ public String toString() { return name().toLowerCase(LOCALE); }
}
/**
@@ -122,26 +123,26 @@
* @return An member of the <tt>OS</tt> enum.
*/
private static final OS determineOS() {
- String osName = System.getProperty("os.name").split(" ")[0].toLowerCase();
- if (osName.startsWith("mac") || osName.startsWith("darwin")) {
+ String osName = System.getProperty("os.name").split(" ")[0];
+ if (startsWithIgnoreCase(osName, "mac") || startsWithIgnoreCase(osName, "darwin")) {
return OS.DARWIN;
- } else if (osName.startsWith("linux")) {
+ } else if (startsWithIgnoreCase(osName, "linux")) {
return OS.LINUX;
- } else if (osName.startsWith("sunos") || osName.startsWith("solaris")) {
+ } else if (startsWithIgnoreCase(osName, "sunos") || startsWithIgnoreCase(osName, "solaris")) {
return OS.SOLARIS;
- } else if (osName.startsWith("aix")) {
+ } else if (startsWithIgnoreCase(osName, "aix")) {
return OS.AIX;
- } else if (osName.startsWith("openbsd")) {
+ } else if (startsWithIgnoreCase(osName, "openbsd")) {
return OS.OPENBSD;
- } else if (osName.startsWith("freebsd")) {
+ } else if (startsWithIgnoreCase(osName, "freebsd")) {
return OS.FREEBSD;
- } else if (osName.startsWith("windows")) {
+ } else if (startsWithIgnoreCase(osName, "windows")) {
return OS.WINDOWS;
} else {
@@ -182,7 +183,7 @@ private static Platform newDefaultPlatform(OS os) {
private static final class ArchHolder {
- public static final CPU CPU = determineCPU();
+ public static final CPU cpu = determineCPU();
/**
* Determines the CPU architecture the JVM is running on.
@@ -195,11 +196,11 @@ private static Platform newDefaultPlatform(OS os) {
private static CPU determineCPU() {
String archString = null;
try {
- archString = Foreign.getInstance().getArch().toLowerCase();
+ archString = Foreign.getInstance().getArch().toUpperCase(LOCALE).toLowerCase(LOCALE);
} catch (UnsatisfiedLinkError ex) {}
if (archString == null || "unknown".equals(archString)) {
- archString = System.getProperty("os.arch", "unknown").toLowerCase();
+ archString = System.getProperty("os.arch", "unknown").toUpperCase(LOCALE).toLowerCase(LOCALE);
}
if ("x86".equals(archString) || "i386".equals(archString) || "i86pc".equals(archString)) {
@@ -224,7 +225,7 @@ private static CPU determineCPU() {
// Try to find by lookup up in the CPU list
try {
- return CPU.valueOf(archString.toUpperCase());
+ return CPU.valueOf(archString.toUpperCase(LOCALE));
} catch (IllegalArgumentException ex) {
return CPU.UNKNOWN;
@@ -279,7 +280,7 @@ public final OS getOS() {
* @return A <tt>CPU</tt> value representing the current processor architecture.
*/
public final CPU getCPU() {
- return ArchHolder.CPU;
+ return ArchHolder.cpu;
}
/**
@@ -323,7 +324,7 @@ public final long addressMask() {
*/
public String getName() {
String osName = System.getProperty("os.name").split(" ")[0];
- return getCPU().name().toLowerCase() + "-" + osName;
+ return getCPU().name().toLowerCase(LOCALE) + "-" + osName;
}
/**
@@ -431,5 +432,11 @@ public final int longSize() {
return 32;
}
}
+
+ private static boolean startsWithIgnoreCase(String s1, String s2) {
+ return s1.startsWith(s2)
+ || s1.toUpperCase(LOCALE).startsWith(s2.toUpperCase(LOCALE))
+ || s1.toLowerCase(LOCALE).startsWith(s2.toLowerCase(LOCALE));
+ }
}
@@ -34,6 +34,7 @@
public final static int VERSION_MAJOR = getVersionField("MAJOR");
public final static int VERSION_MINOR = getVersionField("MINOR");
private static final String versionClassName = "com.kenai.jffi.Version";
+ private static final java.util.Locale LOCALE = java.util.Locale.ENGLISH;
private static final String bootPropertyFilename = "boot.properties";
private static final String bootLibraryPropertyName = "jffi.boot.library.path";
@@ -42,8 +43,10 @@
private static final OS OS_ = determineOS();
private static final CPU CPU_ = determineCPU();
+
private static volatile Throwable failureCause = null;
private static volatile boolean loaded = false;
+
public static final boolean isLoaded() {
@@ -79,7 +82,7 @@ public static final Throwable getFailureCause() {
UNKNOWN;
@Override
- public String toString() { return name().toLowerCase(); }
+ public String toString() { return name().toLowerCase(LOCALE); }
}
/**
@@ -109,29 +112,29 @@ public static final Throwable getFailureCause() {
UNKNOWN;
@Override
- public String toString() { return name().toLowerCase(); }
+ public String toString() { return name().toLowerCase(LOCALE); }
}
/**
* Determines the operating system jffi is running on
*
* @return An member of the <tt>OS</tt> enum.
*/
- private static final OS determineOS() {
- String osName = System.getProperty("os.name").split(" ")[0].toLowerCase();
- if (osName.startsWith("mac") || osName.startsWith("darwin")) {
+ private static OS determineOS() {
+ String osName = System.getProperty("os.name").split(" ")[0];
+ if (startsWithIgnoreCase(osName, "mac") || startsWithIgnoreCase(osName, "darwin")) {
return OS.DARWIN;
- } else if (osName.startsWith("linux")) {
+ } else if (startsWithIgnoreCase(osName, "linux")) {
return OS.LINUX;
- } else if (osName.startsWith("sunos") || osName.startsWith("solaris")) {
+ } else if (startsWithIgnoreCase(osName, "sunos") || startsWithIgnoreCase(osName, "solaris")) {
return OS.SOLARIS;
- } else if (osName.startsWith("aix")) {
+ } else if (startsWithIgnoreCase(osName, "aix")) {
return OS.AIX;
- } else if (osName.startsWith("openbsd")) {
+ } else if (startsWithIgnoreCase(osName, "openbsd")) {
return OS.OPENBSD;
- } else if (osName.startsWith("freebsd")) {
+ } else if (startsWithIgnoreCase(osName, "freebsd")) {
return OS.FREEBSD;
- } else if (osName.startsWith("windows")) {
+ } else if (startsWithIgnoreCase(osName, "windows")) {
return OS.WINDOWS;
} else {
return OS.UNKNOWN;
@@ -147,7 +150,7 @@ private static final OS determineOS() {
* @return A member of the <tt>CPU</tt> enum.
*/
private static final CPU determineCPU() {
- String archString = System.getProperty("os.arch", "unknown").toLowerCase();
+ String archString = System.getProperty("os.arch", "unknown").toUpperCase(LOCALE).toLowerCase(LOCALE);
if ("x86".equals(archString) || "i386".equals(archString) || "i86pc".equals(archString)) {
return CPU.I386;
} else if ("x86_64".equals(archString) || "amd64".equals(archString)) {
@@ -160,7 +163,7 @@ private static final CPU determineCPU() {
// Try to find by lookup up in the CPU list
try {
- return CPU.valueOf(archString.toUpperCase());
+ return CPU.valueOf(archString.toUpperCase(LOCALE));
} catch (IllegalArgumentException ex) {
return CPU.UNKNOWN;
}
@@ -195,7 +198,7 @@ public static String getPlatformName() {
String osName = System.getProperty("os.name").split(" ")[0];
- return getCPU().name().toLowerCase() + "-" + osName;
+ return getCPU().name().toLowerCase(LOCALE) + "-" + osName;
}
/**
* Gets the path within the jar file of the stub native library.
@@ -369,6 +372,12 @@ private static int getVersionField(String name) {
}
}
+ private static boolean startsWithIgnoreCase(String s1, String s2) {
+ return s1.startsWith(s2)
+ || s1.toUpperCase(LOCALE).startsWith(s2.toUpperCase(LOCALE))
+ || s1.toLowerCase(LOCALE).startsWith(s2.toLowerCase(LOCALE));
+ }
+
static {
try {
load();

0 comments on commit f2f3140

Please sign in to comment.