mirror of
https://github.com/apache/maven-mvnd.git
synced 2025-09-09 14:49:06 +00:00
Introduce a SocketFamily instead
This commit is contained in:
172
common/pom.xml
172
common/pom.xml
@@ -107,51 +107,135 @@
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<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>jdk16</id>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<fork>true</fork>
|
||||
<compilerArgs>
|
||||
<arg>-XDignore.symbol.file</arg>
|
||||
</compilerArgs>
|
||||
<release>16</release>
|
||||
<multiReleaseOutput>true</multiReleaseOutput>
|
||||
<compileSourceRoots>
|
||||
<root>${project.basedir}/src/main/java16</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>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>jdk11-15</id>
|
||||
<activation>
|
||||
<jdk>[11,15)</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>
|
||||
</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>
|
||||
<goal>compile</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<fork>true</fork>
|
||||
<compilerArgs>
|
||||
<arg>-XDignore.symbol.file</arg>
|
||||
</compilerArgs>
|
||||
<release>16</release>
|
||||
<multiReleaseOutput>true</multiReleaseOutput>
|
||||
<compileSourceRoots>
|
||||
<root>${project.basedir}/src/main/java16</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>
|
||||
</profiles>
|
||||
|
||||
</project>
|
@@ -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;
|
||||
|
||||
|
@@ -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() {
|
||||
}
|
||||
|
||||
}
|
141
common/src/main/java/org/mvndaemon/mvnd/common/SocketFamily.java
Normal file
141
common/src/main/java/org/mvndaemon/mvnd/common/SocketFamily.java
Normal 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 + "'");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -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);
|
||||
}
|
||||
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 SocketAddress unixSocketAddressOf(String s) {
|
||||
throw new UnsupportedOperationException("Unix sockets are supported only on JDK >= 16");
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
|
||||
}
|
@@ -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);
|
||||
}
|
||||
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 SocketAddress unixSocketAddressOf(String s) {
|
||||
return UnixDomainSocketAddress.of(s);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -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));
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user