001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.lang;
018
019 import java.io.File;
020
021 /**
022 * <p>
023 * Helpers for <code>java.lang.System</code>.
024 * </p>
025 *
026 * <p>
027 * If a system property cannot be read due to security restrictions,
028 * the corresponding field in this class will be set to <code>null</code>
029 * and a message will be written to <code>System.err</code>.
030 * </p>
031 *
032 * <p>
033 * #ThreadSafe#
034 * </p>
035 *
036 * @author Apache Software Foundation
037 * @author Based on code from Avalon Excalibur
038 * @author Based on code from Lucene
039 * @author <a href="mailto:sdowney@panix.com">Steve Downey</a>
040 * @author Gary Gregory
041 * @author Michael Becke
042 * @author Tetsuya Kaneuchi
043 * @author Rafal Krupinski
044 * @author Jason Gritman
045 * @since 1.0
046 * @version $Id: SystemUtils.java 1056988 2011-01-09 17:58:53Z niallp $
047 */
048 public class SystemUtils {
049
050 private static final int JAVA_VERSION_TRIM_SIZE = 3;
051
052 /**
053 * The prefix String for all Windows OS.
054 */
055 private static final String OS_NAME_WINDOWS_PREFIX = "Windows";
056
057 // System property constants
058 // -----------------------------------------------------------------------
059 // These MUST be declared first. Other constants depend on this.
060
061 /**
062 * The System property key for the user home directory.
063 */
064 private static final String USER_HOME_KEY = "user.home";
065
066 /**
067 * The System property key for the user directory.
068 */
069 private static final String USER_DIR_KEY = "user.dir";
070
071 /**
072 * The System property key for the Java IO temporary directory.
073 */
074 private static final String JAVA_IO_TMPDIR_KEY = "java.io.tmpdir";
075
076 /**
077 * The System property key for the Java home directory.
078 */
079 private static final String JAVA_HOME_KEY = "java.home";
080
081 /**
082 * <p>
083 * The <code>awt.toolkit</code> System Property.
084 * </p>
085 * <p>
086 * Holds a class name, on Windows XP this is <code>sun.awt.windows.WToolkit</code>.
087 * </p>
088 * <p>
089 * <b>On platforms without a GUI, this value is <code>null</code>.</b>
090 * </p>
091 *
092 * <p>
093 * Defaults to <code>null</code> if the runtime does not have
094 * security access to read this property or the property does not exist.
095 * </p>
096 *
097 * <p>
098 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
099 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will
100 * be out of sync with that System property.
101 * </p>
102 *
103 * @since 2.1
104 */
105 public static final String AWT_TOOLKIT = getSystemProperty("awt.toolkit");
106
107 /**
108 * <p>
109 * The <code>file.encoding</code> System Property.
110 * </p>
111 * <p>
112 * File encoding, such as <code>Cp1252</code>.
113 * </p>
114 *
115 * <p>
116 * Defaults to <code>null</code> if the runtime does not have
117 * security access to read this property or the property does not exist.
118 * </p>
119 *
120 * <p>
121 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
122 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
123 * will be out of sync with that System property.
124 * </p>
125 *
126 * @since 2.0
127 * @since Java 1.2
128 */
129 public static final String FILE_ENCODING = getSystemProperty("file.encoding");
130
131 /**
132 * <p>
133 * The <code>file.separator</code> System Property. File separator (<code>"/"</code> on UNIX).
134 * </p>
135 *
136 * <p>
137 * Defaults to <code>null</code> if the runtime does not have
138 * security access to read this property or the property does not exist.
139 * </p>
140 *
141 * <p>
142 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
143 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
144 * will be out of sync with that System property.
145 * </p>
146 *
147 * @since Java 1.1
148 */
149 public static final String FILE_SEPARATOR = getSystemProperty("file.separator");
150
151 /**
152 * <p>
153 * The <code>java.awt.fonts</code> System Property.
154 * </p>
155 *
156 * <p>
157 * Defaults to <code>null</code> if the runtime does not have
158 * security access to read this property or the property does not exist.
159 * </p>
160 *
161 * <p>
162 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
163 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
164 * will be out of sync with that System property.
165 * </p>
166 *
167 * @since 2.1
168 */
169 public static final String JAVA_AWT_FONTS = getSystemProperty("java.awt.fonts");
170
171 /**
172 * <p>
173 * The <code>java.awt.graphicsenv</code> System Property.
174 * </p>
175 *
176 * <p>
177 * Defaults to <code>null</code> if the runtime does not have
178 * security access to read this property or the property does not exist.
179 * </p>
180 *
181 * <p>
182 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
183 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
184 * will be out of sync with that System property.
185 * </p>
186 *
187 * @since 2.1
188 */
189 public static final String JAVA_AWT_GRAPHICSENV = getSystemProperty("java.awt.graphicsenv");
190
191 /**
192 * <p>
193 * The <code>java.awt.headless</code> System Property.
194 * The value of this property is the String <code>"true"</code> or <code>"false"</code>.
195 * </p>
196 *
197 * <p>
198 * Defaults to <code>null</code> if the runtime does not have
199 * security access to read this property or the property does not exist.
200 * </p>
201 *
202 * <p>
203 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
204 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
205 * will be out of sync with that System property.
206 * </p>
207 *
208 * @see #isJavaAwtHeadless()
209 * @since 2.1
210 * @since Java 1.4
211 */
212 public static final String JAVA_AWT_HEADLESS = getSystemProperty("java.awt.headless");
213
214 /**
215 * <p>
216 * The <code>java.awt.printerjob</code> System Property.
217 * </p>
218 *
219 * <p>
220 * Defaults to <code>null</code> if the runtime does not have
221 * security access to read this property or the property does not exist.
222 * </p>
223 *
224 * <p>
225 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
226 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
227 * will be out of sync with that System property.
228 * </p>
229 *
230 * @since 2.1
231 */
232 public static final String JAVA_AWT_PRINTERJOB = getSystemProperty("java.awt.printerjob");
233
234 /**
235 * <p>
236 * The <code>java.class.path</code> System Property. Java class path.
237 * </p>
238 *
239 * <p>
240 * Defaults to <code>null</code> if the runtime does not have
241 * security access to read this property or the property does not exist.
242 * </p>
243 *
244 * <p>
245 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
246 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
247 * will be out of sync with that System property.
248 * </p>
249 *
250 * @since Java 1.1
251 */
252 public static final String JAVA_CLASS_PATH = getSystemProperty("java.class.path");
253
254 /**
255 * <p>
256 * The <code>java.class.version</code> System Property. Java class format version number.
257 * </p>
258 *
259 * <p>
260 * Defaults to <code>null</code> if the runtime does not have
261 * security access to read this property or the property does not exist.
262 * </p>
263 *
264 * <p>
265 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
266 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
267 * will be out of sync with that System property.
268 * </p>
269 *
270 * @since Java 1.1
271 */
272 public static final String JAVA_CLASS_VERSION = getSystemProperty("java.class.version");
273
274 /**
275 * <p>
276 * The <code>java.compiler</code> System Property. Name of JIT compiler to use.
277 * First in JDK version 1.2. Not used in Sun JDKs after 1.2.
278 * </p>
279 *
280 * <p>
281 * Defaults to <code>null</code> if the runtime does not have
282 * security access to read this property or the property does not exist.
283 * </p>
284 *
285 * <p>
286 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
287 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
288 * will be out of sync with that System property.
289 * </p>
290 *
291 * @since Java 1.2. Not used in Sun versions after 1.2.
292 */
293 public static final String JAVA_COMPILER = getSystemProperty("java.compiler");
294
295 /**
296 * <p>
297 * The <code>java.endorsed.dirs</code> System Property. Path of endorsed directory or directories.
298 * </p>
299 *
300 * <p>
301 * Defaults to <code>null</code> if the runtime does not have
302 * security access to read this property or the property does not exist.
303 * </p>
304 *
305 * <p>
306 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
307 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
308 * will be out of sync with that System property.
309 * </p>
310 *
311 * @since Java 1.4
312 */
313 public static final String JAVA_ENDORSED_DIRS = getSystemProperty("java.endorsed.dirs");
314
315 /**
316 * <p>
317 * The <code>java.ext.dirs</code> System Property. Path of extension directory or directories.
318 * </p>
319 *
320 * <p>
321 * Defaults to <code>null</code> if the runtime does not have
322 * security access to read this property or the property does not exist.
323 * </p>
324 *
325 * <p>
326 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
327 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
328 * will be out of sync with that System property.
329 * </p>
330 *
331 * @since Java 1.3
332 */
333 public static final String JAVA_EXT_DIRS = getSystemProperty("java.ext.dirs");
334
335 /**
336 * <p>
337 * The <code>java.home</code> System Property. Java installation directory.
338 * </p>
339 *
340 * <p>
341 * Defaults to <code>null</code> if the runtime does not have
342 * security access to read this property or the property does not exist.
343 * </p>
344 *
345 * <p>
346 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
347 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
348 * will be out of sync with that System property.
349 * </p>
350 *
351 * @since Java 1.1
352 */
353 public static final String JAVA_HOME = getSystemProperty(JAVA_HOME_KEY);
354
355 /**
356 * <p>
357 * The <code>java.io.tmpdir</code> System Property. Default temp file path.
358 * </p>
359 *
360 * <p>
361 * Defaults to <code>null</code> if the runtime does not have
362 * security access to read this property or the property does not exist.
363 * </p>
364 *
365 * <p>
366 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
367 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
368 * will be out of sync with that System property.
369 * </p>
370 *
371 * @since Java 1.2
372 */
373 public static final String JAVA_IO_TMPDIR = getSystemProperty(JAVA_IO_TMPDIR_KEY);
374
375 /**
376 * <p>
377 * The <code>java.library.path</code> System Property. List of paths to search when loading libraries.
378 * </p>
379 *
380 * <p>
381 * Defaults to <code>null</code> if the runtime does not have
382 * security access to read this property or the property does not exist.
383 * </p>
384 *
385 * <p>
386 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
387 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
388 * will be out of sync with that System property.
389 * </p>
390 *
391 * @since Java 1.2
392 */
393 public static final String JAVA_LIBRARY_PATH = getSystemProperty("java.library.path");
394
395 /**
396 * <p>
397 * The <code>java.runtime.name</code> System Property. Java Runtime Environment name.
398 * </p>
399 *
400 * <p>
401 * Defaults to <code>null</code> if the runtime does not have
402 * security access to read this property or the property does not exist.
403 * </p>
404 *
405 * <p>
406 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
407 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
408 * will be out of sync with that System property.
409 * </p>
410 *
411 * @since 2.0
412 * @since Java 1.3
413 */
414 public static final String JAVA_RUNTIME_NAME = getSystemProperty("java.runtime.name");
415
416 /**
417 * <p>
418 * The <code>java.runtime.version</code> System Property. Java Runtime Environment version.
419 * </p>
420 *
421 * <p>
422 * Defaults to <code>null</code> if the runtime does not have
423 * security access to read this property or the property does not exist.
424 * </p>
425 *
426 * <p>
427 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
428 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
429 * will be out of sync with that System property.
430 * </p>
431 *
432 * @since 2.0
433 * @since Java 1.3
434 */
435 public static final String JAVA_RUNTIME_VERSION = getSystemProperty("java.runtime.version");
436
437 /**
438 * <p>
439 * The <code>java.specification.name</code> System Property. Java Runtime Environment specification name.
440 * </p>
441 *
442 * <p>
443 * Defaults to <code>null</code> if the runtime does not have
444 * security access to read this property or the property does not exist.
445 * </p>
446 *
447 * <p>
448 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
449 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
450 * will be out of sync with that System property.
451 * </p>
452 *
453 * @since Java 1.2
454 */
455 public static final String JAVA_SPECIFICATION_NAME = getSystemProperty("java.specification.name");
456
457 /**
458 * <p>
459 * The <code>java.specification.vendor</code> System Property. Java Runtime Environment specification vendor.
460 * </p>
461 *
462 * <p>
463 * Defaults to <code>null</code> if the runtime does not have
464 * security access to read this property or the property does not exist.
465 * </p>
466 *
467 * <p>
468 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
469 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
470 * will be out of sync with that System property.
471 * </p>
472 *
473 * @since Java 1.2
474 */
475 public static final String JAVA_SPECIFICATION_VENDOR = getSystemProperty("java.specification.vendor");
476
477 /**
478 * <p>
479 * The <code>java.specification.version</code> System Property. Java Runtime Environment specification version.
480 * </p>
481 *
482 * <p>
483 * Defaults to <code>null</code> if the runtime does not have
484 * security access to read this property or the property does not exist.
485 * </p>
486 *
487 * <p>
488 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
489 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
490 * will be out of sync with that System property.
491 * </p>
492 *
493 * @since Java 1.3
494 */
495 public static final String JAVA_SPECIFICATION_VERSION = getSystemProperty("java.specification.version");
496
497 /**
498 * <p>
499 * The <code>java.util.prefs.PreferencesFactory</code> System Property. A class name.
500 * </p>
501 *
502 * <p>
503 * Defaults to <code>null</code> if the runtime does not have
504 * security access to read this property or the property does not exist.
505 * </p>
506 *
507 * <p>
508 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
509 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
510 * will be out of sync with that System property.
511 * </p>
512 *
513 * @since 2.1
514 * @since Java 1.4
515 */
516 public static final String JAVA_UTIL_PREFS_PREFERENCES_FACTORY =
517 getSystemProperty("java.util.prefs.PreferencesFactory");
518
519 /**
520 * <p>
521 * The <code>java.vendor</code> System Property. Java vendor-specific string.
522 * </p>
523 *
524 * <p>
525 * Defaults to <code>null</code> if the runtime does not have
526 * security access to read this property or the property does not exist.
527 * </p>
528 *
529 * <p>
530 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
531 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
532 * will be out of sync with that System property.
533 * </p>
534 *
535 * @since Java 1.1
536 */
537 public static final String JAVA_VENDOR = getSystemProperty("java.vendor");
538
539 /**
540 * <p>
541 * The <code>java.vendor.url</code> System Property. Java vendor URL.
542 * </p>
543 *
544 * <p>
545 * Defaults to <code>null</code> if the runtime does not have
546 * security access to read this property or the property does not exist.
547 * </p>
548 *
549 * <p>
550 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
551 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
552 * will be out of sync with that System property.
553 * </p>
554 *
555 * @since Java 1.1
556 */
557 public static final String JAVA_VENDOR_URL = getSystemProperty("java.vendor.url");
558
559 /**
560 * <p>
561 * The <code>java.version</code> System Property. Java version number.
562 * </p>
563 *
564 * <p>
565 * Defaults to <code>null</code> if the runtime does not have
566 * security access to read this property or the property does not exist.
567 * </p>
568 *
569 * <p>
570 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
571 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
572 * will be out of sync with that System property.
573 * </p>
574 *
575 * @since Java 1.1
576 */
577 public static final String JAVA_VERSION = getSystemProperty("java.version");
578
579 /**
580 * <p>
581 * The <code>java.vm.info</code> System Property. Java Virtual Machine implementation info.
582 * </p>
583 *
584 * <p>
585 * Defaults to <code>null</code> if the runtime does not have
586 * security access to read this property or the property does not exist.
587 * </p>
588 *
589 * <p>
590 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
591 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
592 * will be out of sync with that System property.
593 * </p>
594 *
595 * @since 2.0
596 * @since Java 1.2
597 */
598 public static final String JAVA_VM_INFO = getSystemProperty("java.vm.info");
599
600 /**
601 * <p>
602 * The <code>java.vm.name</code> System Property. Java Virtual Machine implementation name.
603 * </p>
604 *
605 * <p>
606 * Defaults to <code>null</code> if the runtime does not have
607 * security access to read this property or the property does not exist.
608 * </p>
609 *
610 * <p>
611 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
612 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
613 * will be out of sync with that System property.
614 * </p>
615 *
616 * @since Java 1.2
617 */
618 public static final String JAVA_VM_NAME = getSystemProperty("java.vm.name");
619
620 /**
621 * <p>
622 * The <code>java.vm.specification.name</code> System Property. Java Virtual Machine specification name.
623 * </p>
624 *
625 * <p>
626 * Defaults to <code>null</code> if the runtime does not have
627 * security access to read this property or the property does not exist.
628 * </p>
629 *
630 * <p>
631 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
632 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
633 * will be out of sync with that System property.
634 * </p>
635 *
636 * @since Java 1.2
637 */
638 public static final String JAVA_VM_SPECIFICATION_NAME = getSystemProperty("java.vm.specification.name");
639
640 /**
641 * <p>
642 * The <code>java.vm.specification.vendor</code> System Property. Java Virtual Machine specification vendor.
643 * </p>
644 *
645 * <p>
646 * Defaults to <code>null</code> if the runtime does not have
647 * security access to read this property or the property does not exist.
648 * </p>
649 *
650 * <p>
651 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
652 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
653 * will be out of sync with that System property.
654 * </p>
655 *
656 * @since Java 1.2
657 */
658 public static final String JAVA_VM_SPECIFICATION_VENDOR = getSystemProperty("java.vm.specification.vendor");
659
660 /**
661 * <p>
662 * The <code>java.vm.specification.version</code> System Property. Java Virtual Machine specification version.
663 * </p>
664 *
665 * <p>
666 * Defaults to <code>null</code> if the runtime does not have
667 * security access to read this property or the property does not exist.
668 * </p>
669 *
670 * <p>
671 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
672 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
673 * will be out of sync with that System property.
674 * </p>
675 *
676 * @since Java 1.2
677 */
678 public static final String JAVA_VM_SPECIFICATION_VERSION = getSystemProperty("java.vm.specification.version");
679
680 /**
681 * <p>
682 * The <code>java.vm.vendor</code> System Property. Java Virtual Machine implementation vendor.
683 * </p>
684 *
685 * <p>
686 * Defaults to <code>null</code> if the runtime does not have
687 * security access to read this property or the property does not exist.
688 * </p>
689 *
690 * <p>
691 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
692 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
693 * will be out of sync with that System property.
694 * </p>
695 *
696 * @since Java 1.2
697 */
698 public static final String JAVA_VM_VENDOR = getSystemProperty("java.vm.vendor");
699
700 /**
701 * <p>
702 * The <code>java.vm.version</code> System Property. Java Virtual Machine implementation version.
703 * </p>
704 *
705 * <p>
706 * Defaults to <code>null</code> if the runtime does not have
707 * security access to read this property or the property does not exist.
708 * </p>
709 *
710 * <p>
711 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
712 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
713 * will be out of sync with that System property.
714 * </p>
715 *
716 * @since Java 1.2
717 */
718 public static final String JAVA_VM_VERSION = getSystemProperty("java.vm.version");
719
720 /**
721 * <p>
722 * The <code>line.separator</code> System Property. Line separator (<code>"\n"</code> on UNIX).
723 * </p>
724 *
725 * <p>
726 * Defaults to <code>null</code> if the runtime does not have
727 * security access to read this property or the property does not exist.
728 * </p>
729 *
730 * <p>
731 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
732 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
733 * will be out of sync with that System property.
734 * </p>
735 *
736 * @since Java 1.1
737 */
738 public static final String LINE_SEPARATOR = getSystemProperty("line.separator");
739
740 /**
741 * <p>
742 * The <code>os.arch</code> System Property. Operating system architecture.
743 * </p>
744 *
745 * <p>
746 * Defaults to <code>null</code> if the runtime does not have
747 * security access to read this property or the property does not exist.
748 * </p>
749 *
750 * <p>
751 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
752 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
753 * will be out of sync with that System property.
754 * </p>
755 *
756 * @since Java 1.1
757 */
758 public static final String OS_ARCH = getSystemProperty("os.arch");
759
760 /**
761 * <p>
762 * The <code>os.name</code> System Property. Operating system name.
763 * </p>
764 *
765 * <p>
766 * Defaults to <code>null</code> if the runtime does not have
767 * security access to read this property or the property does not exist.
768 * </p>
769 *
770 * <p>
771 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
772 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
773 * will be out of sync with that System property.
774 * </p>
775 *
776 * @since Java 1.1
777 */
778 public static final String OS_NAME = getSystemProperty("os.name");
779
780 /**
781 * <p>
782 * The <code>os.version</code> System Property. Operating system version.
783 * </p>
784 *
785 * <p>
786 * Defaults to <code>null</code> if the runtime does not have
787 * security access to read this property or the property does not exist.
788 * </p>
789 *
790 * <p>
791 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
792 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
793 * will be out of sync with that System property.
794 * </p>
795 *
796 * @since Java 1.1
797 */
798 public static final String OS_VERSION = getSystemProperty("os.version");
799
800 /**
801 * <p>
802 * The <code>path.separator</code> System Property. Path separator (<code>":"</code> on UNIX).
803 * </p>
804 *
805 * <p>
806 * Defaults to <code>null</code> if the runtime does not have
807 * security access to read this property or the property does not exist.
808 * </p>
809 *
810 * <p>
811 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
812 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
813 * will be out of sync with that System property.
814 * </p>
815 *
816 * @since Java 1.1
817 */
818 public static final String PATH_SEPARATOR = getSystemProperty("path.separator");
819
820 /**
821 * <p>
822 * The <code>user.country</code> or <code>user.region</code> System Property.
823 * User's country code, such as <code>GB</code>. First in
824 * Java version 1.2 as <code>user.region</code>. Renamed to <code>user.country</code> in 1.4
825 * </p>
826 *
827 * <p>
828 * Defaults to <code>null</code> if the runtime does not have
829 * security access to read this property or the property does not exist.
830 * </p>
831 *
832 * <p>
833 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
834 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
835 * will be out of sync with that System property.
836 * </p>
837 *
838 * @since 2.0
839 * @since Java 1.2
840 */
841 public static final String USER_COUNTRY =
842 getSystemProperty("user.country") == null ?
843 getSystemProperty("user.region") : getSystemProperty("user.country");
844
845 /**
846 * <p>
847 * The <code>user.dir</code> System Property. User's current working directory.
848 * </p>
849 *
850 * <p>
851 * Defaults to <code>null</code> if the runtime does not have
852 * security access to read this property or the property does not exist.
853 * </p>
854 *
855 * <p>
856 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
857 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
858 * will be out of sync with that System property.
859 * </p>
860 *
861 * @since Java 1.1
862 */
863 public static final String USER_DIR = getSystemProperty(USER_DIR_KEY);
864
865 /**
866 * <p>
867 * The <code>user.home</code> System Property. User's home directory.
868 * </p>
869 *
870 * <p>
871 * Defaults to <code>null</code> if the runtime does not have
872 * security access to read this property or the property does not exist.
873 * </p>
874 *
875 * <p>
876 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
877 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
878 * will be out of sync with that System property.
879 * </p>
880 *
881 * @since Java 1.1
882 */
883 public static final String USER_HOME = getSystemProperty(USER_HOME_KEY);
884
885 /**
886 * <p>
887 * The <code>user.language</code> System Property. User's language code, such as <code>"en"</code>.
888 * </p>
889 *
890 * <p>
891 * Defaults to <code>null</code> if the runtime does not have
892 * security access to read this property or the property does not exist.
893 * </p>
894 *
895 * <p>
896 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
897 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
898 * will be out of sync with that System property.
899 * </p>
900 *
901 * @since 2.0
902 * @since Java 1.2
903 */
904 public static final String USER_LANGUAGE = getSystemProperty("user.language");
905
906 /**
907 * <p>
908 * The <code>user.name</code> System Property. User's account name.
909 * </p>
910 *
911 * <p>
912 * Defaults to <code>null</code> if the runtime does not have
913 * security access to read this property or the property does not exist.
914 * </p>
915 *
916 * <p>
917 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
918 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
919 * will be out of sync with that System property.
920 * </p>
921 *
922 * @since Java 1.1
923 */
924 public static final String USER_NAME = getSystemProperty("user.name");
925
926 /**
927 * <p>
928 * The <code>user.timezone</code> System Property. For example: <code>"America/Los_Angeles"</code>.
929 * </p>
930 *
931 * <p>
932 * Defaults to <code>null</code> if the runtime does not have
933 * security access to read this property or the property does not exist.
934 * </p>
935 *
936 * <p>
937 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
938 * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
939 * will be out of sync with that System property.
940 * </p>
941 *
942 * @since 2.1
943 */
944 public static final String USER_TIMEZONE = getSystemProperty("user.timezone");
945
946 // Java version
947 // -----------------------------------------------------------------------
948 // This MUST be declared after those above as it depends on the
949 // values being set up
950
951 /**
952 * <p>
953 * Gets the Java version as a <code>String</code> trimming leading letters.
954 * </p>
955 *
956 * <p>
957 * The field will return <code>null</code> if {@link #JAVA_VERSION} is <code>null</code>.
958 * </p>
959 *
960 * @since 2.1
961 */
962 public static final String JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
963
964 // Java version values
965 // -----------------------------------------------------------------------
966 // These MUST be declared after the trim above as they depend on the
967 // value being set up
968
969 /**
970 * <p>
971 * Gets the Java version as a <code>float</code>.
972 * </p>
973 *
974 * <p>
975 * Example return values:
976 * </p>
977 * <ul>
978 * <li><code>1.2f</code> for Java 1.2
979 * <li><code>1.31f</code> for Java 1.3.1
980 * </ul>
981 *
982 * <p>
983 * The field will return zero if {@link #JAVA_VERSION} is <code>null</code>.
984 * </p>
985 *
986 * @since 2.0
987 */
988 public static final float JAVA_VERSION_FLOAT = getJavaVersionAsFloat();
989
990 /**
991 * <p>
992 * Gets the Java version as an <code>int</code>.
993 * </p>
994 *
995 * <p>
996 * Example return values:
997 * </p>
998 * <ul>
999 * <li><code>120</code> for Java 1.2
1000 * <li><code>131</code> for Java 1.3.1
1001 * </ul>
1002 *
1003 * <p>
1004 * The field will return zero if {@link #JAVA_VERSION} is <code>null</code>.
1005 * </p>
1006 *
1007 * @since 2.0
1008 */
1009 public static final int JAVA_VERSION_INT = getJavaVersionAsInt();
1010
1011 // Java version checks
1012 // -----------------------------------------------------------------------
1013 // These MUST be declared after those above as they depend on the
1014 // values being set up
1015
1016 /**
1017 * <p>
1018 * Is <code>true</code> if this is Java version 1.1 (also 1.1.x versions).
1019 * </p>
1020 *
1021 * <p>
1022 * The field will return <code>false</code> if {@link #JAVA_VERSION} is <code>null</code>.
1023 * </p>
1024 */
1025 public static final boolean IS_JAVA_1_1 = getJavaVersionMatches("1.1");
1026
1027 /**
1028 * <p>
1029 * Is <code>true</code> if this is Java version 1.2 (also 1.2.x versions).
1030 * </p>
1031 *
1032 * <p>
1033 * The field will return <code>false</code> if {@link #JAVA_VERSION} is <code>null</code>.
1034 * </p>
1035 */
1036 public static final boolean IS_JAVA_1_2 = getJavaVersionMatches("1.2");
1037
1038 /**
1039 * <p>
1040 * Is <code>true</code> if this is Java version 1.3 (also 1.3.x versions).
1041 * </p>
1042 *
1043 * <p>
1044 * The field will return <code>false</code> if {@link #JAVA_VERSION} is <code>null</code>.
1045 * </p>
1046 */
1047 public static final boolean IS_JAVA_1_3 = getJavaVersionMatches("1.3");
1048
1049 /**
1050 * <p>
1051 * Is <code>true</code> if this is Java version 1.4 (also 1.4.x versions).
1052 * </p>
1053 *
1054 * <p>
1055 * The field will return <code>false</code> if {@link #JAVA_VERSION} is <code>null</code>.
1056 * </p>
1057 */
1058 public static final boolean IS_JAVA_1_4 = getJavaVersionMatches("1.4");
1059
1060 /**
1061 * <p>
1062 * Is <code>true</code> if this is Java version 1.5 (also 1.5.x versions).
1063 * </p>
1064 *
1065 * <p>
1066 * The field will return <code>false</code> if {@link #JAVA_VERSION} is <code>null</code>.
1067 * </p>
1068 */
1069 public static final boolean IS_JAVA_1_5 = getJavaVersionMatches("1.5");
1070
1071 /**
1072 * <p>
1073 * Is <code>true</code> if this is Java version 1.6 (also 1.6.x versions).
1074 * </p>
1075 *
1076 * <p>
1077 * The field will return <code>false</code> if {@link #JAVA_VERSION} is <code>null</code>.
1078 * </p>
1079 */
1080 public static final boolean IS_JAVA_1_6 = getJavaVersionMatches("1.6");
1081
1082 /**
1083 * <p>
1084 * Is <code>true</code> if this is Java version 1.7 (also 1.7.x versions).
1085 * </p>
1086 *
1087 * <p>
1088 * The field will return <code>false</code> if {@link #JAVA_VERSION} is <code>null</code>.
1089 * </p>
1090 *
1091 * @since 2.5
1092 */
1093 public static final boolean IS_JAVA_1_7 = getJavaVersionMatches("1.7");
1094
1095 // Operating system checks
1096 // -----------------------------------------------------------------------
1097 // These MUST be declared after those above as they depend on the
1098 // values being set up
1099 // OS names from http://www.vamphq.com/os.html
1100 // Selected ones included - please advise dev@commons.apache.org
1101 // if you want another added or a mistake corrected
1102
1103 /**
1104 * <p>
1105 * Is <code>true</code> if this is AIX.
1106 * </p>
1107 *
1108 * <p>
1109 * The field will return <code>false</code> if <code>OS_NAME</code> is <code>null</code>.
1110 * </p>
1111 *
1112 * @since 2.0
1113 */
1114 public static final boolean IS_OS_AIX = getOSMatchesName("AIX");
1115
1116 /**
1117 * <p>
1118 * Is <code>true</code> if this is HP-UX.
1119 * </p>
1120 *
1121 * <p>
1122 * The field will return <code>false</code> if <code>OS_NAME</code> is <code>null</code>.
1123 * </p>
1124 *
1125 * @since 2.0
1126 */
1127 public static final boolean IS_OS_HP_UX = getOSMatchesName("HP-UX");
1128
1129 /**
1130 * <p>
1131 * Is <code>true</code> if this is Irix.
1132 * </p>
1133 *
1134 * <p>
1135 * The field will return <code>false</code> if <code>OS_NAME</code> is <code>null</code>.
1136 * </p>
1137 *
1138 * @since 2.0
1139 */
1140 public static final boolean IS_OS_IRIX = getOSMatchesName("Irix");
1141
1142 /**
1143 * <p>
1144 * Is <code>true</code> if this is Linux.
1145 * </p>
1146 *
1147 * <p>
1148 * The field will return <code>false</code> if <code>OS_NAME</code> is <code>null</code>.
1149 * </p>
1150 *
1151 * @since 2.0
1152 */
1153 public static final boolean IS_OS_LINUX = getOSMatchesName("Linux") || getOSMatchesName("LINUX");
1154
1155 /**
1156 * <p>
1157 * Is <code>true</code> if this is Mac.
1158 * </p>
1159 *
1160 * <p>
1161 * The field will return <code>false</code> if <code>OS_NAME</code> is <code>null</code>.
1162 * </p>
1163 *
1164 * @since 2.0
1165 */
1166 public static final boolean IS_OS_MAC = getOSMatchesName("Mac");
1167
1168 /**
1169 * <p>
1170 * Is <code>true</code> if this is Mac.
1171 * </p>
1172 *
1173 * <p>
1174 * The field will return <code>false</code> if <code>OS_NAME</code> is <code>null</code>.
1175 * </p>
1176 *
1177 * @since 2.0
1178 */
1179 public static final boolean IS_OS_MAC_OSX = getOSMatchesName("Mac OS X");
1180
1181 /**
1182 * <p>
1183 * Is <code>true</code> if this is OS/2.
1184 * </p>
1185 *
1186 * <p>
1187 * The field will return <code>false</code> if <code>OS_NAME</code> is <code>null</code>.
1188 * </p>
1189 *
1190 * @since 2.0
1191 */
1192 public static final boolean IS_OS_OS2 = getOSMatchesName("OS/2");
1193
1194 /**
1195 * <p>
1196 * Is <code>true</code> if this is Solaris.
1197 * </p>
1198 *
1199 * <p>
1200 * The field will return <code>false</code> if <code>OS_NAME</code> is <code>null</code>.
1201 * </p>
1202 *
1203 * @since 2.0
1204 */
1205 public static final boolean IS_OS_SOLARIS = getOSMatchesName("Solaris");
1206
1207 /**
1208 * <p>
1209 * Is <code>true</code> if this is SunOS.
1210 * </p>
1211 *
1212 * <p>
1213 * The field will return <code>false</code> if <code>OS_NAME</code> is <code>null</code>.
1214 * </p>
1215 *
1216 * @since 2.0
1217 */
1218 public static final boolean IS_OS_SUN_OS = getOSMatchesName("SunOS");
1219
1220 /**
1221 * <p>
1222 * Is <code>true</code> if this is a UNIX like system,
1223 * as in any of AIX, HP-UX, Irix, Linux, MacOSX, Solaris or SUN OS.
1224 * </p>
1225 *
1226 * <p>
1227 * The field will return <code>false</code> if <code>OS_NAME</code> is <code>null</code>.
1228 * </p>
1229 *
1230 * @since 2.1
1231 */
1232 public static final boolean IS_OS_UNIX =
1233 IS_OS_AIX || IS_OS_HP_UX || IS_OS_IRIX || IS_OS_LINUX ||
1234 IS_OS_MAC_OSX || IS_OS_SOLARIS || IS_OS_SUN_OS;
1235
1236 /**
1237 * <p>
1238 * Is <code>true</code> if this is Windows.
1239 * </p>
1240 *
1241 * <p>
1242 * The field will return <code>false</code> if <code>OS_NAME</code> is <code>null</code>.
1243 * </p>
1244 *
1245 * @since 2.0
1246 */
1247 public static final boolean IS_OS_WINDOWS = getOSMatchesName(OS_NAME_WINDOWS_PREFIX);
1248
1249 /**
1250 * <p>
1251 * Is <code>true</code> if this is Windows 2000.
1252 * </p>
1253 *
1254 * <p>
1255 * The field will return <code>false</code> if <code>OS_NAME</code> is <code>null</code>.
1256 * </p>
1257 *
1258 * @since 2.0
1259 */
1260 public static final boolean IS_OS_WINDOWS_2000 = getOSMatches(OS_NAME_WINDOWS_PREFIX, "5.0");
1261
1262 /**
1263 * <p>
1264 * Is <code>true</code> if this is Windows 95.
1265 * </p>
1266 *
1267 * <p>
1268 * The field will return <code>false</code> if <code>OS_NAME</code> is <code>null</code>.
1269 * </p>
1270 *
1271 * @since 2.0
1272 */
1273 public static final boolean IS_OS_WINDOWS_95 = getOSMatches(OS_NAME_WINDOWS_PREFIX + " 9", "4.0");
1274 // Java 1.2 running on Windows98 returns 'Windows 95', hence the above
1275
1276 /**
1277 * <p>
1278 * Is <code>true</code> if this is Windows 98.
1279 * </p>
1280 *
1281 * <p>
1282 * The field will return <code>false</code> if <code>OS_NAME</code> is <code>null</code>.
1283 * </p>
1284 *
1285 * @since 2.0
1286 */
1287 public static final boolean IS_OS_WINDOWS_98 = getOSMatches(OS_NAME_WINDOWS_PREFIX + " 9", "4.1");
1288 // Java 1.2 running on Windows98 returns 'Windows 95', hence the above
1289
1290 /**
1291 * <p>
1292 * Is <code>true</code> if this is Windows ME.
1293 * </p>
1294 *
1295 * <p>
1296 * The field will return <code>false</code> if <code>OS_NAME</code> is <code>null</code>.
1297 * </p>
1298 *
1299 * @since 2.0
1300 */
1301 public static final boolean IS_OS_WINDOWS_ME = getOSMatches(OS_NAME_WINDOWS_PREFIX, "4.9");
1302 // Java 1.2 running on WindowsME may return 'Windows 95', hence the above
1303
1304 /**
1305 * <p>
1306 * Is <code>true</code> if this is Windows NT.
1307 * </p>
1308 *
1309 * <p>
1310 * The field will return <code>false</code> if <code>OS_NAME</code> is <code>null</code>.
1311 * </p>
1312 *
1313 * @since 2.0
1314 */
1315 public static final boolean IS_OS_WINDOWS_NT = getOSMatchesName(OS_NAME_WINDOWS_PREFIX + " NT");
1316 // Windows 2000 returns 'Windows 2000' but may suffer from same Java1.2 problem
1317
1318 /**
1319 * <p>
1320 * Is <code>true</code> if this is Windows XP.
1321 * </p>
1322 *
1323 * <p>
1324 * The field will return <code>false</code> if <code>OS_NAME</code> is <code>null</code>.
1325 * </p>
1326 *
1327 * @since 2.0
1328 */
1329 public static final boolean IS_OS_WINDOWS_XP = getOSMatches(OS_NAME_WINDOWS_PREFIX, "5.1");
1330
1331 // -----------------------------------------------------------------------
1332 /**
1333 * <p>
1334 * Is <code>true</code> if this is Windows Vista.
1335 * </p>
1336 *
1337 * <p>
1338 * The field will return <code>false</code> if <code>OS_NAME</code> is <code>null</code>.
1339 * </p>
1340 *
1341 * @since 2.4
1342 */
1343 public static final boolean IS_OS_WINDOWS_VISTA = getOSMatches(OS_NAME_WINDOWS_PREFIX, "6.0");
1344
1345 /**
1346 * <p>
1347 * Is <code>true</code> if this is Windows 7.
1348 * </p>
1349 *
1350 * <p>
1351 * The field will return <code>false</code> if <code>OS_NAME</code> is <code>null</code>.
1352 * </p>
1353 *
1354 * @since 2.5
1355 */
1356 public static final boolean IS_OS_WINDOWS_7 = getOSMatches(OS_NAME_WINDOWS_PREFIX, "6.1");
1357
1358 /**
1359 * <p>
1360 * Gets the Java home directory as a <code>File</code>.
1361 * </p>
1362 *
1363 * @return a directory
1364 * @throws SecurityException if a security manager exists and its
1365 * <code>checkPropertyAccess</code> method doesn't allow access to the specified system property.
1366 * @see System#getProperty(String)
1367 * @since 2.1
1368 */
1369 public static File getJavaHome() {
1370 return new File(System.getProperty(JAVA_HOME_KEY));
1371 }
1372
1373 /**
1374 * <p>
1375 * Gets the Java IO temporary directory as a <code>File</code>.
1376 * </p>
1377 *
1378 * @return a directory
1379 * @throws SecurityException if a security manager exists and its
1380 * <code>checkPropertyAccess</code> method doesn't allow access to the specified system
1381 * property.
1382 * @see System#getProperty(String)
1383 * @since 2.1
1384 */
1385 public static File getJavaIoTmpDir() {
1386 return new File(System.getProperty(JAVA_IO_TMPDIR_KEY));
1387 }
1388
1389 /**
1390 * <p>Gets the Java version number as a <code>float</code>.</p>
1391 *
1392 * <p>Example return values:</p>
1393 * <ul>
1394 * <li><code>1.2f</code> for JDK 1.2
1395 * <li><code>1.31f</code> for JDK 1.3.1
1396 * </ul>
1397 *
1398 * @return the version, for example 1.31f for JDK 1.3.1
1399 * @deprecated Use {@link #JAVA_VERSION_FLOAT} instead.
1400 * Method will be removed in Commons Lang 3.0.
1401 */
1402 public static float getJavaVersion() {
1403 return JAVA_VERSION_FLOAT;
1404 }
1405
1406 /**
1407 * <p>
1408 * Gets the Java version number as a <code>float</code>.
1409 * </p>
1410 *
1411 * <p>
1412 * Example return values:
1413 * </p>
1414 * <ul>
1415 * <li><code>1.2f</code> for Java 1.2</li>
1416 * <li><code>1.31f</code> for Java 1.3.1</li>
1417 * <li><code>1.6f</code> for Java 1.6.0_20</li>
1418 * </ul>
1419 *
1420 * <p>
1421 * Patch releases are not reported.
1422 * </p>
1423 *
1424 * @return the version, for example 1.31f for Java 1.3.1
1425 */
1426 private static float getJavaVersionAsFloat() {
1427 return toVersionFloat(toJavaVersionIntArray(SystemUtils.JAVA_VERSION, JAVA_VERSION_TRIM_SIZE));
1428 }
1429
1430 /**
1431 * <p>
1432 * Gets the Java version number as an <code>int</code>.
1433 * </p>
1434 *
1435 * <p>
1436 * Example return values:
1437 * </p>
1438 * <ul>
1439 * <li><code>120</code> for Java 1.2</li>
1440 * <li><code>131</code> for Java 1.3.1</li>
1441 * <li><code>160</code> for Java 1.6.0_20</li>
1442 * </ul>
1443 *
1444 * <p>
1445 * Patch releases are not reported.
1446 * </p>
1447 *
1448 * @return the version, for example 131 for Java 1.3.1
1449 */
1450 private static int getJavaVersionAsInt() {
1451 return toVersionInt(toJavaVersionIntArray(SystemUtils.JAVA_VERSION, JAVA_VERSION_TRIM_SIZE));
1452 }
1453
1454 /**
1455 * <p>
1456 * Decides if the Java version matches.
1457 * </p>
1458 *
1459 * @param versionPrefix
1460 * the prefix for the java version
1461 * @return true if matches, or false if not or can't determine
1462 */
1463 private static boolean getJavaVersionMatches(String versionPrefix) {
1464 return isJavaVersionMatch(JAVA_VERSION_TRIMMED, versionPrefix);
1465 }
1466
1467 /**
1468 * Trims the text of the java version to start with numbers.
1469 *
1470 * @return the trimmed java version
1471 */
1472 private static String getJavaVersionTrimmed() {
1473 if (JAVA_VERSION != null) {
1474 for (int i = 0; i < JAVA_VERSION.length(); i++) {
1475 char ch = JAVA_VERSION.charAt(i);
1476 if (ch >= '0' && ch <= '9') {
1477 return JAVA_VERSION.substring(i);
1478 }
1479 }
1480 }
1481 return null;
1482 }
1483
1484 /**
1485 * Decides if the operating system matches.
1486 *
1487 * @param osNamePrefix
1488 * the prefix for the os name
1489 * @param osVersionPrefix
1490 * the prefix for the version
1491 * @return true if matches, or false if not or can't determine
1492 */
1493 private static boolean getOSMatches(String osNamePrefix, String osVersionPrefix) {
1494 return isOSMatch(OS_NAME, OS_VERSION, osNamePrefix, osVersionPrefix);
1495 }
1496
1497 /**
1498 * Decides if the operating system matches.
1499 *
1500 * @param osNamePrefix
1501 * the prefix for the os name
1502 * @return true if matches, or false if not or can't determine
1503 */
1504 private static boolean getOSMatchesName(String osNamePrefix) {
1505 return isOSNameMatch(OS_NAME, osNamePrefix);
1506 }
1507
1508 // -----------------------------------------------------------------------
1509 /**
1510 * <p>
1511 * Gets a System property, defaulting to <code>null</code> if the property cannot be read.
1512 * </p>
1513 *
1514 * <p>
1515 * If a <code>SecurityException</code> is caught, the return value is <code>null</code> and a message is written to
1516 * <code>System.err</code>.
1517 * </p>
1518 *
1519 * @param property
1520 * the system property name
1521 * @return the system property value or <code>null</code> if a security problem occurs
1522 */
1523 private static String getSystemProperty(String property) {
1524 try {
1525 return System.getProperty(property);
1526 } catch (SecurityException ex) {
1527 // we are not allowed to look at this property
1528 System.err.println("Caught a SecurityException reading the system property '" + property
1529 + "'; the SystemUtils property value will default to null.");
1530 return null;
1531 }
1532 }
1533
1534 /**
1535 * <p>
1536 * Gets the user directory as a <code>File</code>.
1537 * </p>
1538 *
1539 * @return a directory
1540 * @throws SecurityException if a security manager exists and its
1541 * <code>checkPropertyAccess</code> method doesn't allow access to the specified system property.
1542 * @see System#getProperty(String)
1543 * @since 2.1
1544 */
1545 public static File getUserDir() {
1546 return new File(System.getProperty(USER_DIR_KEY));
1547 }
1548
1549 /**
1550 * <p>
1551 * Gets the user home directory as a <code>File</code>.
1552 * </p>
1553 *
1554 * @return a directory
1555 * @throws SecurityException if a security manager exists and its
1556 * <code>checkPropertyAccess</code> method doesn't allow access to the specified system property.
1557 * @see System#getProperty(String)
1558 * @since 2.1
1559 */
1560 public static File getUserHome() {
1561 return new File(System.getProperty(USER_HOME_KEY));
1562 }
1563
1564 /**
1565 * Returns whether the {@link #JAVA_AWT_HEADLESS} value is <code>true</code>.
1566 *
1567 * @return <code>true</code> if <code>JAVA_AWT_HEADLESS</code> is <code>"true"</code>, <code>false</code> otherwise.
1568 *
1569 * @see #JAVA_AWT_HEADLESS
1570 * @since 2.1
1571 * @since Java 1.4
1572 */
1573 public static boolean isJavaAwtHeadless() {
1574 return JAVA_AWT_HEADLESS != null ? JAVA_AWT_HEADLESS.equals(Boolean.TRUE.toString()) : false;
1575 }
1576
1577 /**
1578 * <p>
1579 * Is the Java version at least the requested version.
1580 * </p>
1581 *
1582 * <p>
1583 * Example input:
1584 * </p>
1585 * <ul>
1586 * <li><code>1.2f</code> to test for Java 1.2</li>
1587 * <li><code>1.31f</code> to test for Java 1.3.1</li>
1588 * </ul>
1589 *
1590 * @param requiredVersion
1591 * the required version, for example 1.31f
1592 * @return <code>true</code> if the actual version is equal or greater than the required version
1593 */
1594 public static boolean isJavaVersionAtLeast(float requiredVersion) {
1595 return JAVA_VERSION_FLOAT >= requiredVersion;
1596 }
1597
1598 /**
1599 * <p>
1600 * Is the Java version at least the requested version.
1601 * </p>
1602 *
1603 * <p>
1604 * Example input:
1605 * </p>
1606 * <ul>
1607 * <li><code>120</code> to test for Java 1.2 or greater</li>
1608 * <li><code>131</code> to test for Java 1.3.1 or greater</li>
1609 * </ul>
1610 *
1611 * @param requiredVersion
1612 * the required version, for example 131
1613 * @return <code>true</code> if the actual version is equal or greater than the required version
1614 * @since 2.0
1615 */
1616 public static boolean isJavaVersionAtLeast(int requiredVersion) {
1617 return JAVA_VERSION_INT >= requiredVersion;
1618 }
1619
1620 /**
1621 * <p>
1622 * Decides if the Java version matches.
1623 * </p>
1624 * <p>
1625 * This method is package private instead of private to support unit test invocation.
1626 * </p>
1627 *
1628 * @param version
1629 * the actual Java version
1630 * @param versionPrefix
1631 * the prefix for the expected Java version
1632 * @return true if matches, or false if not or can't determine
1633 */
1634 static boolean isJavaVersionMatch(String version, String versionPrefix) {
1635 if (version == null) {
1636 return false;
1637 }
1638 return version.startsWith(versionPrefix);
1639 }
1640
1641 /**
1642 * Decides if the operating system matches.
1643 * <p>
1644 * This method is package private instead of private to support unit test invocation.
1645 * </p>
1646 *
1647 * @param osName
1648 * the actual OS name
1649 * @param osVersion
1650 * the actual OS version
1651 * @param osNamePrefix
1652 * the prefix for the expected OS name
1653 * @param osVersionPrefix
1654 * the prefix for the expected OS version
1655 * @return true if matches, or false if not or can't determine
1656 */
1657 static boolean isOSMatch(String osName, String osVersion, String osNamePrefix, String osVersionPrefix) {
1658 if (osName == null || osVersion == null) {
1659 return false;
1660 }
1661 return osName.startsWith(osNamePrefix) && osVersion.startsWith(osVersionPrefix);
1662 }
1663
1664 /**
1665 * Decides if the operating system matches.
1666 * <p>
1667 * This method is package private instead of private to support unit test invocation.
1668 * </p>
1669 *
1670 * @param osName
1671 * the actual OS name
1672 * @param osNamePrefix
1673 * the prefix for the expected OS name
1674 * @return true if matches, or false if not or can't determine
1675 */
1676 static boolean isOSNameMatch(String osName, String osNamePrefix) {
1677 if (osName == null) {
1678 return false;
1679 }
1680 return osName.startsWith(osNamePrefix);
1681 }
1682
1683 /**
1684 * <p>
1685 * Converts the given Java version string to a <code>float</code>.
1686 * </p>
1687 *
1688 * <p>
1689 * Example return values:
1690 * </p>
1691 * <ul>
1692 * <li><code>1.2f</code> for Java 1.2</li>
1693 * <li><code>1.31f</code> for Java 1.3.1</li>
1694 * <li><code>1.6f</code> for Java 1.6.0_20</li>
1695 * </ul>
1696 *
1697 * <p>
1698 * Patch releases are not reported.
1699 * </p>
1700 * <p>
1701 * This method is package private instead of private to support unit test invocation.
1702 * </p>
1703 *
1704 * @param version The string version
1705 * @return the version, for example 1.31f for Java 1.3.1
1706 */
1707 static float toJavaVersionFloat(String version) {
1708 return toVersionFloat(toJavaVersionIntArray(version, JAVA_VERSION_TRIM_SIZE));
1709 }
1710
1711 /**
1712 * <p>
1713 * Converts the given Java version string to an <code>int</code>.
1714 * </p>
1715 *
1716 * <p>
1717 * Example return values:
1718 * </p>
1719 * <ul>
1720 * <li><code>120</code> for Java 1.2</li>
1721 * <li><code>131</code> for Java 1.3.1</li>
1722 * <li><code>160</code> for Java 1.6.0_20</li>
1723 * </ul>
1724 *
1725 * <p>
1726 * Patch releases are not reported.
1727 * </p>
1728 * <p>
1729 * This method is package private instead of private to support unit test invocation.
1730 * </p>
1731 *
1732 * @param version The string version
1733 * @return the version, for example 131 for Java 1.3.1
1734 */
1735 static int toJavaVersionInt(String version) {
1736 return toVersionInt(toJavaVersionIntArray(version, JAVA_VERSION_TRIM_SIZE));
1737 }
1738
1739 /**
1740 * <p>
1741 * Converts the given Java version string to an <code>int[]</code> of maximum size <code>3</code>.
1742 * </p>
1743 *
1744 * <p>
1745 * Example return values:
1746 * </p>
1747 * <ul>
1748 * <li><code>[1, 2, 0]</code> for Java 1.2</li>
1749 * <li><code>[1, 3, 1]</code> for Java 1.3.1</li>
1750 * <li><code>[1, 5, 0]</code> for Java 1.5.0_21</li>
1751 * </ul>
1752 * <p>
1753 * This method is package private instead of private to support unit test invocation.
1754 * </p>
1755 *
1756 * @param version The string version
1757 * @return the version, for example [1, 5, 0] for Java 1.5.0_21
1758 */
1759 static int[] toJavaVersionIntArray(String version) {
1760 return toJavaVersionIntArray(version, Integer.MAX_VALUE);
1761 }
1762
1763 /**
1764 * <p>
1765 * Converts the given Java version string to an <code>int[]</code> of maximum size <code>limit</code>.
1766 * </p>
1767 *
1768 * <p>
1769 * Example return values:
1770 * </p>
1771 * <ul>
1772 * <li><code>[1, 2, 0]</code> for Java 1.2</li>
1773 * <li><code>[1, 3, 1]</code> for Java 1.3.1</li>
1774 * <li><code>[1, 5, 0, 21]</code> for Java 1.5.0_21</li>
1775 * </ul>
1776 *
1777 * @param version The string version
1778 * @param limit version limit
1779 * @return the version, for example [1, 5, 0, 21] for Java 1.5.0_21
1780 */
1781 private static int[] toJavaVersionIntArray(String version, int limit) {
1782 if (version == null) {
1783 return ArrayUtils.EMPTY_INT_ARRAY;
1784 }
1785 String[] strings = StringUtils.split(version, "._- ");
1786 int[] ints = new int[Math.min(limit, strings.length)];
1787 int j = 0;
1788 for (int i = 0; i < strings.length && j < limit; i++) {
1789 String s = strings[i];
1790 if (s.length() > 0) {
1791 try {
1792 ints[j] = Integer.parseInt(s);
1793 j++;
1794 } catch (Exception e) {
1795 }
1796 }
1797 }
1798 if (ints.length > j) {
1799 int[] newInts = new int[j];
1800 System.arraycopy(ints, 0, newInts, 0, j);
1801 ints = newInts;
1802 }
1803 return ints;
1804 }
1805
1806 /**
1807 * <p>
1808 * Converts given the Java version array to a <code>float</code>.
1809 * </p>
1810 *
1811 * <p>
1812 * Example return values:
1813 * </p>
1814 * <ul>
1815 * <li><code>1.2f</code> for Java 1.2</li>
1816 * <li><code>1.31f</code> for Java 1.3.1</li>
1817 * <li><code>1.6f</code> for Java 1.6.0_20</li>
1818 * </ul>
1819 *
1820 * <p>
1821 * Patch releases are not reported.
1822 * </p>
1823 *
1824 * @param javaVersions The version numbers
1825 * @return the version, for example 1.31f for Java 1.3.1
1826 */
1827 private static float toVersionFloat(int[] javaVersions) {
1828 if (javaVersions == null || javaVersions.length == 0) {
1829 return 0f;
1830 }
1831 if (javaVersions.length == 1) {
1832 return javaVersions[0];
1833 }
1834 StringBuffer builder = new StringBuffer();
1835 builder.append(javaVersions[0]);
1836 builder.append('.');
1837 for (int i = 1; i < javaVersions.length; i++) {
1838 builder.append(javaVersions[i]);
1839 }
1840 try {
1841 return Float.parseFloat(builder.toString());
1842 } catch (Exception ex) {
1843 return 0f;
1844 }
1845 }
1846
1847 /**
1848 * <p>
1849 * Converts given the Java version array to an <code>int</code>.
1850 * </p>
1851 *
1852 * <p>
1853 * Example return values:
1854 * </p>
1855 * <ul>
1856 * <li><code>120</code> for Java 1.2</li>
1857 * <li><code>131</code> for Java 1.3.1</li>
1858 * <li><code>160</code> for Java 1.6.0_20</li>
1859 * </ul>
1860 *
1861 * <p>
1862 * Patch releases are not reported.
1863 * </p>
1864 *
1865 * @param javaVersions The version numbers
1866 * @return the version, for example 1.31f for Java 1.3.1
1867 */
1868 private static int toVersionInt(int[] javaVersions) {
1869 if (javaVersions == null) {
1870 return 0;
1871 }
1872 int intVersion = 0;
1873 int len = javaVersions.length;
1874 if (len >= 1) {
1875 intVersion = javaVersions[0] * 100;
1876 }
1877 if (len >= 2) {
1878 intVersion += javaVersions[1] * 10;
1879 }
1880 if (len >= 3) {
1881 intVersion += javaVersions[2];
1882 }
1883 return intVersion;
1884 }
1885
1886 // -----------------------------------------------------------------------
1887 /**
1888 * <p>
1889 * SystemUtils instances should NOT be constructed in standard programming. Instead, the class should be used as
1890 * <code>SystemUtils.FILE_SEPARATOR</code>.
1891 * </p>
1892 *
1893 * <p>
1894 * This constructor is public to permit tools that require a JavaBean instance to operate.
1895 * </p>
1896 */
1897 public SystemUtils() {
1898 super();
1899 }
1900
1901 }