Skip to content

Commit d357533

Browse files
committed
Upgrade to Undertow 1.3.0.CR2
The code that uses reflection to determine the protocol and port has been updated to align with changes made in Undertow 1.3 See gh-3969
1 parent a23d11f commit d357533

File tree

1 file changed

+18
-38
lines changed

1 file changed

+18
-38
lines changed

spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainer.java

+18-38
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
package org.springframework.boot.context.embedded.undertow;
1818

1919
import java.lang.reflect.Field;
20-
import java.net.ServerSocket;
20+
import java.net.InetSocketAddress;
21+
import java.net.SocketAddress;
2122
import java.util.ArrayList;
2223
import java.util.List;
2324

@@ -33,6 +34,7 @@
3334
import org.springframework.util.MimeTypeUtils;
3435
import org.springframework.util.ReflectionUtils;
3536
import org.springframework.util.StringUtils;
37+
import org.xnio.channels.BoundChannel;
3638

3739
import io.undertow.Handlers;
3840
import io.undertow.Undertow;
@@ -152,25 +154,15 @@ private String getPortsDescription() {
152154
return "unknown";
153155
}
154156

155-
@SuppressWarnings("rawtypes")
156157
private List<Port> getPorts() {
157158
List<Port> ports = new ArrayList<Port>();
158159
try {
159-
// Use reflection if possible to get the underlying XNIO channels
160160
if (!this.autoStart) {
161161
ports.add(new Port(-1, "unknown"));
162162
}
163163
else {
164-
Field channelsField = ReflectionUtils.findField(Undertow.class,
165-
"channels");
166-
ReflectionUtils.makeAccessible(channelsField);
167-
List channels = (List) ReflectionUtils.getField(channelsField,
168-
this.undertow);
169-
for (Object channel : channels) {
170-
Port port = getPortFromChannel(channel);
171-
if (port != null) {
172-
ports.add(port);
173-
}
164+
for (BoundChannel channel : extractChannels()) {
165+
ports.add(getPortFromChannel(channel));
174166
}
175167
}
176168
}
@@ -180,34 +172,22 @@ private List<Port> getPorts() {
180172
return ports;
181173
}
182174

183-
private Port getPortFromChannel(Object channel) {
184-
Object tcpServer = channel;
185-
String protocol = "http";
186-
Field sslContext = ReflectionUtils.findField(channel.getClass(), "sslContext");
187-
if (sslContext != null) {
188-
tcpServer = getTcpServer(channel);
189-
protocol = "https";
190-
}
191-
ServerSocket socket = getSocket(tcpServer);
192-
if (socket != null) {
193-
return new Port(socket.getLocalPort(), protocol);
194-
}
195-
return null;
175+
@SuppressWarnings("unchecked")
176+
private List<BoundChannel> extractChannels() {
177+
Field channelsField = ReflectionUtils.findField(Undertow.class, "channels");
178+
ReflectionUtils.makeAccessible(channelsField);
179+
return (List<BoundChannel>) ReflectionUtils
180+
.getField(channelsField, this.undertow);
196181
}
197182

198-
private Object getTcpServer(Object channel) {
199-
Field field = ReflectionUtils.findField(channel.getClass(), "tcpServer");
200-
ReflectionUtils.makeAccessible(field);
201-
return ReflectionUtils.getField(field, channel);
202-
}
203-
204-
private ServerSocket getSocket(Object tcpServer) {
205-
Field socketField = ReflectionUtils.findField(tcpServer.getClass(), "socket");
206-
if (socketField == null) {
207-
return null;
183+
private Port getPortFromChannel(BoundChannel channel) {
184+
String protocol = ReflectionUtils.findField(channel.getClass(), "ssl") != null ? "https"
185+
: "http";
186+
SocketAddress socketAddress = channel.getLocalAddress();
187+
if (socketAddress instanceof InetSocketAddress) {
188+
return new Port(((InetSocketAddress) socketAddress).getPort(), protocol);
208189
}
209-
ReflectionUtils.makeAccessible(socketField);
210-
return (ServerSocket) ReflectionUtils.getField(socketField, tcpServer);
190+
return null;
211191
}
212192

213193
@Override

0 commit comments

Comments
 (0)