Skip to content

Commit

Permalink
Move JvmOptionParser to separate project (#13657)
Browse files Browse the repository at this point in the history
This commit moves the JvmOptionParser into its own gradle project.

This enables the JvmOptionParser to remain compatible with Java 1.8 to present a helpful error message to a user attempting to start Logstash using older versions of Java, while allowing the main Logstash code base to freely use idiomatic Java 11 features.
  • Loading branch information
robbavey authored May 18, 2022
1 parent 06bca01 commit 2c7f14d
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 7 deletions.
17 changes: 17 additions & 0 deletions NOTICE.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -8780,6 +8780,23 @@ Eclipse Public License - v 2.0

You may add additional accurate notices of copyright ownership.
==========
Notice for: org.logstash:jvm-options-parser-8.3.0
----------

Copyright (c) 2022 Elasticsearch B.V. <http://www.elastic.co>

Licensed 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.
==========
Notice for: org.reflections:reflections-0.9.11
----------

Expand Down
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ plugins {
id "com.dorongold.task-tree" version "2.1.0"
}


apply plugin: 'de.undercouch.download'
apply from: "rubyUtils.gradle"

Expand All @@ -51,8 +52,8 @@ allprojects {
apply plugin: 'idea'
apply plugin: 'java-library'

project.sourceCompatibility = JavaVersion.VERSION_1_8
project.targetCompatibility = JavaVersion.VERSION_1_8
project.sourceCompatibility = JavaVersion.VERSION_11
project.targetCompatibility = JavaVersion.VERSION_11

tasks.withType(JavaCompile).configureEach {
options.compilerArgs.add("-Xlint:all")
Expand Down
1 change: 1 addition & 0 deletions logstash-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ dependencies {
api(files("../vendor/jruby/lib/jruby.jar") { // jruby-core.jar
builtBy ':downloadAndInstallJRuby'
}) { because "DEPENDENCY: org.jruby:jruby-core:${jrubyVersion}" } // meta-data for generateLicenseReport
implementation project(':jvm-options-parser')
implementation "org.apache.logging.log4j:log4j-api:${log4jVersion}"
annotationProcessor "org.apache.logging.log4j:log4j-core:${log4jVersion}"
api "org.apache.logging.log4j:log4j-core:${log4jVersion}"
Expand Down
3 changes: 2 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
rootProject.name = "logstash"

include ':logstash-core', 'logstash-core-benchmarks', 'ingest-converter', 'benchmark-cli', 'logstash-integration-tests', 'dependencies-report'
include ':logstash-core', 'logstash-core-benchmarks', 'ingest-converter', 'benchmark-cli', 'jvm-options-parser', 'logstash-integration-tests', 'dependencies-report'
project(':logstash-core').projectDir = new File('./logstash-core')
project(':logstash-core-benchmarks').projectDir = new File('./logstash-core/benchmarks')
project(':logstash-integration-tests').projectDir = new File('./qa/integration')
project(':ingest-converter').projectDir = new File('./tools/ingest-converter')
project(':benchmark-cli').projectDir = new File('./tools/benchmark-cli')
project(':dependencies-report').projectDir = new File('./tools/dependencies-report')
project(':jvm-options-parser').projectDir = new File('./tools/jvm-options-parser')

Boolean oss = System.getenv('OSS').equals('true')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ dependency,dependencyUrl,licenseOverride,copyright,sourceURL
"org.eclipse.text:org.eclipse.text:",http://www.eclipse.org/jdt,EPL-1.0
"org.javassist:javassist:",https://github.com/jboss-javassist/javassist,Apache-2.0
"org.jruby:jruby-core:",http://jruby.org/,EPL-2.0
"org.logstash:jvm-options-parser:",http://github.com/elastic/logstash,Apache-2.0
"org.reflections:reflections:",https://github.com/ronmamo/reflections,BSD-2-Clause
"org.slf4j:slf4j-api:",http://www.slf4j.org/,MIT
"paquet:",https://github.com/elastic/logstash,Apache-2.0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright (c) 2022 Elasticsearch B.V. <http://www.elastic.co>

Licensed 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.
2 changes: 2 additions & 0 deletions tools/jvm-options-parser/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Standalone jar for JvmOptionsParser to enable it to run under Java 8 to provide
helpful fail fast message when logstash is run older versions of Java.
43 changes: 43 additions & 0 deletions tools/jvm-options-parser/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. 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.
*/


description = """Logstash JVM options parser"""
version = versionMap['logstash-core']

repositories {
mavenCentral()
}

buildscript {
repositories {
mavenCentral()
}
}

project.sourceCompatibility = JavaVersion.VERSION_1_8
project.targetCompatibility = JavaVersion.VERSION_1_8

dependencies {
testImplementation "junit:junit:4.12"
}

javadoc {
enabled = false
}
2 changes: 2 additions & 0 deletions tools/jvm-options-parser/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
isDistributedArtifact=false

Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. 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.logstash.launchers;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* Helper class to compare current version of JVM with a target version.
* Based on JavaVersion class from elasticsearch java version checker tool
*/
public class JavaVersion implements Comparable<JavaVersion> {

public static final JavaVersion CURRENT = parse(System.getProperty("java.specification.version"));
public static final JavaVersion JAVA_11 = parse("11");
private final List<Integer> version;

private JavaVersion(List<Integer> version){
this.version = version;
}

static JavaVersion parse(final String value) {
if (value.matches("^0*[0-9]+(\\.[0-9]+)*$") == false) {
throw new IllegalArgumentException(value);
}

final List<Integer> version = new ArrayList<Integer>();
final String[] components = value.split("\\.");
for (final String component : components) {
version.add(Integer.valueOf(component));
}
return new JavaVersion(version);
}

public static int majorVersion(final JavaVersion javaVersion) {
Objects.requireNonNull(javaVersion);
if (javaVersion.version.get(0) > 1) {
return javaVersion.version.get(0);
} else {
return javaVersion.version.get(1);
}
}

private static int compare(final JavaVersion leftVersion, final JavaVersion rightVersion) {
List<Integer> left = leftVersion.version;
List<Integer> right = rightVersion.version;
// lexicographically compare two lists, treating missing entries as zeros
final int len = Math.max(left.size(), right.size());
for (int i = 0; i < len; i++) {
final int l = (i < left.size()) ? left.get(i) : 0;
final int r = (i < right.size()) ? right.get(i) : 0;
if (l < r) {
return -1;
}
if (r < l) {
return 1;
}
}
return 0;
}

@Override
public int compareTo(JavaVersion other) {
return compare(this, other);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
package org.logstash.launchers;
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. 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.
*/

import org.logstash.util.JavaVersion;
package org.logstash.launchers;

import java.io.BufferedReader;
import java.io.IOException;
Expand Down Expand Up @@ -106,8 +123,6 @@ static void bailOnOldJava(){
System.exit(1);
}
}


static void handleJvmOptions(String[] args, String lsJavaOpts) {
final JvmOptionsParser parser = new JvmOptionsParser(args[0]);
final String jvmOpts = args.length == 2 ? args[1] : null;
Expand Down

0 comments on commit 2c7f14d

Please sign in to comment.