Skip to content

Commit 9b9c3ed

Browse files
committed
Polish "Return -1 port for non-listening WebServers"
See gh-24606
1 parent 5c61df3 commit 9b9c3ed

File tree

6 files changed

+30
-23
lines changed

6 files changed

+30
-23
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyWebServer.java

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -207,18 +207,6 @@ private String getActualPortsDescription() {
207207
return ports.toString();
208208
}
209209

210-
private Integer getLocalPort(Connector connector) {
211-
try {
212-
// Jetty 9 internals are different, but the method name is the same
213-
return (Integer) ReflectionUtils
214-
.invokeMethod(ReflectionUtils.findMethod(connector.getClass(), "getLocalPort"), connector);
215-
}
216-
catch (Exception ex) {
217-
logger.info("could not determine port ( " + ex.getMessage() + ")");
218-
return 0;
219-
}
220-
}
221-
222210
private String getProtocols(Connector connector) {
223211
List<String> protocols = connector.getProtocols();
224212
return " (" + StringUtils.collectionToDelimitedString(protocols, ", ") + ")";
@@ -275,13 +263,32 @@ public void stop() {
275263
@Override
276264
public int getPort() {
277265
Connector[] connectors = this.server.getConnectors();
278-
Integer localPort = -1;
279266
for (Connector connector : connectors) {
280-
// Probably only one...
281-
localPort = getLocalPort(connector);
282-
break;
267+
Integer localPort = getLocalPort(connector);
268+
if (localPort != null && localPort > 0) {
269+
return localPort;
270+
}
271+
}
272+
return -1;
273+
}
274+
275+
private Integer getLocalPort(Connector connector) {
276+
try {
277+
if (connector instanceof NetworkConnector) {
278+
return ((NetworkConnector) connector).getLocalPort();
279+
}
280+
}
281+
catch (Exception ex) {
282+
}
283+
try {
284+
// Jetty 9 internals are different, but the method name is the same
285+
return (Integer) ReflectionUtils
286+
.invokeMethod(ReflectionUtils.findMethod(connector.getClass(), "getLocalPort"), connector);
287+
}
288+
catch (Exception ex) {
289+
logger.info("could not determine port ( " + ex.getMessage() + ")");
283290
}
284-
return (localPort > 0) ? localPort : -1;
291+
return 0;
285292
}
286293

287294
@Override

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/NettyWebServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatWebServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowWebServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/server/AbstractReactiveWebServerFactoryTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)