Skip to content

Commit

Permalink
fix: Show Java version from OpenJ9 Java runtimes (starship#507)
Browse files Browse the repository at this point in the history
This PR tries to improve the version detection across multiple Java VM vendors. The module captures both STDOUT and STDERR outputs of the java -Xinternalversion call.

Eclipse OpenJ9, Azul Zulu, SapMachine, Amazon Corretto and GraalVM outputs are unit tested now.
  • Loading branch information
yuri1969 authored and matchai committed Oct 25, 2019
1 parent edf5176 commit b7762a3
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 19 deletions.
37 changes: 37 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ sysinfo = "0.9.5"
byte-unit = "3.0.3"
starship_module_config_derive = { version = "0.1.0", path = "starship_module_config_derive" }
yaml-rust = "0.4"
nom = "5.0.1"

[dev-dependencies]
tempfile = "3.1.0"
Expand Down
73 changes: 54 additions & 19 deletions src/modules/java.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use std::process::Command;
use std::process::Output;

use ansi_term::Color;

use super::{Context, Module};

use crate::modules::utils::java_version_parser;

/// Creates a module with the current Java version
///
/// Will display the Java version if any of the following criteria are met:
Expand Down Expand Up @@ -47,27 +50,24 @@ fn get_java_version() -> Option<String> {
};

match Command::new(java_command).arg("-Xinternalversion").output() {
Ok(output) => Some(String::from_utf8(output.stdout).unwrap()),
Ok(output) => Some(combine_outputs(output)),
Err(_) => None,
}
}

