diff --git a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ClientConnector.java b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ClientConnector.java index f94fab824678..865d3c122159 100644 --- a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ClientConnector.java +++ b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ClientConnector.java @@ -15,12 +15,12 @@ import java.io.IOException; import java.net.InetSocketAddress; -import java.net.ProtocolFamily; import java.net.SocketAddress; import java.net.SocketException; import java.net.SocketOption; import java.net.StandardProtocolFamily; import java.net.StandardSocketOptions; +import java.net.UnixDomainSocketAddress; import java.nio.channels.NetworkChannel; import java.nio.channels.SelectableChannel; import java.nio.channels.SelectionKey; @@ -33,7 +33,6 @@ import java.util.concurrent.Executor; import org.eclipse.jetty.util.IO; -import org.eclipse.jetty.util.JavaVersion; import org.eclipse.jetty.util.Promise; import org.eclipse.jetty.util.annotation.ManagedAttribute; import org.eclipse.jetty.util.annotation.ManagedObject; @@ -677,21 +676,11 @@ private static Configurator forUnixDomain(Path path) return new Configurator() { @Override - public ChannelWithAddress newChannelWithAddress(ClientConnector clientConnector, SocketAddress address, Map context) + public ChannelWithAddress newChannelWithAddress(ClientConnector clientConnector, SocketAddress address, Map context) throws IOException { - try - { - ProtocolFamily family = Enum.valueOf(StandardProtocolFamily.class, "UNIX"); - SocketChannel socketChannel = (SocketChannel)SocketChannel.class.getMethod("open", ProtocolFamily.class).invoke(null, family); - Class addressClass = Class.forName("java.net.UnixDomainSocketAddress"); - SocketAddress socketAddress = (SocketAddress)addressClass.getMethod("of", Path.class).invoke(null, path); - return new ChannelWithAddress(socketChannel, socketAddress); - } - catch (Throwable x) - { - String message = "Unix-Domain SocketChannels are available starting from Java 16, your Java version is: " + JavaVersion.VERSION; - throw new UnsupportedOperationException(message, x); - } + SocketChannel socketChannel = SocketChannel.open(StandardProtocolFamily.UNIX); + UnixDomainSocketAddress socketAddress = UnixDomainSocketAddress.of(path); + return new ChannelWithAddress(socketChannel, socketAddress); } }; } diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/ProxyConnectionFactory.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/ProxyConnectionFactory.java index 0d211cb68383..f8b747090417 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/ProxyConnectionFactory.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/ProxyConnectionFactory.java @@ -19,6 +19,7 @@ import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.SocketAddress; +import java.net.UnixDomainSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.ReadPendingException; import java.nio.channels.WritePendingException; @@ -636,11 +637,11 @@ private void parseBodyAndUpgrade() throws IOException { byte[] addr = new byte[108]; byteBuffer.get(addr); - String src = UnixDomain.toPath(addr); + String src = toUnixDomainPath(addr); byteBuffer.get(addr); - String dst = UnixDomain.toPath(addr); - local = UnixDomain.newSocketAddress(dst); - remote = UnixDomain.newSocketAddress(src); + String dst = toUnixDomainPath(addr); + local = UnixDomainSocketAddress.of(dst); + remote = UnixDomainSocketAddress.of(src); } default -> throw new IllegalStateException("Unsupported family " + _family); } @@ -765,6 +766,18 @@ private void releaseAndClose() _buffer.release(); close(); } + + private static String toUnixDomainPath(byte[] bytes) + { + // Unix-Domain paths are zero-terminated. + int i = 0; + while (i < bytes.length) + { + if (bytes[i++] == 0) + break; + } + return new String(bytes, 0, i, StandardCharsets.US_ASCII).trim(); + } } } @@ -973,51 +986,4 @@ public void write(Callback callback, ByteBuffer... buffers) throws WritePendingE _endPoint.write(callback, buffers); } } - - private static class UnixDomain - { - private static final Class unixDomainSocketAddress = probe(); - - private static Class probe() - { - try - { - return ClassLoader.getPlatformClassLoader().loadClass("java.net.UnixDomainSocketAddress"); - } - catch (Throwable x) - { - if (LOG.isDebugEnabled()) - LOG.debug("ignored", x); - return null; - } - } - - private static SocketAddress newSocketAddress(String path) - { - try - { - if (unixDomainSocketAddress != null) - return (SocketAddress)unixDomainSocketAddress.getMethod("of", String.class).invoke(null, path); - return null; - } - catch (Throwable x) - { - if (LOG.isDebugEnabled()) - LOG.debug("ignored", x); - return null; - } - } - - private static String toPath(byte[] bytes) - { - // Unix-Domain paths are zero-terminated. - int i = 0; - while (i < bytes.length) - { - if (bytes[i++] == 0) - break; - } - return new String(bytes, 0, i, StandardCharsets.US_ASCII).trim(); - } - } } diff --git a/jetty-core/jetty-unixdomain-server/src/test/java/org/eclipse/jetty/unixdomain/server/UnixDomainTest.java b/jetty-core/jetty-unixdomain-server/src/test/java/org/eclipse/jetty/unixdomain/server/UnixDomainTest.java index a4fd8b4bfb96..8ce4025e7894 100644 --- a/jetty-core/jetty-unixdomain-server/src/test/java/org/eclipse/jetty/unixdomain/server/UnixDomainTest.java +++ b/jetty-core/jetty-unixdomain-server/src/test/java/org/eclipse/jetty/unixdomain/server/UnixDomainTest.java @@ -15,6 +15,7 @@ import java.io.IOException; import java.net.SocketAddress; +import java.net.UnixDomainSocketAddress; import java.nio.file.Files; import java.nio.file.Path; import java.util.concurrent.TimeUnit; @@ -40,11 +41,7 @@ import org.hamcrest.Matchers; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Assumptions; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.EnabledForJreRange; -import org.junit.jupiter.api.condition.JRE; import static org.eclipse.jetty.client.ProxyProtocolClientConnectionFactory.V1; import static org.eclipse.jetty.client.ProxyProtocolClientConnectionFactory.V2; @@ -56,33 +53,12 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -@EnabledForJreRange(min = JRE.JAVA_16) public class UnixDomainTest { - private static final Class unixDomainSocketAddressClass = probe(); - - private static Class probe() - { - try - { - return ClassLoader.getPlatformClassLoader().loadClass("java.net.UnixDomainSocketAddress"); - } - catch (Throwable x) - { - return null; - } - } - private ConnectionFactory[] factories = new ConnectionFactory[]{new HttpConnectionFactory()}; private Server server; private Path unixDomainPath; - @BeforeEach - public void prepare() - { - Assumptions.assumeTrue(unixDomainSocketAddressClass != null); - } - private void start(Handler handler) throws Exception { server = new Server(); @@ -120,9 +96,9 @@ public boolean handle(Request request, Response response, Callback callback) // Verify the SocketAddresses. SocketAddress local = endPoint.getLocalSocketAddress(); - assertThat(local, Matchers.instanceOf(unixDomainSocketAddressClass)); + assertThat(local, Matchers.instanceOf(UnixDomainSocketAddress.class)); SocketAddress remote = endPoint.getRemoteSocketAddress(); - assertThat(remote, Matchers.instanceOf(unixDomainSocketAddressClass)); + assertThat(remote, Matchers.instanceOf(UnixDomainSocketAddress.class)); // Verify that other address methods don't throw. local = assertDoesNotThrow(endPoint::getLocalAddress); @@ -205,8 +181,8 @@ public boolean handle(Request request, Response response, Callback callback) { EndPoint endPoint = request.getConnectionMetaData().getConnection().getEndPoint(); assertThat(endPoint, Matchers.instanceOf(ProxyConnectionFactory.ProxyEndPoint.class)); - assertThat(endPoint.getLocalSocketAddress(), Matchers.instanceOf(unixDomainSocketAddressClass)); - assertThat(endPoint.getRemoteSocketAddress(), Matchers.instanceOf(unixDomainSocketAddressClass)); + assertThat(endPoint.getLocalSocketAddress(), Matchers.instanceOf(UnixDomainSocketAddress.class)); + assertThat(endPoint.getRemoteSocketAddress(), Matchers.instanceOf(UnixDomainSocketAddress.class)); String target = Request.getPathInContext(request); if ("/v1".equals(target)) { @@ -273,15 +249,8 @@ public void testInvalidUnixDomainPath() private static Path toUnixDomainPath(SocketAddress address) { - try - { - Assertions.assertNotNull(unixDomainSocketAddressClass); - return (Path)unixDomainSocketAddressClass.getMethod("getPath").invoke(address); - } - catch (Throwable x) - { - Assertions.fail(x); - throw new AssertionError(); - } + if (address instanceof UnixDomainSocketAddress unix) + return unix.getPath(); + throw new AssertionError(); } } diff --git a/tests/test-distribution/test-distribution-common/src/test/java/org/eclipse/jetty/tests/distribution/DistributionTests.java b/tests/test-distribution/test-distribution-common/src/test/java/org/eclipse/jetty/tests/distribution/DistributionTests.java index 607cc2fc1820..2569f6d236c5 100644 --- a/tests/test-distribution/test-distribution-common/src/test/java/org/eclipse/jetty/tests/distribution/DistributionTests.java +++ b/tests/test-distribution/test-distribution-common/src/test/java/org/eclipse/jetty/tests/distribution/DistributionTests.java @@ -968,7 +968,6 @@ public void testDefaultLoggingProviderNotActiveWhenExplicitProviderIsPresent() t } @Test - @EnabledForJreRange(min = JRE.JAVA_16) public void testUnixDomain() throws Exception { Path jettyBase = newTestJettyBaseDirectory();