1 /*
2 * This file is part of dependency-check-core.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * Copyright (c) 2022 Jeremy Long. All Rights Reserved.
17 */
18 package org.owasp.dependencycheck.utils;
19
20 import org.apache.commons.lang3.StringUtils;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25 *
26 * @author Jeremy Long
27 */
28 public final class Utils {
29
30 /**
31 * The logger.
32 */
33 private static final Logger LOGGER = LoggerFactory.getLogger(Utils.class);
34
35 /**
36 * Empty constructor for utility class.
37 */
38 private Utils() {
39 }
40
41 /**
42 * Returns the Java major version as a whole number.
43 *
44 * @return the Java major version as a whole number
45 */
46 public static int getJavaVersion() {
47 String version = System.getProperty("java.specification.version");
48 if (version.startsWith("1.")) {
49 version = version.substring(2, 3);
50 } else {
51 final int dot = version.indexOf(".");
52 if (dot != -1) {
53 version = version.substring(0, dot);
54 }
55 }
56 return Integer.parseInt(version);
57 }
58
59 /**
60 * Returns the update version from the Java runtime.
61 *
62 * @return the update version
63 */
64 public static int getJavaUpdateVersion() {
65 //"1.8.0_144" "11.0.2+9" "17.0.8.1"
66 final String runtimeVersion = System.getProperty("java.version");
67 return parseUpdate(runtimeVersion);
68 }
69
70 /**
71 * Parses the update version from the runtime version.
72 *
73 * @param runtimeVersion the runtime version
74 * @return the update version
75 */
76 protected static int parseUpdate(String runtimeVersion) {
77 LOGGER.debug(runtimeVersion);
78 try {
79 final String[] parts = runtimeVersion.split("\\.");
80 if (parts.length == 4 && isNumeric(parts)) {
81 return Integer.parseInt(parts[2]);
82 }
83 int pos = runtimeVersion.indexOf('_');
84 if (pos <= 0) {
85 pos = runtimeVersion.lastIndexOf('.');
86 if (pos <= 0) {
87 //unexpected java version - return 0
88 return 0;
89 }
90 }
91 int end = runtimeVersion.indexOf('+', pos);
92 if (end < 0) {
93 end = runtimeVersion.indexOf('-', pos);
94 }
95 if (end > pos) {
96 return Integer.parseInt(runtimeVersion.substring(pos + 1, end));
97 }
98 return Integer.parseInt(runtimeVersion.substring(pos + 1));
99 } catch (NumberFormatException nfe) {
100 // If the update version is not available, return 0
101 return 0;
102 }
103 }
104
105 /**
106 * Determines if all parts of the string array are numeric.
107 *
108 * @param parts the strings to check
109 * @return true if all of the strings in the array are numeric; otherwise
110 * false
111 */
112 private static boolean isNumeric(String[] parts) {
113 for (String i : parts) {
114 if (!StringUtils.isNumeric(i)) {
115 return false;
116 }
117 }
118 return true;
119 }
120 }