mirror of https://github.com/apache/nifi.git
NIFI-7687 Improved JVM version handling in RunNiFi and OSUtils.
Added unit tests. Signed-off-by: Pierre Villard <pierre.villard.fr@gmail.com> This closes #4442.
This commit is contained in:
parent
0cff54097e
commit
ac60bf6507
|
@ -16,13 +16,6 @@
|
|||
*/
|
||||
package org.apache.nifi.bootstrap;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.nifi.bootstrap.notification.NotificationType;
|
||||
import org.apache.nifi.bootstrap.util.OSUtils;
|
||||
import org.apache.nifi.util.file.FileUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
|
@ -67,6 +60,12 @@ import java.util.concurrent.TimeUnit;
|
|||
import java.util.concurrent.locks.Condition;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.nifi.bootstrap.notification.NotificationType;
|
||||
import org.apache.nifi.bootstrap.util.OSUtils;
|
||||
import org.apache.nifi.util.file.FileUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
@ -163,7 +162,7 @@ public class RunNiFi {
|
|||
System.out.println("java org.apache.nifi.bootstrap.RunNiFi <command> [options]");
|
||||
System.out.println();
|
||||
System.out.println("Valid commands include:");
|
||||
System.out.println("");
|
||||
System.out.println();
|
||||
System.out.println("Start : Start a new instance of Apache NiFi");
|
||||
System.out.println("Stop : Stop a running instance of Apache NiFi");
|
||||
System.out.println("Restart : Stop Apache NiFi, if it is running, and then start a new instance");
|
||||
|
@ -1037,7 +1036,8 @@ public class RunNiFi {
|
|||
|
||||
String runtimeJavaVersion = System.getProperty("java.version");
|
||||
defaultLogger.info("Runtime Java version: {}", runtimeJavaVersion);
|
||||
if (Integer.parseInt(runtimeJavaVersion.substring(0, runtimeJavaVersion.indexOf('.'))) >= 11) {
|
||||
int javaMajorVersion = OSUtils.parseJavaVersion(runtimeJavaVersion);
|
||||
if (javaMajorVersion >= 11) {
|
||||
/* If running on Java 11 or greater, add the JAXB/activation/annotation libs to the classpath.
|
||||
*
|
||||
* TODO: Once the minimum Java version requirement of NiFi is 11, this processing should be removed.
|
||||
|
@ -1091,7 +1091,7 @@ public class RunNiFi {
|
|||
cmd.add("-Dnifi.bootstrap.listen.port=" + listenPort);
|
||||
cmd.add("-Dapp=NiFi");
|
||||
cmd.add("-Dorg.apache.nifi.bootstrap.config.log.dir=" + nifiLogDir);
|
||||
if (runtimeJavaVersion.startsWith("9") || runtimeJavaVersion.startsWith("10")) {
|
||||
if (javaMajorVersion == 9 || javaMajorVersion == 10) {
|
||||
// running on Java 9 or 10, internal module java.xml.bind module must be made available
|
||||
cmd.add("--add-modules=java.xml.bind");
|
||||
}
|
||||
|
|
|
@ -17,14 +17,15 @@
|
|||
|
||||
package org.apache.nifi.bootstrap.util;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import com.sun.jna.Pointer;
|
||||
import com.sun.jna.platform.win32.Kernel32;
|
||||
import com.sun.jna.platform.win32.WinNT;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
/**
|
||||
* OS specific utilities with generic method interfaces
|
||||
|
@ -132,4 +133,38 @@ public final class OSUtils {
|
|||
return pid;
|
||||
}
|
||||
|
||||
// The two Java version methods are copied from CertificateUtils in nifi-commons/nifi-security-utils
|
||||
|
||||
/**
|
||||
* Returns the JVM Java major version based on the System properties (e.g. {@code JVM 1.8.0.231} -> {code 8}).
|
||||
*
|
||||
* @return the Java major version
|
||||
*/
|
||||
public static int getJavaVersion() {
|
||||
String version = System.getProperty("java.version");
|
||||
return parseJavaVersion(version);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the major version parsed from the provided Java version string (e.g. {@code "1.8.0.231"} -> {@code 8}).
|
||||
*
|
||||
* @param version the Java version string
|
||||
* @return the major version as an int
|
||||
*/
|
||||
public static int parseJavaVersion(String version) {
|
||||
String majorVersion;
|
||||
if (version.startsWith("1.")) {
|
||||
majorVersion = version.substring(2, 3);
|
||||
} else {
|
||||
Pattern majorVersion9PlusPattern = Pattern.compile("(\\d+).*");
|
||||
Matcher m = majorVersion9PlusPattern.matcher(version);
|
||||
if (m.find()) {
|
||||
majorVersion = m.group(1);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Could not detect major version of " + version);
|
||||
}
|
||||
}
|
||||
return Integer.parseInt(majorVersion);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,87 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.nifi.bootstrap.util
|
||||
|
||||
|
||||
import org.junit.After
|
||||
import org.junit.AfterClass
|
||||
import org.junit.Before
|
||||
import org.junit.BeforeClass
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.junit.runners.JUnit4
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
@RunWith(JUnit4.class)
|
||||
class OSUtilsTest extends GroovyTestCase {
|
||||
private static final Logger logger = LoggerFactory.getLogger(OSUtilsTest.class)
|
||||
|
||||
@BeforeClass
|
||||
static void setUpOnce() throws Exception {
|
||||
logger.metaClass.methodMissing = { String name, args ->
|
||||
logger.info("[${name?.toUpperCase()}] ${(args as List).join(" ")}")
|
||||
}
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
static void tearDownOnce() throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@Before
|
||||
void setUp() throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@After
|
||||
void tearDown() throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void testShouldParseJavaMajorVersion8Below() {
|
||||
// Arrange
|
||||
def possibleVersionStrings = ["1.8", "1.8.0.0", "1.8.0_262"]
|
||||
|
||||
// Act
|
||||
def results = possibleVersionStrings.collect {
|
||||
OSUtils.parseJavaVersion(it)
|
||||
}
|
||||
logger.info("Parsed Java versions: ${results}")
|
||||
|
||||
// Assert
|
||||
assert results.every { it == 8 }
|
||||
}
|
||||
|
||||
@Test
|
||||
void testShouldParseJavaMajorVersion9Plus() {
|
||||
// Arrange
|
||||
def possibleVersionStrings = [
|
||||
"11.0.6", "11.0.0", "11.12.13", "11"
|
||||
]
|
||||
|
||||
// Act
|
||||
def results = possibleVersionStrings.collect {
|
||||
OSUtils.parseJavaVersion(it)
|
||||
}
|
||||
logger.info("Parsed Java versions: ${results}")
|
||||
|
||||
// Assert
|
||||
assert results.every { it == 11 }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue