Skip to content

Commit aaa2ff5

Browse files
committed
Extract @ConditionalOnEnabledEndpoint
Extract @ConditionalOnEnabledEndpoint to a top level class. See gh-2798
1 parent 968b68c commit aaa2ff5

File tree

4 files changed

+144
-85
lines changed

4 files changed

+144
-85
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java

Lines changed: 1 addition & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717
package org.springframework.boot.actuate.autoconfigure;
1818

1919
import java.io.IOException;
20-
import java.lang.annotation.ElementType;
21-
import java.lang.annotation.Retention;
22-
import java.lang.annotation.RetentionPolicy;
23-
import java.lang.annotation.Target;
2420
import java.util.List;
2521

2622
import javax.servlet.Filter;
@@ -38,6 +34,7 @@
3834
import org.springframework.beans.factory.SmartInitializingSingleton;
3935
import org.springframework.beans.factory.annotation.Autowired;
4036
import org.springframework.boot.actuate.autoconfigure.ManagementServerProperties.Security;
37+
import org.springframework.boot.actuate.condition.ConditionalOnEnabledEndpoint;
4138
import org.springframework.boot.actuate.endpoint.Endpoint;
4239
import org.springframework.boot.actuate.endpoint.EnvironmentEndpoint;
4340
import org.springframework.boot.actuate.endpoint.HealthEndpoint;
@@ -53,17 +50,14 @@
5350
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
5451
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
5552
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
56-
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
5753
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
5854
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
5955
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
6056
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
61-
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
6257
import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration;
6358
import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration;
6459
import org.springframework.boot.autoconfigure.web.ServerProperties;
6560
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
66-
import org.springframework.boot.bind.RelaxedPropertyResolver;
6761
import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
6862
import org.springframework.boot.context.embedded.EmbeddedServletContainerException;
6963
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
@@ -73,14 +67,10 @@
7367
import org.springframework.context.ApplicationListener;
7468
import org.springframework.context.ConfigurableApplicationContext;
7569
import org.springframework.context.annotation.Bean;
76-
import org.springframework.context.annotation.ConditionContext;
77-
import org.springframework.context.annotation.Conditional;
7870
import org.springframework.context.annotation.Configuration;
7971
import org.springframework.context.event.ContextClosedEvent;
80-
import org.springframework.core.annotation.AnnotationAttributes;
8172
import org.springframework.core.env.ConfigurableEnvironment;
8273
import org.springframework.core.env.PropertySource;
83-
import org.springframework.core.type.AnnotatedTypeMetadata;
8474
import org.springframework.web.context.WebApplicationContext;
8575
import org.springframework.web.filter.OncePerRequestFilter;
8676
import org.springframework.web.servlet.DispatcherServlet;
@@ -343,78 +333,4 @@ public static ManagementServerPort get(BeanFactory beanFactory) {
343333

344334
}
345335

