1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.geronimo.system.configuration.condition;
19
20
21
22
23
24
25
26
27 import java.io.File;
28
29 /**
30 * <p>Helpers for <code>java.lang.System</code>.</p>
31 *
32 * <p>If a system property cannot be read due to security restrictions,
33 * the corresponding field in this class will be set to <code>null</code>
34 * and a message will be written to <code>System.err</code>.</p>
35 *
36 * @author Based on code from Avalon Excalibur
37 * @author Based on code from Lucene
38 * @author Stephen Colebourne
39 * @author <a href="mailto:sdowney@panix.com">Steve Downey</a>
40 * @author Gary Gregory
41 * @author Michael Becke
42 * @author Tetsuya Kaneuchi
43 * @author Rafal Krupinski
44 * @author Jason Gritman
45 * @since 1.0
46 * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
47 */
48 public class SystemUtils
49 {
50 /**
51 * The prefix String for all Windows OS.
52 */
53 private static final String OS_NAME_WINDOWS_PREFIX = "Windows";
54
55
56
57
58
59 /**
60 * The System property key for the user home directory.
61 */
62 private static final String USER_HOME_KEY = "user.home";
63
64 /**
65 * The System property key for the user directory.
66 */
67 private static final String USER_DIR_KEY = "user.dir";
68
69 /**
70 * The System property key for the Java IO temporary directory.
71 */
72 private static final String JAVA_IO_TMPDIR_KEY = "java.io.tmpdir";
73
74 /**
75 * The System property key for the Java home directory.
76 */
77 private static final String JAVA_HOME_KEY = "java.home";
78
79 /**
80 * <p>The <code>awt.toolkit</code> System Property.</p>
81 * <p>Holds a class name, on Windows XP this is <code>sun.awt.windows.WToolkit</code>.</p>
82 * <p><b>On platforms without a GUI, this value is <code>null</code>.</b></p>
83 *
84 * <p>Defaults to <code>null</code> if the runtime does not have
85 * security access to read this property or the property does not exist.</p>
86 *
87 * <p>
88 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
89 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
90 * will be out of sync with that System property.
91 * </p>
92 *
93 * @since 2.1
94 */
95 public static final String AWT_TOOLKIT = getSystemProperty("awt.toolkit");
96
97 /**
98 * <p>The <code>file.encoding</code> System Property.</p>
99 * <p>File encoding, such as <code>Cp1252</code>.</p>
100 *
101 * <p>Defaults to <code>null</code> if the runtime does not have
102 * security access to read this property or the property does not exist.</p>
103 *
104 * <p>
105 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
106 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
107 * will be out of sync with that System property.
108 * </p>
109 *
110 * @since 2.0
111 * @since Java 1.2
112 */
113 public static final String FILE_ENCODING = getSystemProperty("file.encoding");
114
115 /**
116 * <p>The <code>file.separator</code> System Property.
117 * File separator (<code>"/"</code> on UNIX).</p>
118 *
119 * <p>Defaults to <code>null</code> if the runtime does not have
120 * security access to read this property or the property does not exist.</p>
121 *
122 * <p>
123 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
124 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
125 * will be out of sync with that System property.
126 * </p>
127 *
128 * @since Java 1.1
129 */
130 public static final String FILE_SEPARATOR = getSystemProperty("file.separator");
131
132 /**
133 * <p>The <code>java.awt.fonts</code> System Property.</p>
134 *
135 * <p>Defaults to <code>null</code> if the runtime does not have
136 * security access to read this property or the property does not exist.</p>
137 *
138 * <p>
139 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
140 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
141 * will be out of sync with that System property.
142 * </p>
143 *
144 * @since 2.1
145 */
146 public static final String JAVA_AWT_FONTS = getSystemProperty("java.awt.fonts");
147
148 /**
149 * <p>The <code>java.awt.graphicsenv</code> System Property.</p>
150 *
151 * <p>Defaults to <code>null</code> if the runtime does not have
152 * security access to read this property or the property does not exist.</p>
153 *
154 * <p>
155 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
156 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
157 * will be out of sync with that System property.
158 * </p>
159 *
160 * @since 2.1
161 */
162 public static final String JAVA_AWT_GRAPHICSENV = getSystemProperty("java.awt.graphicsenv");
163
164 /**
165 * <p>
166 * The <code>java.awt.headless</code> System Property.
167 * The value of this property is the String <code>"true"</code> or <code>"false"</code>.
168 * </p>
169 *
170 * <p>Defaults to <code>null</code> if the runtime does not have
171 * security access to read this property or the property does not exist.</p>
172 *
173 * <p>
174 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
175 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
176 * will be out of sync with that System property.
177 * </p>
178 *
179 * @see #isJavaAwtHeadless()
180 * @since 2.1
181 * @since Java 1.4
182 */
183 public static final String JAVA_AWT_HEADLESS = getSystemProperty("java.awt.headless");
184
185 /**
186 * <p>The <code>java.awt.printerjob</code> System Property.</p>
187 *
188 * <p>Defaults to <code>null</code> if the runtime does not have
189 * security access to read this property or the property does not exist.</p>
190 *
191 * <p>
192 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
193 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
194 * will be out of sync with that System property.
195 * </p>
196 *
197 * @since 2.1
198 */
199 public static final String JAVA_AWT_PRINTERJOB = getSystemProperty("java.awt.printerjob");
200
201 /**
202 * <p>The <code>java.class.path</code> System Property. Java class path.</p>
203 *
204 * <p>Defaults to <code>null</code> if the runtime does not have
205 * security access to read this property or the property does not exist.</p>
206 *
207 * <p>
208 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
209 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
210 * will be out of sync with that System property.
211 * </p>
212 *
213 * @since Java 1.1
214 */
215 public static final String JAVA_CLASS_PATH = getSystemProperty("java.class.path");
216
217 /**
218 * <p>The <code>java.class.version</code> System Property.
219 * Java class format version number.</p>
220 *
221 * <p>Defaults to <code>null</code> if the runtime does not have
222 * security access to read this property or the property does not exist.</p>
223 *
224 * <p>
225 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
226 * or {@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 Java 1.1
231 */
232 public static final String JAVA_CLASS_VERSION = getSystemProperty("java.class.version");
233
234 /**
235 * <p>The <code>java.compiler</code> System Property. Name of JIT compiler to use.
236 * First in JDK version 1.2. Not used in Sun JDKs after 1.2.</p>
237 *
238 * <p>Defaults to <code>null</code> if the runtime does not have
239 * security access to read this property or the property does not exist.</p>
240 *
241 * <p>
242 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
243 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
244 * will be out of sync with that System property.
245 * </p>
246 *
247 * @since Java 1.2. Not used in Sun versions after 1.2.
248 */
249 public static final String JAVA_COMPILER = getSystemProperty("java.compiler");
250
251 /**
252 * <p>The <code>java.endorsed.dirs</code> System Property. Path of endorsed directory
253 * or directories.</p>
254 *
255 * <p>Defaults to <code>null</code> if the runtime does not have
256 * security access to read this property or the property does not exist.</p>
257 *
258 * <p>
259 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
260 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
261 * will be out of sync with that System property.
262 * </p>
263 *
264 * @since Java 1.4
265 */
266 public static final String JAVA_ENDORSED_DIRS = getSystemProperty("java.endorsed.dirs");
267
268 /**
269 * <p>The <code>java.ext.dirs</code> System Property. Path of extension directory
270 * or directories.</p>
271 *
272 * <p>Defaults to <code>null</code> if the runtime does not have
273 * security access to read this property or the property does not exist.</p>
274 *
275 * <p>
276 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
277 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
278 * will be out of sync with that System property.
279 * </p>
280 *
281 * @since Java 1.3
282 */
283 public static final String JAVA_EXT_DIRS = getSystemProperty("java.ext.dirs");
284
285 /**
286 * <p>The <code>java.home</code> System Property. Java installation directory.</p>
287 *
288 * <p>Defaults to <code>null</code> if the runtime does not have
289 * security access to read this property or the property does not exist.</p>
290 *
291 * <p>
292 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
293 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
294 * will be out of sync with that System property.
295 * </p>
296 *
297 * @since Java 1.1
298 */
299 public static final String JAVA_HOME = getSystemProperty(JAVA_HOME_KEY);
300
301 /**
302 * <p>The <code>java.io.tmpdir</code> System Property. Default temp file path.</p>
303 *
304 * <p>Defaults to <code>null</code> if the runtime does not have
305 * security access to read this property or the property does not exist.</p>
306 *
307 * <p>
308 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
309 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
310 * will be out of sync with that System property.
311 * </p>
312 *
313 * @since Java 1.2
314 */
315 public static final String JAVA_IO_TMPDIR = getSystemProperty(JAVA_IO_TMPDIR_KEY);
316
317 /**
318 * <p>The <code>java.library.path</code> System Property. List of paths to search
319 * when loading libraries.</p>
320 *
321 * <p>Defaults to <code>null</code> if the runtime does not have
322 * security access to read this property or the property does not exist.</p>
323 *
324 * <p>
325 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
326 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
327 * will be out of sync with that System property.
328 * </p>
329 *
330 * @since Java 1.2
331 */
332 public static final String JAVA_LIBRARY_PATH = getSystemProperty("java.library.path");
333
334 /**
335 * <p>The <code>java.runtime.name</code> System Property. Java Runtime Environment
336 * name.</p>
337 *
338 * <p>Defaults to <code>null</code> if the runtime does not have
339 * security access to read this property or the property does not exist.</p>
340 *
341 * <p>
342 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
343 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
344 * will be out of sync with that System property.
345 * </p>
346 *
347 * @since 2.0
348 * @since Java 1.3
349 */
350 public static final String JAVA_RUNTIME_NAME = getSystemProperty("java.runtime.name");
351
352 /**
353 * <p>The <code>java.runtime.version</code> System Property. Java Runtime Environment
354 * version.</p>
355 *
356 * <p>Defaults to <code>null</code> if the runtime does not have
357 * security access to read this property or the property does not exist.</p>
358 *
359 * <p>
360 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
361 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
362 * will be out of sync with that System property.
363 * </p>
364 *
365 * @since 2.0
366 * @since Java 1.3
367 */
368 public static final String JAVA_RUNTIME_VERSION = getSystemProperty("java.runtime.version");
369
370 /**
371 * <p>The <code>java.specification.name</code> System Property. Java Runtime Environment
372 * specification name.</p>
373 *
374 * <p>Defaults to <code>null</code> if the runtime does not have
375 * security access to read this property or the property does not exist.</p>
376 *
377 * <p>
378 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
379 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
380 * will be out of sync with that System property.
381 * </p>
382 *
383 * @since Java 1.2
384 */
385 public static final String JAVA_SPECIFICATION_NAME = getSystemProperty("java.specification.name");
386
387 /**
388 * <p>The <code>java.specification.vendor</code> System Property. Java Runtime Environment
389 * specification vendor.</p>
390 *
391 * <p>Defaults to <code>null</code> if the runtime does not have
392 * security access to read this property or the property does not exist.</p>
393 *
394 * <p>
395 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
396 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
397 * will be out of sync with that System property.
398 * </p>
399 *
400 * @since Java 1.2
401 */
402 public static final String JAVA_SPECIFICATION_VENDOR = getSystemProperty("java.specification.vendor");
403
404 /**
405 * <p>The <code>java.specification.version</code> System Property. Java Runtime Environment
406 * specification version.</p>
407 *
408 * <p>Defaults to <code>null</code> if the runtime does not have
409 * security access to read this property or the property does not exist.</p>
410 *
411 * <p>
412 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
413 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
414 * will be out of sync with that System property.
415 * </p>
416 *
417 * @since Java 1.3
418 */
419 public static final String JAVA_SPECIFICATION_VERSION = getSystemProperty("java.specification.version");
420
421 /**
422 * <p>The <code>java.util.prefs.PreferencesFactory</code> System Property. A class name.</p>
423 *
424 * <p>Defaults to <code>null</code> if the runtime does not have
425 * security access to read this property or the property does not exist.</p>
426 *
427 * <p>
428 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
429 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
430 * will be out of sync with that System property.
431 * </p>
432 *
433 * @since 2.1
434 * @since Java 1.4
435 */
436 public static final String JAVA_UTIL_PREFS_PREFERENCES_FACTORY =
437 getSystemProperty("java.util.prefs.PreferencesFactory");
438
439 /**
440 * <p>The <code>java.vendor</code> System Property. Java vendor-specific string.</p>
441 *
442 * <p>Defaults to <code>null</code> if the runtime does not have
443 * security access to read this property or the property does not exist.</p>
444 *
445 * <p>
446 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
447 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
448 * will be out of sync with that System property.
449 * </p>
450 *
451 * @since Java 1.1
452 */
453 public static final String JAVA_VENDOR = getSystemProperty("java.vendor");
454
455 /**
456 * <p>The <code>java.vendor.url</code> System Property. Java vendor URL.</p>
457 *
458 * <p>Defaults to <code>null</code> if the runtime does not have
459 * security access to read this property or the property does not exist.</p>
460 *
461 * <p>
462 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
463 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
464 * will be out of sync with that System property.
465 * </p>
466 *
467 * @since Java 1.1
468 */
469 public static final String JAVA_VENDOR_URL = getSystemProperty("java.vendor.url");
470
471 /**
472 * <p>The <code>java.version</code> System Property. Java version number.</p>
473 *
474 * <p>Defaults to <code>null</code> if the runtime does not have
475 * security access to read this property or the property does not exist.</p>
476 *
477 * <p>
478 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
479 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
480 * will be out of sync with that System property.
481 * </p>
482 *
483 * @since Java 1.1
484 */
485 public static final String JAVA_VERSION = getSystemProperty("java.version");
486
487 /**
488 * <p>The <code>java.vm.info</code> System Property. Java Virtual Machine implementation
489 * info.</p>
490 *
491 * <p>Defaults to <code>null</code> if the runtime does not have
492 * security access to read this property or the property does not exist.</p>
493 *
494 * <p>
495 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
496 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
497 * will be out of sync with that System property.
498 * </p>
499 *
500 * @since 2.0
501 * @since Java 1.2
502 */
503 public static final String JAVA_VM_INFO = getSystemProperty("java.vm.info");
504
505 /**
506 * <p>The <code>java.vm.name</code> System Property. Java Virtual Machine implementation
507 * name.</p>
508 *
509 * <p>Defaults to <code>null</code> if the runtime does not have
510 * security access to read this property or the property does not exist.</p>
511 *
512 * <p>
513 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
514 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
515 * will be out of sync with that System property.
516 * </p>
517 *
518 * @since Java 1.2
519 */
520 public static final String JAVA_VM_NAME = getSystemProperty("java.vm.name");
521
522 /**
523 * <p>The <code>java.vm.specification.name</code> System Property. Java Virtual Machine
524 * specification name.</p>
525 *
526 * <p>Defaults to <code>null</code> if the runtime does not have
527 * security access to read this property or the property does not exist.</p>
528 *
529 * <p>
530 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
531 * or {@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.2
536 */
537 public static final String JAVA_VM_SPECIFICATION_NAME = getSystemProperty("java.vm.specification.name");
538
539 /**
540 * <p>The <code>java.vm.specification.vendor</code> System Property. Java Virtual
541 * Machine specification vendor.</p>
542 *
543 * <p>Defaults to <code>null</code> if the runtime does not have
544 * security access to read this property or the property does not exist.</p>
545 *
546 * <p>
547 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
548 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
549 * will be out of sync with that System property.
550 * </p>
551 *
552 * @since Java 1.2
553 */
554 public static final String JAVA_VM_SPECIFICATION_VENDOR = getSystemProperty("java.vm.specification.vendor");
555
556 /**
557 * <p>The <code>java.vm.specification.version</code> System Property. Java Virtual Machine
558 * specification version.</p>
559 *
560 * <p>Defaults to <code>null</code> if the runtime does not have
561 * security access to read this property or the property does not exist.</p>
562 *
563 * <p>
564 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
565 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
566 * will be out of sync with that System property.
567 * </p>
568 *
569 * @since Java 1.2
570 */
571 public static final String JAVA_VM_SPECIFICATION_VERSION = getSystemProperty("java.vm.specification.version");
572
573 /**
574 * <p>The <code>java.vm.vendor</code> System Property. Java Virtual Machine implementation
575 * vendor.</p>
576 *
577 * <p>Defaults to <code>null</code> if the runtime does not have
578 * security access to read this property or the property does not exist.</p>
579 *
580 * <p>
581 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
582 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
583 * will be out of sync with that System property.
584 * </p>
585 *
586 * @since Java 1.2
587 */
588 public static final String JAVA_VM_VENDOR = getSystemProperty("java.vm.vendor");
589
590 /**
591 * <p>The <code>java.vm.version</code> System Property. Java Virtual Machine
592 * implementation version.</p>
593 *
594 * <p>Defaults to <code>null</code> if the runtime does not have
595 * security access to read this property or the property does not exist.</p>
596 *
597 * <p>
598 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
599 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
600 * will be out of sync with that System property.
601 * </p>
602 *
603 * @since Java 1.2
604 */
605 public static final String JAVA_VM_VERSION = getSystemProperty("java.vm.version");
606
607 /**
608 * <p>The <code>line.separator</code> System Property. Line separator
609 * (<code>"\n"</code> on UNIX).</p>
610 *
611 * <p>Defaults to <code>null</code> if the runtime does not have
612 * security access to read this property or the property does not exist.</p>
613 *
614 * <p>
615 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
616 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
617 * will be out of sync with that System property.
618 * </p>
619 *
620 * @since Java 1.1
621 */
622 public static final String LINE_SEPARATOR = getSystemProperty("line.separator");
623
624 /**
625 * <p>The <code>os.arch</code> System Property. Operating system architecture.</p>
626 *
627 * <p>Defaults to <code>null</code> if the runtime does not have
628 * security access to read this property or the property does not exist.</p>
629 *
630 * <p>
631 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
632 * or {@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.1
637 */
638 public static final String OS_ARCH = getSystemProperty("os.arch");
639
640 /**
641 * <p>The <code>os.name</code> System Property. Operating system name.</p>
642 *
643 * <p>Defaults to <code>null</code> if the runtime does not have
644 * security access to read this property or the property does not exist.</p>
645 *
646 * <p>
647 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
648 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
649 * will be out of sync with that System property.
650 * </p>
651 *
652 * @since Java 1.1
653 */
654 public static final String OS_NAME = getSystemProperty("os.name");
655
656 /**
657 * <p>The <code>os.version</code> System Property. Operating system version.</p>
658 *
659 * <p>Defaults to <code>null</code> if the runtime does not have
660 * security access to read this property or the property does not exist.</p>
661 *
662 * <p>
663 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
664 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
665 * will be out of sync with that System property.
666 * </p>
667 *
668 * @since Java 1.1
669 */
670 public static final String OS_VERSION = getSystemProperty("os.version");
671
672 /**
673 * <p>The <code>path.separator</code> System Property. Path separator
674 * (<code>":"</code> on UNIX).</p>
675 *
676 * <p>Defaults to <code>null</code> if the runtime does not have
677 * security access to read this property or the property does not exist.</p>
678 *
679 * <p>
680 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
681 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
682 * will be out of sync with that System property.
683 * </p>
684 *
685 * @since Java 1.1
686 */
687 public static final String PATH_SEPARATOR = getSystemProperty("path.separator");
688
689 /**
690 * <p>The <code>user.country</code> or <code>user.region</code> System Property.
691 * User's country code, such as <code>GB</code>. First in JDK version 1.2 as
692 * <code>user.region</code>. Renamed to <code>user.country</code> in 1.4</p>
693 *
694 * <p>Defaults to <code>null</code> if the runtime does not have
695 * security access to read this property or the property does not exist.</p>
696 *
697 * <p>
698 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
699 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
700 * will be out of sync with that System property.
701 * </p>
702 *
703 * @since 2.0
704 * @since Java 1.2
705 */
706 public static final String USER_COUNTRY =
707 getSystemProperty("user.country") == null ?
708 getSystemProperty("user.region") : getSystemProperty("user.country");
709
710 /**
711 * <p>The <code>user.dir</code> System Property. User's current working
712 * directory.</p>
713 *
714 * <p>Defaults to <code>null</code> if the runtime does not have
715 * security access to read this property or the property does not exist.</p>
716 *
717 * <p>
718 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
719 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
720 * will be out of sync with that System property.
721 * </p>
722 *
723 * @since Java 1.1
724 */
725 public static final String USER_DIR = getSystemProperty(USER_DIR_KEY);
726
727 /**
728 * <p>The <code>user.home</code> System Property. User's home directory.</p>
729 *
730 * <p>Defaults to <code>null</code> if the runtime does not have
731 * security access to read this property or the property does not exist.</p>
732 *
733 * <p>
734 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
735 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
736 * will be out of sync with that System property.
737 * </p>
738 *
739 * @since Java 1.1
740 */
741 public static final String USER_HOME = getSystemProperty(USER_HOME_KEY);
742
743 /**
744 * <p>The <code>user.language</code> System Property. User's language code,
745 * such as <code>"en"</code>.</p>
746 *
747 * <p>Defaults to <code>null</code> if the runtime does not have
748 * security access to read this property or the property does not exist.</p>
749 *
750 * <p>
751 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
752 * or {@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 2.0
757 * @since Java 1.2
758 */
759 public static final String USER_LANGUAGE = getSystemProperty("user.language");
760
761 /**
762 * <p>The <code>user.name</code> System Property. User's account name.</p>
763 *
764 * <p>Defaults to <code>null</code> if the runtime does not have
765 * security access to read this property or the property does not exist.</p>
766 *
767 * <p>
768 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
769 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
770 * will be out of sync with that System property.
771 * </p>
772 *
773 * @since Java 1.1
774 */
775 public static final String USER_NAME = getSystemProperty("user.name");
776
777 /**
778 * <p>The <code>user.timezone</code> System Property.
779 * For example: <code>"America/Los_Angeles"</code>.</p>
780 *
781 * <p>Defaults to <code>null</code> if the runtime does not have
782 * security access to read this property or the property does not exist.</p>
783 *
784 * <p>
785 * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)}
786 * or {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value
787 * will be out of sync with that System property.
788 * </p>
789 *
790 * @since 2.1
791 */
792 public static final String USER_TIMEZONE = getSystemProperty("user.timezone");
793
794
795
796
797
798
799 /**
800 * <p>Gets the Java version as a <code>String</code> trimming leading letters.</p>
801 *
802 * <p>The field will return <code>null</code> if {@link #JAVA_VERSION} is <code>null</code>.</p>
803 *
804 * @since 2.1
805 */
806 public static final String JAVA_VERSION_TRIMMED = getJavaVersionTrimmed();
807
808
809
810
811
812
813 /**
814 * <p>Gets the Java version as a <code>float</code>.</p>
815 *
816 * <p>Example return values:</p>
817 * <ul>
818 * <li><code>1.2f</code> for JDK 1.2
819 * <li><code>1.31f</code> for JDK 1.3.1
820 * </ul>
821 *
822 * <p>The field will return zero if {@link #JAVA_VERSION} is <code>null</code>.</p>
823 *
824 * @since 2.0
825 */
826 public static final float JAVA_VERSION_FLOAT = getJavaVersionAsFloat();
827
828 /**
829 * <p>Gets the Java version as an <code>int</code>.</p>
830 *
831 * <p>Example return values:</p>
832 * <ul>
833 * <li><code>120</code> for JDK 1.2
834 * <li><code>131</code> for JDK 1.3.1
835 * </ul>
836 *
837 * <p>The field will return zero if {@link #JAVA_VERSION} is <code>null</code>.</p>
838 *
839 * @since 2.0
840 */
841 public static final int JAVA_VERSION_INT = getJavaVersionAsInt();
842
843
844
845
846
847
848 /**
849 * <p>Is <code>true</code> if this is Java version 1.1 (also 1.1.x versions).</p>
850 *
851 * <p>The field will return <code>false</code> if {@link #JAVA_VERSION} is
852 * <code>null</code>.</p>
853 */
854 public static final boolean IS_JAVA_1_1 = getJavaVersionMatches("1.1");
855
856 /**
857 * <p>Is <code>true</code> if this is Java version 1.2 (also 1.2.x versions).</p>
858 *
859 * <p>The field will return <code>false</code> if {@link #JAVA_VERSION} is
860 * <code>null</code>.</p>
861 */
862 public static final boolean IS_JAVA_1_2 = getJavaVersionMatches("1.2");
863
864 /**
865 * <p>Is <code>true</code> if this is Java version 1.3 (also 1.3.x versions).</p>
866 *
867 * <p>The field will return <code>false</code> if {@link #JAVA_VERSION} is
868 * <code>null</code>.</p>
869 */
870 public static final boolean IS_JAVA_1_3 = getJavaVersionMatches("1.3");
871
872 /**
873 * <p>Is <code>true</code> if this is Java version 1.4 (also 1.4.x versions).</p>
874 *
875 * <p>The field will return <code>false</code> if {@link #JAVA_VERSION} is
876 * <code>null</code>.</p>
877 */
878 public static final boolean IS_JAVA_1_4 = getJavaVersionMatches("1.4");
879
880 /**
881 * <p>Is <code>true</code> if this is Java version 1.5 (also 1.5.x versions).</p>
882 *
883 * <p>The field will return <code>false</code> if {@link #JAVA_VERSION} is
884 * <code>null</code>.</p>
885 */
886 public static final boolean IS_JAVA_1_5 = getJavaVersionMatches("1.5");
887
888 /**
889 * <p>Is <code>true</code> if this is Java version 1.6 (also 1.6.x versions).</p>
890 *
891 * <p>The field will return <code>false</code> if {@link #JAVA_VERSION} is
892 * <code>null</code>.</p>
893 */
894 public static final boolean IS_JAVA_1_6 = getJavaVersionMatches("1.6");
895
896
897
898
899
900
901
902
903
904 /**
905 * <p>Is <code>true</code> if this is AIX.</p>
906 *
907 * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
908 * <code>null</code>.</p>
909 *
910 * @since 2.0
911 */
912 public static final boolean IS_OS_AIX = getOSMatches("AIX");
913
914 /**
915 * <p>Is <code>true</code> if this is HP-UX.</p>
916 *
917 * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
918 * <code>null</code>.</p>
919 *
920 * @since 2.0
921 */
922 public static final boolean IS_OS_HP_UX = getOSMatches("HP-UX");
923
924 /**
925 * <p>Is <code>true</code> if this is Irix.</p>
926 *
927 * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
928 * <code>null</code>.</p>
929 *
930 * @since 2.0
931 */
932 public static final boolean IS_OS_IRIX = getOSMatches("Irix");
933
934 /**
935 * <p>Is <code>true</code> if this is Linux.</p>
936 *
937 * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
938 * <code>null</code>.</p>
939 *
940 * @since 2.0
941 */
942 public static final boolean IS_OS_LINUX = getOSMatches("Linux") || getOSMatches("LINUX");
943
944 /**
945 * <p>Is <code>true</code> if this is Mac.</p>
946 *
947 * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
948 * <code>null</code>.</p>
949 *
950 * @since 2.0
951 */
952 public static final boolean IS_OS_MAC = getOSMatches("Mac");
953
954 /**
955 * <p>Is <code>true</code> if this is Mac.</p>
956 *
957 * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
958 * <code>null</code>.</p>
959 *
960 * @since 2.0
961 */
962 public static final boolean IS_OS_MAC_OSX = getOSMatches("Mac OS X");
963
964 /**
965 * <p>Is <code>true</code> if this is OS/2.</p>
966 *
967 * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
968 * <code>null</code>.</p>
969 *
970 * @since 2.0
971 */
972 public static final boolean IS_OS_OS2 = getOSMatches("OS/2");
973
974 /**
975 * <p>Is <code>true</code> if this is Solaris.</p>
976 *
977 * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
978 * <code>null</code>.</p>
979 *
980 * @since 2.0
981 */
982 public static final boolean IS_OS_SOLARIS = getOSMatches("Solaris");
983
984 /**
985 * <p>Is <code>true</code> if this is SunOS.</p>
986 *
987 * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
988 * <code>null</code>.</p>
989 *
990 * @since 2.0
991 */
992 public static final boolean IS_OS_SUN_OS = getOSMatches("SunOS");
993
994 /**
995 * <p>Is <code>true</code> if this is a POSIX compilant system,
996 * as in any of AIX, HP-UX, Irix, Linux, MacOSX, Solaris or SUN OS.</p>
997 *
998 * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
999 * <code>null</code>.</p>
1000 *
1001 * @since 2.1
1002 */
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 /**
1008 * <p>Is <code>true</code> if this is Windows.</p>
1009 *
1010 * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
1011 * <code>null</code>.</p>
1012 *
1013 * @since 2.0
1014 */
1015 public static final boolean IS_OS_WINDOWS = getOSMatches(OS_NAME_WINDOWS_PREFIX);
1016
1017 /**
1018 * <p>Is <code>true</code> if this is Windows 2000.</p>
1019 *
1020 * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
1021 * <code>null</code>.</p>
1022 *
1023 * @since 2.0
1024 */
1025 public static final boolean IS_OS_WINDOWS_2000 = getOSMatches(OS_NAME_WINDOWS_PREFIX, "5.0");
1026
1027 /**
1028 * <p>Is <code>true</code> if this is Windows 95.</p>
1029 *
1030 * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
1031 * <code>null</code>.</p>
1032 *
1033 * @since 2.0
1034 */
1035 public static final boolean IS_OS_WINDOWS_95 = getOSMatches(OS_NAME_WINDOWS_PREFIX + " 9", "4.0");
1036
1037
1038 /**
1039 * <p>Is <code>true</code> if this is Windows 98.</p>
1040 *
1041 * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
1042 * <code>null</code>.</p>
1043 *
1044 * @since 2.0
1045 */
1046 public static final boolean IS_OS_WINDOWS_98 = getOSMatches(OS_NAME_WINDOWS_PREFIX + " 9", "4.1");
1047
1048
1049 /**
1050 * <p>Is <code>true</code> if this is Windows ME.</p>
1051 *
1052 * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
1053 * <code>null</code>.</p>
1054 *
1055 * @since 2.0
1056 */
1057 public static final boolean IS_OS_WINDOWS_ME = getOSMatches(OS_NAME_WINDOWS_PREFIX, "4.9");
1058
1059
1060 /**
1061 * <p>Is <code>true</code> if this is Windows NT.</p>
1062 *
1063 * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
1064 * <code>null</code>.</p>
1065 *
1066 * @since 2.0
1067 */
1068 public static final boolean IS_OS_WINDOWS_NT = getOSMatches(OS_NAME_WINDOWS_PREFIX + " NT");
1069
1070
1071 /**
1072 * <p>Is <code>true</code> if this is Windows XP.</p>
1073 *
1074 * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
1075 * <code>null</code>.</p>
1076 *
1077 * @since 2.0
1078 */
1079 public static final boolean IS_OS_WINDOWS_XP = getOSMatches(OS_NAME_WINDOWS_PREFIX, "5.1");
1080
1081
1082 /**
1083 * <p>SystemUtils instances should NOT be constructed in standard
1084 * programming. Instead, the class should be used as
1085 * <code>SystemUtils.FILE_SEPARATOR</code>.</p>
1086 *
1087 * <p>This constructor is public to permit tools that require a JavaBean
1088 * instance to operate.</p>
1089 */
1090 public SystemUtils() {
1091 super();
1092 }
1093
1094
1095 /**
1096 * <p>Gets the Java version number as a <code>float</code>.</p>
1097 *
1098 * <p>Example return values:</p>
1099 * <ul>
1100 * <li><code>1.2f</code> for JDK 1.2
1101 * <li><code>1.31f</code> for JDK 1.3.1
1102 * </ul>
1103 *
1104 * @return the version, for example 1.31f for JDK 1.3.1
1105 * @deprecated Use {@link #JAVA_VERSION_FLOAT} instead.
1106 * Method will be removed in Commons Lang 3.0.
1107 */
1108 public static float getJavaVersion() {
1109 return JAVA_VERSION_FLOAT;
1110 }
1111
1112 /**
1113 * <p>Gets the Java version number as a <code>float</code>.</p>
1114 *
1115 * <p>Example return values:</p>
1116 * <ul>
1117 * <li><code>1.2f</code> for JDK 1.2
1118 * <li><code>1.31f</code> for JDK 1.3.1
1119 * </ul>
1120 *
1121 * <p>Patch releases are not reported.
1122 * Zero is returned if {@link #JAVA_VERSION_TRIMMED} is <code>null</code>.</p>
1123 *
1124 * @return the version, for example 1.31f for JDK 1.3.1
1125 */
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 /**
1142 * <p>Gets the Java version number as an <code>int</code>.</p>
1143 *
1144 * <p>Example return values:</p>
1145 * <ul>
1146 * <li><code>120</code> for JDK 1.2
1147 * <li><code>131</code> for JDK 1.3.1
1148 * </ul>
1149 *
1150 * <p>Patch releases are not reported.
1151 * Zero is returned if {@link #JAVA_VERSION_TRIMMED} is <code>null</code>.</p>
1152 *
1153 * @return the version, for example 131 for JDK 1.3.1
1154 */
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 /**
1174 * Trims the text of the java version to start with numbers.
1175 *
1176 * @return the trimmed java version
1177 */
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 /**
1191 * <p>Decides if the java version matches.</p>
1192 *
1193 * @param versionPrefix the prefix for the java version
1194 * @return true if matches, or false if not or can't determine
1195 */
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 /**
1204 * <p>Decides if the operating system matches.</p>
1205 *
1206 * @param osNamePrefix the prefix for the os name
1207 * @return true if matches, or false if not or can't determine
1208 */
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 /**
1217 * <p>Decides if the operating system matches.</p>
1218 *
1219 * @param osNamePrefix the prefix for the os name
1220 * @param osVersionPrefix the prefix for the version
1221 * @return true if matches, or false if not or can't determine
1222 */
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
1231 /**
1232 * <p>Gets a System property, defaulting to <code>null</code> if the property
1233 * cannot be read.</p>
1234 *
1235 * <p>If a <code>SecurityException</code> is caught, the return
1236 * value is <code>null</code> and a message is written to <code>System.err</code>.</p>
1237 *
1238 * @param property the system property name
1239 * @return the system property value or <code>null</code> if a security problem occurs
1240 */
1241 private static String getSystemProperty(String property) {
1242 try {
1243 return System.getProperty(property);
1244 } catch (SecurityException ex) {
1245
1246 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 /**
1255 * <p>Is the Java version at least the requested version.</p>
1256 *
1257 * <p>Example input:</p>
1258 * <ul>
1259 * <li><code>1.2f</code> to test for JDK 1.2</li>
1260 * <li><code>1.31f</code> to test for JDK 1.3.1</li>
1261 * </ul>
1262 *
1263 * @param requiredVersion the required version, for example 1.31f
1264 * @return <code>true</code> if the actual version is equal or greater
1265 * than the required version
1266 */
1267 public static boolean isJavaVersionAtLeast(float requiredVersion) {
1268 return JAVA_VERSION_FLOAT >= requiredVersion;
1269 }
1270
1271 /**
1272 * <p>Is the Java version at least the requested version.</p>
1273 *
1274 * <p>Example input:</p>
1275 * <ul>
1276 * <li><code>120</code> to test for JDK 1.2 or greater</li>
1277 * <li><code>131</code> to test for JDK 1.3.1 or greater</li>
1278 * </ul>
1279 *
1280 * @param requiredVersion the required version, for example 131
1281 * @return <code>true</code> if the actual version is equal or greater
1282 * than the required version
1283 * @since 2.0
1284 */
1285 public static boolean isJavaVersionAtLeast(int requiredVersion) {
1286 return JAVA_VERSION_INT >= requiredVersion;
1287 }
1288
1289 /**
1290 * Returns whether the {@link #JAVA_AWT_HEADLESS} value is <code>true</code>.
1291 *
1292 * @return <code>true</code> if <code>JAVA_AWT_HEADLESS</code> is <code>"true"</code>,
1293 * <code>false</code> otherwise.
1294 *
1295 * @see #JAVA_AWT_HEADLESS
1296 * @since 2.1
1297 * @since Java 1.4
1298 */
1299 public static boolean isJavaAwtHeadless() {
1300 return JAVA_AWT_HEADLESS != null ? JAVA_AWT_HEADLESS.equals(Boolean.TRUE.toString()) : false;
1301 }
1302 /**
1303 * <p>Gets the Java home directory as a <code>File</code>.</p>
1304 *
1305 * @return a directory
1306 * @throws SecurityException if a security manager exists and its
1307 * <code>checkPropertyAccess</code> method doesn't allow
1308 * access to the specified system property.
1309 * @see System#getProperty(String)
1310 * @since 2.1
1311 */
1312 public static File getJavaHome() {
1313 return new File(System.getProperty(JAVA_HOME_KEY));
1314 }
1315
1316 /**
1317 * <p>Gets the Java IO temporary directory as a <code>File</code>.</p>
1318 *
1319 * @return a directory
1320 * @throws SecurityException if a security manager exists and its
1321 * <code>checkPropertyAccess</code> method doesn't allow
1322 * access to the specified system property.
1323 * @see System#getProperty(String)
1324 * @since 2.1
1325 */
1326 public static File getJavaIoTmpDir() {
1327 return new File(System.getProperty(JAVA_IO_TMPDIR_KEY));
1328 }
1329
1330 /**
1331 * <p>Gets the user directory as a <code>File</code>.</p>
1332 *
1333 * @return a directory
1334 * @throws SecurityException if a security manager exists and its
1335 * <code>checkPropertyAccess</code> method doesn't allow
1336 * access to the specified system property.
1337 * @see System#getProperty(String)
1338 * @since 2.1
1339 */
1340 public static File getUserDir() {
1341 return new File(System.getProperty(USER_DIR_KEY));
1342 }
1343
1344 /**
1345 * <p>Gets the user home directory as a <code>File</code>.</p>
1346 *
1347 * @return a directory
1348 * @throws SecurityException if a security manager exists and its
1349 * <code>checkPropertyAccess</code> method doesn't allow
1350 * access to the specified system property.
1351 * @see System#getProperty(String)
1352 * @since 2.1
1353 */
1354 public static File getUserHome() {
1355 return new File(System.getProperty(USER_HOME_KEY));
1356 }
1357
1358 }