diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyWebServer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyWebServer.java index 1d6ba620d5..aebc93149d 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyWebServer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyWebServer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-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. @@ -207,18 +207,6 @@ public class JettyWebServer implements WebServer { return ports.toString(); } - private Integer getLocalPort(Connector connector) { - try { - // Jetty 9 internals are different, but the method name is the same - return (Integer) ReflectionUtils - .invokeMethod(ReflectionUtils.findMethod(connector.getClass(), "getLocalPort"), connector); - } - catch (Exception ex) { - logger.info("could not determine port ( " + ex.getMessage() + ")"); - return 0; - } - } - private String getProtocols(Connector connector) { List protocols = connector.getProtocols(); return " (" + StringUtils.collectionToDelimitedString(protocols, ", ") + ")"; @@ -276,8 +264,29 @@ public class JettyWebServer implements WebServer { public int getPort() { Connector[] connectors = this.server.getConnectors(); for (Connector connector : connectors) { - // Probably only one... - return getLocalPort(connector); + Integer localPort = getLocalPort(connector); + if (localPort != null && localPort > 0) { + return localPort; + } + } + return -1; + } + + private Integer getLocalPort(Connector connector) { + try { + if (connector instanceof NetworkConnector) { + return ((NetworkConnector) connector).getLocalPort(); + } + } + catch (Exception ex) { + } + try { + // Jetty 9 internals are different, but the method name is the same + return (Integer) ReflectionUtils + .invokeMethod(ReflectionUtils.findMethod(connector.getClass(), "getLocalPort"), connector); + } + catch (Exception ex) { + logger.info("could not determine port ( " + ex.getMessage() + ")"); } return 0; } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/NettyWebServer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/NettyWebServer.java index e2daf9bde3..f3c5697e1b 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/NettyWebServer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/NettyWebServer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-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. @@ -217,7 +217,7 @@ public class NettyWebServer implements WebServer { return -1; } } - return 0; + return -1; } } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatWebServer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatWebServer.java index 6f282bb15d..25c260c4f0 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatWebServer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatWebServer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-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. @@ -364,7 +364,7 @@ public class TomcatWebServer implements WebServer { if (connector != null) { return connector.getLocalPort(); } - return 0; + return -1; } private String getContextPath() { diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowWebServer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowWebServer.java index 5715f1ea3c..5278bf6920 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowWebServer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowWebServer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-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. @@ -295,7 +295,7 @@ public class UndertowWebServer implements WebServer { public int getPort() { List ports = getActualPorts(); if (ports.isEmpty()) { - return 0; + return -1; } return ports.get(0).getNumber(); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/server/AbstractReactiveWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/server/AbstractReactiveWebServerFactoryTests.java index 5db51932a1..25d1bea19d 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/server/AbstractReactiveWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/server/AbstractReactiveWebServerFactoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-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. @@ -115,6 +115,16 @@ public abstract class AbstractReactiveWebServerFactoryTests { assertThat(this.webServer.getPort()).isEqualTo(specificPort); } + @Test + void portIsMinusOneWhenConnectionIsClosed() { + AbstractReactiveWebServerFactory factory = getFactory(); + this.webServer = factory.getWebServer(new EchoHandler()); + this.webServer.start(); + assertThat(this.webServer.getPort()).isGreaterThan(0); + this.webServer.stop(); + assertThat(this.webServer.getPort()).isEqualTo(-1); + } + @Test void basicSslFromClassPath() { testBasicSslWithKeyStore("classpath:test.jks", "password"); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java index 7e92f7f48f..3b6b073491 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-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. @@ -250,7 +250,7 @@ public abstract class AbstractServletWebServerFactoryTests { factory.setPort(-1); this.webServer = factory.getWebServer(exampleServletRegistration()); this.webServer.start(); - assertThat(this.webServer.getPort()).isLessThan(0); // Jetty is -2 + assertThat(this.webServer.getPort()).isEqualTo(-1); } @Test @@ -300,6 +300,16 @@ public abstract class AbstractServletWebServerFactoryTests { assertThat(servlet.getInitCount()).isEqualTo(1); } + @Test + void portIsMinusOneWhenConnectionIsClosed() { + AbstractServletWebServerFactory factory = getFactory(); + this.webServer = factory.getWebServer(); + this.webServer.start(); + assertThat(this.webServer.getPort()).isGreaterThan(0); + this.webServer.stop(); + assertThat(this.webServer.getPort()).isEqualTo(-1); + } + @Test void specificPort() throws Exception { AbstractServletWebServerFactory factory = getFactory();