1 /*
   2  * Copyright (c) 1999, 2017, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.io.PrintStream;
  29 import java.util.Arrays;
  30 import java.util.List;
  31 import java.util.Optional;
  32 
  33 class VersionProps {
  34 
  35 
  36     private static final String launcher_name =
  37         "@@LAUNCHER_NAME@@";
  38 
  39     private static final String java_version =
  40         "@@VERSION_SHORT@@";
  41 
  42     private static final String java_runtime_name =
  43         "@@RUNTIME_NAME@@";
  44 
  45     private static final String java_runtime_version =
  46         "@@VERSION_STRING@@";
  47 
  48     private static final String VERSION_NUMBER =
  49         "@@VERSION_NUMBER@@";
  50 
  51     private static final String VERSION_BUILD =
  52         "@@VERSION_BUILD@@";
  53 
  54     private static final String VERSION_PRE =
  55         "@@VERSION_PRE@@";
  56 
  57     private static final String VERSION_OPT =
  58         "@@VERSION_OPT@@";
  59 
  60     static {
  61         init();
  62     }
  63 
  64     public static void init() {
  65         System.setProperty("java.version", java_version);
  66         System.setProperty("java.runtime.version", java_runtime_version);
  67         System.setProperty("java.runtime.name", java_runtime_name);
  68     }
  69 
  70     private static int parseVersionNumber(String version, int prevIndex, int index) {
  71         if (index - prevIndex > 1 &&
  72             Character.digit(version.charAt(prevIndex), 10) <= 0)
  73             throw new IllegalArgumentException("Leading zeros not supported (" +
  74                     version.substring(prevIndex, index) + ")");
  75         return Integer.parseInt(version, prevIndex, index, 10);
  76     }
  77 
  78     // This method is reflectively used by regression tests.
  79     static List<Integer> parseVersionNumbers(String version) {
  80         // Let's find the size of an array required to hold $VNUM components
  81         int size = 0;
  82         int prevIndex = 0;
  83         do {
  84             prevIndex = version.indexOf('.', prevIndex) + 1;
  85             size++;
  86         } while (prevIndex > 0);
  87         Integer[] verNumbers = new Integer[size];
  88 
  89         // Fill in the array with $VNUM components
  90         int n = 0;
  91         prevIndex = 0;
  92         int index = version.indexOf('.');
  93         while (index > -1) {
  94             verNumbers[n] = parseVersionNumber(version, prevIndex, index);
  95             prevIndex = index + 1; // Skip the period
  96             index = version.indexOf('.', prevIndex);
  97             n++;
  98         }
  99         verNumbers[n] = parseVersionNumber(version, prevIndex, version.length());
 100 
 101         if (verNumbers[0] == 0 || verNumbers[n] == 0)
 102             throw new IllegalArgumentException("Leading/trailing zeros not allowed (" +
 103                     Arrays.toString(verNumbers) + ")");
 104 
 105         return List.of(verNumbers);
 106     }
 107 
 108     static List<Integer> versionNumbers() {
 109         return parseVersionNumbers(VERSION_NUMBER);
 110     }
 111 
 112     static Optional<String> pre() {
 113         return optionalOf(VERSION_PRE);
 114     }
 115 
 116     static Optional<Integer> build() {
 117         return VERSION_BUILD.isEmpty() ?
 118                 Optional.empty() :
 119                 Optional.of(Integer.parseInt(VERSION_BUILD));
 120     }
 121 
 122     static Optional<String> optional() {
 123         return optionalOf(VERSION_OPT);
 124     }
 125 
 126     // Treat empty strings as value not being present
 127     private static Optional<String> optionalOf(String value) {
 128         if (!value.isEmpty()) {
 129             return Optional.of(value);
 130         } else {
 131             return Optional.empty();
 132         }
 133     }
 134 
 135     /**
 136      * In case you were wondering this method is called by java -version.
 137      */
 138     public static void print(boolean err) {
 139         print(err, false);
 140     }
 141 
 142     /**
 143      * This is the same as print except that it adds an extra line-feed
 144      * at the end, typically used by the -showversion in the launcher
 145      */
 146     public static void println(boolean err) {
 147         print(err, true);
 148     }
 149 
 150     /**
 151      * Print version info.
 152      */
 153     private static void print(boolean err, boolean newln) {
 154         boolean isHeadless = false;
 155         PrintStream ps = err ? System.err : System.out;
 156 
 157         /* Report that we're running headless if the property is true */
 158         String headless = System.getProperty("java.awt.headless");
 159         if ( (headless != null) && (headless.equalsIgnoreCase("true")) ) {
 160             isHeadless = true;
 161         }
 162 
 163         /* First line: platform version. */
 164         if (err) {
 165             ps.println(launcher_name + " version \"" + java_version + "\"");
 166         } else {
 167             /* Use a format more in line with GNU conventions */
 168             ps.println(launcher_name + " " + java_version);
 169         }
 170 
 171         /* Second line: runtime version (ie, libraries). */
 172         String jdk_debug_level = System.getProperty("jdk.debug", "release");
 173         /* Debug level is not printed for "release" builds */
 174         if ("release".equals(jdk_debug_level)) {
 175             jdk_debug_level = "";
 176         } else {
 177             jdk_debug_level = jdk_debug_level + " ";
 178         }
 179 
 180         ps.print(java_runtime_name + " (" + jdk_debug_level + "build " + java_runtime_version);
 181 
 182         ps.println(')');
 183 
 184         /* Third line: JVM information. */
 185         String java_vm_name    = System.getProperty("java.vm.name");
 186         String java_vm_version = System.getProperty("java.vm.version");
 187         String java_vm_info    = System.getProperty("java.vm.info");
 188         ps.println(java_vm_name + " (" + jdk_debug_level + "build " + java_vm_version + ", " +
 189                    java_vm_info + ")");
 190     }
 191 
 192 }