|
16 | 16 |
|
17 | 17 | package org.springframework.boot.actuate.autoconfigure.endpoint.condition;
|
18 | 18 |
|
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.EnumSet; |
19 | 21 | import java.util.HashSet;
|
| 22 | +import java.util.LinkedHashSet; |
20 | 23 | import java.util.Map;
|
| 24 | +import java.util.Optional; |
21 | 25 | import java.util.Set;
|
22 | 26 |
|
| 27 | +import org.springframework.boot.actuate.autoconfigure.endpoint.expose.EndpointExposure; |
23 | 28 | import org.springframework.boot.actuate.autoconfigure.endpoint.expose.IncludeExcludeEndpointFilter;
|
24 |
| -import org.springframework.boot.actuate.autoconfigure.endpoint.expose.IncludeExcludeEndpointFilter.DefaultIncludes; |
25 | 29 | import org.springframework.boot.actuate.endpoint.EndpointId;
|
26 | 30 | import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
|
| 31 | +import org.springframework.boot.actuate.endpoint.annotation.Endpoint; |
| 32 | +import org.springframework.boot.actuate.endpoint.annotation.EndpointExtension; |
27 | 33 | import org.springframework.boot.autoconfigure.condition.ConditionMessage;
|
28 | 34 | import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
|
| 35 | +import org.springframework.boot.autoconfigure.condition.SpringBootCondition; |
29 | 36 | import org.springframework.boot.cloud.CloudPlatform;
|
| 37 | +import org.springframework.context.annotation.Bean; |
30 | 38 | import org.springframework.context.annotation.ConditionContext;
|
| 39 | +import org.springframework.core.annotation.MergedAnnotation; |
| 40 | +import org.springframework.core.annotation.MergedAnnotations; |
| 41 | +import org.springframework.core.annotation.MergedAnnotations.SearchStrategy; |
31 | 42 | import org.springframework.core.env.Environment;
|
32 | 43 | import org.springframework.core.type.AnnotatedTypeMetadata;
|
| 44 | +import org.springframework.core.type.MethodMetadata; |
| 45 | +import org.springframework.util.Assert; |
| 46 | +import org.springframework.util.ClassUtils; |
33 | 47 | import org.springframework.util.ConcurrentReferenceHashMap;
|
34 | 48 |
|
35 | 49 | /**
|
|
40 | 54 | * @author Phillip Webb
|
41 | 55 | * @see ConditionalOnAvailableEndpoint
|
42 | 56 | */
|
43 |
| -class OnAvailableEndpointCondition extends AbstractEndpointCondition { |
| 57 | +class OnAvailableEndpointCondition extends SpringBootCondition { |
44 | 58 |
|
45 | 59 | private static final String JMX_ENABLED_KEY = "spring.jmx.enabled";
|
46 | 60 |
|
47 |
| - private static final Map<Environment, Set<Exposure>> exposuresCache = new ConcurrentReferenceHashMap<>(); |
| 61 | + private static final String ENABLED_BY_DEFAULT_KEY = "management.endpoints.enabled-by-default"; |
| 62 | + |
| 63 | + private static final Map<Environment, Set<ExposureFilter>> exposureFiltersCache = new ConcurrentReferenceHashMap<>(); |
| 64 | + |
| 65 | + private static final ConcurrentReferenceHashMap<Environment, Optional<Boolean>> enabledByDefaultCache = new ConcurrentReferenceHashMap<>(); |
48 | 66 |
|
49 | 67 | @Override
|
50 | 68 | public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
51 |
| - ConditionOutcome enablementOutcome = getEnablementOutcome(context, metadata, |
52 |
| - ConditionalOnAvailableEndpoint.class); |
| 69 | + Environment environment = context.getEnvironment(); |
| 70 | + MergedAnnotation<ConditionalOnAvailableEndpoint> conditionAnnotation = metadata.getAnnotations() |
| 71 | + .get(ConditionalOnAvailableEndpoint.class); |
| 72 | + Class<?> target = getTarget(context, metadata, conditionAnnotation); |
| 73 | + MergedAnnotation<Endpoint> endpointAnnotation = getEndpointAnnotation(target); |
| 74 | + return getMatchOutcome(environment, conditionAnnotation, endpointAnnotation); |
| 75 | + } |
| 76 | + |
| 77 | + private Class<?> getTarget(ConditionContext context, AnnotatedTypeMetadata metadata, |
| 78 | + MergedAnnotation<ConditionalOnAvailableEndpoint> condition) { |
| 79 | + Class<?> target = condition.getClass("endpoint"); |
| 80 | + if (target != Void.class) { |
| 81 | + return target; |
| 82 | + } |
| 83 | + Assert.state(metadata instanceof MethodMetadata && metadata.isAnnotated(Bean.class.getName()), |
| 84 | + "EndpointCondition must be used on @Bean methods when the endpoint is not specified"); |
| 85 | + MethodMetadata methodMetadata = (MethodMetadata) metadata; |
| 86 | + try { |
| 87 | + return ClassUtils.forName(methodMetadata.getReturnTypeName(), context.getClassLoader()); |
| 88 | + } |
| 89 | + catch (Throwable ex) { |
| 90 | + throw new IllegalStateException("Failed to extract endpoint id for " |
| 91 | + + methodMetadata.getDeclaringClassName() + "." + methodMetadata.getMethodName(), ex); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + protected MergedAnnotation<Endpoint> getEndpointAnnotation(Class<?> target) { |
| 96 | + MergedAnnotations annotations = MergedAnnotations.from(target, SearchStrategy.TYPE_HIERARCHY); |
| 97 | + MergedAnnotation<Endpoint> endpoint = annotations.get(Endpoint.class); |
| 98 | + if (endpoint.isPresent()) { |
| 99 | + return endpoint; |
| 100 | + } |
| 101 | + MergedAnnotation<EndpointExtension> extension = annotations.get(EndpointExtension.class); |
| 102 | + Assert.state(extension.isPresent(), "No endpoint is specified and the return type of the @Bean method is " |
| 103 | + + "neither an @Endpoint, nor an @EndpointExtension"); |
| 104 | + return getEndpointAnnotation(extension.getClass("endpoint")); |
| 105 | + } |
| 106 | + |
| 107 | + private ConditionOutcome getMatchOutcome(Environment environment, |
| 108 | + MergedAnnotation<ConditionalOnAvailableEndpoint> conditionAnnotation, |
| 109 | + MergedAnnotation<Endpoint> endpointAnnotation) { |
| 110 | + ConditionMessage.Builder message = ConditionMessage.forCondition(ConditionalOnAvailableEndpoint.class); |
| 111 | + EndpointId endpointId = EndpointId.of(environment, endpointAnnotation.getString("id")); |
| 112 | + ConditionOutcome enablementOutcome = getEnablementOutcome(environment, endpointAnnotation, endpointId, message); |
53 | 113 | if (!enablementOutcome.isMatch()) {
|
54 | 114 | return enablementOutcome;
|
55 | 115 | }
|
56 |
| - ConditionMessage message = enablementOutcome.getConditionMessage(); |
57 |
| - Environment environment = context.getEnvironment(); |
58 | 116 | if (CloudPlatform.CLOUD_FOUNDRY.isActive(environment)) {
|
59 |
| - return new ConditionOutcome(true, message.andCondition(ConditionalOnAvailableEndpoint.class) |
60 |
| - .because("application is running on Cloud Foundry")); |
| 117 | + return ConditionOutcome.match(message.because("application is running on Cloud Foundry")); |
61 | 118 | }
|
62 |
| - EndpointId id = EndpointId.of(environment, |
63 |
| - getEndpointAttributes(ConditionalOnAvailableEndpoint.class, context, metadata).getString("id")); |
64 |
| - Set<Exposure> exposures = getExposures(environment); |
65 |
| - for (Exposure exposure : exposures) { |
66 |
| - if (exposure.isExposed(id)) { |
67 |
| - return new ConditionOutcome(true, |
68 |
| - message.andCondition(ConditionalOnAvailableEndpoint.class) |
69 |
| - .because("marked as exposed by a 'management.endpoints." + exposure.getPrefix() |
70 |
| - + ".exposure' property")); |
| 119 | + Set<EndpointExposure> exposuresToCheck = getExposuresToCheck(conditionAnnotation); |
| 120 | + Set<ExposureFilter> exposureFilters = getExposureFilters(environment); |
| 121 | + for (ExposureFilter exposureFilter : exposureFilters) { |
| 122 | + if (exposuresToCheck.contains(exposureFilter.getExposure()) && exposureFilter.isExposed(endpointId)) { |
| 123 | + return ConditionOutcome.match(message.because("marked as exposed by a 'management.endpoints." |
| 124 | + + exposureFilter.getExposure().name().toLowerCase() + ".exposure' property")); |
71 | 125 | }
|
72 | 126 | }
|
73 |
| - return new ConditionOutcome(false, message.andCondition(ConditionalOnAvailableEndpoint.class) |
74 |
| - .because("no 'management.endpoints' property marked it as exposed")); |
| 127 | + return ConditionOutcome.noMatch(message.because("no 'management.endpoints' property marked it as exposed")); |
| 128 | + } |
| 129 | + |
| 130 | + private ConditionOutcome getEnablementOutcome(Environment environment, |
| 131 | + MergedAnnotation<Endpoint> endpointAnnotation, EndpointId endpointId, ConditionMessage.Builder message) { |
| 132 | + String key = "management.endpoint." + endpointId.toLowerCaseString() + ".enabled"; |
| 133 | + Boolean userDefinedEnabled = environment.getProperty(key, Boolean.class); |
| 134 | + if (userDefinedEnabled != null) { |
| 135 | + return new ConditionOutcome(userDefinedEnabled, |
| 136 | + message.because("found property " + key + " with value " + userDefinedEnabled)); |
| 137 | + } |
| 138 | + Boolean userDefinedDefault = isEnabledByDefault(environment); |
| 139 | + if (userDefinedDefault != null) { |
| 140 | + return new ConditionOutcome(userDefinedDefault, message.because( |
| 141 | + "no property " + key + " found so using user defined default from " + ENABLED_BY_DEFAULT_KEY)); |
| 142 | + } |
| 143 | + boolean endpointDefault = endpointAnnotation.getBoolean("enableByDefault"); |
| 144 | + return new ConditionOutcome(endpointDefault, |
| 145 | + message.because("no property " + key + " found so using endpoint default of " + endpointDefault)); |
| 146 | + } |
| 147 | + |
| 148 | + private Boolean isEnabledByDefault(Environment environment) { |
| 149 | + Optional<Boolean> enabledByDefault = enabledByDefaultCache.get(environment); |
| 150 | + if (enabledByDefault == null) { |
| 151 | + enabledByDefault = Optional.ofNullable(environment.getProperty(ENABLED_BY_DEFAULT_KEY, Boolean.class)); |
| 152 | + enabledByDefaultCache.put(environment, enabledByDefault); |
| 153 | + } |
| 154 | + return enabledByDefault.orElse(null); |
| 155 | + } |
| 156 | + |
| 157 | + private Set<EndpointExposure> getExposuresToCheck( |
| 158 | + MergedAnnotation<ConditionalOnAvailableEndpoint> conditionAnnotation) { |
| 159 | + EndpointExposure[] exposure = conditionAnnotation.getEnumArray("exposure", EndpointExposure.class); |
| 160 | + return (exposure.length == 0) ? EnumSet.allOf(EndpointExposure.class) |
| 161 | + : new LinkedHashSet<>(Arrays.asList(exposure)); |
75 | 162 | }
|
76 | 163 |
|
77 |
| - private Set<Exposure> getExposures(Environment environment) { |
78 |
| - Set<Exposure> exposures = exposuresCache.get(environment); |
79 |
| - if (exposures == null) { |
80 |
| - exposures = new HashSet<>(2); |
| 164 | + private Set<ExposureFilter> getExposureFilters(Environment environment) { |
| 165 | + Set<ExposureFilter> exposureFilters = exposureFiltersCache.get(environment); |
| 166 | + if (exposureFilters == null) { |
| 167 | + exposureFilters = new HashSet<>(2); |
81 | 168 | if (environment.getProperty(JMX_ENABLED_KEY, Boolean.class, false)) {
|
82 |
| - exposures.add(new Exposure(environment, "jmx", DefaultIncludes.JMX)); |
| 169 | + exposureFilters.add(new ExposureFilter(environment, EndpointExposure.JMX)); |
83 | 170 | }
|
84 |
| - exposures.add(new Exposure(environment, "web", DefaultIncludes.WEB)); |
85 |
| - exposuresCache.put(environment, exposures); |
| 171 | + exposureFilters.add(new ExposureFilter(environment, EndpointExposure.WEB)); |
| 172 | + exposureFiltersCache.put(environment, exposureFilters); |
86 | 173 | }
|
87 |
| - return exposures; |
| 174 | + return exposureFilters; |
88 | 175 | }
|
89 | 176 |
|
90 |
| - static class Exposure extends IncludeExcludeEndpointFilter<ExposableEndpoint<?>> { |
| 177 | + static final class ExposureFilter extends IncludeExcludeEndpointFilter<ExposableEndpoint<?>> { |
91 | 178 |
|
92 |
| - private final String prefix; |
| 179 | + private final EndpointExposure exposure; |
93 | 180 |
|
94 |
| - @SuppressWarnings({ "rawtypes", "unchecked" }) |
95 |
| - Exposure(Environment environment, String prefix, DefaultIncludes defaultIncludes) { |
96 |
| - super((Class) ExposableEndpoint.class, environment, "management.endpoints." + prefix + ".exposure", |
97 |
| - defaultIncludes); |
98 |
| - this.prefix = prefix; |
| 181 | + @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 182 | + private ExposureFilter(Environment environment, EndpointExposure exposure) { |
| 183 | + super((Class) ExposableEndpoint.class, environment, |
| 184 | + "management.endpoints." + exposure.name().toLowerCase() + ".exposure", |
| 185 | + exposure.getDefaultIncludes()); |
| 186 | + this.exposure = exposure; |
99 | 187 | }
|
100 | 188 |
|
101 |
| - String getPrefix() { |
102 |
| - return this.prefix; |
| 189 | + EndpointExposure getExposure() { |
| 190 | + return this.exposure; |
103 | 191 | }
|
104 | 192 |
|
105 | 193 | boolean isExposed(EndpointId id) {
|
|
0 commit comments