|
| 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<>(); |
| 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 | +} |
0 commit comments