jdk/src/share/classes/sun/misc/Version.java.template
changeset 4115 e09be02771b6
parent 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
4113:e2d47cad2647 4115:e09be02771b6
       
     1 /*
       
     2  * Copyright 1999-2007 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package sun.misc;
       
    27 import java.io.PrintStream;
       
    28 
       
    29 public class Version {
       
    30 
       
    31 
       
    32     private static final String launcher_name =
       
    33         "@@launcher_name@@";
       
    34 
       
    35     private static final String java_version =
       
    36         "@@java_version@@";
       
    37 
       
    38     private static final String java_runtime_name =
       
    39         "@@java_runtime_name@@";
       
    40 
       
    41     private static final String java_runtime_version =
       
    42         "@@java_runtime_version@@";
       
    43 
       
    44     static {
       
    45         init();
       
    46     }
       
    47 
       
    48     public static void init() {
       
    49         System.setProperty("java.version", java_version);
       
    50         System.setProperty("java.runtime.version", java_runtime_version);
       
    51         System.setProperty("java.runtime.name", java_runtime_name);
       
    52     }
       
    53 
       
    54     private static boolean versionsInitialized = false;
       
    55     private static int jvm_major_version = 0;
       
    56     private static int jvm_minor_version = 0;
       
    57     private static int jvm_micro_version = 0;
       
    58     private static int jvm_update_version = 0;
       
    59     private static int jvm_build_number = 0;
       
    60     private static String jvm_special_version = null;
       
    61     private static int jdk_major_version = 0;
       
    62     private static int jdk_minor_version = 0;
       
    63     private static int jdk_micro_version = 0;
       
    64     private static int jdk_update_version = 0;
       
    65     private static int jdk_build_number = 0;
       
    66     private static String jdk_special_version = null;
       
    67 
       
    68     /**
       
    69      * In case you were wondering this method is called by java -version.
       
    70      * Sad that it prints to stderr; would be nicer if default printed on
       
    71      * stdout.
       
    72      */
       
    73     public static void print() {
       
    74         print(System.err);
       
    75     }
       
    76 
       
    77     /**
       
    78      * This is the same as print except that it adds an extra line-feed
       
    79      * at the end, typically used by the -showversion in the launcher
       
    80      */
       
    81     public static void println() {
       
    82         print(System.err);
       
    83         System.err.println();
       
    84     }
       
    85 
       
    86     /**
       
    87      * Give a stream, it will print version info on it.
       
    88      */
       
    89     public static void print(PrintStream ps) {
       
    90         /* First line: platform version. */
       
    91         ps.println(launcher_name + " version \"" + java_version + "\"");
       
    92 
       
    93         /* Second line: runtime version (ie, libraries). */
       
    94         ps.println(java_runtime_name + " (build " +
       
    95                            java_runtime_version + ")");
       
    96 
       
    97         /* Third line: JVM information. */
       
    98         String java_vm_name    = System.getProperty("java.vm.name");
       
    99         String java_vm_version = System.getProperty("java.vm.version");
       
   100         String java_vm_info    = System.getProperty("java.vm.info");
       
   101         ps.println(java_vm_name + " (build " + java_vm_version + ", " +
       
   102                    java_vm_info + ")");
       
   103     }
       
   104 
       
   105 
       
   106     /**
       
   107      * Returns the major version of the running JVM if it's 1.6 or newer
       
   108      * or any RE VM build. It will return 0 if it's an internal 1.5 or
       
   109      * 1.4.x build.
       
   110      *
       
   111      * @since 1.6
       
   112      */
       
   113     public static synchronized int jvmMajorVersion() {
       
   114         if (!versionsInitialized) {
       
   115             initVersions();
       
   116         }
       
   117         return jvm_major_version;
       
   118     }
       
   119 
       
   120     /**
       
   121      * Returns the minor version of the running JVM if it's 1.6 or newer
       
   122      * or any RE VM build. It will return 0 if it's an internal 1.5 or
       
   123      * 1.4.x build.
       
   124      * @since 1.6
       
   125      */
       
   126     public static synchronized int jvmMinorVersion() {
       
   127         if (!versionsInitialized) {
       
   128             initVersions();
       
   129         }
       
   130         return jvm_minor_version;
       
   131     }
       
   132 
       
   133 
       
   134     /**
       
   135      * Returns the micro version of the running JVM if it's 1.6 or newer
       
   136      * or any RE VM build. It will return 0 if it's an internal 1.5 or
       
   137      * 1.4.x build.
       
   138      * @since 1.6
       
   139      */
       
   140     public static synchronized int jvmMicroVersion() {
       
   141         if (!versionsInitialized) {
       
   142             initVersions();
       
   143         }
       
   144         return jvm_micro_version;
       
   145     }
       
   146 
       
   147     /**
       
   148      * Returns the update release version of the running JVM if it's
       
   149      * a RE build. It will return 0 if it's an internal build.
       
   150      * @since 1.6
       
   151      */
       
   152     public static synchronized int jvmUpdateVersion() {
       
   153         if (!versionsInitialized) {
       
   154             initVersions();
       
   155         }
       
   156         return jvm_update_version;
       
   157     }
       
   158 
       
   159     public static synchronized String jvmSpecialVersion() {
       
   160         if (!versionsInitialized) {
       
   161             initVersions();
       
   162         }
       
   163         if (jvm_special_version == null) {
       
   164             jvm_special_version = getJvmSpecialVersion();
       
   165         }
       
   166         return jvm_special_version;
       
   167     }
       
   168     public static native String getJvmSpecialVersion();
       
   169 
       
   170     /**
       
   171      * Returns the build number of the running JVM if it's a RE build
       
   172      * It will return 0 if it's an internal build.
       
   173      * @since 1.6
       
   174      */
       
   175     public static synchronized int jvmBuildNumber() {
       
   176         if (!versionsInitialized) {
       
   177             initVersions();
       
   178         }
       
   179         return jvm_build_number;
       
   180     }
       
   181 
       
   182     /**
       
   183      * Returns the major version of the running JDK.
       
   184      *
       
   185      * @since 1.6
       
   186      */
       
   187     public static synchronized int jdkMajorVersion() {
       
   188         if (!versionsInitialized) {
       
   189             initVersions();
       
   190         }
       
   191         return jdk_major_version;
       
   192     }
       
   193 
       
   194     /**
       
   195      * Returns the minor version of the running JDK.
       
   196      * @since 1.6
       
   197      */
       
   198     public static synchronized int jdkMinorVersion() {
       
   199         if (!versionsInitialized) {
       
   200             initVersions();
       
   201         }
       
   202         return jdk_minor_version;
       
   203     }
       
   204 
       
   205     /**
       
   206      * Returns the micro version of the running JDK.
       
   207      * @since 1.6
       
   208      */
       
   209     public static synchronized int jdkMicroVersion() {
       
   210         if (!versionsInitialized) {
       
   211             initVersions();
       
   212         }
       
   213         return jdk_micro_version;
       
   214     }
       
   215 
       
   216     /**
       
   217      * Returns the update release version of the running JDK if it's
       
   218      * a RE build. It will return 0 if it's an internal build.
       
   219      * @since 1.6
       
   220      */
       
   221     public static synchronized int jdkUpdateVersion() {
       
   222         if (!versionsInitialized) {
       
   223             initVersions();
       
   224         }
       
   225         return jdk_update_version;
       
   226     }
       
   227 
       
   228     public static synchronized String jdkSpecialVersion() {
       
   229         if (!versionsInitialized) {
       
   230             initVersions();
       
   231         }
       
   232         if (jdk_special_version == null) {
       
   233             jdk_special_version = getJdkSpecialVersion();
       
   234         }
       
   235         return jdk_special_version;
       
   236     }
       
   237     public static native String getJdkSpecialVersion();
       
   238 
       
   239     /**
       
   240      * Returns the build number of the running JDK if it's a RE build
       
   241      * It will return 0 if it's an internal build.
       
   242      * @since 1.6
       
   243      */
       
   244     public static synchronized int jdkBuildNumber() {
       
   245         if (!versionsInitialized) {
       
   246             initVersions();
       
   247         }
       
   248         return jdk_build_number;
       
   249     }
       
   250 
       
   251     // true if JVM exports the version info including the capabilities
       
   252     private static boolean jvmVersionInfoAvailable;
       
   253     private static synchronized void initVersions() {
       
   254         if (versionsInitialized) {
       
   255             return;
       
   256         }
       
   257         jvmVersionInfoAvailable = getJvmVersionInfo();
       
   258         if (!jvmVersionInfoAvailable) {
       
   259             // parse java.vm.version for older JVM before the
       
   260             // new JVM_GetVersionInfo is added.
       
   261             // valid format of the version string is:
       
   262             // n.n.n[_uu[c]][-<identifer>]-bxx
       
   263             CharSequence cs = System.getProperty("java.vm.version");
       
   264             if (cs.length() >= 5 &&
       
   265                 Character.isDigit(cs.charAt(0)) && cs.charAt(1) == '.' &&
       
   266                 Character.isDigit(cs.charAt(2)) && cs.charAt(3) == '.' &&
       
   267                 Character.isDigit(cs.charAt(4))) {
       
   268                 jvm_major_version = Character.digit(cs.charAt(0), 10);
       
   269                 jvm_minor_version = Character.digit(cs.charAt(2), 10);
       
   270                 jvm_micro_version = Character.digit(cs.charAt(4), 10);
       
   271                 cs = cs.subSequence(5, cs.length());
       
   272                 if (cs.charAt(0) == '_' && cs.length() >= 3 &&
       
   273                     Character.isDigit(cs.charAt(1)) &&
       
   274                     Character.isDigit(cs.charAt(2))) {
       
   275                     int nextChar = 3;
       
   276                     try {
       
   277                         String uu = cs.subSequence(1, 3).toString();
       
   278                         jvm_update_version = Integer.valueOf(uu).intValue();
       
   279                         if (cs.length() >= 4) {
       
   280                             char c = cs.charAt(3);
       
   281                             if (c >= 'a' && c <= 'z') {
       
   282                                 jvm_special_version = Character.toString(c);
       
   283                                 nextChar++;
       
   284                             }
       
   285                         }
       
   286                     } catch (NumberFormatException e) {
       
   287                         // not conforming to the naming convention
       
   288                         return;
       
   289                     }
       
   290                     cs = cs.subSequence(nextChar, cs.length());
       
   291                 }
       
   292                 if (cs.charAt(0) == '-') {
       
   293                     // skip the first character
       
   294                     // valid format: <identifier>-bxx or bxx
       
   295                     // non-product VM will have -debug|-release appended
       
   296                     cs = cs.subSequence(1, cs.length());
       
   297                     String[] res = cs.toString().split("-");
       
   298                     for (String s : res) {
       
   299                         if (s.charAt(0) == 'b' && s.length() == 3 &&
       
   300                             Character.isDigit(s.charAt(1)) &&
       
   301                             Character.isDigit(s.charAt(2))) {
       
   302                             jvm_build_number =
       
   303                                 Integer.valueOf(s.substring(1, 3)).intValue();
       
   304                             break;
       
   305                         }
       
   306                     }
       
   307                 }
       
   308             }
       
   309         }
       
   310         getJdkVersionInfo();
       
   311         versionsInitialized = true;
       
   312     }
       
   313 
       
   314     // Gets the JVM version info if available and sets the jvm_*_version fields
       
   315     // and its capabilities.
       
   316     //
       
   317     // Return false if not available which implies an old VM (Tiger or before).
       
   318     private static native boolean getJvmVersionInfo();
       
   319     private static native void getJdkVersionInfo();
       
   320 
       
   321 }
       
   322 
       
   323 // Help Emacs a little because this file doesn't end in .java.
       
   324 //
       
   325 // Local Variables: ***
       
   326 // mode: java ***
       
   327 // End: ***