17
17
package org .springframework .boot .context .embedded .undertow ;
18
18
19
19
import java .lang .reflect .Field ;
20
- import java .net .ServerSocket ;
20
+ import java .net .InetSocketAddress ;
21
+ import java .net .SocketAddress ;
21
22
import java .util .ArrayList ;
22
23
import java .util .List ;
23
24
33
34
import org .springframework .util .MimeTypeUtils ;
34
35
import org .springframework .util .ReflectionUtils ;
35
36
import org .springframework .util .StringUtils ;
37
+ import org .xnio .channels .BoundChannel ;
36
38
37
39
import io .undertow .Handlers ;
38
40
import io .undertow .Undertow ;
@@ -152,25 +154,15 @@ private String getPortsDescription() {
152
154
return "unknown" ;
153
155
}
154
156
155
- @ SuppressWarnings ("rawtypes" )
156
157
private List <Port > getPorts () {
157
158
List <Port > ports = new ArrayList <Port >();
158
159
try {
159
- // Use reflection if possible to get the underlying XNIO channels
160
160
if (!this .autoStart ) {
161
161
ports .add (new Port (-1 , "unknown" ));
162
162
}
163
163
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 ));
174
166
}
175
167
}
176
168
}
@@ -180,34 +172,22 @@ private List<Port> getPorts() {
180
172
return ports ;
181
173
}
182
174
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 );
196
181
}
197
182
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 );
208
189
}
209
- ReflectionUtils .makeAccessible (socketField );
210
- return (ServerSocket ) ReflectionUtils .getField (socketField , tcpServer );
190
+ return null ;
211
191
}
212
192
213
193
@ Override
0 commit comments