Merge branch '2.2.x'
Closes gh-21053
This commit is contained in:
@@ -175,7 +175,7 @@ public class JettyWebServer implements WebServer {
|
||||
}
|
||||
catch (IOException ex) {
|
||||
if (connector instanceof NetworkConnector && findBindException(ex) != null) {
|
||||
throw new PortInUseException(((NetworkConnector) connector).getPort());
|
||||
throw new PortInUseException(((NetworkConnector) connector).getPort(), ex);
|
||||
}
|
||||
throw ex;
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.util.function.BiFunction;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import io.netty.channel.group.DefaultChannelGroup;
|
||||
import io.netty.channel.unix.Errors.NativeIoException;
|
||||
import io.netty.util.concurrent.DefaultEventExecutor;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
@@ -53,7 +54,12 @@ import org.springframework.util.Assert;
|
||||
*/
|
||||
public class NettyWebServer implements WebServer {
|
||||
|
||||
private static final Predicate<HttpServerRequest> ALWAYS = (r) -> true;
|
||||
/**
|
||||
* Permission denied error code from {@code errno.h}.
|
||||
*/
|
||||
private static final int ERROR_NO_EACCES = -13;
|
||||
|
||||
private static final Predicate<HttpServerRequest> ALWAYS = (request) -> true;
|
||||
|
||||
private static final Log logger = LogFactory.getLog(NettyWebServer.class);
|
||||
|
||||
@@ -111,8 +117,8 @@ public class NettyWebServer implements WebServer {
|
||||
}
|
||||
catch (Exception ex) {
|
||||
ChannelBindException bindException = findBindException(ex);
|
||||
if (bindException != null) {
|
||||
throw new PortInUseException(bindException.localPort());
|
||||
if (bindException != null && !isPermissionDenied(bindException.getCause())) {
|
||||
throw new PortInUseException(bindException.localPort(), ex);
|
||||
}
|
||||
throw new WebServerException("Unable to start Netty", ex);
|
||||
}
|
||||
@@ -121,6 +127,17 @@ public class NettyWebServer implements WebServer {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isPermissionDenied(Throwable bindExceptionCause) {
|
||||
try {
|
||||
if (bindExceptionCause instanceof NativeIoException) {
|
||||
return ((NativeIoException) bindExceptionCause).expectedErr() == ERROR_NO_EACCES;
|
||||
}
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shutDownGracefully() {
|
||||
return this.shutdown.shutDownGracefully();
|
||||
|
||||
@@ -178,7 +178,7 @@ public class UndertowServletWebServer implements WebServer {
|
||||
List<Port> actualPorts = getActualPorts();
|
||||
failedPorts.removeAll(actualPorts);
|
||||
if (failedPorts.size() == 1) {
|
||||
throw new PortInUseException(failedPorts.iterator().next().getNumber());
|
||||
throw new PortInUseException(failedPorts.iterator().next().getNumber(), ex);
|
||||
}
|
||||
}
|
||||
throw new WebServerException("Unable to start embedded Undertow", ex);
|
||||
|
||||
@@ -126,7 +126,7 @@ public class UndertowWebServer implements WebServer {
|
||||
List<UndertowWebServer.Port> actualPorts = getActualPorts();
|
||||
failedPorts.removeAll(actualPorts);
|
||||
if (failedPorts.size() == 1) {
|
||||
throw new PortInUseException(failedPorts.iterator().next().getNumber());
|
||||
throw new PortInUseException(failedPorts.iterator().next().getNumber(), ex);
|
||||
}
|
||||
}
|
||||
throw new WebServerException("Unable to start embedded Undertow", ex);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2012-2020 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.
|
||||
@@ -32,7 +32,16 @@ public class PortInUseException extends WebServerException {
|
||||
* @param port the port that was in use
|
||||
*/
|
||||
public PortInUseException(int port) {
|
||||
super("Port " + port + " is already in use", null);
|
||||
this(port, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new port in use exception for the given {@code port}.
|
||||
* @param port the port that was in use
|
||||
* @param cause the cause of the exception
|
||||
*/
|
||||
public PortInUseException(int port, Throwable cause) {
|
||||
super("Port " + port + " is already in use", cause);
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
|
||||
@@ -66,7 +66,7 @@ class NettyReactiveWebServerFactoryTests extends AbstractReactiveWebServerFactor
|
||||
this.webServer.start();
|
||||
factory.setPort(this.webServer.getPort());
|
||||
assertThatExceptionOfType(PortInUseException.class).isThrownBy(factory.getWebServer(new EchoHandler())::start)
|
||||
.satisfies(this::portMatchesRequirement);
|
||||
.satisfies(this::portMatchesRequirement).withCauseInstanceOf(Throwable.class);
|
||||
}
|
||||
|
||||
private void portMatchesRequirement(PortInUseException exception) {
|
||||
|
||||
Reference in New Issue
Block a user