|
| 1 | +/* |
| 2 | + * Copyright 2012-2018 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 | + * http://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.beans.factory.NoSuchBeanDefinitionException; |
| 23 | +import org.springframework.boot.actuate.health.HealthAggregator; |
| 24 | +import org.springframework.boot.actuate.health.HealthIndicator; |
| 25 | +import org.springframework.boot.actuate.health.HealthIndicatorRegistry; |
| 26 | +import org.springframework.boot.actuate.health.HealthIndicatorRegistryFactory; |
| 27 | +import org.springframework.boot.actuate.health.OrderedHealthAggregator; |
| 28 | +import org.springframework.boot.actuate.health.ReactiveHealthIndicator; |
| 29 | +import org.springframework.context.ApplicationContext; |
| 30 | +import org.springframework.util.ClassUtils; |
| 31 | + |
| 32 | +/** |
| 33 | + * Creates a {@link HealthIndicatorRegistry} from beans in the {@link ApplicationContext}. |
| 34 | + * |
| 35 | + * @author Phillip Webb |
| 36 | + * @author Stephane Nicoll |
| 37 | + */ |
| 38 | +final class HealthIndicatorRegistryBeans { |
| 39 | + |
| 40 | + private HealthIndicatorRegistryBeans() { |
| 41 | + } |
| 42 | + |
| 43 | + public static HealthIndicatorRegistry get(ApplicationContext applicationContext) { |
| 44 | + HealthAggregator healthAggregator = getHealthAggregator(applicationContext); |
| 45 | + Map<String, HealthIndicator> indicators = new LinkedHashMap<>(); |
| 46 | + indicators.putAll(applicationContext.getBeansOfType(HealthIndicator.class)); |
| 47 | + if (ClassUtils.isPresent("reactor.core.publisher.Flux", null)) { |
| 48 | + new ReactiveHealthIndicators().get(applicationContext) |
| 49 | + .forEach(indicators::putIfAbsent); |
| 50 | + } |
| 51 | + HealthIndicatorRegistryFactory factory = new HealthIndicatorRegistryFactory(); |
| 52 | + return factory.createHealthIndicatorRegistry(healthAggregator, indicators); |
| 53 | + } |
| 54 | + |
| 55 | + private static HealthAggregator getHealthAggregator( |
| 56 | + ApplicationContext applicationContext) { |
| 57 | + try { |
| 58 | + return applicationContext.getBean(HealthAggregator.class); |
| 59 | + } |
| 60 | + catch (NoSuchBeanDefinitionException ex) { |
| 61 | + return new OrderedHealthAggregator(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private static class ReactiveHealthIndicators { |
| 66 | + |
| 67 | + public Map<String, HealthIndicator> get(ApplicationContext applicationContext) { |
| 68 | + Map<String, HealthIndicator> indicators = new LinkedHashMap<>(); |
| 69 | + applicationContext.getBeansOfType(ReactiveHealthIndicator.class) |
| 70 | + .forEach((name, indicator) -> indicators.put(name, adapt(indicator))); |
| 71 | + return indicators; |
| 72 | + } |
| 73 | + |
| 74 | + private HealthIndicator adapt(ReactiveHealthIndicator indicator) { |
| 75 | + return () -> indicator.health().block(); |
| 76 | + } |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | +} |
0 commit comments