Skip to content

Commit

Permalink
Display warning in case of environment mismatch apache#122
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Oct 22, 2020
1 parent 41088ab commit af762d5
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public ExecutionResult execute(ClientOutput output, List<String> argv) {
daemon.dispatch(new Message.BuildRequest(
args,
layout.userDir().toString(),
layout.multiModuleProjectDirectory().toString()));
layout.multiModuleProjectDirectory().toString(), System.getenv()));

while (true) {
Message m = daemon.receive();
Expand Down
33 changes: 31 additions & 2 deletions common/src/main/java/org/jboss/fuse/mvnd/common/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -37,11 +39,13 @@ public static class BuildRequest extends Message {
final List<String> args;
final String workingDir;
final String projectDir;
final Map<String, String> env;

public BuildRequest(List<String> args, String workingDir, String projectDir) {
public BuildRequest(List<String> args, String workingDir, String projectDir, Map<String, String> env) {
this.args = args;
this.workingDir = workingDir;
this.projectDir = projectDir;
this.env = env;
}

public List<String> getArgs() {
Expand All @@ -56,6 +60,10 @@ public String getProjectDir() {
return projectDir;
}

public Map<String, String> getEnv() {
return env;
}

@Override
public String toString() {
return "BuildRequest{" +
Expand Down Expand Up @@ -221,13 +229,15 @@ private BuildRequest readBuildRequest(DataInputStream input) throws IOException
List<String> args = readStringList(input);
String workingDir = readUTF(input);
String projectDir = readUTF(input);
return new BuildRequest(args, workingDir, projectDir);
Map<String, String> 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 {
Expand Down Expand Up @@ -283,6 +293,25 @@ private void writeStringList(DataOutputStream output, List<String> value) throws
}
}

private Map<String, String> readStringMap(DataInputStream input) throws IOException {
LinkedHashMap<String, String> 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<String, String> value) throws IOException {
output.writeInt(value.size());
for (Map.Entry<String, String> 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;
Expand Down
28 changes: 27 additions & 1 deletion daemon/src/main/java/org/apache/maven/cli/DaemonMavenCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, String> clientEnv) throws Exception {
Properties props = (Properties) System.getProperties().clone();
try {
initialize(cliRequest);
Expand All @@ -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;
Expand Down Expand Up @@ -472,6 +477,27 @@ private void repository(CliRequest cliRequest)
}
}

private void environment(Map<String, String> clientEnv) {
Map<String, String> requested = new TreeMap<>(clientEnv);
Map<String, String> actual = new TreeMap<>(System.getenv());
List<String> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ private void handle(DaemonConnection<Message> 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) {
Expand Down

0 comments on commit af762d5

Please sign in to comment.