Introduce a SocketFamily instead

This commit is contained in:
Guillaume Nodet
2021-07-22 09:53:22 +02:00
parent b67ab81c3d
commit aa89bd64ba
18 changed files with 400 additions and 428 deletions

View File

@@ -31,8 +31,8 @@
<name>Maven Daemon - Documentation Maven Plugin</name>
<properties>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<roaster.version>2.22.2.Final</roaster.version>
<maven.plugin-tools.version>3.6.0</maven.plugin-tools.version>

View File

@@ -30,7 +30,6 @@ import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
import javax.security.sasl.SaslClientFactory;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
@@ -124,7 +123,7 @@ public class DocMojo extends AbstractMojo {
}
public Object nextElement() {
return (SaslClientFactory) it.next();
return it.next();
}
};
}

View File

@@ -18,7 +18,6 @@ package org.mvndaemon.mvnd.client;
import java.io.File;
import java.io.IOException;
import java.net.SocketAddress;
import java.net.StandardProtocolFamily;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.file.Files;
@@ -50,7 +49,7 @@ import org.mvndaemon.mvnd.common.Environment;
import org.mvndaemon.mvnd.common.MavenDaemon;
import org.mvndaemon.mvnd.common.Message;
import org.mvndaemon.mvnd.common.Os;
import org.mvndaemon.mvnd.common.SocketHelper;
import org.mvndaemon.mvnd.common.SocketFamily;
import org.mvndaemon.mvnd.common.logging.ClientOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -375,7 +374,7 @@ public class DaemonConnector {
Environment.MVND_REGISTRY.addCommandLineOption(args, parameters.registry().toString());
Environment.MVND_SOCKET_FAMILY.addCommandLineOption(args,
parameters.socketFamily().orElseGet(
() -> getJavaVersion() >= 16.0f ? StandardProtocolFamily.UNIX : StandardProtocolFamily.INET)
() -> getJavaVersion() >= 16.0f ? SocketFamily.unix : SocketFamily.inet)
.toString());
parameters.discriminatingCommandLineOptions(args);
args.add(MavenDaemon.class.getName());
@@ -476,11 +475,11 @@ public class DaemonConnector {
}
public DaemonConnection connect(String str, byte[] token) throws DaemonException.ConnectException {
SocketAddress address = SocketHelper.socketAddressFromString(str);
StandardProtocolFamily family = SocketHelper.getSocketFamily(address);
SocketAddress address = SocketFamily.fromString(str);
try {
LOGGER.debug("Trying to connect to address {}.", address);
SocketChannel socketChannel = SocketHelper.openSocket(family);
SocketFamily family = SocketFamily.familyOf(address);
SocketChannel socketChannel = family.openSocket();
socketChannel.configureBlocking(false);
boolean connected = socketChannel.connect(address);
if (!connected) {

View File

@@ -18,7 +18,6 @@ package org.mvndaemon.mvnd.client;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.StandardProtocolFamily;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@@ -44,6 +43,7 @@ import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.mvndaemon.mvnd.common.BuildProperties;
import org.mvndaemon.mvnd.common.Environment;
import org.mvndaemon.mvnd.common.Os;
import org.mvndaemon.mvnd.common.SocketFamily;
import org.mvndaemon.mvnd.common.TimeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -324,8 +324,8 @@ public class DaemonParameters {
return property(Environment.MVND_LOG_PURGE_PERIOD).orFail().asDuration();
}
public Optional<StandardProtocolFamily> socketFamily() {
return property(Environment.MVND_SOCKET_FAMILY).asOptional().map(StandardProtocolFamily::valueOf);
public Optional<SocketFamily> socketFamily() {
return property(Environment.MVND_SOCKET_FAMILY).asOptional().map(SocketFamily::valueOf);
}
public static String findDefaultMultimoduleProjectDirectory(Path pwd) {

View File

@@ -107,6 +107,17 @@
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>jdk11-15</id>
<activation>
<jdk>[11,15)</jdk>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
@@ -122,6 +133,77 @@
</compilerArgs>
</configuration>
</execution>
<execution>
<id>jdk11</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<fork>true</fork>
<compilerArgs>
<arg>-XDignore.symbol.file</arg>
</compilerArgs>
<release>11</release>
<multiReleaseOutput>true</multiReleaseOutput>
<compileSourceRoots>
<root>${project.basedir}/src/main/java11</root>
</compileSourceRoots>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>jdk16+</id>
<activation>
<jdk>[16,)</jdk>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<fork>true</fork>
<compilerArgs>
<arg>-XDignore.symbol.file</arg>
</compilerArgs>
</configuration>
</execution>
<execution>
<id>jdk11</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<fork>true</fork>
<compilerArgs>
<arg>-XDignore.symbol.file</arg>
</compilerArgs>
<release>11</release>
<multiReleaseOutput>true</multiReleaseOutput>
<compileSourceRoots>
<root>${project.basedir}/src/main/java11</root>
</compileSourceRoots>
</configuration>
</execution>
<execution>
<id>jdk16</id>
<goals>
@@ -153,5 +235,7 @@
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

View File

@@ -243,7 +243,7 @@ public enum Environment {
/**
* Socket family to use
*/
MVND_SOCKET_FAMILY("mvnd.socketFamily", null, "INET", OptionType.STRING, Flags.DISCRIMINATING);
MVND_SOCKET_FAMILY("mvnd.socketFamily", null, "inet", OptionType.STRING, Flags.DISCRIMINATING);
static Properties properties;

View File

@@ -0,0 +1,23 @@
/*
* Copyright 2021 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.mvndaemon.mvnd.common;
public class ProcessHelper {
public static void killChildrenProcesses() {
}
}

View File

@@ -0,0 +1,141 @@
/*
* Copyright 2021 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.mvndaemon.mvnd.common;
import java.io.IOException;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.StandardProtocolFamily;
import java.net.UnknownHostException;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Locale;
public enum SocketFamily {
inet,
unix;
public SocketChannel openSocket() throws IOException {
switch (this) {
case inet:
return SocketChannel.open();
case unix:
return SocketHelper.openUnixSocket();
default:
throw new IllegalStateException();
}
}
public ServerSocketChannel openServerSocket() throws IOException {
switch (this) {
case inet:
return ServerSocketChannel.open().bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
case unix:
return SocketHelper.openUnixServerSocket();
default:
throw new IllegalStateException();
}
}
private StandardProtocolFamily getStandardProtocolFamily() {
return StandardProtocolFamily.valueOf(name().toUpperCase(Locale.ROOT));
}
private SocketAddress getLoopbackAddress() {
if (this == inet) {
return new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
} else {
return null;
}
}
public static SocketAddress fromString(String str) {
if (str.startsWith("inet:")) {
String s = str.substring("inet:".length());
int ic = s.lastIndexOf(':');
String ia = s.substring(0, ic);
int is = ia.indexOf('/');
String h = ia.substring(0, is);
String a = ia.substring(is + 1);
String p = s.substring(ic + 1);
InetAddress addr;
if ("<unresolved>".equals(a)) {
return InetSocketAddress.createUnresolved(h, Integer.parseInt(p));
} else {
if (a.indexOf('.') > 0) {
String[] as = a.split("\\.");
if (as.length != 4) {
throw new IllegalArgumentException("Unsupported socket address: '" + str + "'");
}
byte[] ab = new byte[4];
for (int i = 0; i < 4; i++) {
ab[i] = (byte) Integer.parseInt(as[i]);
}
try {
addr = InetAddress.getByAddress(h.isEmpty() ? null : h, ab);
} catch (UnknownHostException e) {
throw new IllegalArgumentException("Unsupported address: " + str, e);
}
} else {
throw new IllegalArgumentException("Unsupported address: " + str);
}
return new InetSocketAddress(addr, Integer.parseInt(p));
}
} else if (str.startsWith("unix:")) {
return SocketHelper.unixSocketAddressOf(str.substring("unix:".length()));
} else {
throw new IllegalArgumentException("Unsupported socket address: '" + str + "'");
}
}
public static String toString(SocketAddress address) {
switch (familyOf(address)) {
case inet:
InetSocketAddress isa = (InetSocketAddress) address;
String host = isa.getHostString();
InetAddress addr = isa.getAddress();
int port = isa.getPort();
String formatted;
if (addr == null) {
formatted = host + "/<unresolved>";
} else {
formatted = addr.toString();
if (addr instanceof Inet6Address) {
int i = formatted.lastIndexOf("/");
formatted = formatted.substring(0, i + 1) + "[" + formatted.substring(i + 1) + "]";
}
}
return "inet:" + formatted + ":" + port;
case unix:
return "unix:" + address;
default:
throw new IllegalArgumentException("Unsupported socket address: '" + address + "'");
}
}
public static SocketFamily familyOf(SocketAddress address) {
if (address instanceof InetSocketAddress) {
return SocketFamily.inet;
} else if ("java.net.UnixDomainSocketAddress".equals(address.getClass().getName())) {
return SocketFamily.unix;
} else {
throw new IllegalArgumentException("Unsupported socket address '" + address + "'");
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2019 the original author or authors.
* Copyright 2021 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.
@@ -16,165 +16,21 @@
package org.mvndaemon.mvnd.common;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.StandardProtocolFamily;
import java.net.UnknownHostException;
import java.nio.channels.ByteChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Objects;
import static java.net.StandardProtocolFamily.INET;
import static java.net.StandardProtocolFamily.INET6;
public class SocketHelper {
public static StandardProtocolFamily getSocketFamily(SocketAddress address) {
if (address instanceof InetSocketAddress) {
InetSocketAddress isa = (InetSocketAddress) address;
InetAddress ia = isa.getAddress();
if (ia instanceof Inet6Address) {
return INET6;
} else {
return INET;
}
// } else if (address instanceof UnixDomainSocketAddress) {
// return UNIX;
} else {
throw new IllegalArgumentException("Unsupported socket address '" + address + "'");
}
public static SocketChannel openUnixSocket() throws IOException {
throw new UnsupportedOperationException("Unix sockets are supported only on JDK >= 16");
}
public static void checkFamily(StandardProtocolFamily family, SocketAddress address) {
Objects.requireNonNull(family);
Objects.requireNonNull(address);
if (address instanceof InetSocketAddress) {
InetSocketAddress isa = (InetSocketAddress) address;
InetAddress ia = isa.getAddress();
if (ia != null
&& !(ia instanceof Inet4Address && family == INET)
&& !(ia instanceof Inet6Address && family == INET6)) {
throw new IllegalArgumentException(
"Socket address '" + address + "' does not match required family '" + family + "'");
}
// } else if (address instanceof UnixDomainSocketAddress) {
// if (family != StandardProtocolFamily.UNIX) {
// throw new IllegalArgumentException("Socket address '" + address + "' does not match required family '" + family + "'");
// }
} else {
throw new IllegalArgumentException(
"Socket address '" + address + "' does not match required family '" + family + "'");
}
public static ServerSocketChannel openUnixServerSocket() throws IOException {
throw new UnsupportedOperationException("Unix sockets are supported only on JDK >= 16");
}
public static SocketAddress socketAddressFromString(String str) {
if (str.startsWith("inet:")) {
String s = str.substring("inet:".length());
int ic = s.lastIndexOf(':');
String ia = s.substring(0, ic);
int is = ia.indexOf('/');
String h = ia.substring(0, is);
String a = ia.substring(is + 1);
String p = s.substring(ic + 1);
InetAddress addr;
if ("<unresolved>".equals(a)) {
return InetSocketAddress.createUnresolved(h, Integer.parseInt(p));
} else {
if (a.indexOf('.') > 0) {
String[] as = a.split("\\.");
if (as.length != 4) {
throw new IllegalArgumentException("Unsupported address: " + str);
public static SocketAddress unixSocketAddressOf(String s) {
throw new UnsupportedOperationException("Unix sockets are supported only on JDK >= 16");
}
byte[] ab = new byte[4];
for (int i = 0; i < 4; i++) {
ab[i] = (byte) Integer.parseInt(as[i]);
}
try {
addr = InetAddress.getByAddress(h.isEmpty() ? null : h, ab);
} catch (UnknownHostException e) {
throw new IllegalArgumentException("Unsupported address: " + str, e);
}
} else {
throw new IllegalArgumentException("Unsupported address: " + str);
}
return new InetSocketAddress(addr, Integer.parseInt(p));
}
// } else if (str.startsWith("unix:")) {
// return UnixDomainSocketAddress.of(str.substring("unix:".length()));
} else {
throw new IllegalArgumentException("Unsupported address: " + str);
}
}
public static String socketAddressToString(SocketAddress address) {
if (address instanceof InetSocketAddress) {
InetSocketAddress isa = (InetSocketAddress) address;
String host = isa.getHostString();
InetAddress addr = isa.getAddress();
int port = isa.getPort();
String formatted;
if (addr == null) {
formatted = host + "/<unresolved>";
} else {
formatted = addr.toString();
if (addr instanceof Inet6Address) {
int i = formatted.lastIndexOf("/");
formatted = formatted.substring(0, i + 1) + "[" + formatted.substring(i + 1) + "]";
}
}
return "inet:" + formatted + ":" + port;
// } else if (address instanceof UnixDomainSocketAddress) {
// return "unix:" + address;
} else {
throw new IllegalArgumentException("Unsupported socket address: '" + address + "'");
}
}
public static SocketChannel openSocket(StandardProtocolFamily family) throws IOException {
Objects.requireNonNull(family);
if (family == INET || family == INET6) {
return SocketChannel.open();
} else {
throw new IllegalArgumentException("Unsupported socket family: " + family);
}
// return SocketChannel.open(family);
}
public static ServerSocketChannel openServerSocket(StandardProtocolFamily family) throws IOException {
Objects.requireNonNull(family);
if (family == INET || family == INET6) {
return ServerSocketChannel.open().bind(getLoopbackAddress(family), 0);
} else {
throw new IllegalArgumentException("Unsupported socket family: " + family);
}
// return ServerSocketChannel.open(family).bind(getLoopbackAddress(family), 0);
}
private static SocketAddress getLoopbackAddress(StandardProtocolFamily family) {
try {
Objects.requireNonNull(family);
switch (family) {
case INET:
return new InetSocketAddress(Inet4Address.getByAddress(new byte[] { 127, 0, 0, 1 }), 0);
case INET6:
return new InetSocketAddress(
Inet6Address.getByAddress(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }), 0);
// case UNIX:
// return null;
default:
throw new IllegalArgumentException("Unsupported family: " + family);
}
} catch (UnknownHostException e) {
throw new IllegalArgumentException("Unsupported family: " + family, e);
}
}
public static ByteChannel wrapChannel(ByteChannel channel) {
return new ByteChannelWrapper(channel);
}
}

View File

@@ -0,0 +1,24 @@
/*
* Copyright 2021 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.mvndaemon.mvnd.common;
public class ProcessHelper {
public static void killChildrenProcesses() {
ProcessHandle.current().descendants().forEach(ProcessHandle::destroy);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2019 the original author or authors.
* Copyright 2021 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.
@@ -16,157 +16,23 @@
package org.mvndaemon.mvnd.common;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.StandardProtocolFamily;
import java.net.UnixDomainSocketAddress;
import java.net.UnknownHostException;
import java.nio.channels.ByteChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Objects;
import static java.net.StandardProtocolFamily.INET;
import static java.net.StandardProtocolFamily.INET6;
import static java.net.StandardProtocolFamily.UNIX;
public class SocketHelper {
public static StandardProtocolFamily getSocketFamily(SocketAddress address) {
if (address instanceof InetSocketAddress) {
InetSocketAddress isa = (InetSocketAddress) address;
InetAddress ia = isa.getAddress();
if (ia instanceof Inet6Address) {
return INET6;
} else {
return INET;
}
} else if (address instanceof UnixDomainSocketAddress) {
return UNIX;
} else {
throw new IllegalArgumentException("Unsupported socket address '" + address + "'");
}
public static SocketChannel openUnixSocket() throws IOException {
return SocketChannel.open(StandardProtocolFamily.UNIX);
}
public static void checkFamily(StandardProtocolFamily family, SocketAddress address) {
Objects.requireNonNull(family);
Objects.requireNonNull(address);
if (address instanceof InetSocketAddress) {
InetSocketAddress isa = (InetSocketAddress) address;
InetAddress ia = isa.getAddress();
if (ia != null
&& !(ia instanceof Inet4Address && family == INET)
&& !(ia instanceof Inet6Address && family == INET6)) {
throw new IllegalArgumentException(
"Socket address '" + address + "' does not match required family '" + family + "'");
}
} else if (address instanceof UnixDomainSocketAddress) {
if (family != StandardProtocolFamily.UNIX) {
throw new IllegalArgumentException("Socket address '" + address + "' does not match required family '" + family + "'");
}
} else {
throw new IllegalArgumentException(
"Socket address '" + address + "' does not match required family '" + family + "'");
}
public static ServerSocketChannel openUnixServerSocket() throws IOException {
return ServerSocketChannel.open(StandardProtocolFamily.UNIX).bind(null, 0);
}
public static SocketAddress socketAddressFromString(String str) {
if (str.startsWith("inet:")) {
String s = str.substring("inet:".length());
int ic = s.lastIndexOf(':');
String ia = s.substring(0, ic);
int is = ia.indexOf('/');
String h = ia.substring(0, is);
String a = ia.substring(is + 1);
String p = s.substring(ic + 1);
InetAddress addr;
if ("<unresolved>".equals(a)) {
return InetSocketAddress.createUnresolved(h, Integer.parseInt(p));
} else {
if (a.indexOf('.') > 0) {
String[] as = a.split("\\.");
if (as.length != 4) {
throw new IllegalArgumentException("Unsupported address: " + str);
public static SocketAddress unixSocketAddressOf(String s) {
return UnixDomainSocketAddress.of(s);
}
byte[] ab = new byte[4];
for (int i = 0; i < 4; i++) {
ab[i] = (byte) Integer.parseInt(as[i]);
}
try {
addr = InetAddress.getByAddress(h.isEmpty() ? null : h, ab);
} catch (UnknownHostException e) {
throw new IllegalArgumentException("Unsupported address: " + str, e);
}
} else {
throw new IllegalArgumentException("Unsupported address: " + str);
}
return new InetSocketAddress(addr, Integer.parseInt(p));
}
} else if (str.startsWith("unix:")) {
return UnixDomainSocketAddress.of(str.substring("unix:".length()));
} else {
throw new IllegalArgumentException("Unsupported address: " + str);
}
}
public static String socketAddressToString(SocketAddress address) {
if (address instanceof InetSocketAddress) {
InetSocketAddress isa = (InetSocketAddress) address;
String host = isa.getHostString();
InetAddress addr = isa.getAddress();
int port = isa.getPort();
String formatted;
if (addr == null) {
formatted = host + "/<unresolved>";
} else {
formatted = addr.toString();
if (addr instanceof Inet6Address) {
int i = formatted.lastIndexOf("/");
formatted = formatted.substring(0, i + 1) + "[" + formatted.substring(i + 1) + "]";
}
}
return "inet:" + formatted + ":" + port;
} else if (address instanceof UnixDomainSocketAddress) {
return "unix:" + address;
} else {
throw new IllegalArgumentException("Unsupported socket address: '" + address + "'");
}
}
public static SocketChannel openSocket(StandardProtocolFamily family) throws IOException {
Objects.requireNonNull(family);
return SocketChannel.open(family);
}
public static ServerSocketChannel openServerSocket(StandardProtocolFamily family) throws IOException {
Objects.requireNonNull(family);
return ServerSocketChannel.open(family).bind(getLoopbackAddress(family), 0);
}
private static SocketAddress getLoopbackAddress(StandardProtocolFamily family) {
try {
Objects.requireNonNull(family);
switch (family) {
case INET:
return new InetSocketAddress(Inet4Address.getByAddress(new byte[] { 127, 0, 0, 1 }), 0);
case INET6:
return new InetSocketAddress(
Inet6Address.getByAddress(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }), 0);
case UNIX:
return null;
default:
throw new IllegalArgumentException("Unsupported family: " + family);
}
} catch (UnknownHostException e) {
throw new IllegalArgumentException("Unsupported family: " + family, e);
}
}
public static ByteChannel wrapChannel(ByteChannel channel) {
return new ByteChannelWrapper(channel);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2019 the original author or authors.
* Copyright 2021 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.
@@ -17,53 +17,56 @@ package org.mvndaemon.mvnd.common;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.StandardProtocolFamily;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class SocketHelperTest {
public class SocketFamilyTest {
@Test
void testIpv4NullHost() throws UnknownHostException {
void testInetNullHost() throws UnknownHostException {
InetSocketAddress i4a = new InetSocketAddress(
InetAddress.getByAddress(null, new byte[] { (byte) 192, (byte) 168, 0, 1 }), 8080);
assertEquals("inet:/192.168.0.1:8080", SocketHelper.socketAddressToString(i4a));
assertEquals(i4a, SocketHelper.socketAddressFromString("inet:/192.168.0.1:8080"));
assertEquals("inet:/192.168.0.1:8080", SocketFamily.toString(i4a));
assertEquals(i4a, SocketFamily.fromString("inet:/192.168.0.1:8080"));
}
@Test
void testIpv4DummyHost() throws UnknownHostException {
void testInetDummyHost() throws UnknownHostException {
InetSocketAddress i4a = new InetSocketAddress(
InetAddress.getByAddress("dummy.org", new byte[] { (byte) 192, (byte) 168, 0, 1 }), 8080);
assertEquals("inet:dummy.org/192.168.0.1:8080", SocketHelper.socketAddressToString(i4a));
assertEquals(i4a, SocketHelper.socketAddressFromString("inet:dummy.org/192.168.0.1:8080"));
assertEquals("inet:dummy.org/192.168.0.1:8080", SocketFamily.toString(i4a));
assertEquals(i4a, SocketFamily.fromString("inet:dummy.org/192.168.0.1:8080"));
}
@Test
void testIpv4Loopback() throws UnknownHostException {
void testInetLoopback() throws UnknownHostException {
InetSocketAddress i4a = new InetSocketAddress(8080);
assertEquals("inet:0.0.0.0/0.0.0.0:8080", SocketHelper.socketAddressToString(i4a));
assertEquals(i4a, SocketHelper.socketAddressFromString("inet:0.0.0.0/0.0.0.0:8080"));
assertEquals("inet:0.0.0.0/0.0.0.0:8080", SocketFamily.toString(i4a));
assertEquals(i4a, SocketFamily.fromString("inet:0.0.0.0/0.0.0.0:8080"));
}
@Test
void testIpv4Unresolved() throws UnknownHostException {
void testInetUnresolved() throws UnknownHostException {
InetSocketAddress i4a = InetSocketAddress.createUnresolved("google.com", 8080);
assertEquals("inet:google.com/<unresolved>:8080", SocketHelper.socketAddressToString(i4a));
assertEquals(i4a, SocketHelper.socketAddressFromString("inet:google.com/<unresolved>:8080"));
assertEquals("inet:google.com/<unresolved>:8080", SocketFamily.toString(i4a));
assertEquals(i4a, SocketFamily.fromString("inet:google.com/<unresolved>:8080"));
}
@Test
void testCheckInetAddress() {
String family = "INET";
String address = "inet:/127.0.0.1:8192";
SocketHelper.checkFamily(StandardProtocolFamily.valueOf(family),
SocketHelper.socketAddressFromString(address));
@EnabledForJreRange(min = JRE.JAVA_16)
void testUnixFromTo() {
SocketAddress address = SocketFamily.fromString("unix:/tmp/foo-0123456.socket");
assertEquals(SocketFamily.unix, SocketFamily.familyOf(address));
assertEquals("unix:/tmp/foo-0123456.socket", SocketFamily.toString(address));
}
}

View File

@@ -17,7 +17,6 @@ package org.mvndaemon.mvnd.daemon;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.StandardProtocolFamily;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
@@ -54,7 +53,8 @@ import org.mvndaemon.mvnd.common.Environment;
import org.mvndaemon.mvnd.common.Message;
import org.mvndaemon.mvnd.common.Message.BuildRequest;
import org.mvndaemon.mvnd.common.Os;
import org.mvndaemon.mvnd.common.SocketHelper;
import org.mvndaemon.mvnd.common.ProcessHelper;
import org.mvndaemon.mvnd.common.SocketFamily;
import org.mvndaemon.mvnd.daemon.DaemonExpiration.DaemonExpirationResult;
import org.mvndaemon.mvnd.daemon.DaemonExpiration.DaemonExpirationStrategy;
import org.mvndaemon.mvnd.logging.smart.BuildEventListener;
@@ -111,15 +111,15 @@ public class Server implements AutoCloseable, Runnable {
this.noDaemon = Environment.MVND_NO_DAEMON.asBoolean();
this.keepAliveMs = Environment.MVND_KEEP_ALIVE.asDuration().toMillis();
StandardProtocolFamily socketFamily = Environment.MVND_SOCKET_FAMILY
SocketFamily socketFamily = Environment.MVND_SOCKET_FAMILY
.asOptional()
.map(StandardProtocolFamily::valueOf)
.orElse(StandardProtocolFamily.INET);
.map(SocketFamily::valueOf)
.orElse(SocketFamily.inet);
try {
cli = new DaemonMavenCli();
registry = new DaemonRegistry(Environment.MVND_REGISTRY.asPath());
socket = SocketHelper.openServerSocket(socketFamily);
socket = socketFamily.openServerSocket();
executor = Executors.newScheduledThreadPool(1);
strategy = DaemonExpiration.master();
memoryStatus = new DaemonMemoryStatus(executor);
@@ -141,7 +141,7 @@ public class Server implements AutoCloseable, Runnable {
Environment.MVND_JAVA_HOME.asString(),
Environment.MVND_HOME.asString(),
DaemonRegistry.getProcessId(),
SocketHelper.socketAddressToString(socket.getLocalAddress()),
SocketFamily.toString(socket.getLocalAddress()),
token,
Locale.getDefault().toLanguageTag(),
opts,
@@ -425,8 +425,9 @@ public class Server implements AutoCloseable, Runnable {
stateLock.lock();
try {
try {
ProcessHandle.current().descendants().forEach(ProcessHandle::destroy);
ProcessHelper.killChildrenProcesses();
} catch (Throwable t) {
LOGGER.debug("Error killing children processes", t);
t.printStackTrace();
}
long rem;

View File

@@ -47,16 +47,6 @@
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<!-- parent pom 1.11 forces version instead of using property -->
<source>${jdkTarget}</source>
<target>${jdkTarget}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
@@ -125,17 +115,4 @@
</dependency>
</dependencies>
<profiles>
<profile>
<id>jdk15+</id>
<activation>
<jdk>[15,)</jdk>
</activation>
<properties>
<jdkTarget>1.7</jdkTarget>
<javadocSource>7</javadocSource>
</properties>
</profile>
</profiles>
</project>

View File

@@ -30,20 +30,20 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>8</maven.compiler.target>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<trimStackTrace>false</trimStackTrace>
<!-- dependency versions a..z -->
<apiguardian-api.version>1.0.0</apiguardian-api.version>
<assertj.version>3.16.1</assertj.version>
<commons-compress.version>1.21</commons-compress.version>
<graalvm.version>20.3.0</graalvm.version>
<graalvm.version>20.3.2</graalvm.version>
<groovy.version>3.0.7</groovy.version>
<jakarta.inject.version>1.0</jakarta.inject.version>
<jansi.version>2.3.2</jansi.version>
<jline.version>3.20.0</jline.version>
<junit.jupiter.version>5.6.0</junit.jupiter.version>
<junit.jupiter.version>5.7.2</junit.jupiter.version>
<logback.version>1.2.3</logback.version>
<maven.version>3.8.2</maven.version>
<maven.resolver.version>1.6.2</maven.resolver.version>

View File

@@ -23,7 +23,6 @@ import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.RandomAccessFile;
import java.net.SocketAddress;
import java.net.StandardProtocolFamily;
import java.nio.channels.ByteChannel;
import java.nio.channels.Channels;
import java.nio.channels.FileLock;
@@ -48,8 +47,9 @@ import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.mvndaemon.mvnd.common.ByteChannelWrapper;
import org.mvndaemon.mvnd.common.JavaVersion;
import org.mvndaemon.mvnd.common.SocketHelper;
import org.mvndaemon.mvnd.common.SocketFamily;
import static org.mvndaemon.mvnd.sync.IpcMessages.REQUEST_ACQUIRE;
import static org.mvndaemon.mvnd.sync.IpcMessages.REQUEST_CLOSE;
@@ -82,7 +82,7 @@ public class IpcClient {
synchronized void ensureInitialized() throws IOException {
if (socket == null) {
socket = createClient();
ByteChannel wrapper = SocketHelper.wrapChannel(socket);
ByteChannel wrapper = new ByteChannelWrapper(socket);
input = new DataInputStream(Channels.newInputStream(wrapper));
output = new DataOutputStream(Channels.newOutputStream(wrapper));
receiver = new Thread(this::receive);
@@ -93,9 +93,9 @@ public class IpcClient {
SocketChannel createClient() throws IOException {
String familyProp = System.getProperty(FAMILY_PROP);
StandardProtocolFamily family = familyProp != null
? StandardProtocolFamily.valueOf(familyProp)
: JavaVersion.getJavaSpec() >= 16.0f ? StandardProtocolFamily.UNIX : StandardProtocolFamily.INET;
SocketFamily family = familyProp != null
? SocketFamily.valueOf(familyProp)
: JavaVersion.getJavaSpec() >= 16.0f ? SocketFamily.unix : SocketFamily.inet;
Path lockFile = repository.resolve(".maven-resolver-ipc-lock-" + family.name().toLowerCase())
.toAbsolutePath().normalize();
@@ -110,15 +110,15 @@ public class IpcClient {
String line = raf.readLine();
if (line != null) {
try {
SocketAddress address = SocketHelper.socketAddressFromString(line);
SocketAddress address = SocketFamily.fromString(line);
return SocketChannel.open(address);
} catch (IOException e) {
// ignore
}
}
ServerSocketChannel ss = SocketHelper.openServerSocket(family);
String tmpaddr = SocketHelper.socketAddressToString(ss.getLocalAddress());
ServerSocketChannel ss = family.openServerSocket();
String tmpaddr = SocketFamily.toString(ss.getLocalAddress());
String rand = Long.toHexString(new Random().nextLong());
boolean noNative = Boolean.getBoolean(IpcServer.NO_NATIVE_PROP);
@@ -142,7 +142,7 @@ public class IpcClient {
String javaCmd = win ? "bin\\java.exe" : "bin/java";
String java = Paths.get(javaHome).resolve(javaCmd).toAbsolutePath().toString();
args.add(java);
String classpath = getJarPath(getClass()) + File.pathSeparator + getJarPath(SocketHelper.class);
String classpath = getJarPath(getClass()) + File.pathSeparator + getJarPath(SocketFamily.class);
args.add("-cp");
args.add(classpath);
String timeout = System.getProperty(IpcServer.IDLE_TIMEOUT_PROP);
@@ -210,7 +210,7 @@ public class IpcClient {
throw new IllegalStateException("IpcServer did not respond with the correct random");
}
SocketAddress addr = SocketHelper.socketAddressFromString(res[1]);
SocketAddress addr = SocketFamily.fromString(res[1]);
SocketChannel socket = SocketChannel.open(addr);
raf.seek(0);
@@ -361,7 +361,7 @@ public class IpcClient {
private String getAddress() {
try {
return SocketHelper.socketAddressToString(socket.getLocalAddress());
return SocketFamily.toString(socket.getLocalAddress());
} catch (IOException e) {
return "[not bound]";
}

View File

@@ -19,7 +19,6 @@ import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.SocketAddress;
import java.net.StandardProtocolFamily;
import java.nio.channels.ByteChannel;
import java.nio.channels.Channels;
import java.nio.channels.ServerSocketChannel;
@@ -35,7 +34,8 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.mvndaemon.mvnd.common.SocketHelper;
import org.mvndaemon.mvnd.common.ByteChannelWrapper;
import org.mvndaemon.mvnd.common.SocketFamily;
import static org.mvndaemon.mvnd.sync.IpcMessages.REQUEST_ACQUIRE;
import static org.mvndaemon.mvnd.sync.IpcMessages.REQUEST_CLOSE;
@@ -69,8 +69,8 @@ public class IpcServer {
private volatile long lastUsed;
private volatile boolean closing;
public IpcServer(StandardProtocolFamily family) throws IOException {
serverSocket = SocketHelper.openServerSocket(family);
public IpcServer(SocketFamily family) throws IOException {
serverSocket = family.openServerSocket();
long timeout = IDLE_TIMEOUT;
String str = System.getProperty(IDLE_TIMEOUT_PROP);
if (str != null) {
@@ -111,15 +111,14 @@ public class IpcServer {
String tmpAddress = args[1];
String rand = args[2];
runServer(StandardProtocolFamily.valueOf(family), tmpAddress, rand);
runServer(SocketFamily.valueOf(family), tmpAddress, rand);
}
static IpcServer runServer(StandardProtocolFamily family, String tmpAddress, String rand) throws IOException {
static IpcServer runServer(SocketFamily family, String tmpAddress, String rand) throws IOException {
IpcServer server = new IpcServer(family);
run(server::run);
String address = SocketHelper.socketAddressToString(server.getLocalAddress());
SocketAddress socketAddress = SocketHelper.socketAddressFromString(tmpAddress);
SocketHelper.checkFamily(family, socketAddress);
String address = SocketFamily.toString(server.getLocalAddress());
SocketAddress socketAddress = SocketFamily.fromString(tmpAddress);
try (SocketChannel socket = SocketChannel.open(socketAddress)) {
try (DataOutputStream dos = new DataOutputStream(Channels.newOutputStream(socket))) {
dos.writeUTF(rand);
@@ -179,7 +178,7 @@ public class IpcServer {
use();
Map<String, Context> clientContexts = new ConcurrentHashMap<>();
try {
ByteChannel wrapper = SocketHelper.wrapChannel(socket);
ByteChannel wrapper = new ByteChannelWrapper(socket);
DataInputStream input = new DataInputStream(Channels.newInputStream(wrapper));
DataOutputStream output = new DataOutputStream(Channels.newOutputStream(wrapper));
while (!closing) {

View File

@@ -38,7 +38,7 @@ public class IpcSyncContextTest {
@BeforeAll
static void setup() {
System.setProperty(IpcServer.IDLE_TIMEOUT_PROP, "5");
System.setProperty(IpcServer.FAMILY_PROP, "INET");
System.setProperty(IpcServer.FAMILY_PROP, "inet");
System.setProperty(IpcServer.NO_NATIVE_PROP, "true");
}