annotate src/share/classes/sun/misc/Version.java.template @ 6573:c024147205f6

8003256: (profiles) Add support for profile identification Reviewed-by: dholmes, mchung, ksrini
author alanb
date Mon, 21 Jan 2013 23:21:15 -0500
parents fe232d7e4ff1
children eb8f7bc6f964
rev   line source
duke@0 1 /*
alanb@6573 2 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
ohair@2362 7 * published by the Free Software Foundation. Oracle designates this
duke@0 8 * particular file as subject to the "Classpath" exception as provided
ohair@2362 9 * by Oracle in the LICENSE file that accompanied this code.
duke@0 10 *
duke@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 15 * accompanied this code).
duke@0 16 *
duke@0 17 * You should have received a copy of the GNU General Public License version
duke@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 20 *
ohair@2362 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@2362 22 * or visit www.oracle.com if you need additional information or have any
ohair@2362 23 * questions.
duke@0 24 */
duke@0 25
duke@0 26 package sun.misc;
duke@0 27 import java.io.PrintStream;
duke@0 28
duke@0 29 public class Version {
duke@0 30
duke@0 31
duke@0 32 private static final String launcher_name =
duke@0 33 "@@launcher_name@@";
duke@0 34
duke@0 35 private static final String java_version =
duke@0 36 "@@java_version@@";
duke@0 37
duke@0 38 private static final String java_runtime_name =
alanb@6573 39 "@@java_runtime_name@@";
alanb@6573 40
alanb@6573 41 private static final String java_profile_name =
alanb@6573 42 "@@java_profile_name@@";
alanb@6573 43
duke@0 44 private static final String java_runtime_version =
duke@0 45 "@@java_runtime_version@@";
duke@0 46
duke@0 47 static {
duke@0 48 init();
duke@0 49 }
duke@0 50
duke@0 51 public static void init() {
duke@0 52 System.setProperty("java.version", java_version);
duke@0 53 System.setProperty("java.runtime.version", java_runtime_version);
duke@0 54 System.setProperty("java.runtime.name", java_runtime_name);
alanb@6573 55 if (java_profile_name.length() > 0)
alanb@6573 56 System.setProperty("java.runtime.profile", java_profile_name);
duke@0 57 }
duke@0 58
duke@0 59 private static boolean versionsInitialized = false;
duke@0 60 private static int jvm_major_version = 0;
duke@0 61 private static int jvm_minor_version = 0;
duke@0 62 private static int jvm_micro_version = 0;
duke@0 63 private static int jvm_update_version = 0;
duke@0 64 private static int jvm_build_number = 0;
duke@0 65 private static String jvm_special_version = null;
duke@0 66 private static int jdk_major_version = 0;
duke@0 67 private static int jdk_minor_version = 0;
duke@0 68 private static int jdk_micro_version = 0;
duke@0 69 private static int jdk_update_version = 0;
duke@0 70 private static int jdk_build_number = 0;
duke@0 71 private static String jdk_special_version = null;
duke@0 72
duke@0 73 /**
duke@0 74 * In case you were wondering this method is called by java -version.
duke@0 75 * Sad that it prints to stderr; would be nicer if default printed on
duke@0 76 * stdout.
duke@0 77 */
duke@0 78 public static void print() {
duke@0 79 print(System.err);
duke@0 80 }
duke@0 81
duke@0 82 /**
duke@0 83 * This is the same as print except that it adds an extra line-feed
duke@0 84 * at the end, typically used by the -showversion in the launcher
duke@0 85 */
duke@0 86 public static void println() {
duke@0 87 print(System.err);
duke@0 88 System.err.println();
duke@0 89 }
duke@0 90
duke@0 91 /**
duke@0 92 * Give a stream, it will print version info on it.
duke@0 93 */
duke@0 94 public static void print(PrintStream ps) {
dholmes@3867 95 boolean isHeadless = false;
dholmes@3867 96
dholmes@3867 97 /* Report that we're running headless if the property is true */
alanb@6573 98 String headless = System.getProperty("java.awt.headless");
alanb@6573 99 if ( (headless != null) && (headless.equalsIgnoreCase("true")) ) {
dholmes@3867 100 isHeadless = true;
alanb@6573 101 }
dholmes@3867 102
duke@0 103 /* First line: platform version. */
duke@0 104 ps.println(launcher_name + " version \"" + java_version + "\"");
duke@0 105
duke@0 106 /* Second line: runtime version (ie, libraries). */
dholmes@3867 107
alanb@6573 108 ps.print(java_runtime_name + " (build " + java_runtime_version);
dholmes@3867 109
alanb@6573 110 if (java_profile_name.length() > 0) {
alanb@6573 111 // profile name
alanb@6573 112 ps.print(", profile " + java_profile_name);
alanb@6573 113 }
alanb@6573 114
alanb@6573 115 if (java_runtime_name.indexOf("Embedded") != -1 && isHeadless) {
alanb@6573 116 // embedded builds report headless state
alanb@6573 117 ps.print(", headless");
alanb@6573 118 }
alanb@6573 119 ps.println(')');
duke@0 120
duke@0 121 /* Third line: JVM information. */
duke@0 122 String java_vm_name = System.getProperty("java.vm.name");
duke@0 123 String java_vm_version = System.getProperty("java.vm.version");
duke@0 124 String java_vm_info = System.getProperty("java.vm.info");
duke@0 125 ps.println(java_vm_name + " (build " + java_vm_version + ", " +
duke@0 126 java_vm_info + ")");
duke@0 127 }
duke@0 128
duke@0 129
duke@0 130 /**
duke@0 131 * Returns the major version of the running JVM if it's 1.6 or newer
duke@0 132 * or any RE VM build. It will return 0 if it's an internal 1.5 or
duke@0 133 * 1.4.x build.
duke@0 134 *
duke@0 135 * @since 1.6
duke@0 136 */
duke@0 137 public static synchronized int jvmMajorVersion() {
duke@0 138 if (!versionsInitialized) {
duke@0 139 initVersions();
duke@0 140 }
duke@0 141 return jvm_major_version;
duke@0 142 }
duke@0 143
duke@0 144 /**
duke@0 145 * Returns the minor version of the running JVM if it's 1.6 or newer
duke@0 146 * or any RE VM build. It will return 0 if it's an internal 1.5 or
duke@0 147 * 1.4.x build.
duke@0 148 * @since 1.6
duke@0 149 */
duke@0 150 public static synchronized int jvmMinorVersion() {
duke@0 151 if (!versionsInitialized) {
duke@0 152 initVersions();
duke@0 153 }
duke@0 154 return jvm_minor_version;
duke@0 155 }
duke@0 156
duke@0 157
duke@0 158 /**
duke@0 159 * Returns the micro version of the running JVM if it's 1.6 or newer
duke@0 160 * or any RE VM build. It will return 0 if it's an internal 1.5 or
duke@0 161 * 1.4.x build.
duke@0 162 * @since 1.6
duke@0 163 */
duke@0 164 public static synchronized int jvmMicroVersion() {
duke@0 165 if (!versionsInitialized) {
duke@0 166 initVersions();
duke@0 167 }
duke@0 168 return jvm_micro_version;
duke@0 169 }
duke@0 170
duke@0 171 /**
duke@0 172 * Returns the update release version of the running JVM if it's
duke@0 173 * a RE build. It will return 0 if it's an internal build.
duke@0 174 * @since 1.6
duke@0 175 */
duke@0 176 public static synchronized int jvmUpdateVersion() {
duke@0 177 if (!versionsInitialized) {
duke@0 178 initVersions();
duke@0 179 }
duke@0 180 return jvm_update_version;
duke@0 181 }
duke@0 182
duke@0 183 public static synchronized String jvmSpecialVersion() {
duke@0 184 if (!versionsInitialized) {
duke@0 185 initVersions();
duke@0 186 }
duke@0 187 if (jvm_special_version == null) {
duke@0 188 jvm_special_version = getJvmSpecialVersion();
duke@0 189 }
duke@0 190 return jvm_special_version;
duke@0 191 }
duke@0 192 public static native String getJvmSpecialVersion();
duke@0 193
duke@0 194 /**
duke@0 195 * Returns the build number of the running JVM if it's a RE build
duke@0 196 * It will return 0 if it's an internal build.
duke@0 197 * @since 1.6
duke@0 198 */
duke@0 199 public static synchronized int jvmBuildNumber() {
duke@0 200 if (!versionsInitialized) {
duke@0 201 initVersions();
duke@0 202 }
duke@0 203 return jvm_build_number;
duke@0 204 }
duke@0 205
duke@0 206 /**
duke@0 207 * Returns the major version of the running JDK.
duke@0 208 *
duke@0 209 * @since 1.6
duke@0 210 */
duke@0 211 public static synchronized int jdkMajorVersion() {
duke@0 212 if (!versionsInitialized) {
duke@0 213 initVersions();
duke@0 214 }
duke@0 215 return jdk_major_version;
duke@0 216 }
duke@0 217
duke@0 218 /**
duke@0 219 * Returns the minor version of the running JDK.
duke@0 220 * @since 1.6
duke@0 221 */
duke@0 222 public static synchronized int jdkMinorVersion() {
duke@0 223 if (!versionsInitialized) {
duke@0 224 initVersions();
duke@0 225 }
duke@0 226 return jdk_minor_version;
duke@0 227 }
duke@0 228
duke@0 229 /**
duke@0 230 * Returns the micro version of the running JDK.
duke@0 231 * @since 1.6
duke@0 232 */
duke@0 233 public static synchronized int jdkMicroVersion() {
duke@0 234 if (!versionsInitialized) {
duke@0 235 initVersions();
duke@0 236 }
duke@0 237 return jdk_micro_version;
duke@0 238 }
duke@0 239
duke@0 240 /**
duke@0 241 * Returns the update release version of the running JDK if it's
duke@0 242 * a RE build. It will return 0 if it's an internal build.
duke@0 243 * @since 1.6
duke@0 244 */
duke@0 245 public static synchronized int jdkUpdateVersion() {
duke@0 246 if (!versionsInitialized) {
duke@0 247 initVersions();
duke@0 248 }
duke@0 249 return jdk_update_version;
duke@0 250 }
duke@0 251
duke@0 252 public static synchronized String jdkSpecialVersion() {
duke@0 253 if (!versionsInitialized) {
duke@0 254 initVersions();
duke@0 255 }
duke@0 256 if (jdk_special_version == null) {
duke@0 257 jdk_special_version = getJdkSpecialVersion();
duke@0 258 }
duke@0 259 return jdk_special_version;
duke@0 260 }
duke@0 261 public static native String getJdkSpecialVersion();
duke@0 262
duke@0 263 /**
duke@0 264 * Returns the build number of the running JDK if it's a RE build
duke@0 265 * It will return 0 if it's an internal build.
duke@0 266 * @since 1.6
duke@0 267 */
duke@0 268 public static synchronized int jdkBuildNumber() {
duke@0 269 if (!versionsInitialized) {
duke@0 270 initVersions();
duke@0 271 }
duke@0 272 return jdk_build_number;
duke@0 273 }
duke@0 274
duke@0 275 // true if JVM exports the version info including the capabilities
duke@0 276 private static boolean jvmVersionInfoAvailable;
duke@0 277 private static synchronized void initVersions() {
duke@0 278 if (versionsInitialized) {
duke@0 279 return;
duke@0 280 }
duke@0 281 jvmVersionInfoAvailable = getJvmVersionInfo();
duke@0 282 if (!jvmVersionInfoAvailable) {
duke@0 283 // parse java.vm.version for older JVM before the
duke@0 284 // new JVM_GetVersionInfo is added.
duke@0 285 // valid format of the version string is:
duke@0 286 // n.n.n[_uu[c]][-<identifer>]-bxx
duke@0 287 CharSequence cs = System.getProperty("java.vm.version");
duke@0 288 if (cs.length() >= 5 &&
duke@0 289 Character.isDigit(cs.charAt(0)) && cs.charAt(1) == '.' &&
duke@0 290 Character.isDigit(cs.charAt(2)) && cs.charAt(3) == '.' &&
duke@0 291 Character.isDigit(cs.charAt(4))) {
duke@0 292 jvm_major_version = Character.digit(cs.charAt(0), 10);
duke@0 293 jvm_minor_version = Character.digit(cs.charAt(2), 10);
duke@0 294 jvm_micro_version = Character.digit(cs.charAt(4), 10);
duke@0 295 cs = cs.subSequence(5, cs.length());
duke@0 296 if (cs.charAt(0) == '_' && cs.length() >= 3 &&
duke@0 297 Character.isDigit(cs.charAt(1)) &&
duke@0 298 Character.isDigit(cs.charAt(2))) {
duke@0 299 int nextChar = 3;
duke@0 300 try {
duke@0 301 String uu = cs.subSequence(1, 3).toString();
duke@0 302 jvm_update_version = Integer.valueOf(uu).intValue();
duke@0 303 if (cs.length() >= 4) {
duke@0 304 char c = cs.charAt(3);
duke@0 305 if (c >= 'a' && c <= 'z') {
duke@0 306 jvm_special_version = Character.toString(c);
duke@0 307 nextChar++;
duke@0 308 }
duke@0 309 }
duke@0 310 } catch (NumberFormatException e) {
duke@0 311 // not conforming to the naming convention
duke@0 312 return;
duke@0 313 }
duke@0 314 cs = cs.subSequence(nextChar, cs.length());
duke@0 315 }
duke@0 316 if (cs.charAt(0) == '-') {
duke@0 317 // skip the first character
duke@0 318 // valid format: <identifier>-bxx or bxx
duke@0 319 // non-product VM will have -debug|-release appended
duke@0 320 cs = cs.subSequence(1, cs.length());
duke@0 321 String[] res = cs.toString().split("-");
duke@0 322 for (String s : res) {
duke@0 323 if (s.charAt(0) == 'b' && s.length() == 3 &&
duke@0 324 Character.isDigit(s.charAt(1)) &&
duke@0 325 Character.isDigit(s.charAt(2))) {
duke@0 326 jvm_build_number =
duke@0 327 Integer.valueOf(s.substring(1, 3)).intValue();
duke@0 328 break;
duke@0 329 }
duke@0 330 }
duke@0 331 }
duke@0 332 }
duke@0 333 }
duke@0 334 getJdkVersionInfo();
duke@0 335 versionsInitialized = true;
duke@0 336 }
duke@0 337
duke@0 338 // Gets the JVM version info if available and sets the jvm_*_version fields
duke@0 339 // and its capabilities.
duke@0 340 //
duke@0 341 // Return false if not available which implies an old VM (Tiger or before).
duke@0 342 private static native boolean getJvmVersionInfo();
duke@0 343 private static native void getJdkVersionInfo();
duke@0 344
alanb@6573 345 // Possible runtime profiles, ordered from small to large
alanb@6573 346 private final static String[] PROFILES = { "compact1", "compact2", "compact3" };
alanb@6573 347
alanb@6573 348 /**
alanb@6573 349 * Returns the name of the profile that this runtime implements. The empty
alanb@6573 350 * string is returned for the full Java Runtime.
alanb@6573 351 */
alanb@6573 352 public static String profileName() {
alanb@6573 353 return java_profile_name;
alanb@6573 354 }
alanb@6573 355
alanb@6573 356 /**
alanb@6573 357 * Indicates if this runtime implements the full Java Runtime.
alanb@6573 358 */
alanb@6573 359 public static boolean isFullJre() {
alanb@6573 360 return java_profile_name.length() == 0;
alanb@6573 361 }
alanb@6573 362
alanb@6573 363 // cached index of this profile's name in PROFILES (1-based)
alanb@6573 364 private static int thisRuntimeIndex;
alanb@6573 365
alanb@6573 366 /**
alanb@6573 367 * Indicates if this runtime supports the given profile. Profile names are
alanb@6573 368 * case sensitive.
alanb@6573 369 *
alanb@6573 370 * @return {@code true} if the given profile is supported
alanb@6573 371 */
alanb@6573 372 public static boolean supportsProfile(String requiredProfile) {
alanb@6573 373 int x = thisRuntimeIndex - 1;
alanb@6573 374 if (x < 0) {
alanb@6573 375 String profile = profileName();
alanb@6573 376 if (profile.length() > 0) {
alanb@6573 377 x = 0;
alanb@6573 378 while (x < PROFILES.length) {
alanb@6573 379 if (PROFILES[x].equals(profile))
alanb@6573 380 break;
alanb@6573 381 x++;
alanb@6573 382 }
alanb@6573 383 if (x >= PROFILES.length)
alanb@6573 384 throw new InternalError(profile + " not known to sun.misc.Version");
alanb@6573 385
alanb@6573 386 // okay if another thread has already set it
alanb@6573 387 thisRuntimeIndex = x + 1;
alanb@6573 388 }
alanb@6573 389 // else we are a full JRE
alanb@6573 390 }
alanb@6573 391
alanb@6573 392 int y = 0;
alanb@6573 393 while (y < PROFILES.length) {
alanb@6573 394 if (PROFILES[y].equals(requiredProfile))
alanb@6573 395 break;
alanb@6573 396 y++;
alanb@6573 397 }
alanb@6573 398 if (y >= PROFILES.length) {
alanb@6573 399 // profile not found so caller has requested something that is not defined
alanb@6573 400 return false;
alanb@6573 401 }
alanb@6573 402
alanb@6573 403 return x < 0 || x >= y;
alanb@6573 404 }
alanb@6573 405
duke@0 406 }
duke@0 407
duke@0 408 // Help Emacs a little because this file doesn't end in .java.
duke@0 409 //
duke@0 410 // Local Variables: ***
duke@0 411 // mode: java ***
duke@0 412 // End: ***