|
17 | 17 | package org.springframework.boot.health.autoconfigure.actuate.endpoint; |
18 | 18 |
|
19 | 19 | import java.util.Set; |
| 20 | +import java.util.function.BiFunction; |
20 | 21 |
|
21 | 22 | import org.jspecify.annotations.Nullable; |
22 | 23 |
|
|
34 | 35 | import org.springframework.boot.health.actuate.endpoint.SimpleStatusAggregator; |
35 | 36 | import org.springframework.boot.health.actuate.endpoint.StatusAggregator; |
36 | 37 | import org.springframework.boot.health.contributor.HealthContributors; |
| 38 | +import org.springframework.boot.health.contributor.ReactiveHealthContributors; |
37 | 39 | import org.springframework.boot.health.registry.HealthContributorRegistry; |
38 | 40 | import org.springframework.boot.health.registry.ReactiveHealthContributorRegistry; |
39 | 41 | import org.springframework.context.ApplicationContext; |
@@ -78,8 +80,10 @@ GroupsHealthContributorNameValidator groupsHealthContributorNameValidator( |
78 | 80 | @Bean |
79 | 81 | @ConditionalOnBooleanProperty(name = "management.endpoint.health.validate-group-membership", matchIfMissing = true) |
80 | 82 | HealthEndpointGroupMembershipValidator healthEndpointGroupMembershipValidator(HealthEndpointProperties properties, |
81 | | - HealthContributorRegistry healthContributorRegistry) { |
82 | | - return new HealthEndpointGroupMembershipValidator(properties, healthContributorRegistry); |
| 83 | + HealthContributorRegistry healthContributorRegistry, |
| 84 | + ObjectProvider<ReactiveHealthContributorRegistry> reactiveHealthContributorRegistry) { |
| 85 | + return new HealthEndpointGroupMembershipValidator(properties, healthContributorRegistry, |
| 86 | + reactiveHealthContributorRegistry.getIfAvailable()); |
83 | 87 | } |
84 | 88 |
|
85 | 89 | @Bean |
@@ -138,10 +142,13 @@ static class HealthEndpointGroupMembershipValidator implements SmartInitializing |
138 | 142 |
|
139 | 143 | private final HealthContributorRegistry registry; |
140 | 144 |
|
141 | | - HealthEndpointGroupMembershipValidator(HealthEndpointProperties properties, |
142 | | - HealthContributorRegistry registry) { |
| 145 | + @Nullable ReactiveHealthContributorRegistry fallbackRegistry; |
| 146 | + |
| 147 | + HealthEndpointGroupMembershipValidator(HealthEndpointProperties properties, HealthContributorRegistry registry, |
| 148 | + @Nullable ReactiveHealthContributorRegistry fallbackRegistry) { |
143 | 149 | this.properties = properties; |
144 | 150 | this.registry = registry; |
| 151 | + this.fallbackRegistry = fallbackRegistry; |
145 | 152 | } |
146 | 153 |
|
147 | 154 | @Override |
@@ -172,13 +179,28 @@ private void validate(@Nullable Set<String> names, String type, String group) { |
172 | 179 | } |
173 | 180 |
|
174 | 181 | private boolean contributorExists(String[] path) { |
| 182 | + return contributorExistsInMainRegistry(path) || contributorExistsInFallbackRegistry(path); |
| 183 | + } |
| 184 | + |
| 185 | + private boolean contributorExistsInMainRegistry(String[] path) { |
| 186 | + return contributorExists(path, this.registry, HealthContributors.class, HealthContributors::getContributor); |
| 187 | + } |
| 188 | + |
| 189 | + private boolean contributorExistsInFallbackRegistry(String[] path) { |
| 190 | + return contributorExists(path, this.fallbackRegistry, ReactiveHealthContributors.class, |
| 191 | + ReactiveHealthContributors::getContributor); |
| 192 | + } |
| 193 | + |
| 194 | + @SuppressWarnings("unchecked") |
| 195 | + private <C> boolean contributorExists(String[] path, @Nullable Object registry, Class<C> collectionType, |
| 196 | + BiFunction<C, String, Object> getFromCollection) { |
175 | 197 | int pathOffset = 0; |
176 | | - Object contributor = this.registry; |
| 198 | + Object contributor = registry; |
177 | 199 | while (pathOffset < path.length) { |
178 | | - if (!(contributor instanceof HealthContributors)) { |
| 200 | + if (contributor == null || !collectionType.isInstance(contributor)) { |
179 | 201 | return false; |
180 | 202 | } |
181 | | - contributor = ((HealthContributors) contributor).getContributor(path[pathOffset]); |
| 203 | + contributor = getFromCollection.apply((C) contributor, path[pathOffset]); |
182 | 204 | pathOffset++; |
183 | 205 | } |
184 | 206 | return (contributor != null); |
|
0 commit comments