346-
/**
347-
* {@link Conditional} that checks whether or not an endpoint is enabled. Matches if
348-
* the value of the {@code endpoints.<name>.enabled} property is {@code true}. Does
349-
* not match if the property's value or {@code enabledByDefault} is {@code false}.
350-
* Otherwise, matches if the value of the {@code endpoints.enabled} property is
351-
* {@code true} or if the property is not configured.
352-
*
353-
* @since 1.2.4
354-
*/
355-
@Conditional(OnEnabledEndpointCondition.class)
356-
@Retention(RetentionPolicy.RUNTIME)
357-
@Target(ElementType.METHOD)
358-
public static @interface ConditionalOnEnabledEndpoint {
359-
360-
/**
361-
* The name of the endpoint.
362-
* @return The name of the endpoint
363-
*/
364-
public String value();
365-
366-
/**
367-
* Returns whether or not the endpoint is enabled by default.
368-
* @return {@code true} if the endpoint is enabled by default, otherwise
369-
* {@code false}
370-
*/
371-
public boolean enabledByDefault() default true;
372-
373-
}
374-
375-
private static class OnEnabledEndpointCondition extends SpringBootCondition {
376-
377-
@Override
378-
public ConditionOutcome getMatchOutcome(ConditionContext context,
379-
AnnotatedTypeMetadata metadata) {
380-
AnnotationAttributes annotationAttributes = AnnotationAttributes
381-
.fromMap(metadata
382-
.getAnnotationAttributes(ConditionalOnEnabledEndpoint.class
383-
.getName()));
384-
String endpointName = annotationAttributes.getString("value");
385-
boolean enabledByDefault = annotationAttributes
386-
.getBoolean("enabledByDefault");
387-
ConditionOutcome specificEndpointOutcome = determineSpecificEndpointOutcome(
388-
endpointName, enabledByDefault, context);
389-
if (specificEndpointOutcome != null) {
390-
return specificEndpointOutcome;
391-
}
392-
return determineAllEndpointsOutcome(context);
393-
394-
}
395-
396-
private ConditionOutcome determineSpecificEndpointOutcome(String endpointName,
397-
boolean enabledByDefault, ConditionContext context) {
398-
RelaxedPropertyResolver endpointPropertyResolver = new RelaxedPropertyResolver(
399-
context.getEnvironment(), "endpoints." + endpointName + ".");
400-
if (endpointPropertyResolver.containsProperty("enabled") || !enabledByDefault) {
401-
boolean match = endpointPropertyResolver.getProperty("enabled",
402-
Boolean.class, enabledByDefault);
403-
return new ConditionOutcome(match, "The " + endpointName + " is "
404-
+ (match ? "enabled" : "disabled"));
405-
}
406-
return null;
407-
}
408-
409-
private ConditionOutcome determineAllEndpointsOutcome(ConditionContext context) {
410-
RelaxedPropertyResolver allEndpointsPropertyResolver = new RelaxedPropertyResolver(
411-
context.getEnvironment(), "endpoints.");
412-
boolean match = Boolean.valueOf(allEndpointsPropertyResolver.getProperty(
413-
"enabled", "true"));
414-
return new ConditionOutcome(match, "All endpoints are "
415-
+ (match ? "enabled" : "disabled") + " by default");
416-
}
417-
418-
}
419-
420336
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2012-2015 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+
* http://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.condition;
18+
19+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.RetentionPolicy;
22+
import java.lang.annotation.Target;
23+
24+
import org.springframework.context.annotation.Conditional;
25+
26+
/**
27+
* {@link Conditional} that checks whether or not an endpoint is enabled. Matches if the
28+
* value of the {@code endpoints.<name>.enabled} property is {@code true}. Does not match
29+
* if the property's value or {@code enabledByDefault} is {@code false}. Otherwise,
30+
* matches if the value of the {@code endpoints.enabled} property is {@code true} or if
31+
* the property is not configured.
32+
*
33+
* @author Andy Wilkinson
34+
* @since 1.2.4
35+
*/
36+
@Conditional(OnEnabledEndpointCondition.class)
37+
@Retention(RetentionPolicy.RUNTIME)
38+
@Target(ElementType.METHOD)
39+
public @interface ConditionalOnEnabledEndpoint {
40+
41+
/**
42+
* The name of the endpoint.
43+
* @return The name of the endpoint
44+
*/
45+
public String value();
46+
47+
/**
48+
* Returns whether or not the endpoint is enabled by default.
49+
* @return {@code true} if the endpoint is enabled by default, otherwise {@code false}
50+
*/
51+
public boolean enabledByDefault() default true;
52+
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2012-2015 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+
* http://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.condition;
18+
19+
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
20+
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
21+
import org.springframework.boot.bind.RelaxedPropertyResolver;
22+
import org.springframework.context.annotation.Condition;
23+
import org.springframework.context.annotation.ConditionContext;
24+
import org.springframework.core.annotation.AnnotationAttributes;
25+
import org.springframework.core.type.AnnotatedTypeMetadata;
26+
27+
/**
28+
* {@link Condition} that checks whether or not an endpoint is enabled.
29+
*
30+
* @author Andy Wilkinson
31+
*/
32+
class OnEnabledEndpointCondition extends SpringBootCondition {
33+
34+
@Override
35+
public ConditionOutcome getMatchOutcome(ConditionContext context,
36+
AnnotatedTypeMetadata metadata) {
37+
AnnotationAttributes annotationAttributes = AnnotationAttributes.fromMap(metadata
38+
.getAnnotationAttributes(ConditionalOnEnabledEndpoint.class.getName()));
39+
String endpointName = annotationAttributes.getString("value");
40+
boolean enabledByDefault = annotationAttributes.getBoolean("enabledByDefault");
41+
ConditionOutcome outcome = determineEndpointOutcome(endpointName,
42+
enabledByDefault, context);
43+
if (outcome != null) {
44+
return outcome;
45+
}
46+
return determineAllEndpointsOutcome(context);
47+
}
48+
49+
private ConditionOutcome determineEndpointOutcome(String endpointName,
50+
boolean enabledByDefault, ConditionContext context) {
51+
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(
52+
context.getEnvironment(), "endpoints." + endpointName + ".");
53+
if (resolver.containsProperty("enabled") || !enabledByDefault) {
54+
boolean match = resolver.getProperty("enabled", Boolean.class,
55+
enabledByDefault);
56+
return new ConditionOutcome(match, "The " + endpointName + " is "
57+
+ (match ? "enabled" : "disabled"));
58+
}
59+
return null;
60+
}
61+
62+
private ConditionOutcome determineAllEndpointsOutcome(ConditionContext context) {
63+
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(
64+
context.getEnvironment(), "endpoints.");
65+
boolean match = Boolean.valueOf(resolver.getProperty("enabled", "true"));
66+
return new ConditionOutcome(match, "All endpoints are "
67+
+ (match ? "enabled" : "disabled") + " by default");
68+
}
69+
70+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2012-2015 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+
* http://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+
/**
18+
* {@code @Condition} annotations and supporting classes.
19+
*/
20+
package org.springframework.boot.actuate.condition;

0 commit comments

Comments
 (0)