Harmonize ConditionOutcome messages

Add ConditionMessage class to help build condition messages in a
uniform format and update existing conditions to use it.

Fixes gh-6756
This commit is contained in:
Phillip Webb
2016-09-01 17:49:23 +01:00
parent 41dc53f5dd
commit 7396ccfe04
49 changed files with 1325 additions and 404 deletions

View File

@@ -26,6 +26,7 @@ import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
@@ -92,8 +93,13 @@ public class EndpointMBeanExportAutoConfiguration {
AnnotatedTypeMetadata metadata) {
boolean jmxEnabled = isEnabled(context, "spring.jmx.");
boolean jmxEndpointsEnabled = isEnabled(context, "endpoints.jmx.");
return new ConditionOutcome(jmxEnabled && jmxEndpointsEnabled,
"JMX Endpoints");
if (jmxEnabled && jmxEndpointsEnabled) {
return ConditionOutcome.match(
ConditionMessage.forCondition("JMX Enabled").found("properties")
.items("spring.jmx.enabled", "endpoints.jmx.enabled"));
}
return ConditionOutcome.noMatch(ConditionMessage.forCondition("JMX Enabled")
.because("spring.jmx.enabled or endpoints.jmx.enabled is not set"));
}
private boolean isEnabled(ConditionContext context, String prefix) {

View File

@@ -40,6 +40,7 @@ import org.springframework.boot.actuate.endpoint.mvc.ManagementServletContext;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@@ -333,13 +334,18 @@ public class EndpointWebMvcAutoConfiguration
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) {
ConditionMessage.Builder message = ConditionMessage
.forCondition("Management Server MVC");
if (!(context.getResourceLoader() instanceof WebApplicationContext)) {
return ConditionOutcome.noMatch("Non WebApplicationContext");
return ConditionOutcome
.noMatch(message.because("non WebApplicationContext"));
}
ManagementServerPort port = ManagementServerPort.get(context.getEnvironment(),
context.getBeanFactory());
return new ConditionOutcome(port == ManagementServerPort.SAME,
"Management context");
if (port == ManagementServerPort.SAME) {
return ConditionOutcome.match(message.because("port is same"));
}
return ConditionOutcome.noMatch(message.because("port is not same"));
}
}

View File

@@ -37,6 +37,7 @@ import org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoints;
import org.springframework.boot.actuate.endpoint.mvc.ShutdownMvcEndpoint;
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -182,20 +183,22 @@ public class EndpointWebMvcManagementContextConfiguration {
AnnotatedTypeMetadata metadata) {
Environment environment = context.getEnvironment();
String config = environment.resolvePlaceholders("${logging.file:}");
ConditionMessage.Builder message = ConditionMessage
.forCondition("Log File");
if (StringUtils.hasText(config)) {
return ConditionOutcome.match("Found logging.file: " + config);
return ConditionOutcome.match(message.found("logging.file").items(config));
}
config = environment.resolvePlaceholders("${logging.path:}");
if (StringUtils.hasText(config)) {
return ConditionOutcome.match("Found logging.path: " + config);
return ConditionOutcome.match(message.found("logging.path").items(config));
}
config = new RelaxedPropertyResolver(environment, "endpoints.logfile.")
.getProperty("external-file");
if (StringUtils.hasText(config)) {
return ConditionOutcome
.match("Found endpoints.logfile.external-file: " + config);
return ConditionOutcome.match(
message.found("endpoints.logfile.external-file").items(config));
}
return ConditionOutcome.noMatch("Found no log file configuration");
return ConditionOutcome.noMatch(message.didNotFind("logging file").atAll());
}
}

View File

@@ -25,6 +25,7 @@ import org.springframework.boot.actuate.endpoint.mvc.JolokiaMvcEndpoint;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -97,8 +98,12 @@ public class JolokiaAutoConfiguration {
public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) {
boolean endpointsEnabled = isEnabled(context, "endpoints.", true);
boolean enabled = isEnabled(context, "endpoints.jolokia.", endpointsEnabled);
return new ConditionOutcome(enabled, "Jolokia enabled");
ConditionMessage.Builder message = ConditionMessage
.forCondition("Jolokia");
if (isEnabled(context, "endpoints.jolokia.", endpointsEnabled)) {
return ConditionOutcome.match(message.because("enabled"));
}
return ConditionOutcome.noMatch(message.because("not enabled"));
}
private boolean isEnabled(ConditionContext context, String prefix,

View File

@@ -33,6 +33,7 @@ import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -223,10 +224,13 @@ public class ManagementWebSecurityAutoConfiguration {
.getProperty("management.security.enabled", "true");
String basicEnabled = context.getEnvironment()
.getProperty("security.basic.enabled", "true");
return new ConditionOutcome(
"true".equalsIgnoreCase(managementEnabled)
&& !"true".equalsIgnoreCase(basicEnabled),
"Management security enabled and basic disabled");
ConditionMessage.Builder message = ConditionMessage
.forCondition("WebSecurityEnabled");
if ("true".equalsIgnoreCase(managementEnabled)
&& !"true".equalsIgnoreCase(basicEnabled)) {
return ConditionOutcome.match(message.because("security enabled"));
}
return ConditionOutcome.noMatch(message.because("security disabled"));
}
}

