32
32
import java .nio .file .Path ;
33
33
import java .nio .file .Paths ;
34
34
import java .util .*;
35
+ import java .util .function .Predicate ;
35
36
import javax .management .*;
36
37
import jakarta .servlet .ServletConfig ;
37
38
import jakarta .servlet .ServletException ;
@@ -92,7 +93,7 @@ public class JMXServlet extends HttpServlet {
92
93
93
94
private Path dataDir ;
94
95
private Path tokenFile ;
95
-
96
+
96
97
@ Override
97
98
protected void doGet (final HttpServletRequest request , final HttpServletResponse response ) throws ServletException , IOException {
98
99
@@ -116,7 +117,7 @@ protected void doGet(final HttpServletRequest request, final HttpServletResponse
116
117
}
117
118
118
119
private void writeXmlData (final HttpServletRequest request , final HttpServletResponse response ) throws ServletException , IOException {
119
- Element root ;
120
+ final Element root ;
120
121
121
122
final String operation = request .getParameter ("operation" );
122
123
if ("ping" .equals (operation )) {
@@ -215,23 +216,21 @@ public void init(final ServletConfig config) throws ServletException {
215
216
void registerLocalHostAddresses () {
216
217
217
218
try {
218
- for (final NetworkInterface networkInterface : NetworkInterface .networkInterfaces ().toList ()) {
219
- if (networkInterface .isLoopback ()) {
220
- final Enumeration <InetAddress > inetAddresses = networkInterface .getInetAddresses ();
221
- while (inetAddresses .hasMoreElements ()) {
222
- final String hostAddress = inetAddresses .nextElement ().getHostAddress ();
223
- if (hostAddress != null ) {
224
- if (hostAddress .contains ("%" )) {
225
- final int position = hostAddress .indexOf ("%" );
226
- localhostAddresses .add (hostAddress .substring (0 , position ));
227
- } else {
228
- localhostAddresses .add (hostAddress );
229
- }
230
- }
231
- }
232
-
219
+ final Predicate <NetworkInterface > isLoopback = networkInterface -> {
220
+ try {
221
+ return networkInterface .isLoopback ();
222
+ } catch (final SocketException e ) {
223
+ LOG .error ("Unable to determine if {} is a loopback interface" , e .getMessage (), e );
224
+ return false ;
233
225
}
234
- }
226
+ };
227
+
228
+ NetworkInterface .networkInterfaces ()
229
+ .filter (isLoopback )
230
+ .flatMap (NetworkInterface ::inetAddresses )
231
+ .map (InetAddress ::getHostAddress )
232
+ .map (a -> a .replaceFirst ("%.+" , "" ))
233
+ .forEach (localhostAddresses ::add );
235
234
236
235
} catch (final SocketException e ) {
237
236
LOG .error ("Unable to determine localhost loopback addresses: {} " , e .getMessage (), e );
0 commit comments