Skip to content

Commit 2b10afa

Browse files
authored
Fix java detection for JDKs without jrunscript (#55903)
1 parent 1d93753 commit 2b10afa

File tree

2 files changed

+40
-34
lines changed

2 files changed

+40
-34
lines changed

buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import com.github.jengelman.gradle.plugins.shadow.ShadowPlugin
2323
import org.apache.commons.io.IOUtils
2424
import org.apache.tools.ant.taskdefs.condition.Os
2525
import org.elasticsearch.gradle.precommit.PrecommitTasks
26+
import org.gradle.api.Action
2627
import org.gradle.api.GradleException
2728
import org.gradle.api.InvalidUserDataException
2829
import org.gradle.api.JavaVersion
@@ -53,6 +54,7 @@ import org.gradle.api.tasks.javadoc.Javadoc
5354
import org.gradle.internal.jvm.Jvm
5455
import org.gradle.process.ExecResult
5556
import org.gradle.process.ExecSpec
57+
import org.gradle.process.internal.ExecException
5658
import org.gradle.util.GradleVersion
5759

5860
import java.nio.charset.StandardCharsets
@@ -155,8 +157,8 @@ class BuildPlugin implements Plugin<Project> {
155157
runtimeJavaVersionEnum = JavaVersion.toVersion(findJavaSpecificationVersion(project, runtimeJavaHome))
156158
}
157159

158-
String inFipsJvmScript = 'print(java.security.Security.getProviders()[0].name.toLowerCase().contains("fips"));'
159-
boolean inFipsJvm = Boolean.parseBoolean(runJavaAsScript(project, runtimeJavaHome, inFipsJvmScript))
160+
String inFipsJvmScript = 'java.security.Security.getProviders()[0].name.toLowerCase().contains("fips")'
161+
boolean inFipsJvm = Boolean.parseBoolean(runScript(project, runtimeJavaHome, inFipsJvmScript))
160162

161163
// Build debugging info
162164
println '======================================='
@@ -446,55 +448,62 @@ class BuildPlugin implements Plugin<Project> {
446448

447449
/** Finds printable java version of the given JAVA_HOME */
448450
private static String findJavaVersionDetails(Project project, String javaHome) {
449-
String versionInfoScript = 'print(' +
450-
'java.lang.System.getProperty("java.vendor.version", java.lang.System.getProperty("java.vendor")) + " " + ' +
451+
String versionInfoScript = 'java.lang.System.getProperty("java.vendor.version", java.lang.System.getProperty("java.vendor")) + " " + ' +
451452
'java.lang.System.getProperty("java.version") + " [" +' +
452453
'java.lang.System.getProperty("java.vm.name") + " " + ' +
453-
'java.lang.System.getProperty("java.vm.version") + "]");'
454-
return runJavaAsScript(project, javaHome, versionInfoScript).trim()
454+
'java.lang.System.getProperty("java.vm.version") + "]"'
455+
return runScript(project, javaHome, versionInfoScript).trim()
455456
}
456457

457458
/** Finds the parsable java specification version */
458459
private static String findJavaSpecificationVersion(Project project, String javaHome) {
459-
String versionScript = 'print(java.lang.System.getProperty("java.specification.version"));'
460-
return runJavaAsScript(project, javaHome, versionScript)
461-
}
462-
463-
private static String findJavaVendor(Project project, String javaHome) {
464-
String vendorScript = 'print(java.lang.System.getProperty("java.vendor.version", System.getProperty("java.vendor"));'
465-
return runJavaAsScript(project, javaHome, vendorScript)
466-
}
467-
468-
/** Finds the parsable java specification version */
469-
private static String findJavaVersion(Project project, String javaHome) {
470-
String versionScript = 'print(java.lang.System.getProperty("java.version"));'
471-
return runJavaAsScript(project, javaHome, versionScript)
460+
String versionScript = 'java.lang.System.getProperty("java.specification.version")'
461+
return runScript(project, javaHome, versionScript)
472462
}
473463

474464
/** Runs the given javascript using jjs from the jdk, and returns the output */
475-
private static String runJavaAsScript(Project project, String javaHome, String script) {
476-
ByteArrayOutputStream stdout = new ByteArrayOutputStream()
477-
ByteArrayOutputStream stderr = new ByteArrayOutputStream()
465+
private static String runScript(Project project, String javaHome, String script) {
478466
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
479467
// gradle/groovy does not properly escape the double quote for windows
480468
script = script.replace('"', '\\"')
481469
}
482-
File jrunscriptPath = new File(javaHome, 'bin/jrunscript')
483-
ExecResult result = project.exec {
484-
executable = jrunscriptPath
485-
args '-e', script
486-
standardOutput = stdout
487-
errorOutput = stderr
488-
ignoreExitValue = true
470+
471+
try {
472+
File jrunscriptPath = new File(javaHome, 'bin/jrunscript')
473+
return exec(project, false) { spec ->
474+
spec.executable = jrunscriptPath
475+
spec.args '-e', "print($script);"
476+
}
477+
} catch (ExecException ex) {
478+
// if jrunscript fails, let's try jshell
479+
ByteArrayInputStream input = new ByteArrayInputStream("System.err.print(${script});\n".getBytes('UTF-8'))
480+
File jshellPath = new File(javaHome, 'bin/jshell')
481+
return exec(project, true) { spec ->
482+
spec.executable = jshellPath
483+
spec.args '-s'
484+
spec.standardInput = input
485+
}
486+
}
487+
}
488+
489+
private static String exec(Project project, boolean captureStdErr, Action<? super ExecSpec> spec) {
490+
ByteArrayOutputStream stdout = new ByteArrayOutputStream()
491+
ByteArrayOutputStream stderr = new ByteArrayOutputStream()
492+
ExecResult result = project.exec { ExecSpec s ->
493+
spec.execute(s)
494+
s.standardOutput = stdout
495+
s.errorOutput = stderr
496+
s.ignoreExitValue = true
489497
}
490498
if (result.exitValue != 0) {
491499
project.logger.error("STDOUT:")
492500
stdout.toString('UTF-8').eachLine { line -> project.logger.error(line) }
493501
project.logger.error("STDERR:")
494502
stderr.toString('UTF-8').eachLine { line -> project.logger.error(line) }
495503
result.rethrowFailure()
504+
result.assertNormalExitValue() // assert exit value in case the failure cause is null
496505
}
497-
return stdout.toString('UTF-8').trim()
506+
return (captureStdErr ? stderr : stdout).toString('UTF-8').trim()
498507
}
499508

500509
/** Return the configuration name used for finding transitive deps of the given dependency. */

x-pack/plugin/sql/qa/security/with-ssl/build.gradle

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,7 @@ integTestCluster {
205205
return tmpFile.exists()
206206
}
207207
}
208-
Closure notRunningFips = {
209-
Boolean.parseBoolean(BuildPlugin.runJavaAsScript(project, project.runtimeJavaHome,
210-
'print(java.security.Security.getProviders()[0].name.toLowerCase().contains("fips"));')) == false
211-
}
208+
Closure notRunningFips = { rootProject.ext.inFipsJvm == false }
212209

213210
// Do not attempt to form a cluster in a FIPS JVM, as doing so with a JKS keystore will fail.
214211
// TODO Revisit this when SQL CLI client can handle key/certificate instead of only Keystores.

0 commit comments

Comments
 (0)