1 16 package org.apache.commons.lang; 17 18 import java.io.File ; 19 20 39 public class SystemUtils { 40 41 44 private static final String OS_NAME_WINDOWS_PREFIX = "Windows"; 45 46 50 53 private static final String USER_HOME_KEY = "user.home"; 54 55 58 private static final String USER_DIR_KEY = "user.dir"; 59 60 63 private static final String JAVA_IO_TMPDIR_KEY = "java.io.tmpdir"; 64 65 68 private static final String JAVA_HOME_KEY = "java.home"; 69 70 86 public static final String AWT_TOOLKIT = getSystemProperty("awt.toolkit"); 87 88 104 public static final String FILE_ENCODING = getSystemProperty("file.encoding"); 105 106 121 public static final String FILE_SEPARATOR = getSystemProperty("file.separator"); 122 123 137 public static final String JAVA_AWT_FONTS = getSystemProperty("java.awt.fonts"); 138 139 153 public static final String JAVA_AWT_GRAPHICSENV = getSystemProperty("java.awt.graphicsenv"); 154 155 174 public static final String JAVA_AWT_HEADLESS = getSystemProperty("java.awt.headless"); 175 176 190 public static final String JAVA_AWT_PRINTERJOB = getSystemProperty("java.awt.printerjob"); 191 192 206 public static final String JAVA_CLASS_PATH = getSystemProperty("java.class.path"); 207 208 223 public static final String JAVA_CLASS_VERSION = getSystemProperty("java.class.version"); 224 225 240 public static final String JAVA_COMPILER = getSystemProperty("java.compiler"); 241 242 257 public static final String JAVA_ENDORSED_DIRS = getSystemProperty("java.endorsed.dirs"); 258 259 274 public static final String JAVA_EXT_DIRS = getSystemProperty("java.ext.dirs"); 275 276 290 public static final String JAVA_HOME = getSystemProperty(JAVA_HOME_KEY); 291 292 306 public static final String JAVA_IO_TMPDIR = getSystemProperty(JAVA_IO_TMPDIR_KEY); 307 308 323 public static final String JAVA_LIBRARY_PATH = getSystemProperty("java.library.path"); 324 325 341 public static final String JAVA_RUNTIME_NAME = getSystemProperty("java.runtime.name"); 342 343 359 public static final String JAVA_RUNTIME_VERSION = getSystemProperty("java.runtime.version"); 360 361 376 public static final String JAVA_SPECIFICATION_NAME = getSystemProperty("java.specification.name"); 377 378 393 public static final String JAVA_SPECIFICATION_VENDOR = getSystemProperty("java.specification.vendor"); 394 395 410 public static final String JAVA_SPECIFICATION_VERSION = getSystemProperty("java.specification.version"); 411 412 427 public static final String JAVA_UTIL_PREFS_PREFERENCES_FACTORY = 428 getSystemProperty("java.util.prefs.PreferencesFactory"); 429 430 444 public static final String JAVA_VENDOR = getSystemProperty("java.vendor"); 445 446 460 public static final String JAVA_VENDOR_URL = getSystemProperty("java.vendor.url"); 461 462 476 public static final String JAVA_VERSION = getSystemProperty("java.version"); 477 478 494 public static final String JAVA_VM_INFO = getSystemProperty("java.vm.info"); 495 496 511 public static final String JAVA_VM_NAME = getSystemProperty("java.vm.name"); 512 513 528 public static final String JAVA_VM_SPECIFICATION_NAME = getSystemProperty("java.vm.specification.name"); 529 530 545 public static final String JAVA_VM_SPECIFICATION_VENDOR = getSystemProperty("java.vm.specification.vendor"); 546 547 562 public static final String JAVA_VM_SPECIFICATION_VERSION = getSystemProperty("java.vm.specification.version"); 563 564 579 public static final String JAVA_VM_VENDOR = getSystemProperty("java.vm.vendor"); 580 581 596 public static final String JAVA_VM_VERSION = getSystemProperty("java.vm.version"); 597 598 613 public static final String LINE_SEPARATOR = getSystemProperty("line.separator"); 614 615 629 public static final String OS_ARCH = getSystemProperty("os.arch"); 630 631 645 public static final String OS_NAME = getSystemProperty("os.name"); 646 647 661 public static final String OS_VERSION = getSystemProperty("os.version"); 662 663 678 public static final String PATH_SEPARATOR = getSystemProperty("path.separator"); 679 680 697 public static final String USER_COUNTRY = 698 (getSystemProperty("user.country") == null ? 699 getSystemProperty("user.region") : getSystemProperty("user.country")); 700 701 716 public static final String USER_DIR = getSystemProperty(USER_DIR_KEY); 717 718 732 public static final String USER_HOME = getSystemProperty(USER_HOME_KEY); 733 734 750 public static final String USER_LANGUAGE = getSystemProperty("user.language"); 751 752 766 public static final String USER_NAME = getSystemProperty("user.name"); 767 768 783 public static final String USER_TIMEZONE = getSystemProperty("user.timezone"); 784 785 790 797 public static final String JAVA_VERSION_TRIMMED = getJavaVersionTrimmed(); 798 799 804 817 public static final float JAVA_VERSION_FLOAT = getJavaVersionAsFloat(); 818 819 832 public static final int JAVA_VERSION_INT = getJavaVersionAsInt(); 833 834 839 845 public static final boolean IS_JAVA_1_1 = getJavaVersionMatches("1.1"); 846 847 853 public static final boolean IS_JAVA_1_2 = getJavaVersionMatches("1.2"); 854 855 861 public static final boolean IS_JAVA_1_3 = getJavaVersionMatches("1.3"); 862 863 869 public static final boolean IS_JAVA_1_4 = getJavaVersionMatches("1.4"); 870 871 877 public static final boolean IS_JAVA_1_5 = getJavaVersionMatches("1.5"); 878 879 887 895 public static final boolean IS_OS_AIX = getOSMatches("AIX"); 896 897 905 public static final boolean IS_OS_HP_UX = getOSMatches("HP-UX"); 906 907 915 public static final boolean IS_OS_IRIX = getOSMatches("Irix"); 916 917 925 public static final boolean IS_OS_LINUX = getOSMatches("Linux") || getOSMatches("LINUX"); 926 927 935 public static final boolean IS_OS_MAC = getOSMatches("Mac"); 936 937 945 public static final boolean IS_OS_MAC_OSX = getOSMatches("Mac OS X"); 946 947 955 public static final boolean IS_OS_OS2 = getOSMatches("OS/2"); 956 957 965 public static final boolean IS_OS_SOLARIS = getOSMatches("Solaris"); 966 967 975 public static final boolean IS_OS_SUN_OS = getOSMatches("SunOS"); 976 977 986 public static final boolean IS_OS_UNIX = 987 IS_OS_AIX || IS_OS_HP_UX || IS_OS_IRIX || IS_OS_LINUX || 988 IS_OS_MAC_OSX || IS_OS_SOLARIS || IS_OS_SUN_OS; 989 990 998 public static final boolean IS_OS_WINDOWS = getOSMatches(OS_NAME_WINDOWS_PREFIX); 999 1000 1008 public static final boolean IS_OS_WINDOWS_2000 = getOSMatches(OS_NAME_WINDOWS_PREFIX, "5.0"); 1009 1010 1018 public static final boolean IS_OS_WINDOWS_95 = getOSMatches(OS_NAME_WINDOWS_PREFIX + " 9", "4.0"); 1019 1021 1029 public static final boolean IS_OS_WINDOWS_98 = getOSMatches(OS_NAME_WINDOWS_PREFIX + " 9", "4.1"); 1030 1032 1040 public static final boolean IS_OS_WINDOWS_ME = getOSMatches(OS_NAME_WINDOWS_PREFIX, "4.9"); 1041 1043 1051 public static final boolean IS_OS_WINDOWS_NT = getOSMatches(OS_NAME_WINDOWS_PREFIX + " NT"); 1052 1054 1062 public static final boolean IS_OS_WINDOWS_XP = getOSMatches(OS_NAME_WINDOWS_PREFIX, "5.1"); 1063 1064 1073 public SystemUtils() { 1074 } 1076 1077 1091 public static float getJavaVersion() { 1092 return JAVA_VERSION_FLOAT; 1093 } 1094 1095 1109 private static float getJavaVersionAsFloat() { 1110 if (JAVA_VERSION_TRIMMED == null) { 1111 return 0f; 1112 } 1113 String str = JAVA_VERSION_TRIMMED.substring(0, 3); 1114 if (JAVA_VERSION_TRIMMED.length() >= 5) { 1115 str = str + JAVA_VERSION_TRIMMED.substring(4, 5); 1116 } 1117 return Float.parseFloat(str); 1118 } 1119 1120 1134 private static int getJavaVersionAsInt() { 1135 if (JAVA_VERSION_TRIMMED == null) { 1136 return 0; 1137 } 1138 String str = JAVA_VERSION_TRIMMED.substring(0, 1); 1139 str = str + JAVA_VERSION_TRIMMED.substring(2, 3); 1140 if (JAVA_VERSION_TRIMMED.length() >= 5) { 1141 str = str + JAVA_VERSION_TRIMMED.substring(4, 5); 1142 } else { 1143 str = str + "0"; 1144 } 1145 return Integer.parseInt(str); 1146 } 1147 1148 1153 private static String getJavaVersionTrimmed() { 1154 if (JAVA_VERSION != null) { 1155 for (int i = 0; i < JAVA_VERSION.length(); i++) { 1156 char ch = JAVA_VERSION.charAt(i); 1157 if (ch >= '0' && ch <= '9') { 1158 return JAVA_VERSION.substring(i); 1159 } 1160 } 1161 } 1162 return null; 1163 } 1164 1165 1171 private static boolean getJavaVersionMatches(String versionPrefix) { 1172 if (JAVA_VERSION_TRIMMED == null) { 1173 return false; 1174 } 1175 return JAVA_VERSION_TRIMMED.startsWith(versionPrefix); 1176 } 1177 1178 1184 private static boolean getOSMatches(String osNamePrefix) { 1185 if (OS_NAME == null) { 1186 return false; 1187 } 1188 return OS_NAME.startsWith(osNamePrefix); 1189 } 1190 1191 1198 private static boolean getOSMatches(String osNamePrefix, String osVersionPrefix) { 1199 if (OS_NAME == null || OS_VERSION == null) { 1200 return false; 1201 } 1202 return OS_NAME.startsWith(osNamePrefix) && OS_VERSION.startsWith(osVersionPrefix); 1203 } 1204 1205 1216 private static String getSystemProperty(String property) { 1217 try { 1218 return System.getProperty(property); 1219 } catch (SecurityException ex) { 1220 System.err.println( 1222 "Caught a SecurityException reading the system property '" + property 1223 + "'; the SystemUtils property value will default to null." 1224 ); 1225 return null; 1226 } 1227 } 1228 1229 1242 public static boolean isJavaVersionAtLeast(float requiredVersion) { 1243 return JAVA_VERSION_FLOAT >= requiredVersion; 1244 } 1245 1246 1260 public static boolean isJavaVersionAtLeast(int requiredVersion) { 1261 return JAVA_VERSION_INT >= requiredVersion; 1262 } 1263 1264 1274 public static boolean isJavaAwtHeadless() { 1275 return JAVA_AWT_HEADLESS != null ? JAVA_AWT_HEADLESS.equals(Boolean.TRUE.toString()) : false; 1276 } 1277 1287 public static File getJavaHome() { 1288 return new File (System.getProperty(JAVA_HOME_KEY)); 1289 } 1290 1291 1301 public static File getJavaIoTmpDir() { 1302 return new File (System.getProperty(JAVA_IO_TMPDIR_KEY)); 1303 } 1304 1305 1315 public static File getUserDir() { 1316 return new File (System.getProperty(USER_DIR_KEY)); 1317 } 1318 1319 1329 public static File getUserHome() { 1330 return new File (System.getProperty(USER_HOME_KEY)); 1331 } 1332 1333} 1334 | Popular Tags |