View Javadoc

1   /* ====================================================================
2    * The Apache Software License, Version 1.1
3    *
4    * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights
5    * reserved.
6    *
7    * Redistribution and use in source and binary forms, with or without
8    * modification, are permitted provided that the following conditions
9    * are met:
10   *
11   * 1. Redistributions of source code must retain the above copyright
12   *    notice, this list of conditions and the following disclaimer.
13   *
14   * 2. Redistributions in binary form must reproduce the above copyright
15   *    notice, this list of conditions and the following disclaimer in
16   *    the documentation and/or other materials provided with the
17   *    distribution.
18   *
19   * 3. The end-user documentation included with the redistribution, if
20   *    any, must include the following acknowledgement:
21   *       "This product includes software developed by the
22   *        Apache Software Foundation (http://www.apache.org/)."
23   *    Alternately, this acknowledgement may appear in the software itself,
24   *    if and wherever such third-party acknowledgements normally appear.
25   *
26   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
27   *    Foundation" must not be used to endorse or promote products derived
28   *    from this software without prior written permission. For written
29   *    permission, please contact apache@apache.org.
30   *
31   * 5. Products derived from this software may not be called "Apache"
32   *    nor may "Apache" appear in their names without prior written
33   *    permission of the Apache Software Foundation.
34   *
35   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46   * SUCH DAMAGE.
47   * ====================================================================
48   *
49   * This software consists of voluntary contributions made by many
50   * individuals on behalf of the Apache Software Foundation.  For more
51   * information on the Apache Software Foundation, please see
52   * <http://www.apache.org/>.
53   */
54  package org.apache.commons.lang;
55  
56  /***
57   * <p>Helpers for <code>java.lang.System</code>.</p>
58   *
59   * <p>If a system property cannot be read due to security restrictions,
60   * the corresponding field in this class will be set to <code>null</code>
61   * and a message will be written to <code>System.err</code>.</p>
62   *
63   * @author Based on code from Avalon Excalibur
64   * @author Based on code from Lucene
65   * @author Stephen Colebourne
66   * @author <a href="mailto:sdowney@panix.com">Steve Downey</a>
67   * @author Gary Gregory
68   * @author Michael Becke
69   * @author Tetsuya Kaneuchi
70   * @since 1.0
71   * @version SystemUtils.java,v 1.23 2003/08/22 16:34:06 ggregory Exp
72   * @version $Id: SystemUtils.java,v 1.1 2004/05/13 01:22:34 dquintela Exp $
73   */
74  public class SystemUtils {
75  
76      // System property constants
77      //-----------------------------------------------------------------------
78      // These MUST be declared first. Other constants depend on this.
79  
80      /***
81       * <p>The <code>file.encoding</code> System Property.</p>
82       * <p>File encoding, such as <code>Cp1252</code>.</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       * @since 2.0
88       * @since Java 1.2.
89       */
90      public static final String FILE_ENCODING = getSystemProperty("file.encoding");
91  
92      /***
93       * <p>The <code>file.separator</code> System Property.
94       * File separator (<code>&quot;/&quot;</code> on UNIX).</p>
95       *
96       * <p>Defaults to <code>null</code> if the runtime does not have
97       * security access to read this property or the property does not exist.</p>
98       *
99       * @since Java 1.1.
100      */
101     public static final String FILE_SEPARATOR = getSystemProperty("file.separator");
102 
103     /***
104      * <p>The <code>java.class.path</code> System Property. Java class path.</p>
105      *
106      * <p>Defaults to <code>null</code> if the runtime does not have
107      * security access to read this property or the property does not exist.</p>
108      *
109      * @since Java 1.1.
110      */
111     public static final String JAVA_CLASS_PATH = getSystemProperty("java.class.path");
112 
113     /***
114      * <p>The <code>java.class.version</code> System Property.
115      * Java class format version number.</p>
116      *
117      * <p>Defaults to <code>null</code> if the runtime does not have
118      * security access to read this property or the property does not exist.</p>
119      *
120      * @since Java 1.1.
121      */
122     public static final String JAVA_CLASS_VERSION = getSystemProperty("java.class.version");
123 
124     /***
125      * <p>The <code>java.compiler</code> System Property. Name of JIT compiler to use.
126      * First in JDK version 1.2. Not used in Sun JDKs after 1.2.</p>
127      *
128      * <p>Defaults to <code>null</code> if the runtime does not have
129      * security access to read this property or the property does not exist.</p>
130      *
131      * @since Java 1.2. Not used in Sun versions after 1.2.
132      */
133     public static final String JAVA_COMPILER = getSystemProperty("java.compiler");
134 
135     /***
136      * <p>The <code>java.ext.dirs</code> System Property. Path of extension directory
137      * or directories.</p>
138      *
139      * <p>Defaults to <code>null</code> if the runtime does not have
140      * security access to read this property or the property does not exist.</p>
141      *
142      * @since Java 1.3
143      */
144     public static final String JAVA_EXT_DIRS = getSystemProperty("java.ext.dirs");
145 
146     /***
147      * <p>The <code>java.home</code> System Property. Java installation directory.</p>
148      *
149      * <p>Defaults to <code>null</code> if the runtime does not have
150      * security access to read this property or the property does not exist.</p>
151      *
152      * @since Java 1.1
153      */
154     public static final String JAVA_HOME = getSystemProperty("java.home");
155 
156     /***
157      * <p>The <code>java.io.tmpdir</code> System Property. Default temp file path.</p>
158      *
159      * <p>Defaults to <code>null</code> if the runtime does not have
160      * security access to read this property or the property does not exist.</p>
161      *
162      * @since Java 1.2
163      */
164     public static final String JAVA_IO_TMPDIR = getSystemProperty("java.io.tmpdir");
165 
166     /***
167      * <p>The <code>java.library.path</code> System Property. List of paths to search
168      * when loading libraries.</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      * @since Java 1.2
174      */
175     public static final String JAVA_LIBRARY_PATH = getSystemProperty("java.library.path");
176 
177     /***
178      * <p>The <code>java.runtime.name</code> System Property. Java Runtime Environment
179      * name.</p>
180      *
181      * <p>Defaults to <code>null</code> if the runtime does not have
182      * security access to read this property or the property does not exist.</p>
183      *
184      * @since 2.0
185      * @since Java 1.3
186      */
187     public static final String JAVA_RUNTIME_NAME = getSystemProperty("java.runtime.name");
188 
189     /***
190      * <p>The <code>java.runtime.version</code> System Property. Java Runtime Environment
191      * version.</p>
192      *
193      * <p>Defaults to <code>null</code> if the runtime does not have
194      * security access to read this property or the property does not exist.</p>
195      *
196      * @since 2.0
197      * @since Java 1.3
198      */
199     public static final String JAVA_RUNTIME_VERSION = getSystemProperty("java.runtime.version");
200 
201     /***
202      * <p>The <code>java.specification.name</code> System Property. Java Runtime Environment
203      * specification name.</p>
204      *
205      * <p>Defaults to <code>null</code> if the runtime does not have
206      * security access to read this property or the property does not exist.</p>
207      *
208      * @since Java 1.2
209      */
210     public static final String JAVA_SPECIFICATION_NAME = getSystemProperty("java.specification.name");
211 
212     /***
213      * <p>The <code>java.specification.vendor</code> System Property. Java Runtime Environment
214      * specification vendor.</p>
215      *
216      * <p>Defaults to <code>null</code> if the runtime does not have
217      * security access to read this property or the property does not exist.</p>
218      *
219      * @since Java 1.2
220      */
221     public static final String JAVA_SPECIFICATION_VENDOR = getSystemProperty("java.specification.vendor");
222 
223     /***
224      * <p>The <code>java.specification.version</code> System Property. Java Runtime Environment
225      * specification version.</p>
226      *
227      * <p>Defaults to <code>null</code> if the runtime does not have
228      * security access to read this property or the property does not exist.</p>
229      *
230      * @since Java 1.3
231      */
232     public static final String JAVA_SPECIFICATION_VERSION = getSystemProperty("java.specification.version");
233 
234     /***
235      * <p>The <code>java.vendor</code> System Property. Java vendor-specific string.</p>
236      *
237      * <p>Defaults to <code>null</code> if the runtime does not have
238      * security access to read this property or the property does not exist.</p>
239      *
240      * @since Java 1.1
241      */
242     public static final String JAVA_VENDOR = getSystemProperty("java.vendor");
243 
244     /***
245      * <p>The <code>java.vendor.url</code> System Property. Java vendor URL.</p>
246      *
247      * <p>Defaults to <code>null</code> if the runtime does not have
248      * security access to read this property or the property does not exist.</p>
249       *
250      * @since Java 1.1
251     */
252     public static final String JAVA_VENDOR_URL = getSystemProperty("java.vendor.url");
253 
254     /***
255      * <p>The <code>java.version</code> System Property. Java version number.</p>
256      *
257      * <p>Defaults to <code>null</code> if the runtime does not have
258      * security access to read this property or the property does not exist.</p>
259      *
260      * @since Java 1.1
261      */
262     public static final String JAVA_VERSION = getSystemProperty("java.version");
263 
264     /***
265      * <p>The <code>java.vm.info</code> System Property. Java Virtual Machine implementation
266      * info.</p>
267      *
268      * <p>Defaults to <code>null</code> if the runtime does not have
269      * security access to read this property or the property does not exist.</p>
270      *
271      * @since 2.0
272      * @since Java 1.2
273      */
274     public static final String JAVA_VM_INFO = getSystemProperty("java.vm.info");
275 
276     /***
277      * <p>The <code>java.vm.name</code> System Property. Java Virtual Machine implementation
278      * name.</p>
279      *
280      * <p>Defaults to <code>null</code> if the runtime does not have
281      * security access to read this property or the property does not exist.</p>
282      *
283      * @since Java 1.2
284      */
285     public static final String JAVA_VM_NAME = getSystemProperty("java.vm.name");
286 
287     /***
288      * <p>The <code>java.vm.specification.name</code> System Property. Java Virtual Machine
289      * specification name.</p>
290      *
291      * <p>Defaults to <code>null</code> if the runtime does not have
292      * security access to read this property or the property does not exist.</p>
293      *
294      * @since Java 1.2
295      */
296     public static final String JAVA_VM_SPECIFICATION_NAME = getSystemProperty("java.vm.specification.name");
297 
298     /***
299      * <p>The <code>java.vm.specification.vendor</code> System Property. Java Virtual
300      * Machine specification vendor.</p>
301      *
302      * <p>Defaults to <code>null</code> if the runtime does not have
303      * security access to read this property or the property does not exist.</p>
304      *
305      * @since Java 1.2
306      */
307     public static final String JAVA_VM_SPECIFICATION_VENDOR = getSystemProperty("java.vm.specification.vendor");
308 
309     /***
310      * <p>The <code>java.vm.specification.version</code> System Property. Java Virtual Machine
311      * specification version.</p>
312      *
313      * <p>Defaults to <code>null</code> if the runtime does not have
314      * security access to read this property or the property does not exist.</p>
315      *
316      * @since Java 1.2
317      */
318     public static final String JAVA_VM_SPECIFICATION_VERSION = getSystemProperty("java.vm.specification.version");
319 
320     /***
321      * <p>The <code>java.vm.vendor</code> System Property. Java Virtual Machine implementation
322      * vendor.</p>
323      *
324      * <p>Defaults to <code>null</code> if the runtime does not have
325      * security access to read this property or the property does not exist.</p>
326      *
327      * @since Java 1.2
328      */
329     public static final String JAVA_VM_VENDOR = getSystemProperty("java.vm.vendor");
330 
331     /***
332      * <p>The <code>java.vm.version</code> System Property. Java Virtual Machine
333      * implementation version.</p>
334      *
335      * <p>Defaults to <code>null</code> if the runtime does not have
336      * security access to read this property or the property does not exist.</p>
337      *
338      * @since Java 1.2
339      */
340     public static final String JAVA_VM_VERSION = getSystemProperty("java.vm.version");
341 
342     /***
343      * <p>The <code>line.separator</code> System Property. Line separator
344      * (<code>&quot;\n<&quot;</code> on UNIX).</p>
345      *
346      * <p>Defaults to <code>null</code> if the runtime does not have
347      * security access to read this property or the property does not exist.</p>
348      *
349      * @since Java 1.1
350      */
351     public static final String LINE_SEPARATOR = getSystemProperty("line.separator");
352 
353     /***
354      * <p>The <code>os.arch</code> System Property. Operating system architecture.</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      * @since Java 1.1
360      */
361     public static final String OS_ARCH = getSystemProperty("os.arch");
362 
363     /***
364      * <p>The <code>os.name</code> System Property. Operating system name.</p>
365      *
366      * <p>Defaults to <code>null</code> if the runtime does not have
367      * security access to read this property or the property does not exist.</p>
368      *
369      * @since Java 1.1
370      */
371     public static final String OS_NAME = getSystemProperty("os.name");
372 
373     /***
374      * <p>The <code>os.version</code> System Property. Operating system version.</p>
375      *
376      * <p>Defaults to <code>null</code> if the runtime does not have
377      * security access to read this property or the property does not exist.</p>
378      *
379      * @since Java 1.1
380      */
381     public static final String OS_VERSION = getSystemProperty("os.version");
382 
383     /***
384      * <p>The <code>path.separator</code> System Property. Path separator
385      * (<code>&quot;:&quot;</code> on UNIX).</p>
386      *
387      * <p>Defaults to <code>null</code> if the runtime does not have
388      * security access to read this property or the property does not exist.</p>
389      *
390      * @since Java 1.1
391      */
392     public static final String PATH_SEPARATOR = getSystemProperty("path.separator");
393 
394     /***
395      * <p>The <code>user.country</code> or <code>user.region</code> System Property.
396      * User's country code, such as <code>GB</code>. First in JDK version 1.2 as
397      * <code>user.region</code>. Renamed to <code>user.country</code> in 1.4</p>
398      *
399      * <p>Defaults to <code>null</code> if the runtime does not have
400      * security access to read this property or the property does not exist.</p>
401      *
402      * @since 2.0
403      * @since Java 1.2
404      */
405     public static final String USER_COUNTRY =
406         (getSystemProperty("user.country") == null ?
407             getSystemProperty("user.region") : getSystemProperty("user.country"));
408 
409     /***
410      * <p>The <code>user.dir</code> System Property. User's current working
411      * directory.</p>
412      *
413      * <p>Defaults to <code>null</code> if the runtime does not have
414      * security access to read this property or the property does not exist.</p>
415      *
416      * @since Java 1.1
417      */
418     public static final String USER_DIR = getSystemProperty("user.dir");
419 
420     /***
421      * <p>The <code>user.home</code> System Property. User's home directory.</p>
422      *
423      * <p>Defaults to <code>null</code> if the runtime does not have
424      * security access to read this property or the property does not exist.</p>
425      *
426      * @since Java 1.1
427      */
428     public static final String USER_HOME = getSystemProperty("user.home");
429 
430     /***
431      * <p>The <code>user.language</code> System Property. User's language code,
432      * such as 'en'.</p>
433      *
434      * <p>Defaults to <code>null</code> if the runtime does not have
435      * security access to read this property or the property does not exist.</p>
436      *
437      * @since 2.0
438      * @since Java 1.2
439      */
440     public static final String USER_LANGUAGE = getSystemProperty("user.language");
441 
442     /***
443      * <p>The <code>user.name</code> System Property. User's account name.</p>
444      *
445      * <p>Defaults to <code>null</code> if the runtime does not have
446      * security access to read this property or the property does not exist.</p>
447      *
448      * @since Java 1.1
449      */
450     public static final String USER_NAME = getSystemProperty("user.name");
451 
452     // Java version
453     //-----------------------------------------------------------------------
454     // These MUST be declared after those above as they depend on the
455     // values being set up
456 
457     /***
458      * <p>Gets the Java version as a <code>float</code>.</p>
459      *
460      * <p>Example return values:</p>
461      * <ul>
462      *  <li><code>1.2f</code> for JDK 1.2
463      *  <li><code>1.31f</code> for JDK 1.3.1
464      * </ul>
465      *
466      * <p>The field will return zero if {@link #JAVA_VERSION} is <code>null</code>.</p>
467      *
468      * @since 2.0
469      */
470     public static final float JAVA_VERSION_FLOAT = getJavaVersionAsFloat();
471 
472     /***
473      * <p>Gets the Java version as an <code>int</code>.</p>
474      *
475      * <p>Example return values:</p>
476      * <ul>
477      *  <li><code>120</code> for JDK 1.2
478      *  <li><code>131</code> for JDK 1.3.1
479      * </ul>
480      *
481      * <p>The field will return zero if {@link #JAVA_VERSION} is <code>null</code>.</p>
482      *
483      * @since 2.0
484      */
485     public static final int JAVA_VERSION_INT = getJavaVersionAsInt();
486 
487     // Java version checks
488     //-----------------------------------------------------------------------
489     // These MUST be declared after those above as they depend on the
490     // values being set up
491 
492     /***
493      * <p>Is <code>true</code> if this is Java version 1.1 (also 1.1.x versions).</p>
494      *
495      * <p>The field will return <code>false</code> if {@link #JAVA_VERSION} is
496      * <code>null</code>.</p>
497      */
498     public static final boolean IS_JAVA_1_1 = getJavaVersionMatches("1.1");
499 
500     /***
501      * <p>Is <code>true</code> if this is Java version 1.2 (also 1.2.x versions).</p>
502      *
503      * <p>The field will return <code>false</code> if {@link #JAVA_VERSION} is
504      * <code>null</code>.</p>
505      */
506     public static final boolean IS_JAVA_1_2 = getJavaVersionMatches("1.2");
507 
508     /***
509      * <p>Is <code>true</code> if this is Java version 1.3 (also 1.3.x versions).</p>
510      *
511      * <p>The field will return <code>false</code> if {@link #JAVA_VERSION} is
512      * <code>null</code>.</p>
513      */
514     public static final boolean IS_JAVA_1_3 = getJavaVersionMatches("1.3");
515 
516     /***
517      * <p>Is <code>true</code> if this is Java version 1.4 (also 1.4.x versions).</p>
518      *
519      * <p>The field will <code>false</code> false if {@link #JAVA_VERSION} is
520      * <code>null</code>.</p>
521      */
522     public static final boolean IS_JAVA_1_4 = getJavaVersionMatches("1.4");
523 
524     /***
525      * <p>Is <code>true</code> if this is Java version 1.5 (also 1.5.x versions).</p>
526      *
527      * <p>The field will return <code>false</code> if {@link #JAVA_VERSION} is
528      * <code>null</code>.</p>
529      */
530     public static final boolean IS_JAVA_1_5 = getJavaVersionMatches("1.5");
531 
532     // Operating system checks
533     //-----------------------------------------------------------------------
534     // These MUST be declared after those above as they depend on the
535     // values being set up
536     // OS names from http://www.vamphq.com/os.html
537     // Selected ones included - please advise commons-dev@jakarta.apache.org
538     // if you want another added or a mistake corrected
539 
540     /***
541      * <p>Is <code>true</code> if this is AIX.</p>
542      *
543      * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
544      * <code>null</code>.</p>
545      *
546      * @since 2.0
547      */
548     public static final boolean IS_OS_AIX = getOSMatches("AIX");
549 
550     /***
551      * <p>Is <code>true</code> if this is HP-UX.</p>
552      *
553      * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
554      * <code>null</code>.</p>
555      *
556      * @since 2.0
557      */
558     public static final boolean IS_OS_HP_UX = getOSMatches("HP-UX");
559 
560     /***
561      * <p>Is <code>true</code> if this is Irix.</p>
562      *
563      * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
564      * <code>null</code>.</p>
565      *
566      * @since 2.0
567      */
568     public static final boolean IS_OS_IRIX = getOSMatches("Irix");
569 
570     /***
571      * <p>Is <code>true</code> if this is Linux.</p>
572      *
573      * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
574      * <code>null</code>.</p>
575      *
576      * @since 2.0
577      */
578     public static final boolean IS_OS_LINUX = getOSMatches("Linux") || getOSMatches("LINUX");
579 
580     /***
581      * <p>Is <code>true</code> if this is Mac.</p>
582      *
583      * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
584      * <code>null</code>.</p>
585      *
586      * @since 2.0
587      */
588     public static final boolean IS_OS_MAC = getOSMatches("Mac");
589 
590     /***
591      * <p>Is <code>true</code> if this is Mac.</p>
592      *
593      * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
594      * <code>null</code>.</p>
595      *
596      * @since 2.0
597      */
598     public static final boolean IS_OS_MAC_OSX = getOSMatches("Mac OS X");
599 
600     /***
601      * <p>Is <code>true</code> if this is OS/2.</p>
602      *
603      * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
604      * <code>null</code>.</p>
605      *
606      * @since 2.0
607      */
608     public static final boolean IS_OS_OS2 = getOSMatches("OS/2");
609 
610     /***
611      * <p>Is <code>true</code> if this is Solaris.</p>
612      *
613      * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
614      * <code>null</code>.</p>
615      *
616      * @since 2.0
617      */
618     public static final boolean IS_OS_SOLARIS = getOSMatches("Solaris");
619 
620     /***
621      * <p>Is <code>true</code> if this is SunOS.</p>
622      *
623      * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
624      * <code>null</code>.</p>
625      *
626      * @since 2.0
627      */
628     public static final boolean IS_OS_SUN_OS = getOSMatches("SunOS");
629 
630     /***
631      * <p>Is <code>true</code> if this is Windows.</p>
632      *
633      * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
634      * <code>null</code>.</p>
635      *
636      * @since 2.0
637      */
638     public static final boolean IS_OS_WINDOWS = getOSMatches("Windows");
639 
640     /***
641      * <p>Is <code>true</code> if this is Windows 2000.</p>
642      *
643      * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
644      * <code>null</code>.</p>
645      *
646      * @since 2.0
647      */
648     public static final boolean IS_OS_WINDOWS_2000 = getOSMatches("Windows", "5.0");
649 
650     /***
651      * <p>Is <code>true</code> if this is Windows 95.</p>
652      *
653      * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
654      * <code>null</code>.</p>
655      *
656      * @since 2.0
657      */
658     public static final boolean IS_OS_WINDOWS_95 = getOSMatches("Windows 9", "4.0");
659     // JDK 1.2 running on Windows98 returns 'Windows 95', hence the above
660 
661     /***
662      * <p>Is <code>true</code> if this is Windows 98.</p>
663      *
664      * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
665      * <code>null</code>.</p>
666      *
667      * @since 2.0
668      */
669     public static final boolean IS_OS_WINDOWS_98 = getOSMatches("Windows 9", "4.1");
670     // JDK 1.2 running on Windows98 returns 'Windows 95', hence the above
671 
672     /***
673      * <p>Is <code>true</code> if this is Windows ME.</p>
674      *
675      * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
676      * <code>null</code>.</p>
677      *
678      * @since 2.0
679      */
680     public static final boolean IS_OS_WINDOWS_ME = getOSMatches("Windows", "4.9");
681     // JDK 1.2 running on WindowsME may return 'Windows 95', hence the above
682 
683     /***
684      * <p>Is <code>true</code> if this is Windows NT.</p>
685      *
686      * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
687      * <code>null</code>.</p>
688      *
689      * @since 2.0
690      */
691     public static final boolean IS_OS_WINDOWS_NT = getOSMatches("Windows NT");
692     // Windows 2000 returns 'Windows 2000' but may suffer from same JDK1.2 problem
693 
694     /***
695      * <p>Is <code>true</code> if this is Windows XP.</p>
696      *
697      * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
698      * <code>null</code>.</p>
699      *
700      * @since 2.0
701      */
702     public static final boolean IS_OS_WINDOWS_XP = getOSMatches("Windows", "5.1");
703     // Windows XP returns 'Windows 2000' just for fun...
704 
705     //-----------------------------------------------------------------------
706     /***
707      * <p>SystemUtils instances should NOT be constructed in standard
708      * programming. Instead, the class should be used as
709      * <code>SystemUtils.FILE_SEPARATOR</code>.</p>
710      *
711      * <p>This constructor is public to permit tools that require a JavaBean
712      * instance to operate.</p>
713      */
714     public SystemUtils() {
715     }
716 
717     //-----------------------------------------------------------------------
718     /***
719      * <p>Gets the Java version number as a <code>float</code>.</p>
720      *
721      * <p>Example return values:</p>
722      * <ul>
723      *  <li><code>1.2f</code> for JDK 1.2
724      *  <li><code>1.31f</code> for JDK 1.3.1
725      * </ul>
726      *
727      * @return the version, for example 1.31f for JDK 1.3.1
728      * @deprecated Use {@link #JAVA_VERSION_FLOAT} instead.
729      *             Method will be removed in Commons Lang 3.0.
730      */
731     public static float getJavaVersion() {
732         return JAVA_VERSION_FLOAT;
733     }
734 
735     /***
736      * <p>Gets the Java version number as a <code>float</code>.</p>
737      *
738      * <p>Example return values:</p>
739      * <ul>
740      *  <li><code>1.2f</code> for JDK 1.2
741      *  <li><code>1.31f</code> for JDK 1.3.1
742      * </ul>
743      *
744      * <p>Patch releases are not reported.
745      * Zero is returned if {@link #JAVA_VERSION} is <code>null</code>.</p>
746      *
747      * @return the version, for example 1.31f for JDK 1.3.1
748      */
749     private static float getJavaVersionAsFloat() {
750         if (JAVA_VERSION == null) {
751             return 0f;
752         }
753         String str = JAVA_VERSION.substring(0, 3);
754         if (JAVA_VERSION.length() >= 5) {
755             str = str + JAVA_VERSION.substring(4, 5);
756         }
757         return Float.parseFloat(str);
758     }
759 
760     /***
761      * <p>Gets the Java version number as an <code>int</code>.</p>
762      *
763      * <p>Example return values:</p>
764      * <ul>
765      *  <li><code>120</code> for JDK 1.2
766      *  <li><code>131</code> for JDK 1.3.1
767      * </ul>
768      *
769      * <p>Patch releases are not reported.
770      * Zero is returned if {@link #JAVA_VERSION} is <code>null</code>.</p>
771      *
772      * @return the version, for example 131 for JDK 1.3.1
773      */
774     private static int getJavaVersionAsInt() {
775         if (JAVA_VERSION == null) {
776             return 0;
777         }
778         String str = JAVA_VERSION.substring(0, 1);
779         str = str + JAVA_VERSION.substring(2, 3);
780         if (JAVA_VERSION.length() >= 5) {
781             str = str + JAVA_VERSION.substring(4, 5);
782         } else {
783             str = str + "0";
784         }
785         return Integer.parseInt(str);
786     }
787 
788     /***
789      * <p>Decides if the java version matches.</p>
790      *
791      * @param versionPrefix  the prefix for the java version
792      * @return true if matches, or false if not or can't determine
793      */
794     private static boolean getJavaVersionMatches(String versionPrefix) {
795         if (JAVA_VERSION == null) {
796             return false;
797         }
798         return JAVA_VERSION.startsWith(versionPrefix);
799     }
800 
801     /***
802      * <p>Decides if the operating system matches.</p>
803      *
804      * @param osNamePrefix  the prefix for the os name
805      * @return true if matches, or false if not or can't determine
806      */
807     private static boolean getOSMatches(String osNamePrefix) {
808         if (OS_NAME == null) {
809             return false;
810         }
811         return OS_NAME.startsWith(osNamePrefix);
812     }
813 
814     /***
815      * <p>Decides if the operating system matches.</p>
816      *
817      * @param osNamePrefix  the prefix for the os name
818      * @param osVersionPrefix  the prefix for the version
819      * @return true if matches, or false if not or can't determine
820      */
821     private static boolean getOSMatches(String osNamePrefix, String osVersionPrefix) {
822         if (OS_NAME == null || OS_VERSION == null) {
823             return false;
824         }
825         return OS_NAME.startsWith(osNamePrefix) && OS_VERSION.startsWith(osVersionPrefix);
826     }
827 
828     //-----------------------------------------------------------------------
829     /***
830      * <p>Gets a System property, defaulting to <code>null</code> if the property
831      * cannot be read.</p>
832      *
833      * <p>If a <code>SecurityException</code> is caught, the return
834      * value is <code>null</code> and a message is written to <code>System.err</code>.</p>
835      *
836      * @param property the system property name
837      * @return the system property value or <code>null</code> if a security problem occurs
838      */
839     private static String getSystemProperty(String property) {
840         try {
841             return System.getProperty(property);
842         } catch (SecurityException ex) {
843             // we are not allowed to look at this property
844             System.err.println(
845                 "Caught a SecurityException reading the system property '" + property
846                 + "'; the SystemUtils property value will default to null."
847             );
848             return null;
849         }
850     }
851 
852     /***
853      * <p>Is the Java version at least the requested version.</p>
854      *
855      * <p>Example input:</p>
856      * <ul>
857      *  <li><code>1.2f</code> to test for JDK 1.2</li>
858      *  <li><code>1.31f</code> to test for JDK 1.3.1</li>
859      * </ul>
860      *
861      * @param requiredVersion  the required version, for example 1.31f
862      * @return <code>true</code> if the actual version is equal or greater
863      *  than the required version
864      */
865     public static boolean isJavaVersionAtLeast(float requiredVersion) {
866         return (JAVA_VERSION_FLOAT >= requiredVersion);
867     }
868 
869     /***
870      * <p>Is the Java version at least the requested version.</p>
871      *
872      * <p>Example input:</p>
873      * <ul>
874      *  <li><code>120</code> to test for JDK 1.2 or greater</li>
875      *  <li><code>131</code> to test for JDK 1.3.1 or greater</li>
876      * </ul>
877      *
878      * @param requiredVersion  the required version, for example 131
879      * @return <code>true</code> if the actual version is equal or greater
880      *  than the required version
881      * @since 2.0
882      */
883     public static boolean isJavaVersionAtLeast(int requiredVersion) {
884         return (JAVA_VERSION_INT >= requiredVersion);
885     }
886 
887 }