|
1 | 1 | /* |
2 | | - * Copyright 2012-2017 the original author or authors. |
| 2 | + * Copyright 2012-2018 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
19 | 19 | import java.util.LinkedHashMap; |
20 | 20 | import java.util.Map; |
21 | 21 |
|
22 | | -import org.springframework.util.Assert; |
23 | | - |
24 | 22 | /** |
25 | 23 | * {@link HealthIndicator} that returns health indications from all registered delegates. |
26 | 24 | * |
|
31 | 29 | */ |
32 | 30 | public class CompositeHealthIndicator implements HealthIndicator { |
33 | 31 |
|
34 | | - private final Map<String, HealthIndicator> indicators; |
| 32 | + private final HealthIndicatorRegistry registry; |
35 | 33 |
|
36 | | - private final HealthAggregator healthAggregator; |
| 34 | + private final HealthAggregator aggregator; |
37 | 35 |
|
38 | 36 | /** |
39 | 37 | * Create a new {@link CompositeHealthIndicator}. |
40 | 38 | * @param healthAggregator the health aggregator |
| 39 | + * @deprecated since 2.1.0 in favour of |
| 40 | + * {@link #CompositeHealthIndicator(HealthAggregator, HealthIndicatorRegistry)} |
41 | 41 | */ |
| 42 | + @Deprecated |
42 | 43 | public CompositeHealthIndicator(HealthAggregator healthAggregator) { |
43 | | - this(healthAggregator, new LinkedHashMap<>()); |
| 44 | + this(healthAggregator, new DefaultHealthIndicatorRegistry()); |
44 | 45 | } |
45 | 46 |
|
46 | 47 | /** |
47 | | - * Create a new {@link CompositeHealthIndicator} from the specified indicators. |
| 48 | + * Create a new {@link CompositeHealthIndicator} from the specified |
| 49 | + * indicators. |
48 | 50 | * @param healthAggregator the health aggregator |
49 | | - * @param indicators a map of {@link HealthIndicator}s with the key being used as an |
50 | | - * indicator name. |
| 51 | + * @param indicators a map of {@link HealthIndicator HealthIndicators} with |
| 52 | + * the key being used as an indicator name. |
| 53 | + * @deprecated since 2.1.0 in favour of |
| 54 | + * {@link #CompositeHealthIndicator(HealthAggregator, HealthIndicatorRegistry)} |
51 | 55 | */ |
| 56 | + @Deprecated |
52 | 57 | public CompositeHealthIndicator(HealthAggregator healthAggregator, |
53 | 58 | Map<String, HealthIndicator> indicators) { |
54 | | - Assert.notNull(healthAggregator, "HealthAggregator must not be null"); |
55 | | - Assert.notNull(indicators, "Indicators must not be null"); |
56 | | - this.indicators = new LinkedHashMap<>(indicators); |
57 | | - this.healthAggregator = healthAggregator; |
| 59 | + this(healthAggregator, new DefaultHealthIndicatorRegistry(indicators)); |
58 | 60 | } |
59 | 61 |
|
| 62 | + /** |
| 63 | + * Create a new {@link CompositeHealthIndicator} from the indicators in the |
| 64 | + * given {@code registry}. |
| 65 | + * @param healthAggregator the health aggregator |
| 66 | + * @param registry the registry of {@link HealthIndicator HealthIndicators}. |
| 67 | + */ |
| 68 | + public CompositeHealthIndicator(HealthAggregator healthAggregator, |
| 69 | + HealthIndicatorRegistry registry) { |
| 70 | + this.aggregator = healthAggregator; |
| 71 | + this.registry = registry; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Adds the given {@code healthIndicator}, associating it with the given |
| 76 | + * {@code name}. |
| 77 | + * @param name the name of the indicator |
| 78 | + * @param indicator the indicator |
| 79 | + * @throws IllegalStateException if an indicator with the given {@code name} |
| 80 | + * is already registered. |
| 81 | + * @deprecated since 2.1.0 in favour of |
| 82 | + * {@link HealthIndicatorRegistry#register(String, HealthIndicator)} |
| 83 | + */ |
| 84 | + @Deprecated |
60 | 85 | public void addHealthIndicator(String name, HealthIndicator indicator) { |
61 | | - this.indicators.put(name, indicator); |
| 86 | + this.registry.register(name, indicator); |
62 | 87 | } |
63 | 88 |
|
64 | 89 | @Override |
65 | 90 | public Health health() { |
66 | 91 | Map<String, Health> healths = new LinkedHashMap<>(); |
67 | | - for (Map.Entry<String, HealthIndicator> entry : this.indicators.entrySet()) { |
| 92 | + for (Map.Entry<String, HealthIndicator> entry : this.registry.getAll() |
| 93 | + .entrySet()) { |
68 | 94 | healths.put(entry.getKey(), entry.getValue().health()); |
69 | 95 | } |
70 | | - return this.healthAggregator.aggregate(healths); |
| 96 | + return this.aggregator.aggregate(healths); |
71 | 97 | } |
72 | 98 |
|
73 | 99 | } |
0 commit comments