View File

@@ -16,6 +16,9 @@
package org.springframework.boot.actuate.autoconfigure;
import java.lang.annotation.Annotation;
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.boot.bind.RelaxedPropertyResolver;
@@ -33,9 +36,10 @@ abstract class OnEnabledEndpointElementCondition extends SpringBootCondition {
private final String prefix;
private final Class<?> annotationType;
private final Class<? extends Annotation> annotationType;
OnEnabledEndpointElementCondition(String prefix, Class<?> annotationType) {
OnEnabledEndpointElementCondition(String prefix,
Class<? extends Annotation> annotationType) {
this.prefix = prefix;
this.annotationType = annotationType;
}
@@ -69,7 +73,8 @@ abstract class OnEnabledEndpointElementCondition extends SpringBootCondition {
if (resolver.containsProperty("enabled")) {
boolean match = resolver.getProperty("enabled", Boolean.class, true);
return new ConditionOutcome(match,
getEndpointElementOutcomeMessage(endpointName, match));
ConditionMessage.forCondition(this.annotationType).because(
this.prefix + endpointName + ".enabled is " + match));
}
return null;
}
@@ -79,7 +84,8 @@ abstract class OnEnabledEndpointElementCondition extends SpringBootCondition {
context.getEnvironment(), this.prefix + "defaults.");
boolean match = Boolean.valueOf(resolver.getProperty("enabled", "true"));
return new ConditionOutcome(match,
getDefaultEndpointElementOutcomeMessage(match));
ConditionMessage.forCondition(this.annotationType).because(
this.prefix + "defaults.enabled is considered " + match));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
package org.springframework.boot.actuate.condition;
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.boot.bind.RelaxedPropertyResolver;
@@ -31,14 +32,11 @@ import org.springframework.core.type.AnnotatedTypeMetadata;
*/
class OnEnabledEndpointCondition extends SpringBootCondition {
private static final String ANNOTATION_CLASS = ConditionalOnEnabledEndpoint.class
.getName();
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) {
AnnotationAttributes annotationAttributes = AnnotationAttributes
.fromMap(metadata.getAnnotationAttributes(ANNOTATION_CLASS));
AnnotationAttributes annotationAttributes = AnnotationAttributes.fromMap(metadata
.getAnnotationAttributes(ConditionalOnEnabledEndpoint.class.getName()));
String endpointName = annotationAttributes.getString("value");
boolean enabledByDefault = annotationAttributes.getBoolean("enabledByDefault");
ConditionOutcome outcome = determineEndpointOutcome(endpointName,
@@ -56,8 +54,11 @@ class OnEnabledEndpointCondition extends SpringBootCondition {
if (resolver.containsProperty("enabled") || !enabledByDefault) {
boolean match = resolver.getProperty("enabled", Boolean.class,
enabledByDefault);
return new ConditionOutcome(match, "The endpoint " + endpointName + " is "
+ (match ? "enabled" : "disabled"));
ConditionMessage message = ConditionMessage
.forCondition(ConditionalOnEnabledEndpoint.class,
"(" + endpointName + ")")
.because(match ? "enabled" : "disabled");
return new ConditionOutcome(match, message);
}
return null;
}
@@ -66,8 +67,11 @@ class OnEnabledEndpointCondition extends SpringBootCondition {
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(
context.getEnvironment(), "endpoints.");
boolean match = Boolean.valueOf(resolver.getProperty("enabled", "true"));
return new ConditionOutcome(match,
"All endpoints are " + (match ? "enabled" : "disabled") + " by default");
ConditionMessage message = ConditionMessage
.forCondition(ConditionalOnEnabledEndpoint.class)
.because("All endpoints are " + (match ? "enabled" : "disabled")
+ " by default");
return new ConditionOutcome(match, message);
}
}