Rename Outcome -> ConditionOutcome

This commit is contained in:
Phillip Webb 2013-11-05 00:04:06 -08:00
parent a9a6077fdb
commit ab249b034d
12 changed files with 87 additions and 67 deletions

View File

@ -32,7 +32,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.Outcome;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration.DefaultTemplateResolverConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
@ -97,17 +97,17 @@ public class ErrorMvcAutoConfiguration implements EmbeddedServletContainerCustom
private static class ErrorTemplateMissingCondition extends SpringBootCondition {
@Override
public Outcome getMatchOutcome(ConditionContext context,
public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) {
if (ClassUtils.isPresent("org.thymeleaf.spring3.SpringTemplateEngine",
context.getClassLoader())) {
if (DefaultTemplateResolverConfiguration.templateExists(
context.getEnvironment(), context.getResourceLoader(), "error")) {
return Outcome.noMatch("Thymeleaf template found for error view");
return ConditionOutcome.noMatch("Thymeleaf template found for error view");
}
}
// FIXME: add matcher for JSP view if Jasper detected
return Outcome.match("no error template view detected");
return ConditionOutcome.match("no error template view detected");
};
}

View File

@ -17,34 +17,54 @@
package org.springframework.boot.autoconfigure.condition;
/**
* Outcome for a match, including log message.
* Outcome for a condition match, including log message.
*
* @author Phillip Webb
*/
public class Outcome {
public class ConditionOutcome {
private final boolean match;
private final String message;
public Outcome(boolean match, String message) {
public ConditionOutcome(boolean match, String message) {
this.match = match;
this.message = message;
}
public static Outcome match() {
/**
* Create a new {@link ConditionOutcome} instance for a 'match'.
*/
public static ConditionOutcome match() {
return match(null);
}
public static Outcome match(String message) {
return new Outcome(true, message);
/**
* Create a new {@link ConditionOutcome} instance for 'match'.
* @param message the message
*/
public static ConditionOutcome match(String message) {
return new ConditionOutcome(true, message);
}
public static Outcome noMatch(String message) {
return new Outcome(false, message);
/**
* Create a new {@link ConditionOutcome} instance for 'no match'.
* @param message the message
*/
public static ConditionOutcome noMatch(String message) {
return new ConditionOutcome(false, message);
}
/**
* Return {@code true} if the outcome was a match.
*/
public boolean isMatch() {
return this.match;
}
/**
* Return an outcome message or {@code null}.
*/
public String getMessage() {
return this.message;
}

View File

@ -56,7 +56,7 @@ class OnBeanCondition extends SpringBootCondition implements ConfigurationCondit
}
@Override
public Outcome getMatchOutcome(ConditionContext context,
public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) {
if (metadata.isAnnotated(ConditionalOnBean.class.getName())) {
@ -64,7 +64,7 @@ class OnBeanCondition extends SpringBootCondition implements ConfigurationCondit
ConditionalOnBean.class);
List<String> matching = getMatchingBeans(context, spec);
if (matching.isEmpty()) {
return Outcome.noMatch("@ConditionalOnBean " + spec + " found no beans");
return ConditionOutcome.noMatch("@ConditionalOnBean " + spec + " found no beans");
}
}
@ -73,12 +73,12 @@ class OnBeanCondition extends SpringBootCondition implements ConfigurationCondit
ConditionalOnMissingBean.class);
List<String> matching = getMatchingBeans(context, spec);
if (!matching.isEmpty()) {
return Outcome.noMatch("@ConditionalOnMissingBean " + spec
return ConditionOutcome.noMatch("@ConditionalOnMissingBean " + spec
+ " found the following " + matching);
}
}
return Outcome.match();
return ConditionOutcome.match();
}
private List<String> getMatchingBeans(ConditionContext context, BeanSearchSpec beans) {

View File

@ -37,7 +37,7 @@ import org.springframework.util.StringUtils;
class OnClassCondition extends SpringBootCondition {
@Override
public Outcome getMatchOutcome(ConditionContext context,
public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) {
MultiValueMap<String, Object> onClasses = getAttributes(metadata,
@ -46,7 +46,7 @@ class OnClassCondition extends SpringBootCondition {
List<String> missing = getMatchingClasses(onClasses, MatchType.MISSING,
context);
if (!missing.isEmpty()) {
return Outcome.noMatch("required @ConditionalOnClass classes not found: "
return ConditionOutcome.noMatch("required @ConditionalOnClass classes not found: "
+ StringUtils.collectionToCommaDelimitedString(missing));
}
}
@ -57,12 +57,12 @@ class OnClassCondition extends SpringBootCondition {
List<String> present = getMatchingClasses(onMissingClasses,
MatchType.PRESENT, context);
if (!present.isEmpty()) {
return Outcome.noMatch("required @ConditionalOnMissing classes found: "
return ConditionOutcome.noMatch("required @ConditionalOnMissing classes found: "
+ StringUtils.collectionToCommaDelimitedString(present));
}
}
return Outcome.match();
return ConditionOutcome.match();
}
private MultiValueMap<String, Object> getAttributes(AnnotatedTypeMetadata metadata,

View File

@ -33,7 +33,7 @@ import org.springframework.core.type.ClassMetadata;
public class OnExpressionCondition extends SpringBootCondition {
@Override
public Outcome getMatchOutcome(ConditionContext context,
public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) {
String expression = (String) metadata.getAnnotationAttributes(
@ -59,7 +59,7 @@ public class OnExpressionCondition extends SpringBootCondition {
message.append(" on " + ((ClassMetadata) metadata).getClassName());
}
message.append(": " + expression);
return new Outcome(result, message.toString());
return new ConditionOutcome(result, message.toString());
}
}

View File

@ -38,7 +38,7 @@ class OnResourceCondition extends SpringBootCondition {
private ResourceLoader defaultResourceLoader = new DefaultResourceLoader();
@Override
public Outcome getMatchOutcome(ConditionContext context,
public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) {
MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(
ConditionalOnResource.class.getName(), true);
@ -51,11 +51,11 @@ class OnResourceCondition extends SpringBootCondition {
"@ConditionalOnResource annotations must specify at least one resource location");
for (String location : locations) {
if (!loader.getResource(location).exists()) {
return Outcome.noMatch("resource not found: " + location);
return ConditionOutcome.noMatch("resource not found: " + location);
}
}
}
return Outcome.match();
return ConditionOutcome.match();
}
private void collectValues(List<String> names, List<Object> values) {

View File

@ -37,41 +37,41 @@ class OnWebApplicationCondition extends SpringBootCondition {
private static final String WEB_CONTEXT_CLASS = "org.springframework.web.context.support.GenericWebApplicationContext";
@Override
public Outcome getMatchOutcome(ConditionContext context,
public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) {
boolean webApplicationRequired = metadata
.isAnnotated(ConditionalOnWebApplication.class.getName());
Outcome webApplication = isWebApplication(context, metadata);
ConditionOutcome webApplication = isWebApplication(context, metadata);
if (webApplicationRequired && !webApplication.isMatch()) {
return Outcome.noMatch(webApplication.getMessage());
return ConditionOutcome.noMatch(webApplication.getMessage());
}
if (!webApplicationRequired && webApplication.isMatch()) {
return Outcome.noMatch(webApplication.getMessage());
return ConditionOutcome.noMatch(webApplication.getMessage());
}
return Outcome.match(webApplication.getMessage());
return ConditionOutcome.match(webApplication.getMessage());
}
private Outcome isWebApplication(ConditionContext context,
private ConditionOutcome isWebApplication(ConditionContext context,
AnnotatedTypeMetadata metadata) {
if (!ClassUtils.isPresent(WEB_CONTEXT_CLASS, context.getClassLoader())) {
return Outcome.noMatch("web application classes not found");
return ConditionOutcome.noMatch("web application classes not found");
}
if (context.getBeanFactory() != null) {
String[] scopes = context.getBeanFactory().getRegisteredScopeNames();
if (ObjectUtils.containsElement(scopes, "session")) {
return Outcome.match("found web application 'session' scope");
return ConditionOutcome.match("found web application 'session' scope");
}
}
if (context.getEnvironment() instanceof StandardServletEnvironment) {
return Outcome.match("found web application StandardServletEnvironment");
return ConditionOutcome.match("found web application StandardServletEnvironment");
}
return Outcome.noMatch("not a web application");
return ConditionOutcome.noMatch("not a web application");
}
}

View File

@ -40,7 +40,7 @@ public abstract class SpringBootCondition implements Condition {
@Override
public final boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Outcome result = getMatchOutcome(context, metadata);
ConditionOutcome result = getMatchOutcome(context, metadata);
StringBuilder message = getMessage(metadata, result);
if (!result.isMatch()) {
@ -60,7 +60,7 @@ public abstract class SpringBootCondition implements Condition {
return true;
}
private StringBuilder getMessage(AnnotatedTypeMetadata metadata, Outcome result) {
private StringBuilder getMessage(AnnotatedTypeMetadata metadata, ConditionOutcome result) {
StringBuilder message = new StringBuilder();
message.append("Condition ");
message.append(ClassUtils.getShortName(getClass()));
@ -91,7 +91,7 @@ public abstract class SpringBootCondition implements Condition {
/**
* Determine the outcome of the match along with suitable log output.
*/
public abstract Outcome getMatchOutcome(ConditionContext context,
public abstract ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata);
protected final boolean anyMatches(ConditionContext context,

View File

@ -33,7 +33,7 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.Outcome;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.ApplicationContext;
@ -182,30 +182,30 @@ public class DataSourceAutoConfiguration implements EnvironmentAware {
protected abstract String getDataSourceClassName();
@Override
public Outcome getMatchOutcome(ConditionContext context,
public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) {
if (!ClassUtils.isPresent(getDataSourceClassName(), context.getClassLoader())) {
return Outcome.noMatch(getDataSourceClassName()
return ConditionOutcome.noMatch(getDataSourceClassName()
+ " DataSource class not found");
}
String driverClassName = getDriverClassName(context.getEnvironment(),
getDataSourceClassLoader(context));
if (driverClassName == null) {
return Outcome.noMatch("no database driver");
return ConditionOutcome.noMatch("no database driver");
}
String url = getUrl(context.getEnvironment(), context.getClassLoader());
if (url == null) {
return Outcome.noMatch("no database URL");
return ConditionOutcome.noMatch("no database URL");
}
if (ClassUtils.isPresent(driverClassName, context.getClassLoader())) {
return Outcome.match("found database driver " + driverClassName);
return ConditionOutcome.match("found database driver " + driverClassName);
}
return Outcome.noMatch("missing database driver " + driverClassName);
return ConditionOutcome.noMatch("missing database driver " + driverClassName);
}
/**
@ -253,10 +253,10 @@ public class DataSourceAutoConfiguration implements EnvironmentAware {
}
@Override
public Outcome getMatchOutcome(ConditionContext context,
public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) {
if (matches(context, metadata, this.tomcatCondition)) {
return Outcome.noMatch("tomcat DataSource");
return ConditionOutcome.noMatch("tomcat DataSource");
}
return super.getMatchOutcome(context, metadata);
}
@ -278,17 +278,17 @@ public class DataSourceAutoConfiguration implements EnvironmentAware {
private SpringBootCondition dbcpCondition = new BasicDatabaseCondition();
@Override
public Outcome getMatchOutcome(ConditionContext context,
public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) {
if (anyMatches(context, metadata, this.tomcatCondition, this.dbcpCondition)) {
return Outcome.noMatch("existing non-embedded database detected");
return ConditionOutcome.noMatch("existing non-embedded database detected");
}
EmbeddedDatabaseType type = EmbeddedDatabaseConnection.get(
context.getClassLoader()).getType();
if (type == null) {
return Outcome.noMatch("no embedded database detected");
return ConditionOutcome.noMatch("no embedded database detected");
}
return Outcome.match("embedded database " + type + " detected");
return ConditionOutcome.match("embedded database " + type + " detected");
}
}
@ -301,20 +301,20 @@ public class DataSourceAutoConfiguration implements EnvironmentAware {
private SpringBootCondition embeddedCondition = new EmbeddedDatabaseCondition();
@Override
public Outcome getMatchOutcome(ConditionContext context,
public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) {
if (anyMatches(context, metadata, this.tomcatCondition, this.dbcpCondition,
this.embeddedCondition)) {
return Outcome.match("existing auto database detected");
return ConditionOutcome.match("existing auto database detected");
}
if (BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
context.getBeanFactory(), DataSource.class, true, false).length > 0) {
return Outcome.match("Existing bean configured database detected");
return ConditionOutcome.match("Existing bean configured database detected");
}
return Outcome.noMatch("no existing bean configured database");
return ConditionOutcome.noMatch("no existing bean configured database");
}
}
}

View File

@ -16,7 +16,7 @@
package org.springframework.boot.autoconfigure.report;
import org.springframework.boot.autoconfigure.condition.Outcome;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
/**
* Collects details about decision made during autoconfiguration (pass or fail)
@ -27,10 +27,10 @@ public class AutoConfigurationDecision {
private final String message;
private final String classOrMethodName;
private final Outcome outcome;
private final ConditionOutcome outcome;
public AutoConfigurationDecision(String message, String classOrMethodName,
Outcome outcome) {
ConditionOutcome outcome) {
this.message = message;
this.classOrMethodName = classOrMethodName;
this.outcome = outcome;
@ -44,7 +44,7 @@ public class AutoConfigurationDecision {
return this.classOrMethodName;
}
public Outcome getOutcome() {
public ConditionOutcome getOutcome() {
return this.outcome;
}

View File

@ -29,7 +29,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.autoconfigure.condition.Outcome;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
@ -66,7 +66,7 @@ public class AutoConfigurationReport implements ApplicationContextAware,
private boolean initialized = false;
public static void registerDecision(ConditionContext context, String message,
String classOrMethodName, Outcome outcome) {
String classOrMethodName, ConditionOutcome outcome) {
if (context.getBeanFactory().containsBeanDefinition(AUTO_CONFIGURATION_REPORT)
|| context.getBeanFactory().containsSingleton(AUTO_CONFIGURATION_REPORT)) {
AutoConfigurationReport autoconfigurationReport = context.getBeanFactory()
@ -89,7 +89,7 @@ public class AutoConfigurationReport implements ApplicationContextAware,
}
private void registerDecision(String message, String classOrMethodName,
Outcome outcome) {
ConditionOutcome outcome) {
AutoConfigurationDecision decision = new AutoConfigurationDecision(message,
classOrMethodName, outcome);
if (!this.autoconfigurationDecisions.containsKey(classOrMethodName)) {

View File

@ -24,7 +24,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.Outcome;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
@ -65,20 +65,20 @@ public class DispatcherServletAutoConfiguration {
private static class DefaultDispatcherServletCondition extends SpringBootCondition {
@Override
public Outcome getMatchOutcome(ConditionContext context,
public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) {
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
String[] beans = beanFactory.getBeanNamesForType(DispatcherServlet.class,
false, false);
if (beans.length == 0) {
return Outcome.match("no DispatcherServlet found");
return ConditionOutcome.match("no DispatcherServlet found");
}
if (Arrays.asList(beans).contains(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)) {
return Outcome.noMatch("found DispatcherServlet named "
return ConditionOutcome.noMatch("found DispatcherServlet named "
+ DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
}
return Outcome
return ConditionOutcome
.match("one or more DispatcherServlets found and none is named "
+ DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
}