[performance] Cache platform name in SystemReader
SystemReader.isMacOs() and SystemReader.isWindows() return values are unlikely to change during the JVM lifetime (except tests). Don't read system properties each time the methods are called, just use previously calculated value. Change-Id: I495521f67a8b544e7b7247d99bbd05a42ea16d20 Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
This commit is contained in:
parent
1abd51d953
commit
41a972cd1e
|
@ -47,6 +47,7 @@
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
@ -170,6 +171,7 @@ public DateFormat getDateTimeInstance(int dateStyle, int timeStyle) {
|
||||||
* Assign some properties for the currently executing platform
|
* Assign some properties for the currently executing platform
|
||||||
*/
|
*/
|
||||||
public void setCurrentPlatform() {
|
public void setCurrentPlatform() {
|
||||||
|
resetOsNames();
|
||||||
setProperty("os.name", System.getProperty("os.name"));
|
setProperty("os.name", System.getProperty("os.name"));
|
||||||
setProperty("file.separator", System.getProperty("file.separator"));
|
setProperty("file.separator", System.getProperty("file.separator"));
|
||||||
setProperty("path.separator", System.getProperty("path.separator"));
|
setProperty("path.separator", System.getProperty("path.separator"));
|
||||||
|
@ -180,6 +182,7 @@ public void setCurrentPlatform() {
|
||||||
* Emulate Windows
|
* Emulate Windows
|
||||||
*/
|
*/
|
||||||
public void setWindows() {
|
public void setWindows() {
|
||||||
|
resetOsNames();
|
||||||
setProperty("os.name", "Windows");
|
setProperty("os.name", "Windows");
|
||||||
setProperty("file.separator", "\\");
|
setProperty("file.separator", "\\");
|
||||||
setProperty("path.separator", ";");
|
setProperty("path.separator", ";");
|
||||||
|
@ -191,10 +194,25 @@ public void setWindows() {
|
||||||
* Emulate Unix
|
* Emulate Unix
|
||||||
*/
|
*/
|
||||||
public void setUnix() {
|
public void setUnix() {
|
||||||
|
resetOsNames();
|
||||||
setProperty("os.name", "*nix"); // Essentially anything but Windows
|
setProperty("os.name", "*nix"); // Essentially anything but Windows
|
||||||
setProperty("file.separator", "/");
|
setProperty("file.separator", "/");
|
||||||
setProperty("path.separator", ":");
|
setProperty("path.separator", ":");
|
||||||
setProperty("line.separator", "\n");
|
setProperty("line.separator", "\n");
|
||||||
setPlatformChecker();
|
setPlatformChecker();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void resetOsNames() {
|
||||||
|
Field field;
|
||||||
|
try {
|
||||||
|
field = SystemReader.class.getDeclaredField("isWindows");
|
||||||
|
field.setAccessible(true);
|
||||||
|
field.set(null, null);
|
||||||
|
field = SystemReader.class.getDeclaredField("isMacOS");
|
||||||
|
field.setAccessible(true);
|
||||||
|
field.set(null, null);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -71,6 +71,11 @@
|
||||||
*/
|
*/
|
||||||
public abstract class SystemReader {
|
public abstract class SystemReader {
|
||||||
private static final SystemReader DEFAULT;
|
private static final SystemReader DEFAULT;
|
||||||
|
|
||||||
|
private static Boolean isMacOS;
|
||||||
|
|
||||||
|
private static Boolean isWindows;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
SystemReader r = new Default();
|
SystemReader r = new Default();
|
||||||
r.init();
|
r.init();
|
||||||
|
@ -148,6 +153,8 @@ public static SystemReader getInstance() {
|
||||||
* the default instance.
|
* the default instance.
|
||||||
*/
|
*/
|
||||||
public static void setInstance(SystemReader newReader) {
|
public static void setInstance(SystemReader newReader) {
|
||||||
|
isMacOS = null;
|
||||||
|
isWindows = null;
|
||||||
if (newReader == null)
|
if (newReader == null)
|
||||||
INSTANCE = DEFAULT;
|
INSTANCE = DEFAULT;
|
||||||
else {
|
else {
|
||||||
|
@ -293,26 +300,31 @@ public DateFormat getDateTimeInstance(int dateStyle, int timeStyle) {
|
||||||
* @return true if we are running on a Windows.
|
* @return true if we are running on a Windows.
|
||||||
*/
|
*/
|
||||||
public boolean isWindows() {
|
public boolean isWindows() {
|
||||||
String osDotName = AccessController
|
if (isWindows == null) {
|
||||||
.doPrivileged(new PrivilegedAction<String>() {
|
String osDotName = getOsName();
|
||||||
public String run() {
|
isWindows = Boolean.valueOf(osDotName.startsWith("Windows")); //$NON-NLS-1$
|
||||||
return getProperty("os.name"); //$NON-NLS-1$
|
}
|
||||||
}
|
return isWindows.booleanValue();
|
||||||
});
|
|
||||||
return osDotName.startsWith("Windows"); //$NON-NLS-1$
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return true if we are running on Mac OS X
|
* @return true if we are running on Mac OS X
|
||||||
*/
|
*/
|
||||||
public boolean isMacOS() {
|
public boolean isMacOS() {
|
||||||
String osDotName = AccessController
|
if (isMacOS == null) {
|
||||||
.doPrivileged(new PrivilegedAction<String>() {
|
String osDotName = getOsName();
|
||||||
public String run() {
|
isMacOS = Boolean.valueOf(
|
||||||
return getProperty("os.name"); //$NON-NLS-1$
|
"Mac OS X".equals(osDotName) || "Darwin".equals(osDotName)); //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
}
|
}
|
||||||
});
|
return isMacOS.booleanValue();
|
||||||
return "Mac OS X".equals(osDotName) || "Darwin".equals(osDotName); //$NON-NLS-1$ //$NON-NLS-2$
|
}
|
||||||
|
|
||||||
|
private String getOsName() {
|
||||||
|
return AccessController.doPrivileged(new PrivilegedAction<String>() {
|
||||||
|
public String run() {
|
||||||
|
return getProperty("os.name"); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue