Skip to content

Commit bb54e0d

Browse files
feat(issue-1323): add support for Spring InetUtils in DefaultApplicationFactory
1 parent a6afd1d commit bb54e0d

4 files changed

Lines changed: 78 additions & 14 deletions

File tree

spring-boot-admin-client/src/main/java/de/codecentric/boot/admin/client/config/SpringBootAdminClientAutoConfiguration.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.Collections;
2020
import java.util.List;
21+
import java.util.Optional;
2122

2223
import jakarta.servlet.ServletContext;
2324
import org.springframework.beans.factory.ObjectProvider;
@@ -112,11 +113,11 @@ public static class ServletConfiguration {
112113
public ApplicationFactory applicationFactory(InstanceProperties instance, ManagementServerProperties management,
113114
ServerProperties server, ServletContext servletContext, PathMappedEndpoints pathMappedEndpoints,
114115
WebEndpointProperties webEndpoint, ObjectProvider<List<MetadataContributor>> metadataContributors,
115-
DispatcherServletPath dispatcherServletPath) {
116+
DispatcherServletPath dispatcherServletPath, ObjectProvider<?> inetUtilsProvider) {
116117
return new ServletApplicationFactory(instance, management, server, servletContext, pathMappedEndpoints,
117118
webEndpoint,
118119
new CompositeMetadataContributor(metadataContributors.getIfAvailable(Collections::emptyList)),
119-
dispatcherServletPath);
120+
dispatcherServletPath, Optional.ofNullable(inetUtilsProvider.getIfAvailable()));
120121
}
121122

122123
}
@@ -130,10 +131,11 @@ public static class ReactiveConfiguration {
130131
@ConditionalOnMissingBean
131132
public ApplicationFactory applicationFactory(InstanceProperties instance, ManagementServerProperties management,
132133
ServerProperties server, PathMappedEndpoints pathMappedEndpoints, WebEndpointProperties webEndpoint,
133-
ObjectProvider<List<MetadataContributor>> metadataContributors, WebFluxProperties webFluxProperties) {
134+
ObjectProvider<List<MetadataContributor>> metadataContributors, WebFluxProperties webFluxProperties,
135+
ObjectProvider<?> inetUtilsProvider) {
134136
return new ReactiveApplicationFactory(instance, management, server, pathMappedEndpoints, webEndpoint,
135137
new CompositeMetadataContributor(metadataContributors.getIfAvailable(Collections::emptyList)),
136-
webFluxProperties);
138+
webFluxProperties, Optional.ofNullable(inetUtilsProvider.getIfAvailable()));
137139
}
138140

139141
}

spring-boot-admin-client/src/main/java/de/codecentric/boot/admin/client/registration/DefaultApplicationFactory.java

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.net.UnknownHostException;
2121
import java.util.LinkedHashMap;
2222
import java.util.Map;
23+
import java.util.Optional;
2324

