From d357533105da234d53115fda3b6a6e51a150702e Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 24 Sep 2015 10:47:58 +0100 Subject: [PATCH] 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 --- .../UndertowEmbeddedServletContainer.java | 56 ++++++------------- 1 file changed, 18 insertions(+), 38 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainer.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainer.java index ac001f6427..7833c98e79 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainer.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainer.java @@ -17,7 +17,8 @@ package org.springframework.boot.context.embedded.undertow; import java.lang.reflect.Field; -import java.net.ServerSocket; +import java.net.InetSocketAddress; +import java.net.SocketAddress; import java.util.ArrayList; import java.util.List; @@ -33,6 +34,7 @@ import org.springframework.util.MimeType; import org.springframework.util.MimeTypeUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.StringUtils; +import org.xnio.channels.BoundChannel; import io.undertow.Handlers; import io.undertow.Undertow; @@ -152,25 +154,15 @@ public class UndertowEmbeddedServletContainer implements EmbeddedServletContaine return "unknown"; } - @SuppressWarnings("rawtypes") private List getPorts() { List ports = new ArrayList(); try { - // Use reflection if possible to get the underlying XNIO channels if (!this.autoStart) { ports.add(new Port(-1, "unknown")); } else { - Field channelsField = ReflectionUtils.findField(Undertow.class, - "channels"); - ReflectionUtils.makeAccessible(channelsField); - List channels = (List) ReflectionUtils.getField(channelsField, - this.undertow); - for (Object channel : channels) { - Port port = getPortFromChannel(channel); - if (port != null) { - ports.add(port); - } + for (BoundChannel channel : extractChannels()) { + ports.add(getPortFromChannel(channel)); } } } @@ -180,34 +172,22 @@ public class UndertowEmbeddedServletContainer implements EmbeddedServletContaine return ports; } - private Port getPortFromChannel(Object channel) { - Object tcpServer = channel; - String protocol = "http"; - Field sslContext = ReflectionUtils.findField(channel.getClass(), "sslContext"); - if (sslContext != null) { - tcpServer = getTcpServer(channel); - protocol = "https"; - } - ServerSocket socket = getSocket(tcpServer); - if (socket != null) { - return new Port(socket.getLocalPort(), protocol); - } - return null; + @SuppressWarnings("unchecked") + private List extractChannels() { + Field channelsField = ReflectionUtils.findField(Undertow.class, "channels"); + ReflectionUtils.makeAccessible(channelsField); + return (List) ReflectionUtils + .getField(channelsField, this.undertow); } - private Object getTcpServer(Object channel) { - Field field = ReflectionUtils.findField(channel.getClass(), "tcpServer"); - ReflectionUtils.makeAccessible(field); - return ReflectionUtils.getField(field, channel); - } - - private ServerSocket getSocket(Object tcpServer) { - Field socketField = ReflectionUtils.findField(tcpServer.getClass(), "socket"); - if (socketField == null) { - return null; + private Port getPortFromChannel(BoundChannel channel) { + String protocol = ReflectionUtils.findField(channel.getClass(), "ssl") != null ? "https" + : "http"; + SocketAddress socketAddress = channel.getLocalAddress(); + if (socketAddress instanceof InetSocketAddress) { + return new Port(((InetSocketAddress) socketAddress).getPort(), protocol); } - ReflectionUtils.makeAccessible(socketField); - return (ServerSocket) ReflectionUtils.getField(socketField, tcpServer); + return null; } @Override