一、前言
关于cayenne包中org.apache.commons.lang.SystemUtils系统工具类,获取JAVA_HOME地址、getJavaVersion版本、系统属性getSystemProperty及系统应用目录路径等。
二、源码说明
package org.apache.commons.lang;@b@@b@import java.io.File;@b@import java.io.PrintStream;@b@@b@public class SystemUtils@b@{@b@ private static final String OS_NAME_WINDOWS_PREFIX = "Windows";@b@ private static final String USER_HOME_KEY = "user.home";@b@ private static final String USER_DIR_KEY = "user.dir";@b@ private static final String JAVA_IO_TMPDIR_KEY = "java.io.tmpdir";@b@ private static final String JAVA_HOME_KEY = "java.home";@b@ public static final String AWT_TOOLKIT = getSystemProperty("awt.toolkit");@b@ public static final String FILE_ENCODING = getSystemProperty("file.encoding");@b@ public static final String FILE_SEPARATOR = getSystemProperty("file.separator");@b@ public static final String JAVA_AWT_FONTS = getSystemProperty("java.awt.fonts");@b@ public static final String JAVA_AWT_GRAPHICSENV = getSystemProperty("java.awt.graphicsenv");@b@ public static final String JAVA_AWT_HEADLESS = getSystemProperty("java.awt.headless");@b@ public static final String JAVA_AWT_PRINTERJOB = getSystemProperty("java.awt.printerjob");@b@ public static final String JAVA_CLASS_PATH = getSystemProperty("java.class.path");@b@ public static final String JAVA_CLASS_VERSION = getSystemProperty("java.class.version");@b@ public static final String JAVA_COMPILER = getSystemProperty("java.compiler");@b@ public static final String JAVA_ENDORSED_DIRS = getSystemProperty("java.endorsed.dirs");@b@ public static final String JAVA_EXT_DIRS = getSystemProperty("java.ext.dirs");@b@ public static final String JAVA_HOME = getSystemProperty("java.home");@b@ public static final String JAVA_IO_TMPDIR = getSystemProperty("java.io.tmpdir");@b@ public static final String JAVA_LIBRARY_PATH = getSystemProperty("java.library.path");@b@ public static final String JAVA_RUNTIME_NAME = getSystemProperty("java.runtime.name");@b@ public static final String JAVA_RUNTIME_VERSION = getSystemProperty("java.runtime.version");@b@ public static final String JAVA_SPECIFICATION_NAME = getSystemProperty("java.specification.name");@b@ public static final String JAVA_SPECIFICATION_VENDOR = getSystemProperty("java.specification.vendor");@b@ public static final String JAVA_SPECIFICATION_VERSION = getSystemProperty("java.specification.version");@b@ public static final String JAVA_UTIL_PREFS_PREFERENCES_FACTORY = getSystemProperty("java.util.prefs.PreferencesFactory");@b@ public static final String JAVA_VENDOR = getSystemProperty("java.vendor");@b@ public static final String JAVA_VENDOR_URL = getSystemProperty("java.vendor.url");@b@ public static final String JAVA_VERSION = getSystemProperty("java.version");@b@ public static final String JAVA_VM_INFO = getSystemProperty("java.vm.info");@b@ public static final String JAVA_VM_NAME = getSystemProperty("java.vm.name");@b@ public static final String JAVA_VM_SPECIFICATION_NAME = getSystemProperty("java.vm.specification.name");@b@ public static final String JAVA_VM_SPECIFICATION_VENDOR = getSystemProperty("java.vm.specification.vendor");@b@ public static final String JAVA_VM_SPECIFICATION_VERSION = getSystemProperty("java.vm.specification.version");@b@ public static final String JAVA_VM_VENDOR = getSystemProperty("java.vm.vendor");@b@ public static final String JAVA_VM_VERSION = getSystemProperty("java.vm.version");@b@ public static final String LINE_SEPARATOR = getSystemProperty("line.separator");@b@ public static final String OS_ARCH = getSystemProperty("os.arch");@b@ public static final String OS_NAME = getSystemProperty("os.name");@b@ public static final String OS_VERSION = getSystemProperty("os.version");@b@ public static final String PATH_SEPARATOR = getSystemProperty("path.separator");@b@ public static final String USER_COUNTRY = (getSystemProperty("user.country") == null) ? @b@ getSystemProperty("user.region") : getSystemProperty("user.country");@b@ public static final String USER_DIR = getSystemProperty("user.dir");@b@ public static final String USER_HOME = getSystemProperty("user.home");@b@ public static final String USER_LANGUAGE = getSystemProperty("user.language");@b@ public static final String USER_NAME = getSystemProperty("user.name");@b@ public static final String USER_TIMEZONE = getSystemProperty("user.timezone");@b@ public static final String JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();@b@ public static final float JAVA_VERSION_FLOAT = getJavaVersionAsFloat();@b@ public static final int JAVA_VERSION_INT = getJavaVersionAsInt();@b@ public static final boolean IS_JAVA_1_1 = getJavaVersionMatches("1.1");@b@ public static final boolean IS_JAVA_1_2 = getJavaVersionMatches("1.2");@b@ public static final boolean IS_JAVA_1_3 = getJavaVersionMatches("1.3");@b@ public static final boolean IS_JAVA_1_4 = getJavaVersionMatches("1.4");@b@ public static final boolean IS_JAVA_1_5 = getJavaVersionMatches("1.5");@b@ public static final boolean IS_OS_AIX = getOSMatches("AIX");@b@ public static final boolean IS_OS_HP_UX = getOSMatches("HP-UX");@b@ public static final boolean IS_OS_IRIX = getOSMatches("Irix");@b@ public static final boolean IS_OS_LINUX = (getOSMatches("Linux")) || (getOSMatches("LINUX"));@b@ public static final boolean IS_OS_MAC = getOSMatches("Mac");@b@ public static final boolean IS_OS_MAC_OSX = getOSMatches("Mac OS X");@b@ public static final boolean IS_OS_OS2 = getOSMatches("OS/2");@b@ public static final boolean IS_OS_SOLARIS = getOSMatches("Solaris");@b@ public static final boolean IS_OS_SUN_OS = getOSMatches("SunOS");@b@ public static final boolean IS_OS_UNIX = (IS_OS_AIX) || (IS_OS_HP_UX) || (IS_OS_IRIX) || (IS_OS_LINUX) || @b@ (IS_OS_MAC_OSX) || (IS_OS_SOLARIS) || (IS_OS_SUN_OS);@b@ public static final boolean IS_OS_WINDOWS = getOSMatches("Windows");@b@ public static final boolean IS_OS_WINDOWS_2000 = getOSMatches("Windows", "5.0");@b@ public static final boolean IS_OS_WINDOWS_95 = getOSMatches("Windows 9", "4.0");@b@ public static final boolean IS_OS_WINDOWS_98 = getOSMatches("Windows 9", "4.1");@b@ public static final boolean IS_OS_WINDOWS_ME = getOSMatches("Windows", "4.9");@b@ public static final boolean IS_OS_WINDOWS_NT = getOSMatches("Windows NT");@b@ public static final boolean IS_OS_WINDOWS_XP = getOSMatches("Windows", "5.1");@b@@b@ public static File getJavaHome()@b@ {@b@ return new File(System.getProperty("java.home"));@b@ }@b@@b@ public static File getJavaIoTmpDir()@b@ {@b@ return new File(System.getProperty("java.io.tmpdir"));@b@ }@b@@b@ /**@b@ * @deprecated@b@ */@b@ public static float getJavaVersion()@b@ {@b@ return JAVA_VERSION_FLOAT;@b@ }@b@@b@ private static float getJavaVersionAsFloat()@b@ {@b@ if (JAVA_VERSION_TRIMMED == null)@b@ return 0.0F;@b@@b@ String str = JAVA_VERSION_TRIMMED.substring(0, 3);@b@ if (JAVA_VERSION_TRIMMED.length() >= 5)@b@ str = str + JAVA_VERSION_TRIMMED.substring(4, 5);@b@@b@ return Float.parseFloat(str);@b@ }@b@@b@ private static int getJavaVersionAsInt()@b@ {@b@ if (JAVA_VERSION_TRIMMED == null)@b@ return 0;@b@@b@ String str = JAVA_VERSION_TRIMMED.substring(0, 1);@b@ str = str + JAVA_VERSION_TRIMMED.substring(2, 3);@b@ if (JAVA_VERSION_TRIMMED.length() >= 5)@b@ str = str + JAVA_VERSION_TRIMMED.substring(4, 5);@b@ else@b@ str = str + "0";@b@@b@ return Integer.parseInt(str);@b@ }@b@@b@ private static boolean getJavaVersionMatches(String versionPrefix)@b@ {@b@ if (JAVA_VERSION_TRIMMED == null)@b@ return false;@b@@b@ return JAVA_VERSION_TRIMMED.startsWith(versionPrefix);@b@ }@b@@b@ private static String getJavaVersionTrimmed()@b@ {@b@ if (JAVA_VERSION != null)@b@ for (int i = 0; i < JAVA_VERSION.length(); ++i) {@b@ char ch = JAVA_VERSION.charAt(i);@b@ if ((ch >= '0') && (ch <= '9'))@b@ return JAVA_VERSION.substring(i);@b@ }@b@@b@@b@ return null;@b@ }@b@@b@ private static boolean getOSMatches(String osNamePrefix)@b@ {@b@ if (OS_NAME == null)@b@ return false;@b@@b@ return OS_NAME.startsWith(osNamePrefix);@b@ }@b@@b@ private static boolean getOSMatches(String osNamePrefix, String osVersionPrefix)@b@ {@b@ if ((OS_NAME == null) || (OS_VERSION == null))@b@ return false;@b@@b@ return ((OS_NAME.startsWith(osNamePrefix)) && (OS_VERSION.startsWith(osVersionPrefix)));@b@ }@b@@b@ private static String getSystemProperty(String property)@b@ {@b@ try@b@ {@b@ return System.getProperty(property);@b@ }@b@ catch (SecurityException localSecurityException) {@b@ System.err.println(@b@ "Caught a SecurityException reading the system property '" + property + @b@ "'; the SystemUtils property value will default to null.");@b@ }@b@ return null;@b@ }@b@@b@ public static File getUserDir()@b@ {@b@ return new File(System.getProperty("user.dir"));@b@ }@b@@b@ public static File getUserHome()@b@ {@b@ return new File(System.getProperty("user.home"));@b@ }@b@@b@ public static boolean isJavaAwtHeadless()@b@ {@b@ return ((JAVA_AWT_HEADLESS != null) ? JAVA_AWT_HEADLESS.equals(Boolean.TRUE.toString()) : false);@b@ }@b@@b@ public static boolean isJavaVersionAtLeast(float requiredVersion)@b@ {@b@ return (JAVA_VERSION_FLOAT >= requiredVersion);@b@ }@b@@b@ public static boolean isJavaVersionAtLeast(int requiredVersion)@b@ {@b@ return (JAVA_VERSION_INT >= requiredVersion);@b@ }@b@}