Skip to content

Commit d7a472b

Browse files
committed
Restore HealthIndicatorRegistry beans
Restore `HealthIndicatorRegistry` and `ReactiveHealthIndicatorRegistry` auto-configured beans with a version that adapts to the new contributor interfaces. Closes gh-16903
1 parent 5076d85 commit d7a472b

9 files changed

+528
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.health;
18+
19+
import java.util.LinkedHashMap;
20+
import java.util.Map;
21+
22+
import org.springframework.boot.actuate.health.HealthContributor;
23+
import org.springframework.boot.actuate.health.HealthContributorRegistry;
24+
import org.springframework.boot.actuate.health.HealthIndicator;
25+
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
26+
import org.springframework.boot.actuate.health.NamedContributor;
27+
import org.springframework.util.Assert;
28+
29+
/**
30+
* Adapter class to convert a {@link HealthContributorRegistry} to a legacy
31+
* {@link HealthIndicatorRegistry}.
32+
*
33+
* @author Phillip Webb
34+
*/
35+
@SuppressWarnings("deprecation")
36+
class HealthContributorRegistryHealthIndicatorRegistryAdapter implements HealthIndicatorRegistry {
37+
38+
private final HealthContributorRegistry contributorRegistry;
39+
40+
HealthContributorRegistryHealthIndicatorRegistryAdapter(HealthContributorRegistry contributorRegistry) {
41+
Assert.notNull(contributorRegistry, "ContributorRegistry must not be null");
42+
this.contributorRegistry = contributorRegistry;
43+
}
44+
45+
@Override
46+
public void register(String name, HealthIndicator healthIndicator) {
47+
this.contributorRegistry.registerContributor(name, healthIndicator);
48+
}
49+
50+
@Override
51+
public HealthIndicator unregister(String name) {
52+
HealthContributor contributor = this.contributorRegistry.unregisterContributor(name);
53+
if (contributor instanceof HealthIndicator) {
54+
return (HealthIndicator) contributor;
55+
}
56+
return null;
57+
}
58+
59+
@Override
60+
public HealthIndicator get(String name) {
61+
HealthContributor contributor = this.contributorRegistry.getContributor(name);
62+
if (contributor instanceof HealthIndicator) {
63+
return (HealthIndicator) contributor;
64+
}
65+
return null;
66+
}
67+
68+
@Override
69+
public Map<String, HealthIndicator> getAll() {
70+
Map<String, HealthIndicator> all = new LinkedHashMap<String, HealthIndicator>();
71+
for (NamedContributor<?> namedContributor : this.contributorRegistry) {
72+
if (namedContributor.getContributor() instanceof HealthIndicator) {
73+
all.put(namedContributor.getName(), (HealthIndicator) namedContributor.getContributor());
74+
}
75+
}
76+
return all;
77+
}
78+
79+
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/health/HealthEndpointAutoConfiguration.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.springframework.boot.actuate.health.HealthEndpoint;
2121
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2222
import org.springframework.boot.context.properties.EnableConfigurationProperties;
23+
import org.springframework.context.annotation.Bean;
2324
import org.springframework.context.annotation.Configuration;
2425
import org.springframework.context.annotation.Import;
2526

@@ -32,11 +33,20 @@
3233
* @since 2.0.0
3334
*/
3435
@Configuration(proxyBeanMethods = false)
35-
@EnableConfigurationProperties(HealthEndpointProperties.class)
3636
@ConditionalOnAvailableEndpoint(endpoint = HealthEndpoint.class)
37+
@EnableConfigurationProperties
3738
@Import({ LegacyHealthEndpointAdaptersConfiguration.class, LegacyHealthEndpointCompatibilityConfiguration.class,
3839
HealthEndpointConfiguration.class, ReactiveHealthEndpointConfiguration.class,
3940
HealthEndpointWebExtensionConfiguration.class, HealthEndpointReactiveWebExtensionConfiguration.class })
4041
public class HealthEndpointAutoConfiguration {
4142

43+
@Bean
44+
@SuppressWarnings("deprecation")
45+
HealthEndpointProperties healthEndpointProperties(HealthIndicatorProperties healthIndicatorProperties) {
46+
HealthEndpointProperties healthEndpointProperties = new HealthEndpointProperties();
47+
healthEndpointProperties.getStatus().getOrder().addAll(healthIndicatorProperties.getOrder());
48+
healthEndpointProperties.getStatus().getHttpMapping().putAll(healthIndicatorProperties.getHttpMapping());
49+
return healthEndpointProperties;
50+
}
51+
4252
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/health/HealthIndicatorProperties.java

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

1717
package org.springframework.boot.actuate.autoconfigure.health;
1818

19+
import java.util.ArrayList;
20+
import java.util.LinkedHashMap;
1921
import java.util.List;
2022
import java.util.Map;
2123

24+
import org.springframework.boot.context.properties.ConfigurationProperties;
2225
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
2326

2427
/**
@@ -29,26 +32,25 @@
2932
* @deprecated since 2.2.0 in favor of {@link HealthEndpointProperties}
3033
*/
3134
@Deprecated
35+
@ConfigurationProperties(prefix = "management.health.status")
3236
public class HealthIndicatorProperties {
3337

34-
private final HealthEndpointProperties healthEndpointProperties;
38+
private List<String> order = new ArrayList<>();
3539

36-
HealthIndicatorProperties(HealthEndpointProperties healthEndpointProperties) {
37-
this.healthEndpointProperties = healthEndpointProperties;
38-
}
40+
private final Map<String, Integer> httpMapping = new LinkedHashMap<String, Integer>();
3941

4042
@DeprecatedConfigurationProperty(replacement = "management.endpoint.health.status.order")
4143
public List<String> getOrder() {
42-
return this.healthEndpointProperties.getStatus().getOrder();
44+
return this.order;
4345
}
4446

45-
public void setOrder(List<String> statusOrder) {
46-
this.healthEndpointProperties.getStatus().setOrder(statusOrder);
47+
public void setOrder(List<String> order) {
48+
this.order = order;
4749
}
4850

4951
@DeprecatedConfigurationProperty(replacement = "management.endpoint.health.status.http-mapping")
5052
public Map<String, Integer> getHttpMapping() {
51-
return this.healthEndpointProperties.getStatus().getHttpMapping();
53+
return this.httpMapping;
5254
}
5355

5456
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/health/HealthProperties.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.health;
1818

19+
import java.util.ArrayList;
1920
import java.util.HashMap;
2021
import java.util.HashSet;
2122
import java.util.List;
@@ -76,7 +77,7 @@ public static class Status {
7677
/**
7778
* Comma-separated list of health statuses in order of severity.
7879
*/
79-
private List<String> order = null;
80+
private List<String> order = new ArrayList<>();
8081

8182
/**
8283
* Mapping of health statuses to HTTP status codes. By default, registered health

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/health/LegacyHealthEndpointCompatibilityConfiguration.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.health;
1818

19+
import io.micrometer.shaded.reactor.core.publisher.Mono;
20+
1921
import org.springframework.boot.actuate.health.HealthAggregator;
22+
import org.springframework.boot.actuate.health.HealthContributorRegistry;
23+
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
2024
import org.springframework.boot.actuate.health.HealthStatusHttpMapper;
2125
import org.springframework.boot.actuate.health.OrderedHealthAggregator;
26+
import org.springframework.boot.actuate.health.ReactiveHealthContributorRegistry;
27+
import org.springframework.boot.actuate.health.ReactiveHealthIndicatorRegistry;
28+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2229
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
23-
import org.springframework.boot.context.properties.ConfigurationProperties;
30+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2431
import org.springframework.context.annotation.Bean;
2532
import org.springframework.context.annotation.Configuration;
2633

@@ -32,12 +39,12 @@
3239
*/
3340
@Configuration(proxyBeanMethods = false)
3441
@SuppressWarnings("deprecation")
42+
@EnableConfigurationProperties
3543
class LegacyHealthEndpointCompatibilityConfiguration {
3644

3745
@Bean
38-
@ConfigurationProperties(prefix = "management.health.status")
39-
HealthIndicatorProperties healthIndicatorProperties(HealthEndpointProperties healthEndpointProperties) {
40-
return new HealthIndicatorProperties(healthEndpointProperties);
46+
HealthIndicatorProperties healthIndicatorProperties() {
47+
return new HealthIndicatorProperties();
4148
}
4249

4350
@Bean
@@ -60,4 +67,25 @@ HealthStatusHttpMapper healthStatusHttpMapper(HealthIndicatorProperties healthIn
6067
return mapper;
6168
}
6269

70+
@Bean
71+
@ConditionalOnMissingBean(HealthIndicatorRegistry.class)
72+
HealthContributorRegistryHealthIndicatorRegistryAdapter healthIndicatorRegistry(
73+
HealthContributorRegistry healthContributorRegistry) {
74+
return new HealthContributorRegistryHealthIndicatorRegistryAdapter(healthContributorRegistry);
75+
}
76+
77+
@Configuration(proxyBeanMethods = false)
78+
@ConditionalOnClass(Mono.class)
79+
static class LegacyReactiveHealthEndpointCompatibilityConfiguration {
80+
81+
@Bean
82+
@ConditionalOnMissingBean(ReactiveHealthIndicatorRegistry.class)
83+
ReactiveHealthContributorRegistryReactiveHealthIndicatorRegistryAdapter reactiveHealthIndicatorRegistry(
84+
ReactiveHealthContributorRegistry reactiveHealthContributorRegistry) {
85+
return new ReactiveHealthContributorRegistryReactiveHealthIndicatorRegistryAdapter(
86+
reactiveHealthContributorRegistry);
87+
}
88+
89+
}
90+
6391
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.health;
18+
19+
import java.util.LinkedHashMap;
20+
import java.util.Map;
21+
22+
import org.springframework.boot.actuate.health.HealthContributorRegistry;
23+
import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
24+
import org.springframework.boot.actuate.health.NamedContributor;
25+
import org.springframework.boot.actuate.health.ReactiveHealthContributor;
26+
import org.springframework.boot.actuate.health.ReactiveHealthContributorRegistry;
27+
import org.springframework.boot.actuate.health.ReactiveHealthIndicator;
28+
import org.springframework.boot.actuate.health.ReactiveHealthIndicatorRegistry;
29+
import org.springframework.util.Assert;
30+
31+
/**
32+
* Adapter class to convert a {@link HealthContributorRegistry} to a legacy
33+
* {@link HealthIndicatorRegistry}.
34+
*
35+
* @author Phillip Webb
36+
*/
37+
@SuppressWarnings("deprecation")
38+
class ReactiveHealthContributorRegistryReactiveHealthIndicatorRegistryAdapter
39+
implements ReactiveHealthIndicatorRegistry {
40+
41+
private final ReactiveHealthContributorRegistry contributorRegistry;
42+
43+
ReactiveHealthContributorRegistryReactiveHealthIndicatorRegistryAdapter(
44+
ReactiveHealthContributorRegistry contributorRegistry) {
45+
Assert.notNull(contributorRegistry, "ContributorRegistry must not be null");
46+
this.contributorRegistry = contributorRegistry;
47+
}
48+
49+
@Override
50+
public void register(String name, ReactiveHealthIndicator healthIndicator) {
51+
this.contributorRegistry.registerContributor(name, healthIndicator);
52+
}
53+
54+
@Override
55+
public ReactiveHealthIndicator unregister(String name) {
56+
ReactiveHealthContributor contributor = this.contributorRegistry.unregisterContributor(name);
57+
if (contributor instanceof ReactiveHealthIndicator) {
58+
return (ReactiveHealthIndicator) contributor;
59+
}
60+
return null;
61+
}
62+
63+
@Override
64+
public ReactiveHealthIndicator get(String name) {
65+
ReactiveHealthContributor contributor = this.contributorRegistry.getContributor(name);
66+
if (contributor instanceof ReactiveHealthIndicator) {
67+
return (ReactiveHealthIndicator) contributor;
68+
}
69+
return null;
70+
}
71+
72+
@Override
73+
public Map<String, ReactiveHealthIndicator> getAll() {
74+
Map<String, ReactiveHealthIndicator> all = new LinkedHashMap<>();
75+
for (NamedContributor<?> namedContributor : this.contributorRegistry) {
76+
if (namedContributor.getContributor() instanceof ReactiveHealthIndicator) {
77+
all.put(namedContributor.getName(), (ReactiveHealthIndicator) namedContributor.getContributor());
78+
}
79+
}
80+
return all;
81+
}
82+
83+
}

0 commit comments

Comments
 (0)