1 17 18 package org.apache.geronimo.system.configuration.condition; 19 20 27 import java.io.File ; 28 29 48 public class SystemUtils 49 { 50 53 private static final String OS_NAME_WINDOWS_PREFIX = "Windows"; 54 55 59 62 private static final String USER_HOME_KEY = "user.home"; 63 64 67 private static final String USER_DIR_KEY = "user.dir"; 68 69 72 private static final String JAVA_IO_TMPDIR_KEY = "java.io.tmpdir"; 73 74 77 private static final String JAVA_HOME_KEY = "java.home"; 78 79 95 public static final String AWT_TOOLKIT = getSystemProperty("awt.toolkit"); 96 97 113 public static final String FILE_ENCODING = getSystemProperty("file.encoding"); 114 115 130 public static final String FILE_SEPARATOR = getSystemProperty("file.separator"); 131 132 146 public static final String JAVA_AWT_FONTS = getSystemProperty("java.awt.fonts"); 147 148 162 public static final String JAVA_AWT_GRAPHICSENV = getSystemProperty("java.awt.graphicsenv"); 163 164 183 public static final String JAVA_AWT_HEADLESS = getSystemProperty("java.awt.headless"); 184 185 199 public static final String JAVA_AWT_PRINTERJOB = getSystemProperty("java.awt.printerjob"); 200 201 215 public static final String JAVA_CLASS_PATH = getSystemProperty("java.class.path"); 216 217 232 public static final String JAVA_CLASS_VERSION = getSystemProperty("java.class.version"); 233 234 249 public static final String JAVA_COMPILER = getSystemProperty("java.compiler"); 250 251 266 public static final String JAVA_ENDORSED_DIRS = getSystemProperty("java.endorsed.dirs"); 267 268 283 public static final String JAVA_EXT_DIRS = getSystemProperty("java.ext.dirs"); 284 285 299 public static final String JAVA_HOME = getSystemProperty(JAVA_HOME_KEY); 300 301 315 public static final String JAVA_IO_TMPDIR = getSystemProperty(JAVA_IO_TMPDIR_KEY); 316 317 332 public static final String JAVA_LIBRARY_PATH = getSystemProperty("java.library.path"); 333 334 350 public static final String JAVA_RUNTIME_NAME = getSystemProperty("java.runtime.name"); 351 352 368 public static final String JAVA_RUNTIME_VERSION = getSystemProperty("java.runtime.version"); 369 370 385 public static final String JAVA_SPECIFICATION_NAME = getSystemProperty("java.specification.name"); 386 387 402 public static final String JAVA_SPECIFICATION_VENDOR = getSystemProperty("java.specification.vendor"); 403 404 419 public static final String JAVA_SPECIFICATION_VERSION = getSystemProperty("java.specification.version"); 420 421 436 public static final String JAVA_UTIL_PREFS_PREFERENCES_FACTORY = 437 getSystemProperty("java.util.prefs.PreferencesFactory"); 438 439 453 public static final String JAVA_VENDOR = getSystemProperty("java.vendor"); 454 455 469 public static final String JAVA_VENDOR_URL = getSystemProperty("java.vendor.url"); 470 471 485 public static final String JAVA_VERSION = getSystemProperty("java.version"); 486 487 503 public static final String JAVA_VM_INFO = getSystemProperty("java.vm.info"); 504 505 520 public static final String JAVA_VM_NAME = getSystemProperty("java.vm.name"); 521 522 537 public static final String JAVA_VM_SPECIFICATION_NAME = getSystemProperty("java.vm.specification.name"); 538 539 554 public static final String JAVA_VM_SPECIFICATION_VENDOR = getSystemProperty("java.vm.specification.vendor"); 555 556 571 public static final String JAVA_VM_SPECIFICATION_VERSION = getSystemProperty("java.vm.specification.version"); 572 573 588 public static final String JAVA_VM_VENDOR = getSystemProperty("java.vm.vendor"); 589 590 605 public static final String JAVA_VM_VERSION = getSystemProperty("java.vm.version"); 606 607 622 public static final String LINE_SEPARATOR = getSystemProperty("line.separator"); 623 624 638 public static final String OS_ARCH = getSystemProperty("os.arch"); 639 640 654 public static final String OS_NAME = getSystemProperty("os.name"); 655 656 670 public static final String OS_VERSION = getSystemProperty("os.version"); 671 672 687 public static final String PATH_SEPARATOR = getSystemProperty("path.separator"); 688 689 706 public static final String USER_COUNTRY = 707 getSystemProperty("user.country") == null ? 708 getSystemProperty("user.region") : getSystemProperty("user.country"); 709 710 725 public static final String USER_DIR = getSystemProperty(USER_DIR_KEY); 726 727 741 public static final String USER_HOME = getSystemProperty(USER_HOME_KEY); 742 743 759 public static final String USER_LANGUAGE = getSystemProperty("user.language"); 760 761 775 public static final String USER_NAME = getSystemProperty("user.name"); 776 777 792 public static final String USER_TIMEZONE = getSystemProperty("user.timezone"); 793 794 799 806 public static final String JAVA_VERSION_TRIMMED = getJavaVersionTrimmed(); 807 808 813 826 public static final float JAVA_VERSION_FLOAT = getJavaVersionAsFloat(); 827 828 841 public static final int JAVA_VERSION_INT = getJavaVersionAsInt(); 842 843 848 854 public static final boolean IS_JAVA_1_1 = getJavaVersionMatches("1.1"); 855 856 862 public static final boolean IS_JAVA_1_2 = getJavaVersionMatches("1.2"); 863 864 870 public static final boolean IS_JAVA_1_3 = getJavaVersionMatches("1.3"); 871 872 878 public static final boolean IS_JAVA_1_4 = getJavaVersionMatches("1.4"); 879 880 886 public static final boolean IS_JAVA_1_5 = getJavaVersionMatches("1.5"); 887 888 894 public static final boolean IS_JAVA_1_6 = getJavaVersionMatches("1.6"); 895 896 904 912 public static final boolean IS_OS_AIX = getOSMatches("AIX"); 913 914 922 public static final boolean IS_OS_HP_UX = getOSMatches("HP-UX"); 923 924 932 public static final boolean IS_OS_IRIX = getOSMatches("Irix"); 933 934 942 public static final boolean IS_OS_LINUX = getOSMatches("Linux") || getOSMatches("LINUX"); 943 944 952 public static final boolean IS_OS_MAC = getOSMatches("Mac"); 953 954 962 public static final boolean IS_OS_MAC_OSX = getOSMatches("Mac OS X"); 963 964 972 public static final boolean IS_OS_OS2 = getOSMatches("OS/2"); 973 974 982 public static final boolean IS_OS_SOLARIS = getOSMatches("Solaris"); 983 984 992 public static final boolean IS_OS_SUN_OS = getOSMatches("SunOS"); 993 994 1003 public static final boolean IS_OS_UNIX = 1004 IS_OS_AIX || IS_OS_HP_UX || IS_OS_IRIX || IS_OS_LINUX || 1005 IS_OS_MAC_OSX || IS_OS_SOLARIS || IS_OS_SUN_OS; 1006 1007 1015 public static final boolean IS_OS_WINDOWS = getOSMatches(OS_NAME_WINDOWS_PREFIX); 1016 1017 1025 public static final boolean IS_OS_WINDOWS_2000 = getOSMatches(OS_NAME_WINDOWS_PREFIX, "5.0"); 1026 1027 1035 public static final boolean IS_OS_WINDOWS_95 = getOSMatches(OS_NAME_WINDOWS_PREFIX + " 9", "4.0"); 1036 1038 1046 public static final boolean IS_OS_WINDOWS_98 = getOSMatches(OS_NAME_WINDOWS_PREFIX + " 9", "4.1"); 1047 1049 1057 public static final boolean IS_OS_WINDOWS_ME = getOSMatches(OS_NAME_WINDOWS_PREFIX, "4.9"); 1058 1060 1068 public static final boolean IS_OS_WINDOWS_NT = getOSMatches(OS_NAME_WINDOWS_PREFIX + " NT"); 1069 1071 1079 public static final boolean IS_OS_WINDOWS_XP = getOSMatches(OS_NAME_WINDOWS_PREFIX, "5.1"); 1080 1081 1090 public SystemUtils() { 1091 super(); 1092 } 1093 1094 1108 public static float getJavaVersion() { 1109 return JAVA_VERSION_FLOAT; 1110 } 1111 1112 1126 private static float getJavaVersionAsFloat() { 1127 if (JAVA_VERSION_TRIMMED == null) { 1128 return 0f; 1129 } 1130 String str = JAVA_VERSION_TRIMMED.substring(0, 3); 1131 if (JAVA_VERSION_TRIMMED.length() >= 5) { 1132 str = str + JAVA_VERSION_TRIMMED.substring(4, 5); 1133 } 1134 try { 1135 return Float.parseFloat(str); 1136 } catch (Exception ex) { 1137 return 0; 1138 } 1139 } 1140 1141 1155 private static int getJavaVersionAsInt() { 1156 if (JAVA_VERSION_TRIMMED == null) { 1157 return 0; 1158 } 1159 String str = JAVA_VERSION_TRIMMED.substring(0, 1); 1160 str = str + JAVA_VERSION_TRIMMED.substring(2, 3); 1161 if (JAVA_VERSION_TRIMMED.length() >= 5) { 1162 str = str + JAVA_VERSION_TRIMMED.substring(4, 5); 1163 } else { 1164 str = str + "0"; 1165 } 1166 try { 1167 return Integer.parseInt(str); 1168 } catch (Exception ex) { 1169 return 0; 1170 } 1171 } 1172 1173 1178 private static String getJavaVersionTrimmed() { 1179 if (JAVA_VERSION != null) { 1180 for (int i = 0; i < JAVA_VERSION.length(); i++) { 1181 char ch = JAVA_VERSION.charAt(i); 1182 if (ch >= '0' && ch <= '9') { 1183 return JAVA_VERSION.substring(i); 1184 } 1185 } 1186 } 1187 return null; 1188 } 1189 1190 1196 private static boolean getJavaVersionMatches(String versionPrefix) { 1197 if (JAVA_VERSION_TRIMMED == null) { 1198 return false; 1199 } 1200 return JAVA_VERSION_TRIMMED.startsWith(versionPrefix); 1201 } 1202 1203 1209 private static boolean getOSMatches(String osNamePrefix) { 1210 if (OS_NAME == null) { 1211 return false; 1212 } 1213 return OS_NAME.startsWith(osNamePrefix); 1214 } 1215 1216 1223 private static boolean getOSMatches(String osNamePrefix, String osVersionPrefix) { 1224 if (OS_NAME == null || OS_VERSION == null) { 1225 return false; 1226 } 1227 return OS_NAME.startsWith(osNamePrefix) && OS_VERSION.startsWith(osVersionPrefix); 1228 } 1229 1230 1241 private static String getSystemProperty(String property) { 1242 try { 1243 return System.getProperty(property); 1244 } catch (SecurityException ex) { 1245 System.err.println( 1247 "Caught a SecurityException reading the system property '" + property 1248 + "'; the SystemUtils property value will default to null." 1249 ); 1250 return null; 1251 } 1252 } 1253 1254 1267 public static boolean isJavaVersionAtLeast(float requiredVersion) { 1268 return JAVA_VERSION_FLOAT >= requiredVersion; 1269 } 1270 1271 1285 public static boolean isJavaVersionAtLeast(int requiredVersion) { 1286 return JAVA_VERSION_INT >= requiredVersion; 1287 } 1288 1289 1299 public static boolean isJavaAwtHeadless() { 1300 return JAVA_AWT_HEADLESS != null ? JAVA_AWT_HEADLESS.equals(Boolean.TRUE.toString()) : false; 1301 } 1302 1312 public static File getJavaHome() { 1313 return new File (System.getProperty(JAVA_HOME_KEY)); 1314 } 1315 1316 1326 public static File getJavaIoTmpDir() { 1327 return new File (System.getProperty(JAVA_IO_TMPDIR_KEY)); 1328 } 1329 1330 1340 public static File getUserDir() { 1341 return new File (System.getProperty(USER_DIR_KEY)); 1342 } 1343 1344 1354 public static File getUserHome() { 1355 return new File (System.getProperty(USER_HOME_KEY)); 1356 } 1357 1358} | Popular Tags |