This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch mvnd-0.9.x
in repository https://gitbox.apache.org/repos/asf/maven-mvnd.git

commit 2220b012074e17db4dcac739b21a74ccb1ef3dd8
Author: Peter Palaga <ppal...@redhat.com>
AuthorDate: Wed Dec 14 00:35:10 2022 +0100

    Fixup #716 Calculate java home from java command (#721)
    
    * Add a test
    * Simplify the stream processing
    
    Co-authored-by: Guillaume Nodet <gno...@gmail.com>
---
 .../mvndaemon/mvnd/client/DaemonParameters.java    | 11 +++++++---
 .../java/org/mvndaemon/mvnd/common/OsUtils.java    | 25 ++++++++++++++--------
 .../org/mvndaemon/mvnd/common/OsUtilsTest.java     |  7 ++++++
 3 files changed, 31 insertions(+), 12 deletions(-)

diff --git 
a/client/src/main/java/org/mvndaemon/mvnd/client/DaemonParameters.java 
b/client/src/main/java/org/mvndaemon/mvnd/client/DaemonParameters.java
index 858975e5..e81335e7 100644
--- a/client/src/main/java/org/mvndaemon/mvnd/client/DaemonParameters.java
+++ b/client/src/main/java/org/mvndaemon/mvnd/client/DaemonParameters.java
@@ -139,7 +139,8 @@ public class DaemonParameters {
                 .orLocalProperty(provider, globalPropertiesPath())
                 .orSystemProperty()
                 .orEnvironmentVariable()
-                .or(new ValueSource(description -> description.append("java 
command"), this::javaHomeFromPath))
+                .or(new ValueSource(
+                        description -> description.append("java command"), 
DaemonParameters::javaHomeFromPath))
                 .orFail()
                 .asPath();
         try {
@@ -149,8 +150,12 @@ public class DaemonParameters {
         }
     }
 
-    private String javaHomeFromPath() {
-        final String jHome = OsUtils.findJavaHomeFromPath();
+    private static String javaHomeFromPath() {
+        LOG.warn(
+                "Falling back to finding JAVA_HOME by running java executable 
available in PATH."
+                        + " You may want to avoid this time consumig task by 
setting JAVA_HOME environment variable"
+                        + " or by passing java.home system property through 
command line or in one of mvnd configuration files.");
+        final String jHome = OsUtils.findJavaHomeFromJavaExecutable("java");
         if (null != jHome) {
             System.setProperty(Environment.JAVA_HOME.getProperty(), jHome);
         }
diff --git a/common/src/main/java/org/mvndaemon/mvnd/common/OsUtils.java 
b/common/src/main/java/org/mvndaemon/mvnd/common/OsUtils.java
index eb0569db..45ae8826 100644
--- a/common/src/main/java/org/mvndaemon/mvnd/common/OsUtils.java
+++ b/common/src/main/java/org/mvndaemon/mvnd/common/OsUtils.java
@@ -111,16 +111,23 @@ public class OsUtils {
         }
     }
 
-    public static String findJavaHomeFromPath() {
-        String[] cmd = {"java", "-XshowSettings:properties", "-version"};
-        final List<String> output = new ArrayList<String>(1);
+    /**
+     * Executes the given {@code javaExecutable} with {@code 
-XshowSettings:properties -version} parameters and extracts
+     * the value of {@code java.home} from the output.
+     *
+     * @param  javaExecutable pass {@code "java"} to get {@code java} binary 
available in {@code PATH} environment
+     *                        variable or pass an absolute path to a {@code 
"java"} executable
+     * @return                a {@code java.home} value or null
+     */
+    public static String findJavaHomeFromJavaExecutable(String javaExecutable) 
{
+        String[] cmd = {javaExecutable, "-XshowSettings:properties", 
"-version"};
+        final List<String> output = new ArrayList<String>();
         exec(cmd, output);
-        List<String> javaHomeLines =
-                output.stream().filter(l -> l.contains(" java.home = 
")).collect(Collectors.toList());
-        if (javaHomeLines.size() == 1) {
-            return javaHomeLines.get(0).trim().replaceFirst("java.home = ", 
"");
-        }
-        return null;
+        return output.stream()
+                .filter(l -> l.contains(" java.home = "))
+                .map(l -> l.substring(l.indexOf('=') + 1).trim())
+                .findFirst()
+                .orElse(null);
     }
 
     private static void exec(String[] cmd, final List<String> output) {
diff --git a/common/src/test/java/org/mvndaemon/mvnd/common/OsUtilsTest.java 
b/common/src/test/java/org/mvndaemon/mvnd/common/OsUtilsTest.java
index 41cb71bd..dd8eac32 100644
--- a/common/src/test/java/org/mvndaemon/mvnd/common/OsUtilsTest.java
+++ b/common/src/test/java/org/mvndaemon/mvnd/common/OsUtilsTest.java
@@ -31,4 +31,11 @@ public class OsUtilsTest {
         Assertions.assertEquals("1g", OsUtils.kbTohumanReadable(1024 * 1024));
         Assertions.assertEquals("1t", OsUtils.kbTohumanReadable(1024 * 1024 * 
1024));
     }
+
+    @Test
+    void findJavaHomeFromPath() {
+        final String expectedJavaHome = System.getProperty("java.home");
+        Assertions.assertEquals(
+                expectedJavaHome, 
OsUtils.findJavaHomeFromJavaExecutable(expectedJavaHome + "/bin/java"));
+    }
 }

Reply via email to