/// Extract the java version from `java_stdout`.
/// The expected format is similar to: "JRE (1.8.0_222-b10)".
/// Some Java vendors don't follow this format: "JRE (Zulu 8.40.0.25-CA-linux64)").
fn format_java_version(java_stdout: String) -> Option<String> {
let start = java_stdout.find("JRE (")? + "JRE (".len();
let end = start
+ (java_stdout[start..].find(|c| match c {
'0'..='9' | '.' => false,
_ => true,
})?);

if start == end {
None
} else {
Some(format!("v{}", &java_stdout[start..end]))
}
/// Combines the standard and error outputs.
///
/// This is due some Java vendors using `STDERR` as the output.
fn combine_outputs(output: Output) -> String {
let std_out = String::from_utf8(output.stdout).unwrap();
let std_err = String::from_utf8(output.stderr).unwrap();

format!("{}{}", std_out, std_err)
}

/// Extract the java version from `java_out`.
fn format_java_version(java_out: String) -> Option<String> {
java_version_parser::parse_jre_version(&java_out).map(|result| format!("v{}", result))
}

#[cfg(test)]
Expand Down Expand Up @@ -98,8 +98,43 @@ mod tests {

#[test]
fn test_format_java_version_zulu() {
// Not currently supported
let java_8 = String::from("OpenJDK 64-Bit Server VM (25.222-b10) for linux-amd64 JRE (Zulu 8.40.0.25-CA-linux64) (1.8.0_222-b10), built on Jul 11 2019 11:36:39 by \"zulu_re\" with gcc 4.4.7 20120313 (Red Hat 4.4.7-3)");
assert_eq!(format_java_version(java_8), None);
let java_11 = String::from("OpenJDK 64-Bit Server VM (11.0.4+11-LTS) for linux-amd64 JRE (Zulu11.33+15-CA) (11.0.4+11-LTS), built on Jul 11 2019 21:37:17 by \"zulu_re\" with gcc 4.9.2 20150212 (Red Hat 4.9.2-6)");
assert_eq!(format_java_version(java_8), Some(String::from("v1.8.0")));
assert_eq!(format_java_version(java_11), Some(String::from("v11.0.4")));
}

#[test]
fn test_format_java_version_eclipse_openj9() {
let java_8 = String::from("Eclipse OpenJ9 OpenJDK 64-bit Server VM (1.8.0_222-b10) from linux-amd64 JRE with Extensions for OpenJDK for Eclipse OpenJ9 8.0.222.0, built on Jul 17 2019 21:29:18 by jenkins with g++ (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5)");
let java_11 = String::from("Eclipse OpenJ9 OpenJDK 64-bit Server VM (11.0.4+11) from linux-amd64 JRE with Extensions for OpenJDK for Eclipse OpenJ9 11.0.4.0, built on Jul 17 2019 21:51:37 by jenkins with g++ (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5)");
assert_eq!(format_java_version(java_8), Some(String::from("v1.8.0")));
assert_eq!(format_java_version(java_11), Some(String::from("v11.0.4")));
}

#[test]
fn test_format_java_version_graalvm() {
let java_8 = String::from("OpenJDK 64-Bit GraalVM CE 19.2.0.1 (25.222-b08-jvmci-19.2-b02) for linux-amd64 JRE (8u222), built on Jul 19 2019 17:37:13 by \"buildslave\" with gcc 7.3.0");
assert_eq!(format_java_version(java_8), Some(String::from("v8")));
}

#[test]
fn test_format_java_version_amazon_corretto() {
let java_8 = String::from("OpenJDK 64-Bit Server VM (25.222-b10) for linux-amd64 JRE (1.8.0_222-b10), built on Jul 11 2019 20:48:53 by \"root\" with gcc 7.3.1 20180303 (Red Hat 7.3.1-5)");
let java_11 = String::from("OpenJDK 64-Bit Server VM (11.0.4+11-LTS) for linux-amd64 JRE (11.0.4+11-LTS), built on Jul 11 2019 20:06:11 by \"\" with gcc 7.3.1 20180303 (Red Hat 7.3.1-5)");
assert_eq!(format_java_version(java_8), Some(String::from("v1.8.0")));
assert_eq!(format_java_version(java_11), Some(String::from("v11.0.4")));
}

#[test]
fn test_format_java_version_sapmachine() {
let java_11 = String::from("OpenJDK 64-Bit Server VM (11.0.4+11-LTS-sapmachine) for linux-amd64 JRE (11.0.4+11-LTS-sapmachine), built on Jul 17 2019 08:58:43 by \"\" with gcc 7.3.0");
assert_eq!(format_java_version(java_11), Some(String::from("v11.0.4")));
}

#[test]
fn test_format_java_version_unknown() {
let unknown_jre = String::from("Unknown JRE");
assert_eq!(format_java_version(unknown_jre), None);
}
}
1 change: 1 addition & 0 deletions src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod ruby;
mod rust;
mod time;
mod username;
mod utils;

#[cfg(feature = "battery")]
mod battery;
Expand Down
65 changes: 65 additions & 0 deletions src/modules/utils/java_version_parser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use nom::{
branch::alt,
bytes::complete::{tag, take_until, take_while1},
combinator::rest,
sequence::{preceded, tuple},
IResult,
};

fn is_version(c: char) -> bool {
c >= '0' && c <= '9' || c == '.'
}

fn version(input: &str) -> IResult<&str, &str> {
take_while1(&is_version)(input)
}

fn zulu(input: &str) -> IResult<&str, &str> {
let zulu_prefix_value = preceded(take_until("("), tag("("));
preceded(zulu_prefix_value, version)(input)
}

fn jre_prefix(input: &str) -> IResult<&str, &str> {
preceded(take_until("JRE ("), tag("JRE ("))(input)
}

fn j9_prefix(input: &str) -> IResult<&str, &str> {
preceded(take_until("VM ("), tag("VM ("))(input)
}

fn suffix(input: &str) -> IResult<&str, &str> {
rest(input)
}

fn parse(input: &str) -> IResult<&str, &str> {
let prefix = alt((jre_prefix, j9_prefix));
let version_or_zulu = alt((version, zulu));
let (input, (_, version, _)) = tuple((prefix, version_or_zulu, suffix))(input)?;

Ok((input, version))
}

/// Parse the java version from `java -Xinternalversion` format.
///
/// The expected format is similar to:
/// "JRE (1.8.0_222-b10)"
/// "JRE (Zulu 8.40.0.25-CA-linux64) (1.8.0_222-b10)"
/// "VM (1.8.0_222-b10)".
///
/// Some Java vendors might not follow this format.
pub fn parse_jre_version(input: &str) -> Option<&str> {
parse(input).map(|result| result.1).ok()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_parse_eclipse_openj9() {
let java_8 = "Eclipse OpenJ9 OpenJDK 64-bit Server VM (1.8.0_222-b10) from linux-amd64 JRE with Extensions for OpenJDK for Eclipse OpenJ9 8.0.222.0, built on Jul 17 2019 21:29:18 by jenkins with g++ (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5)";
let java_11 = "Eclipse OpenJ9 OpenJDK 64-bit Server VM (11.0.4+11) from linux-amd64 JRE with Extensions for OpenJDK for Eclipse OpenJ9 11.0.4.0, built on Jul 17 2019 21:51:37 by jenkins with g++ (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5)";
assert_eq!(parse(java_8), Ok(("", "1.8.0")));
assert_eq!(parse(java_11), Ok(("", "11.0.4")));
}
}
1 change: 1 addition & 0 deletions src/modules/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod java_version_parser;

0 comments on commit b7762a3

Please sign in to comment.