mirror of
https://github.com/apache/maven-mvnd.git
synced 2025-10-13 13:44:26 +00:00
Code improvements.
This commit is contained in:
@@ -24,6 +24,7 @@ import java.io.IOException;
|
|||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
@@ -150,7 +151,7 @@ public class DocMojo extends AbstractMojo {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void store(OutputStream out, String comments) throws IOException {
|
public void store(OutputStream out, String comments) throws IOException {
|
||||||
this.store(new OutputStreamWriter(out, "8859_1"), comments);
|
this.store(new OutputStreamWriter(out, StandardCharsets.ISO_8859_1), comments);
|
||||||
}
|
}
|
||||||
|
|
||||||
static class SkipFirstLineBufferedWriter extends BufferedWriter {
|
static class SkipFirstLineBufferedWriter extends BufferedWriter {
|
||||||
|
@@ -24,7 +24,6 @@ import java.net.SocketAddress;
|
|||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.channels.ServerSocketChannel;
|
import java.nio.channels.ServerSocketChannel;
|
||||||
import java.nio.channels.SocketChannel;
|
import java.nio.channels.SocketChannel;
|
||||||
import java.nio.file.DirectoryStream;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.StandardOpenOption;
|
import java.nio.file.StandardOpenOption;
|
||||||
@@ -34,13 +33,18 @@ import java.util.Collections;
|
|||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
import java.util.function.Supplier;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import org.mvndaemon.mvnd.common.DaemonCompatibilitySpec;
|
import org.mvndaemon.mvnd.common.DaemonCompatibilitySpec;
|
||||||
import org.mvndaemon.mvnd.common.DaemonCompatibilitySpec.Result;
|
import org.mvndaemon.mvnd.common.DaemonCompatibilitySpec.Result;
|
||||||
@@ -69,6 +73,11 @@ import static org.mvndaemon.mvnd.common.DaemonState.Canceled;
|
|||||||
public class DaemonConnector {
|
public class DaemonConnector {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(DaemonConnector.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(DaemonConnector.class);
|
||||||
|
private static final Function<DaemonInfo, Boolean> daemonIsIdle = di -> di.getState() == DaemonState.Idle;
|
||||||
|
private static final Predicate<DaemonInfo> daemonIsCanceled = di -> di.getState() == Canceled;
|
||||||
|
private static final Predicate<Path> isJarFile =
|
||||||
|
s -> s.getFileName().toString().endsWith(".jar");
|
||||||
|
private static final Predicate<String> nonEmpty = s -> !s.isEmpty();
|
||||||
|
|
||||||
private final DaemonRegistry registry;
|
private final DaemonRegistry registry;
|
||||||
private final DaemonParameters parameters;
|
private final DaemonParameters parameters;
|
||||||
@@ -100,7 +109,7 @@ public class DaemonConnector {
|
|||||||
new DaemonCompatibilitySpec(parameters.javaHome(), parameters.getDaemonOpts());
|
new DaemonCompatibilitySpec(parameters.javaHome(), parameters.getDaemonOpts());
|
||||||
output.accept(Message.buildStatus("Looking up daemon..."));
|
output.accept(Message.buildStatus("Looking up daemon..."));
|
||||||
Map<Boolean, List<DaemonInfo>> idleBusy =
|
Map<Boolean, List<DaemonInfo>> idleBusy =
|
||||||
registry.getAll().stream().collect(Collectors.groupingBy(di -> di.getState() == DaemonState.Idle));
|
registry.getAll().stream().collect(Collectors.groupingBy(daemonIsIdle));
|
||||||
final Collection<DaemonInfo> idleDaemons = idleBusy.getOrDefault(true, Collections.emptyList());
|
final Collection<DaemonInfo> idleDaemons = idleBusy.getOrDefault(true, Collections.emptyList());
|
||||||
final Collection<DaemonInfo> busyDaemons = idleBusy.getOrDefault(false, Collections.emptyList());
|
final Collection<DaemonInfo> busyDaemons = idleBusy.getOrDefault(false, Collections.emptyList());
|
||||||
|
|
||||||
@@ -194,6 +203,7 @@ public class DaemonConnector {
|
|||||||
.collect(Collectors.groupingBy(DaemonStopEvent::getDaemonId, Collectors.minBy(this::compare)))
|
.collect(Collectors.groupingBy(DaemonStopEvent::getDaemonId, Collectors.minBy(this::compare)))
|
||||||
.values()
|
.values()
|
||||||
.stream()
|
.stream()
|
||||||
|
.filter(Optional::isPresent)
|
||||||
.map(Optional::get)
|
.map(Optional::get)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
for (DaemonStopEvent stopEvent : recentStopEvents) {
|
for (DaemonStopEvent stopEvent : recentStopEvents) {
|
||||||
@@ -251,7 +261,7 @@ public class DaemonConnector {
|
|||||||
Collection<DaemonInfo> busyDaemons, DaemonCompatibilitySpec constraint) {
|
Collection<DaemonInfo> busyDaemons, DaemonCompatibilitySpec constraint) {
|
||||||
DaemonClientConnection connection = null;
|
DaemonClientConnection connection = null;
|
||||||
List<DaemonInfo> canceledBusy =
|
List<DaemonInfo> canceledBusy =
|
||||||
busyDaemons.stream().filter(di -> di.getState() == Canceled).collect(Collectors.toList());
|
busyDaemons.stream().filter(daemonIsCanceled).collect(Collectors.toList());
|
||||||
final List<DaemonInfo> compatibleCanceledDaemons = getCompatibleDaemons(canceledBusy, constraint);
|
final List<DaemonInfo> compatibleCanceledDaemons = getCompatibleDaemons(canceledBusy, constraint);
|
||||||
LOGGER.debug(
|
LOGGER.debug(
|
||||||
"Found {} busy daemons, {} cancelled, {} compatibles",
|
"Found {} busy daemons, {} cancelled, {} compatibles",
|
||||||
@@ -334,46 +344,57 @@ public class DaemonConnector {
|
|||||||
return String.format("%08x", new Random().nextInt());
|
return String.format("%08x", new Random().nextInt());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String findLocation(
|
||||||
|
Path startPath, Predicate<Path> searchedJarFile, Supplier<IllegalStateException> exception)
|
||||||
|
throws IOException, IllegalStateException {
|
||||||
|
try (Stream<Path> jarPaths = Files.list(startPath)) {
|
||||||
|
return jarPaths.filter(isJarFile)
|
||||||
|
.filter(searchedJarFile)
|
||||||
|
.findFirst()
|
||||||
|
.map(Path::toString)
|
||||||
|
.orElseThrow(exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String findPlexusClassworldsJarLocation(Path mavenDaemonHomeBoot)
|
||||||
|
throws IOException, IllegalStateException {
|
||||||
|
return findLocation(
|
||||||
|
mavenDaemonHomeBoot,
|
||||||
|
s -> s.getFileName().toString().startsWith("plexus-classworlds-"),
|
||||||
|
() -> new IllegalStateException("Could not find plexus-classworlds jar in boot/"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String findMavenDaemonAgentLocation(Path mavenDaemonAgent) throws IOException, IllegalStateException {
|
||||||
|
return findLocation(
|
||||||
|
mavenDaemonAgent,
|
||||||
|
s -> s.getFileName().toString().startsWith("mvnd-agent-"),
|
||||||
|
() -> new IllegalStateException("Could not find mvnd-agent jar in mvn/lib/mvnd/"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private int findPort() {
|
||||||
|
try (ServerSocketChannel channel = SocketFamily.inet.openServerSocket()) {
|
||||||
|
return ((InetSocketAddress) channel.getLocalAddress()).getPort();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new IllegalStateException("Unable to find a free debug port", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private Process startDaemonProcess(String daemonId, ClientOutput output) {
|
private Process startDaemonProcess(String daemonId, ClientOutput output) {
|
||||||
final Path mvndHome = parameters.mvndHome();
|
final Path mvndHome = parameters.mvndHome();
|
||||||
final Path workingDir = parameters.userDir();
|
final Path workingDir = parameters.userDir();
|
||||||
String command = "";
|
String command = "";
|
||||||
try {
|
try {
|
||||||
List<String> args = new ArrayList<>();
|
|
||||||
// executable
|
// executable
|
||||||
final String java = Os.current().isUnixLike() ? "bin/java" : "bin\\java.exe";
|
final String java = Os.current().isUnixLike() ? "bin/java" : "bin\\java.exe";
|
||||||
|
|
||||||
|
List<String> args = new ArrayList<>();
|
||||||
args.add(parameters.javaHome().resolve(java).toString());
|
args.add(parameters.javaHome().resolve(java).toString());
|
||||||
// classpath
|
|
||||||
String mvndAgentPath = null;
|
String plexusClassworldsPath =
|
||||||
String plexusClassworldsPath = null;
|
findPlexusClassworldsJarLocation(mvndHome.resolve("mvn").resolve("boot"));
|
||||||
try (DirectoryStream<Path> jarPaths = Files.newDirectoryStream(
|
String mvndAgentPath = findMavenDaemonAgentLocation(
|
||||||
mvndHome.resolve("mvn").resolve("lib").resolve("mvnd"))) {
|
mvndHome.resolve("mvn").resolve("lib").resolve("mvnd"));
|
||||||
for (Path jar : jarPaths) {
|
|
||||||
String s = jar.getFileName().toString();
|
|
||||||
if (s.endsWith(".jar")) {
|
|
||||||
if (s.startsWith("mvnd-agent-")) {
|
|
||||||
mvndAgentPath = jar.toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
try (DirectoryStream<Path> jarPaths =
|
|
||||||
Files.newDirectoryStream(mvndHome.resolve("mvn").resolve("boot"))) {
|
|
||||||
for (Path jar : jarPaths) {
|
|
||||||
String s = jar.getFileName().toString();
|
|
||||||
if (s.endsWith(".jar")) {
|
|
||||||
if (s.startsWith("plexus-classworlds-")) {
|
|
||||||
plexusClassworldsPath = jar.toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (mvndAgentPath == null) {
|
|
||||||
throw new IllegalStateException("Could not find mvnd-agent jar in mvn/lib/mvnd/");
|
|
||||||
}
|
|
||||||
if (plexusClassworldsPath == null) {
|
|
||||||
throw new IllegalStateException("Could not find plexus-classworlds jar in boot/");
|
|
||||||
}
|
|
||||||
args.add("-classpath");
|
args.add("-classpath");
|
||||||
args.add(plexusClassworldsPath);
|
args.add(plexusClassworldsPath);
|
||||||
args.add("-javaagent:" + mvndAgentPath);
|
args.add("-javaagent:" + mvndAgentPath);
|
||||||
@@ -391,30 +412,21 @@ public class DaemonConnector {
|
|||||||
host = "localhost";
|
host = "localhost";
|
||||||
port = address;
|
port = address;
|
||||||
}
|
}
|
||||||
if (!port.matches("[0-9]+")) {
|
if (!port.matches("\\d+")) {
|
||||||
throw new IllegalArgumentException("Wrong debug address syntax: " + address);
|
throw new IllegalArgumentException("Wrong debug address syntax: " + address);
|
||||||
}
|
}
|
||||||
int iPort = Integer.parseInt(port);
|
int iPort = Integer.parseInt(port);
|
||||||
if (iPort == 0) {
|
if (iPort == 0) {
|
||||||
try (ServerSocketChannel channel = SocketFamily.inet.openServerSocket()) {
|
iPort = findPort();
|
||||||
iPort = ((InetSocketAddress) channel.getLocalAddress()).getPort();
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new IllegalStateException("Unable to find a free debug port", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
address = host + ":" + iPort;
|
address = host + ":" + iPort;
|
||||||
output.accept(Message.buildStatus("Daemon listening for debugger on address: " + address));
|
output.accept(Message.buildStatus("Daemon listening for debugger on address: " + address));
|
||||||
args.add("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=" + address);
|
args.add("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=" + address);
|
||||||
}
|
}
|
||||||
// jvm args
|
// jvm args
|
||||||
String jvmArgs = parameters.jvmArgs();
|
String jvmArgs = Objects.toString(parameters.jvmArgs(), "");
|
||||||
if (jvmArgs != null) {
|
args.addAll(Stream.of(jvmArgs.split(" ")).filter(nonEmpty).collect(Collectors.toList()));
|
||||||
for (String arg : jvmArgs.split(" ")) {
|
|
||||||
if (!arg.isEmpty()) {
|
|
||||||
args.add(arg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// memory
|
// memory
|
||||||
String minHeapSize = parameters.minHeapSize();
|
String minHeapSize = parameters.minHeapSize();
|
||||||
if (minHeapSize != null) {
|
if (minHeapSize != null) {
|
||||||
@@ -468,13 +480,13 @@ public class DaemonConnector {
|
|||||||
processBuilder
|
processBuilder
|
||||||
.environment()
|
.environment()
|
||||||
.put(Environment.JDK_JAVA_OPTIONS.getEnvironmentVariable(), parameters.jdkJavaOpts());
|
.put(Environment.JDK_JAVA_OPTIONS.getEnvironmentVariable(), parameters.jdkJavaOpts());
|
||||||
Process process = processBuilder
|
|
||||||
|
return processBuilder
|
||||||
.directory(workingDir.toFile())
|
.directory(workingDir.toFile())
|
||||||
.command(args)
|
.command(args)
|
||||||
.redirectOutput(redirect)
|
.redirectOutput(redirect)
|
||||||
.redirectError(redirect)
|
.redirectError(redirect)
|
||||||
.start();
|
.start();
|
||||||
return process;
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new DaemonException.StartException(
|
throw new DaemonException.StartException(
|
||||||
String.format(
|
String.format(
|
||||||
|
Reference in New Issue
Block a user