|
| 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.ArrayList; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.LinkedHashMap; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Set; |
| 25 | +import java.util.function.Predicate; |
| 26 | +import java.util.function.Supplier; |
| 27 | + |
| 28 | +import org.springframework.beans.factory.BeanFactory; |
| 29 | +import org.springframework.beans.factory.BeanFactoryUtils; |
| 30 | +import org.springframework.beans.factory.ListableBeanFactory; |
| 31 | +import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
| 32 | +import org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils; |
| 33 | +import org.springframework.boot.actuate.autoconfigure.health.HealthEndpointProperties.Group; |
| 34 | +import org.springframework.boot.actuate.autoconfigure.health.HealthProperties.ShowDetails; |
| 35 | +import org.springframework.boot.actuate.autoconfigure.health.HealthProperties.Status; |
| 36 | +import org.springframework.boot.actuate.health.HealthEndpointGroup; |
| 37 | +import org.springframework.boot.actuate.health.HealthEndpointGroups; |
| 38 | +import org.springframework.boot.actuate.health.HttpCodeStatusMapper; |
| 39 | +import org.springframework.boot.actuate.health.SimpleHttpCodeStatusMapper; |
| 40 | +import org.springframework.boot.actuate.health.SimpleStatusAggregator; |
| 41 | +import org.springframework.boot.actuate.health.StatusAggregator; |
| 42 | +import org.springframework.context.ApplicationContext; |
| 43 | +import org.springframework.context.ConfigurableApplicationContext; |
| 44 | +import org.springframework.util.CollectionUtils; |
| 45 | +import org.springframework.util.ObjectUtils; |
| 46 | + |
| 47 | +/** |
| 48 | + * Auto-configured {@link HealthEndpointGroups}. |
| 49 | + * |
| 50 | + * @author Phillip Webb |
| 51 | + */ |
| 52 | +class AutoConfiguredHealthEndpointGroups implements HealthEndpointGroups { |
| 53 | + |
| 54 | + private static Predicate<String> ALL = (name) -> true; |
| 55 | + |
| 56 | + private final HealthEndpointGroup primaryGroup; |
| 57 | + |
| 58 | + private final Map<String, HealthEndpointGroup> groups; |
| 59 | + |
| 60 | + /** |
| 61 | + * Create a new {@link AutoConfiguredHealthEndpointGroups} instance. |
| 62 | + * @param applicationContext the application context used to check for override beans |
| 63 | + * @param properties the health endpoint properties |
| 64 | + */ |
| 65 | + AutoConfiguredHealthEndpointGroups(ApplicationContext applicationContext, HealthEndpointProperties properties) { |
| 66 | + ListableBeanFactory beanFactory = (applicationContext instanceof ConfigurableApplicationContext) |
| 67 | + ? ((ConfigurableApplicationContext) applicationContext).getBeanFactory() : applicationContext; |
| 68 | + ShowDetails showDetails = properties.getShowDetails(); |
| 69 | + Set<String> roles = properties.getRoles(); |
| 70 | + StatusAggregator statusAggregator = getNonQualifiedBean(beanFactory, StatusAggregator.class); |
| 71 | + if (statusAggregator == null) { |
| 72 | + statusAggregator = new SimpleStatusAggregator(properties.getStatus().getOrder()); |
| 73 | + } |
| 74 | + HttpCodeStatusMapper httpCodeStatusMapper = getNonQualifiedBean(beanFactory, HttpCodeStatusMapper.class); |
| 75 | + if (httpCodeStatusMapper == null) { |
| 76 | + httpCodeStatusMapper = new SimpleHttpCodeStatusMapper(properties.getStatus().getHttpMapping()); |
| 77 | + } |
| 78 | + this.primaryGroup = new AutoConfiguredHealthEndpointGroup(ALL, statusAggregator, httpCodeStatusMapper, |
| 79 | + showDetails, roles); |
| 80 | + this.groups = createGroups(properties.getGroup(), beanFactory, statusAggregator, httpCodeStatusMapper, |
| 81 | + showDetails, roles); |
| 82 | + } |
| 83 | + |
| 84 | + private Map<String, HealthEndpointGroup> createGroups(Map<String, Group> groupProperties, BeanFactory beanFactory, |
| 85 | + StatusAggregator defaultStatusAggregator, HttpCodeStatusMapper defaultHttpCodeStatusMapper, |
| 86 | + ShowDetails defaultShowDetails, Set<String> defaultRoles) { |
| 87 | + Map<String, HealthEndpointGroup> groups = new LinkedHashMap<String, HealthEndpointGroup>(); |
| 88 | + groupProperties.forEach((groupName, group) -> { |
| 89 | + Status status = group.getStatus(); |
| 90 | + ShowDetails showDetails = (group.getShowDetails() != null) ? group.getShowDetails() : defaultShowDetails; |
| 91 | + Set<String> roles = !CollectionUtils.isEmpty(group.getRoles()) ? group.getRoles() : defaultRoles; |
| 92 | + StatusAggregator statusAggregator = getQualifiedBean(beanFactory, StatusAggregator.class, groupName, () -> { |
| 93 | + if (!CollectionUtils.isEmpty(status.getOrder())) { |
| 94 | + return new SimpleStatusAggregator(status.getOrder()); |
| 95 | + } |
| 96 | + return defaultStatusAggregator; |
| 97 | + }); |
| 98 | + HttpCodeStatusMapper httpCodeStatusMapper = getQualifiedBean(beanFactory, HttpCodeStatusMapper.class, |
| 99 | + groupName, () -> { |
| 100 | + if (!CollectionUtils.isEmpty(status.getHttpMapping())) { |
| 101 | + return new SimpleHttpCodeStatusMapper(status.getHttpMapping()); |
| 102 | + } |
| 103 | + return defaultHttpCodeStatusMapper; |
| 104 | + }); |
| 105 | + Predicate<String> members = new IncludeExcludeGroupMemberPredicate(group.getInclude(), group.getExclude()); |
| 106 | + groups.put(groupName, new AutoConfiguredHealthEndpointGroup(members, statusAggregator, httpCodeStatusMapper, |
| 107 | + showDetails, roles)); |
| 108 | + }); |
| 109 | + return Collections.unmodifiableMap(groups); |
| 110 | + } |
| 111 | + |
| 112 | + private <T> T getNonQualifiedBean(ListableBeanFactory beanFactory, Class<T> type) { |
| 113 | + List<String> candidates = new ArrayList<>(); |
| 114 | + for (String beanName : BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, type)) { |
| 115 | + String[] aliases = beanFactory.getAliases(beanName); |
| 116 | + if (!BeanFactoryAnnotationUtils.isQualifierMatch( |
| 117 | + (qualifier) -> !qualifier.equals(beanName) && !ObjectUtils.containsElement(aliases, qualifier), |
| 118 | + beanName, beanFactory)) { |
| 119 | + candidates.add(beanName); |
| 120 | + } |
| 121 | + } |
| 122 | + if (candidates.isEmpty()) { |
| 123 | + return null; |
| 124 | + } |
| 125 | + if (candidates.size() == 1) { |
| 126 | + return beanFactory.getBean(candidates.get(0), type); |
| 127 | + } |
| 128 | + return beanFactory.getBean(type); |
| 129 | + } |
| 130 | + |
| 131 | + private <T> T getQualifiedBean(BeanFactory beanFactory, Class<T> type, String qualifier, Supplier<T> fallback) { |
| 132 | + try { |
| 133 | + return BeanFactoryAnnotationUtils.qualifiedBeanOfType(beanFactory, type, qualifier); |
| 134 | + } |
| 135 | + catch (NoSuchBeanDefinitionException ex) { |
| 136 | + return fallback.get(); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public HealthEndpointGroup getPrimary() { |
| 142 | + return this.primaryGroup; |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public Set<String> getNames() { |
| 147 | + return this.groups.keySet(); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public HealthEndpointGroup get(String name) { |
| 152 | + return this.groups.get(name); |
| 153 | + } |
| 154 | + |
| 155 | +} |
0 commit comments