2425
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
2526
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties;
@@ -45,6 +46,8 @@
4546
*/
4647
public class DefaultApplicationFactory implements ApplicationFactory {
4748

49+
// Removed InetUtils dependency; using standard Java APIs for host resolution.
50+
4851
private final InstanceProperties instance;
4952

5053
private final ServerProperties server;
@@ -57,6 +60,8 @@ public class DefaultApplicationFactory implements ApplicationFactory {
5760

5861
private final MetadataContributor metadataContributor;
5962

63+
private final Optional<?> inetUtils;
64+
6065
@Nullable
6166
private Integer localServerPort;
6267

@@ -72,6 +77,19 @@ public DefaultApplicationFactory(InstanceProperties instance, ManagementServerPr
7277
this.pathMappedEndpoints = pathMappedEndpoints;
7378
this.webEndpoint = webEndpoint;
7479
this.metadataContributor = metadataContributor;
80+
this.inetUtils = Optional.empty();
81+
}
82+
83+
public DefaultApplicationFactory(InstanceProperties instance, ManagementServerProperties management,
84+
ServerProperties server, PathMappedEndpoints pathMappedEndpoints, WebEndpointProperties webEndpoint,
85+
MetadataContributor metadataContributor, Optional<?> inetUtils) {
86+
this.instance = instance;
87+
this.management = management;
88+
this.server = server;
89+
this.pathMappedEndpoints = pathMappedEndpoints;
90+
this.webEndpoint = webEndpoint;
91+
this.metadataContributor = metadataContributor;
92+
this.inetUtils = (inetUtils != null) ? inetUtils : Optional.empty();
7593
}
7694

7795
@Override
@@ -192,11 +210,31 @@ protected String getManagementHost() {
192210
}
193211

194212
protected InetAddress getLocalHost() {
213+
// Try using Spring Cloud Commons InetUtils if available (optional dependency)
214+
if (this.inetUtils != null && this.inetUtils.isPresent()) {
215+
Object utils = this.inetUtils.get();
216+
try {
217+
java.lang.reflect.Method m = utils.getClass().getMethod("findFirstNonLoopbackHostInfo");
218+
Object host = m.invoke(utils);
219+
if (host instanceof String && StringUtils.hasText((String) host)) {
220+
try {
221+
return InetAddress.getByName((String) host);
222+
}
223+
catch (UnknownHostException ex) {
224+
// fall through to default
225+
}
226+
}
227+
}
228+
catch (Exception ex) {
229+
// ignore and fall back
230+
}
231+
}
232+
195233
try {
196234
return InetAddress.getLocalHost();
197235
}
198236
catch (UnknownHostException ex) {
199-
throw new IllegalArgumentException(ex.getMessage(), ex);
237+
throw new IllegalStateException("Cannot determine local host address", ex);
200238
}
201239
}
202240

@@ -236,11 +274,14 @@ protected String getHost(InetAddress address) {
236274
return address.getHostAddress();
237275
}
238276

239-
return switch (this.instance.getServiceHostType()) {
240-
case IP -> address.getHostAddress();
241-
case HOST_NAME -> address.getHostName();
242-
default -> address.getCanonicalHostName();
243-
};
277+
switch (this.instance.getServiceHostType()) {
278+
case IP:
279+
return address.getHostAddress();
280+
case HOST_NAME:
281+
return address.getHostName();
282+
default:
283+
return address.getCanonicalHostName();
284+
}
244285
}
245286

246287
@EventListener

spring-boot-admin-client/src/main/java/de/codecentric/boot/admin/client/registration/ReactiveApplicationFactory.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package de.codecentric.boot.admin.client.registration;
1818

19+
import java.util.Optional;
20+
1921
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
2022
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties;
2123
import org.springframework.boot.actuate.endpoint.web.PathMappedEndpoints;
@@ -40,14 +42,22 @@ public class ReactiveApplicationFactory extends DefaultApplicationFactory {
4042

4143
public ReactiveApplicationFactory(InstanceProperties instance, ManagementServerProperties management,
4244
ServerProperties server, PathMappedEndpoints pathMappedEndpoints, WebEndpointProperties webEndpoint,
43-
MetadataContributor metadataContributor, WebFluxProperties webFluxProperties) {
44-
super(instance, management, server, pathMappedEndpoints, webEndpoint, metadataContributor);
45+
MetadataContributor metadataContributor, WebFluxProperties webFluxProperties, Optional<?> inetUtils) {
46+
super(instance, management, server, pathMappedEndpoints, webEndpoint, metadataContributor, inetUtils);
4547
this.management = management;
4648
this.server = server;
4749
this.webflux = webFluxProperties;
4850
this.instance = instance;
4951
}
5052

53+
// Backward-compatible constructor for tests and callers that don't provide InetUtils
54+
public ReactiveApplicationFactory(InstanceProperties instance, ManagementServerProperties management,
55+
ServerProperties server, PathMappedEndpoints pathMappedEndpoints, WebEndpointProperties webEndpoint,
56+
MetadataContributor metadataContributor, WebFluxProperties webFluxProperties) {
57+
this(instance, management, server, pathMappedEndpoints, webEndpoint, metadataContributor, webFluxProperties,
58+
Optional.empty());
59+
}
60+
5161
@Override
5262
protected String getServiceUrl() {
5363
if (instance.getServiceUrl() != null) {

spring-boot-admin-client/src/main/java/de/codecentric/boot/admin/client/registration/ServletApplicationFactory.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package de.codecentric.boot.admin.client.registration;
1818

19+
import java.util.Optional;
20+
1921
import jakarta.servlet.ServletContext;
2022
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
2123
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties;
@@ -44,15 +46,24 @@ public class ServletApplicationFactory extends DefaultApplicationFactory {
4446
public ServletApplicationFactory(InstanceProperties instance, ManagementServerProperties management,
4547
ServerProperties server, ServletContext servletContext, PathMappedEndpoints pathMappedEndpoints,
4648
WebEndpointProperties webEndpoint, MetadataContributor metadataContributor,
47-
DispatcherServletPath dispatcherServletPath) {
48-
super(instance, management, server, pathMappedEndpoints, webEndpoint, metadataContributor);
49+
DispatcherServletPath dispatcherServletPath, Optional<?> inetUtils) {
50+
super(instance, management, server, pathMappedEndpoints, webEndpoint, metadataContributor, inetUtils);
4951
this.servletContext = servletContext;
5052
this.server = server;
5153
this.management = management;
5254
this.instance = instance;
5355
this.dispatcherServletPath = dispatcherServletPath;
5456
}
5557

58+
// Backward-compatible constructor for callers that don't provide InetUtils
59+
public ServletApplicationFactory(InstanceProperties instance, ManagementServerProperties management,
60+
ServerProperties server, ServletContext servletContext, PathMappedEndpoints pathMappedEndpoints,
61+
WebEndpointProperties webEndpoint, MetadataContributor metadataContributor,
62+
DispatcherServletPath dispatcherServletPath) {
63+
this(instance, management, server, servletContext, pathMappedEndpoints, webEndpoint, metadataContributor,
64+
dispatcherServletPath, Optional.empty());
65+
}
66+
5667
@Override
5768
protected String getServiceUrl() {
5869
if (instance.getServiceUrl() != null) {

0 commit comments

Comments
 (0)