一、前言
关于apache的commons-lang(3-3.2)源码包中的系统工具类org.apache.commons.lang3.SystemUtils实现虚拟机jvm环境、操作系统环境等常用的配置使用参考。
二、源码说明
package org.apache.commons.lang3;@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@ private static final JavaVersion JAVA_SPECIFICATION_VERSION_AS_ENUM = JavaVersion.get(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) ? 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 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_JAVA_1_6 = getJavaVersionMatches("1.6");@b@ public static final boolean IS_JAVA_1_7 = getJavaVersionMatches("1.7");@b@ public static final boolean IS_OS_AIX = getOSMatchesName("AIX");@b@ public static final boolean IS_OS_HP_UX = getOSMatchesName("HP-UX");@b@ public static final boolean IS_OS_IRIX = getOSMatchesName("Irix");@b@ public static final boolean IS_OS_LINUX = (getOSMatchesName("Linux")) || (getOSMatchesName("LINUX"));@b@ public static final boolean IS_OS_MAC = getOSMatchesName("Mac");@b@ public static final boolean IS_OS_MAC_OSX = getOSMatchesName("Mac OS X");@b@ public static final boolean IS_OS_FREE_BSD = getOSMatchesName("FreeBSD");@b@ public static final boolean IS_OS_OPEN_BSD = getOSMatchesName("OpenBSD");@b@ public static final boolean IS_OS_NET_BSD = getOSMatchesName("NetBSD");@b@ public static final boolean IS_OS_OS2 = getOSMatchesName("OS/2");@b@ public static final boolean IS_OS_SOLARIS = getOSMatchesName("Solaris");@b@ public static final boolean IS_OS_SUN_OS = getOSMatchesName("SunOS");@b@ public static final boolean IS_OS_UNIX = (IS_OS_AIX) || (IS_OS_HP_UX) || (IS_OS_IRIX) || (IS_OS_LINUX) || (IS_OS_MAC_OSX) || (IS_OS_SOLARIS) || (IS_OS_SUN_OS) || (IS_OS_FREE_BSD) || (IS_OS_OPEN_BSD) || (IS_OS_NET_BSD);@b@ public static final boolean IS_OS_WINDOWS = getOSMatchesName("Windows");@b@ public static final boolean IS_OS_WINDOWS_2000 = getOSMatches("Windows", "5.0");@b@ public static final boolean IS_OS_WINDOWS_2003 = getOSMatches("Windows", "5.2");@b@ public static final boolean IS_OS_WINDOWS_2008 = getOSMatches("Windows Server 2008", "6.1");@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 = getOSMatchesName("Windows NT");@b@ public static final boolean IS_OS_WINDOWS_XP = getOSMatches("Windows", "5.1");@b@ public static final boolean IS_OS_WINDOWS_VISTA = getOSMatches("Windows", "6.0");@b@ public static final boolean IS_OS_WINDOWS_7 = getOSMatches("Windows", "6.1");@b@ public static final boolean IS_OS_WINDOWS_8 = getOSMatches("Windows", "6.2");@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@ private static boolean getJavaVersionMatches(String versionPrefix)@b@ {@b@ return isJavaVersionMatch(JAVA_SPECIFICATION_VERSION, versionPrefix);@b@ }@b@@b@ private static boolean getOSMatches(String osNamePrefix, String osVersionPrefix)@b@ {@b@ return isOSMatch(OS_NAME, OS_VERSION, osNamePrefix, osVersionPrefix);@b@ }@b@@b@ private static boolean getOSMatchesName(String osNamePrefix)@b@ {@b@ return isOSNameMatch(OS_NAME, osNamePrefix);@b@ }@b@@b@ private static String getSystemProperty(String property)@b@ {@b@ try@b@ {@b@ return System.getProperty(property);@b@ }@b@ catch (SecurityException ex) {@b@ System.err.println("Caught a SecurityException reading the system property '" + property + "'; 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(JavaVersion requiredVersion)@b@ {@b@ return JAVA_SPECIFICATION_VERSION_AS_ENUM.atLeast(requiredVersion);@b@ }@b@@b@ static boolean isJavaVersionMatch(String version, String versionPrefix)@b@ {@b@ if (version == null)@b@ return false;@b@@b@ return version.startsWith(versionPrefix);@b@ }@b@@b@ static boolean isOSMatch(String osName, String osVersion, String osNamePrefix, String osVersionPrefix)@b@ {@b@ if ((osName == null) || (osVersion == null))@b@ return false;@b@@b@ return ((osName.startsWith(osNamePrefix)) && (osVersion.startsWith(osVersionPrefix)));@b@ }@b@@b@ static boolean isOSNameMatch(String osName, String osNamePrefix)@b@ {@b@ if (osName == null)@b@ return false;@b@@b@ return osName.startsWith(osNamePrefix);@b@ }@b@}