From 3d3c34de93b5b1378f21eab02fe01c554b4b6c0f Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Wed, 16 Oct 2024 16:27:32 +0200 Subject: [PATCH 1/3] enforce Java version --- .../org/opengrok/indexer/index/Indexer.java | 8 +++- .../opengrok/indexer/util/RuntimeUtil.java | 44 +++++++++++++++++++ .../java/org/opengrok/web/WebappListener.java | 10 +++-- 3 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 opengrok-indexer/src/main/java/org/opengrok/indexer/util/RuntimeUtil.java diff --git a/opengrok-indexer/src/main/java/org/opengrok/indexer/index/Indexer.java b/opengrok-indexer/src/main/java/org/opengrok/indexer/index/Indexer.java index 435e098f57e..c235674f9a4 100644 --- a/opengrok-indexer/src/main/java/org/opengrok/indexer/index/Indexer.java +++ b/opengrok-indexer/src/main/java/org/opengrok/indexer/index/Indexer.java @@ -18,7 +18,7 @@ */ /* - * Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved. * Portions Copyright (c) 2011, Jens Elkner. * Portions Copyright (c) 2017, 2020, Chris Fraire . */ @@ -86,6 +86,8 @@ import org.opengrok.indexer.util.OptionParser; import org.opengrok.indexer.util.Statistics; +import static org.opengrok.indexer.util.RuntimeUtil.checkJavaVersion; + /** * Creates and updates an inverted source index as well as generates Xref, file * stats etc., if specified in the options. @@ -371,7 +373,9 @@ public static void main(String[] argv) { } LOGGER.log(Level.INFO, "Indexer version {0} ({1}) running on Java {2}", - new Object[]{Info.getVersion(), Info.getRevision(), System.getProperty("java.version")}); + new Object[]{Info.getVersion(), Info.getRevision(), Runtime.version()}); + + checkJavaVersion(); // Create history cache first. if (searchRepositories) { diff --git a/opengrok-indexer/src/main/java/org/opengrok/indexer/util/RuntimeUtil.java b/opengrok-indexer/src/main/java/org/opengrok/indexer/util/RuntimeUtil.java new file mode 100644 index 00000000000..3618052642b --- /dev/null +++ b/opengrok-indexer/src/main/java/org/opengrok/indexer/util/RuntimeUtil.java @@ -0,0 +1,44 @@ +/* + * CDDL HEADER START + * + * The contents of this file are subject to the terms of the + * Common Development and Distribution License (the "License"). + * You may not use this file except in compliance with the License. + * + * See LICENSE.txt included in this distribution for the specific + * language governing permissions and limitations under the License. + * + * When distributing Covered Code, include this CDDL HEADER in each + * file and include the License file at LICENSE.txt. + * If applicable, add the following below this CDDL HEADER, with the + * fields enclosed by brackets "[]" replaced with your own identifying + * information: Portions Copyright [yyyy] [name of copyright owner] + * + * CDDL HEADER END + */ + +/* + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. + */ +package org.opengrok.indexer.util; + +public class RuntimeUtil { + private RuntimeUtil() { + // private for static + } + + /* + * interval of supported Java versions + */ + static final int JAVA_VERSION_MIN = 11; + static final int JAVA_VERSION_MAX = 17; + + public static void checkJavaVersion() throws RuntimeException { + Runtime.Version javaVersion = Runtime.version(); + int majorVersion = javaVersion.version().get(0); + if (majorVersion < JAVA_VERSION_MIN || majorVersion > JAVA_VERSION_MAX) { + throw new RuntimeException(String.format("unsupported Java version %d [%d,%d)", + majorVersion, JAVA_VERSION_MIN, JAVA_VERSION_MAX)); + } + } +} diff --git a/opengrok-web/src/main/java/org/opengrok/web/WebappListener.java b/opengrok-web/src/main/java/org/opengrok/web/WebappListener.java index 114849bb2e0..7c806d9c5e4 100644 --- a/opengrok-web/src/main/java/org/opengrok/web/WebappListener.java +++ b/opengrok-web/src/main/java/org/opengrok/web/WebappListener.java @@ -18,7 +18,7 @@ */ /* - * Copyright (c) 2007, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2007, 2024, Oracle and/or its affiliates. All rights reserved. * Portions Copyright (c) 2018, 2019, Chris Fraire . */ package org.opengrok.web; @@ -55,6 +55,8 @@ import java.util.logging.Level; import java.util.logging.Logger; +import static org.opengrok.indexer.util.RuntimeUtil.checkJavaVersion; + /** * Initialize webapp context. * @@ -77,8 +79,10 @@ public void contextInitialized(final ServletContextEvent servletContextEvent) { ServletContext context = servletContextEvent.getServletContext(); RuntimeEnvironment env = RuntimeEnvironment.getInstance(); - LOGGER.log(Level.INFO, "Starting webapp with version {0} ({1})", - new Object[]{Info.getVersion(), Info.getRevision()}); + LOGGER.log(Level.INFO, "Starting webapp with version {0} ({1}) on Java {2}", + new Object[]{Info.getVersion(), Info.getRevision(), Runtime.version()}); + + checkJavaVersion(); String configPath = Optional.ofNullable(context.getInitParameter("CONFIGURATION")) .orElseThrow(() -> new WebappError("CONFIGURATION parameter missing in the web.xml file")); From f9c1be446ca60c35fad6236650a05a37526ec805 Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Wed, 16 Oct 2024 18:04:40 +0200 Subject: [PATCH 2/3] be less strict current version of Lucene will fail with 22 or later --- .../src/main/java/org/opengrok/indexer/util/RuntimeUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opengrok-indexer/src/main/java/org/opengrok/indexer/util/RuntimeUtil.java b/opengrok-indexer/src/main/java/org/opengrok/indexer/util/RuntimeUtil.java index 3618052642b..99ff06125b0 100644 --- a/opengrok-indexer/src/main/java/org/opengrok/indexer/util/RuntimeUtil.java +++ b/opengrok-indexer/src/main/java/org/opengrok/indexer/util/RuntimeUtil.java @@ -31,7 +31,7 @@ private RuntimeUtil() { * interval of supported Java versions */ static final int JAVA_VERSION_MIN = 11; - static final int JAVA_VERSION_MAX = 17; + static final int JAVA_VERSION_MAX = 21; public static void checkJavaVersion() throws RuntimeException { Runtime.Version javaVersion = Runtime.version(); From 2828ac7d0886ef1aef8ad83ab2f63b07593eaca3 Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Wed, 16 Oct 2024 18:19:34 +0200 Subject: [PATCH 3/3] add javadoc --- .../src/main/java/org/opengrok/indexer/util/RuntimeUtil.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/opengrok-indexer/src/main/java/org/opengrok/indexer/util/RuntimeUtil.java b/opengrok-indexer/src/main/java/org/opengrok/indexer/util/RuntimeUtil.java index 99ff06125b0..3669e47d624 100644 --- a/opengrok-indexer/src/main/java/org/opengrok/indexer/util/RuntimeUtil.java +++ b/opengrok-indexer/src/main/java/org/opengrok/indexer/util/RuntimeUtil.java @@ -33,6 +33,10 @@ private RuntimeUtil() { static final int JAVA_VERSION_MIN = 11; static final int JAVA_VERSION_MAX = 21; + /** + * @throws RuntimeException if the Java runtime version is outside + * {@link #JAVA_VERSION_MIN} and {@link #JAVA_VERSION_MAX}. + */ public static void checkJavaVersion() throws RuntimeException { Runtime.Version javaVersion = Runtime.version(); int majorVersion = javaVersion.version().get(0);