Skip to content

Commit

Permalink
enforce Java version (#4677)
Browse files Browse the repository at this point in the history
current version of Lucene will fail with 22 or later
  • Loading branch information
vladak authored Oct 17, 2024
1 parent fc478df commit 785896c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>.
*/
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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 = 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);
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));
}
}
}
10 changes: 7 additions & 3 deletions opengrok-web/src/main/java/org/opengrok/web/WebappListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>.
*/
package org.opengrok.web;
Expand Down Expand Up @@ -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.
*
Expand All @@ -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"));
Expand Down

0 comments on commit 785896c

Please sign in to comment.