Skip to content

Commit

Permalink
Make sure the current directory is changed when processing a new mave…
Browse files Browse the repository at this point in the history
…n request, apache#397
  • Loading branch information
gnodet committed May 19, 2021
1 parent c29c8ab commit 0f9332a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 7 deletions.
17 changes: 14 additions & 3 deletions daemon/src/main/java/org/apache/maven/cli/DaemonMavenCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public int main(List<String> arguments,
try {
CliRequest req = new CliRequest(null, null);
req.args = arguments.toArray(new String[0]);
req.workingDirectory = workingDirectory;
req.workingDirectory = new File(workingDirectory).getCanonicalPath();
req.multiModuleProjectDirectory = new File(projectDirectory);
return doMain(req, clientEnv);
} finally {
Expand Down Expand Up @@ -684,8 +684,7 @@ private void environment(String workingDir, Map<String, String> clientEnv) {
CLibrary.setenv(key, vr);
}
setEnv(clientEnv);
CLibrary.chdir(workingDir);
System.setProperty("user.dir", workingDir);
chDir(workingDir);
} catch (Exception e) {
slf4jLogger.warn("Environment mismatch ! Could not set the environment (" + e + ")");
slf4jLogger.warn("A few environment mismatches have been detected between the client and the daemon.");
Expand All @@ -699,6 +698,18 @@ private void environment(String workingDir, Map<String, String> clientEnv) {
}
}

protected static void chDir(String workingDir) throws Exception {
CLibrary.chdir(workingDir);
System.setProperty("user.dir", workingDir);
Class<?> fileClass = Class.forName("java.io.File");
Field fsField = fileClass.getDeclaredField("fs");
fsField.setAccessible(true);
Object fs = fsField.get(null);
Field userDirField = fs.getClass().getDeclaredField("userDir");
userDirField.setAccessible(true);
userDirField.set(fs, workingDir);
}

@SuppressWarnings("unchecked")
protected static void setEnv(Map<String, String> newenv) throws Exception {
try {
Expand Down
33 changes: 33 additions & 0 deletions daemon/src/test/java/org/apache/maven/cli/DaemonMavenCliTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.maven.cli;

import java.io.File;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class DaemonMavenCliTest {

@Test
void testChdir() throws Exception {
File d = new File("target/tstDir").getAbsoluteFile();
d.mkdirs();
DaemonMavenCli.chDir(d.toString());
assertEquals(new File(d, "test").getAbsolutePath(), new File("test").getAbsolutePath());
}

}
2 changes: 2 additions & 0 deletions native/src/main/java/org/mvndaemon/mvnd/nativ/CLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.mvndaemon.mvnd.nativ;

import java.lang.reflect.Field;

/**
* Interface to access some low level POSIX functions, loaded by
* <a href="http://fusesource.github.io/hawtjni/">HawtJNI</a> Runtime
Expand Down
19 changes: 15 additions & 4 deletions sync/src/main/java/org/mvndaemon/mvnd/sync/IpcClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -144,18 +145,28 @@ Socket createClient() throws IOException {
.redirectError(discard)
.start();

Future<int[]> future = ForkJoinPool.commonPool().submit(() -> {
ExecutorService es = Executors.newSingleThreadExecutor();
Future<int[]> future = es.submit(() -> {
Socket s = ss.accept();
DataInputStream dis = new DataInputStream(s.getInputStream());
int rand2 = dis.readInt();
int port2 = dis.readInt();
return new int[] { rand2, port2 };
});
int[] res = future.get(5, TimeUnit.SECONDS);
int[] res;
try {
res = future.get(5, TimeUnit.SECONDS);
} catch (Exception e) {
process.destroyForcibly();
throw e;
} finally {
es.shutdownNow();
ss.close();
}
if (rand != res[0]) {
process.destroyForcibly();
throw new IllegalStateException("IpcServer did not respond with the correct random");
}
ss.close();

int port = res[1];
Socket socket = new Socket();
Expand Down

0 comments on commit 0f9332a

Please sign in to comment.