From af762d572171b8c449684a55445461b477bba18a Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Thu, 22 Oct 2020 15:27:38 +0200 Subject: [PATCH] Display warning in case of environment mismatch #122 --- .../jboss/fuse/mvnd/client/DefaultClient.java | 2 +- .../org/jboss/fuse/mvnd/common/Message.java | 33 +++++++++++++++++-- .../org/apache/maven/cli/DaemonMavenCli.java | 28 +++++++++++++++- .../org/jboss/fuse/mvnd/daemon/Server.java | 2 +- 4 files changed, 60 insertions(+), 5 deletions(-) diff --git a/client/src/main/java/org/jboss/fuse/mvnd/client/DefaultClient.java b/client/src/main/java/org/jboss/fuse/mvnd/client/DefaultClient.java index 432804fd9..1300a8fab 100644 --- a/client/src/main/java/org/jboss/fuse/mvnd/client/DefaultClient.java +++ b/client/src/main/java/org/jboss/fuse/mvnd/client/DefaultClient.java @@ -189,7 +189,7 @@ public ExecutionResult execute(ClientOutput output, List argv) { daemon.dispatch(new Message.BuildRequest( args, layout.userDir().toString(), - layout.multiModuleProjectDirectory().toString())); + layout.multiModuleProjectDirectory().toString(), System.getenv())); while (true) { Message m = daemon.receive(); diff --git a/common/src/main/java/org/jboss/fuse/mvnd/common/Message.java b/common/src/main/java/org/jboss/fuse/mvnd/common/Message.java index 07c240a5c..b9d6536e6 100644 --- a/common/src/main/java/org/jboss/fuse/mvnd/common/Message.java +++ b/common/src/main/java/org/jboss/fuse/mvnd/common/Message.java @@ -23,7 +23,9 @@ import java.io.StringWriter; import java.io.UTFDataFormatException; import java.util.ArrayList; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; public abstract class Message { @@ -37,11 +39,13 @@ public static class BuildRequest extends Message { final List args; final String workingDir; final String projectDir; + final Map env; - public BuildRequest(List args, String workingDir, String projectDir) { + public BuildRequest(List args, String workingDir, String projectDir, Map env) { this.args = args; this.workingDir = workingDir; this.projectDir = projectDir; + this.env = env; } public List getArgs() { @@ -56,6 +60,10 @@ public String getProjectDir() { return projectDir; } + public Map getEnv() { + return env; + } + @Override public String toString() { return "BuildRequest{" + @@ -221,13 +229,15 @@ private BuildRequest readBuildRequest(DataInputStream input) throws IOException List args = readStringList(input); String workingDir = readUTF(input); String projectDir = readUTF(input); - return new BuildRequest(args, workingDir, projectDir); + Map env = readStringMap(input); + return new BuildRequest(args, workingDir, projectDir, env); } private void writeBuildRequest(DataOutputStream output, BuildRequest value) throws IOException { writeStringList(output, value.args); writeUTF(output, value.workingDir); writeUTF(output, value.projectDir); + writeStringMap(output, value.env); } private BuildEvent readBuildEvent(DataInputStream input) throws IOException { @@ -283,6 +293,25 @@ private void writeStringList(DataOutputStream output, List value) throws } } + private Map readStringMap(DataInputStream input) throws IOException { + LinkedHashMap m = new LinkedHashMap<>(); + int nb = input.readInt(); + for (int i = 0; i < nb; i++) { + String k = readUTF(input); + String v = readUTF(input); + m.put(k, v); + } + return m; + } + + private void writeStringMap(DataOutputStream output, Map value) throws IOException { + output.writeInt(value.size()); + for (Map.Entry e : value.entrySet()) { + writeUTF(output, e.getKey()); + writeUTF(output, e.getValue()); + } + } + private static final String INVALID_BYTE = "Invalid byte"; private static final int UTF_BUFS_CHAR_CNT = 256; private static final int UTF_BUFS_BYTE_CNT = UTF_BUFS_CHAR_CNT * 3; diff --git a/daemon/src/main/java/org/apache/maven/cli/DaemonMavenCli.java b/daemon/src/main/java/org/apache/maven/cli/DaemonMavenCli.java index d4be9a1fc..4860abdd4 100644 --- a/daemon/src/main/java/org/apache/maven/cli/DaemonMavenCli.java +++ b/daemon/src/main/java/org/apache/maven/cli/DaemonMavenCli.java @@ -32,11 +32,15 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Objects; import java.util.Properties; import java.util.Set; import java.util.StringTokenizer; +import java.util.TreeMap; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; import org.apache.commons.cli.ParseException; @@ -157,7 +161,7 @@ public DaemonMavenCli() throws Exception { } // TODO need to externalize CliRequest - public int doMain(CliRequest cliRequest) throws Exception { + public int doMain(CliRequest cliRequest, Map clientEnv) throws Exception { Properties props = (Properties) System.getProperties().clone(); try { initialize(cliRequest); @@ -170,6 +174,7 @@ public int doMain(CliRequest cliRequest) throws Exception { populateRequest(cliRequest); encryption(cliRequest); repository(cliRequest); + environment(clientEnv); return execute(cliRequest); } catch (ExitException e) { return e.exitCode; @@ -472,6 +477,27 @@ private void repository(CliRequest cliRequest) } } + private void environment(Map clientEnv) { + Map requested = new TreeMap<>(clientEnv); + Map actual = new TreeMap<>(System.getenv()); + List diffs = Stream.concat(requested.keySet().stream(), actual.keySet().stream()) + .sorted() + .distinct() + .filter(s -> !s.startsWith("JAVA_MAIN_CLASS_")) + .filter(key -> !Objects.equals(requested.get(key), actual.get(key))) + .collect(Collectors.toList()); + if (!diffs.isEmpty()) { + slf4jLogger.warn("Environment mistmach !"); + slf4jLogger.warn("A few environment mismatches have been detected between the client and the daemon."); + diffs.forEach(key -> { + String vr = requested.get(key); + String va = actual.get(key); + slf4jLogger.warn(" {} -> {} instead of {}", key, va, vr); + }); + slf4jLogger.warn("This is usually harmless."); + } + } + private int execute(CliRequest cliRequest) throws MavenExecutionRequestPopulationException { commands(cliRequest); diff --git a/daemon/src/main/java/org/jboss/fuse/mvnd/daemon/Server.java b/daemon/src/main/java/org/jboss/fuse/mvnd/daemon/Server.java index 54e7b4523..7fd464cec 100644 --- a/daemon/src/main/java/org/jboss/fuse/mvnd/daemon/Server.java +++ b/daemon/src/main/java/org/jboss/fuse/mvnd/daemon/Server.java @@ -423,7 +423,7 @@ private void handle(DaemonConnection connection, BuildRequest buildRequ }); pumper.start(); try { - cli.doMain(req); + cli.doMain(req, buildRequest.getEnv()); LOGGER.info("Build finished, finishing message dispatch"); loggingSpy.finish(); } catch (Throwable t) {