Skip to content

Commit 785896c

Browse files
authored
enforce Java version (#4677)
current version of Lucene will fail with 22 or later
1 parent fc478df commit 785896c

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/index/Indexer.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2011, Jens Elkner.
2323
* Portions Copyright (c) 2017, 2020, Chris Fraire <cfraire@me.com>.
2424
*/
@@ -86,6 +86,8 @@
8686
import org.opengrok.indexer.util.OptionParser;
8787
import org.opengrok.indexer.util.Statistics;
8888

89+
import static org.opengrok.indexer.util.RuntimeUtil.checkJavaVersion;
90+
8991
/**
9092
* Creates and updates an inverted source index as well as generates Xref, file
9193
* stats etc., if specified in the options.
@@ -371,7 +373,9 @@ public static void main(String[] argv) {
371373
}
372374

373375
LOGGER.log(Level.INFO, "Indexer version {0} ({1}) running on Java {2}",
374-
new Object[]{Info.getVersion(), Info.getRevision(), System.getProperty("java.version")});
376+
new Object[]{Info.getVersion(), Info.getRevision(), Runtime.version()});
377+
378+
checkJavaVersion();
375379

376380
// Create history cache first.
377381
if (searchRepositories) {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
22+
*/
23+
package org.opengrok.indexer.util;
24+
25+
public class RuntimeUtil {
26+
private RuntimeUtil() {
27+
// private for static
28+
}
29+
30+
/*
31+
* interval of supported Java versions
32+
*/
33+
static final int JAVA_VERSION_MIN = 11;
34+
static final int JAVA_VERSION_MAX = 21;
35+
36+
/**
37+
* @throws RuntimeException if the Java runtime version is outside
38+
* {@link #JAVA_VERSION_MIN} and {@link #JAVA_VERSION_MAX}.
39+
*/
40+
public static void checkJavaVersion() throws RuntimeException {
41+
Runtime.Version javaVersion = Runtime.version();
42+
int majorVersion = javaVersion.version().get(0);
43+
if (majorVersion < JAVA_VERSION_MIN || majorVersion > JAVA_VERSION_MAX) {
44+
throw new RuntimeException(String.format("unsupported Java version %d [%d,%d)",
45+
majorVersion, JAVA_VERSION_MIN, JAVA_VERSION_MAX));
46+
}
47+
}
48+
}

opengrok-web/src/main/java/org/opengrok/web/WebappListener.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2007, 2023, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2007, 2024, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2018, 2019, Chris Fraire <cfraire@me.com>.
2323
*/
2424
package org.opengrok.web;
@@ -55,6 +55,8 @@
5555
import java.util.logging.Level;
5656
import java.util.logging.Logger;
5757

58+
import static org.opengrok.indexer.util.RuntimeUtil.checkJavaVersion;
59+
5860
/**
5961
* Initialize webapp context.
6062
*
@@ -77,8 +79,10 @@ public void contextInitialized(final ServletContextEvent servletContextEvent) {
7779
ServletContext context = servletContextEvent.getServletContext();
7880
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
7981

80-
LOGGER.log(Level.INFO, "Starting webapp with version {0} ({1})",
81-
new Object[]{Info.getVersion(), Info.getRevision()});
82+
LOGGER.log(Level.INFO, "Starting webapp with version {0} ({1}) on Java {2}",
83+
new Object[]{Info.getVersion(), Info.getRevision(), Runtime.version()});
84+
85+
checkJavaVersion();
8286

8387
String configPath = Optional.ofNullable(context.getInitParameter("CONFIGURATION"))
8488
.orElseThrow(() -> new WebappError("CONFIGURATION parameter missing in the web.xml file"));

0 commit comments

Comments
 (0)