001/*
002 * Copyright 2012-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.actuate.autoconfigure.health;
018
019import java.util.Collections;
020import java.util.Map;
021
022import reactor.core.publisher.Flux;
023
024import org.springframework.beans.factory.ObjectProvider;
025import org.springframework.boot.actuate.health.ApplicationHealthIndicator;
026import org.springframework.boot.actuate.health.HealthAggregator;
027import org.springframework.boot.actuate.health.HealthIndicator;
028import org.springframework.boot.actuate.health.HealthIndicatorRegistry;
029import org.springframework.boot.actuate.health.OrderedHealthAggregator;
030import org.springframework.boot.actuate.health.ReactiveHealthIndicator;
031import org.springframework.boot.actuate.health.ReactiveHealthIndicatorRegistry;
032import org.springframework.boot.actuate.health.ReactiveHealthIndicatorRegistryFactory;
033import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
034import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
035import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
036import org.springframework.boot.context.properties.EnableConfigurationProperties;
037import org.springframework.context.ApplicationContext;
038import org.springframework.context.annotation.Bean;
039import org.springframework.context.annotation.Configuration;
040
041/**
042 * {@link EnableAutoConfiguration Auto-configuration} for {@link HealthIndicator}s.
043 *
044 * @author Andy Wilkinson
045 * @author Stephane Nicoll
046 * @author Phillip Webb
047 * @author Vedran Pavic
048 * @since 2.0.0
049 */
050@Configuration
051@EnableConfigurationProperties({ HealthIndicatorProperties.class })
052public class HealthIndicatorAutoConfiguration {
053
054        private final HealthIndicatorProperties properties;
055
056        public HealthIndicatorAutoConfiguration(HealthIndicatorProperties properties) {
057                this.properties = properties;
058        }
059
060        @Bean
061        @ConditionalOnMissingBean({ HealthIndicator.class, ReactiveHealthIndicator.class })
062        public ApplicationHealthIndicator applicationHealthIndicator() {
063                return new ApplicationHealthIndicator();
064        }
065
066        @Bean
067        @ConditionalOnMissingBean(HealthAggregator.class)
068        public OrderedHealthAggregator healthAggregator() {
069                OrderedHealthAggregator healthAggregator = new OrderedHealthAggregator();
070                if (this.properties.getOrder() != null) {
071                        healthAggregator.setStatusOrder(this.properties.getOrder());
072                }
073                return healthAggregator;
074        }
075
076        @Bean
077        @ConditionalOnMissingBean(HealthIndicatorRegistry.class)
078        public HealthIndicatorRegistry healthIndicatorRegistry(
079                        ApplicationContext applicationContext) {
080                return HealthIndicatorRegistryBeans.get(applicationContext);
081        }
082
083        @Configuration
084        @ConditionalOnClass(Flux.class)
085        static class ReactiveHealthIndicatorConfiguration {
086
087                @Bean
088                @ConditionalOnMissingBean
089                public ReactiveHealthIndicatorRegistry reactiveHealthIndicatorRegistry(
090                                ObjectProvider<Map<String, ReactiveHealthIndicator>> reactiveHealthIndicators,
091                                ObjectProvider<Map<String, HealthIndicator>> healthIndicators) {
092                        return new ReactiveHealthIndicatorRegistryFactory()
093                                        .createReactiveHealthIndicatorRegistry(
094                                                        reactiveHealthIndicators
095                                                                        .getIfAvailable(Collections::emptyMap),
096                                                        healthIndicators.getIfAvailable(Collections::emptyMap));
097                }
098
099        }
100
101}