Fixes #6990 - UnixDomainServerConnector throws misleading exception on invalid socket path.
Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
This commit is contained in:
parent
b06ccf286d
commit
6aedf4af6e
|
@ -233,24 +233,37 @@ public class UnixDomainServerConnector extends AbstractConnector
|
|||
return serverChannel;
|
||||
}
|
||||
|
||||
private ServerSocketChannel bindServerSocketChannel()
|
||||
private ServerSocketChannel bindServerSocketChannel() throws IOException
|
||||
{
|
||||
Path unixDomainPath = getUnixDomainPath();
|
||||
|
||||
ServerSocketChannel serverChannel;
|
||||
SocketAddress socketAddress;
|
||||
try
|
||||
{
|
||||
ProtocolFamily family = Enum.valueOf(StandardProtocolFamily.class, "UNIX");
|
||||
Class<?> channelClass = Class.forName("java.nio.channels.ServerSocketChannel");
|
||||
ServerSocketChannel serverChannel = (ServerSocketChannel)channelClass.getMethod("open", ProtocolFamily.class).invoke(null, family);
|
||||
serverChannel = (ServerSocketChannel)channelClass.getMethod("open", ProtocolFamily.class).invoke(null, family);
|
||||
// Unix-Domain does not support SO_REUSEADDR.
|
||||
Class<?> addressClass = Class.forName("java.net.UnixDomainSocketAddress");
|
||||
SocketAddress socketAddress = (SocketAddress)addressClass.getMethod("of", Path.class).invoke(null, getUnixDomainPath());
|
||||
serverChannel.bind(socketAddress, getAcceptQueueSize());
|
||||
return serverChannel;
|
||||
socketAddress = (SocketAddress)addressClass.getMethod("of", Path.class).invoke(null, unixDomainPath);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
serverChannel.bind(socketAddress, getAcceptQueueSize());
|
||||
return serverChannel;
|
||||
}
|
||||
catch (IOException x)
|
||||
{
|
||||
String message = String.format("Could not bind %s to %s", UnixDomainServerConnector.class.getSimpleName(), unixDomainPath);
|
||||
throw new IOException(message, x);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
|
||||
package org.eclipse.jetty.unixdomain.server;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.SocketAddress;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
@ -53,6 +54,7 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
|||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@EnabledForJreRange(min = JRE.JAVA_16)
|
||||
|
@ -256,6 +258,16 @@ public class UnixDomainTest
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidUnixDomainPath()
|
||||
{
|
||||
server = new Server();
|
||||
UnixDomainServerConnector connector = new UnixDomainServerConnector(server, factories);
|
||||
connector.setUnixDomainPath(Path.of("/does/not/exist"));
|
||||
server.addConnector(connector);
|
||||
assertThrows(IOException.class, () -> server.start());
|
||||
}
|
||||
|
||||
private static Path toUnixDomainPath(SocketAddress address)
|
||||
{
|
||||
try
|
||||
|
|
Loading…
Reference in New Issue