Rework auto-configure report
Update the auto-configuration report to improve log formatting and to separate the internal report data-structure from the JSON friendly endpoint data-structure.pull/118/merge
parent
04fd7fdbbe
commit
dafeddca09
@ -0,0 +1,144 @@
|
||||
/*
|
||||
* Copyright 2012-2013 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
|
||||
import org.springframework.context.annotation.Condition;
|
||||
|
||||
/**
|
||||
* Records auto-configuration details for reporting and logging.
|
||||
*
|
||||
* @author Greg Turnquist
|
||||
* @author Dave Syer
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class AutoConfigurationReport {
|
||||
|
||||
private static final String BEAN_NAME = "autoConfigurationReport";
|
||||
|
||||
private final SortedMap<String, ConditionAndOutcomes> outcomes = new TreeMap<String, ConditionAndOutcomes>();
|
||||
|
||||
/**
|
||||
* Private constructor.
|
||||
* @see #get(ConfigurableListableBeanFactory)
|
||||
*/
|
||||
private AutoConfigurationReport() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Record the occurrence of condition evaluation.
|
||||
* @param source the source of the condition (class or method name)
|
||||
* @param condition the condition evaluated
|
||||
* @param outcome the condition outcome
|
||||
*/
|
||||
public void recordConditionEvaluation(String source, Condition condition,
|
||||
ConditionOutcome outcome) {
|
||||
if (!this.outcomes.containsKey(source)) {
|
||||
this.outcomes.put(source, new ConditionAndOutcomes());
|
||||
}
|
||||
this.outcomes.get(source).add(condition, outcome);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns condition outcomes from this report, grouped by the source.
|
||||
*/
|
||||
public Map<String, ConditionAndOutcomes> getConditionAndOutcomesBySource() {
|
||||
return Collections.unmodifiableMap(this.outcomes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtain a {@link AutoConfigurationReport} for the specified bean factory.
|
||||
* @param beanFactory the bean factory
|
||||
* @return an existing or new {@link AutoConfigurationReport}
|
||||
*/
|
||||
public static AutoConfigurationReport get(ConfigurableListableBeanFactory beanFactory) {
|
||||
synchronized (beanFactory) {
|
||||
try {
|
||||
return beanFactory.getBean(BEAN_NAME, AutoConfigurationReport.class);
|
||||
}
|
||||
catch (NoSuchBeanDefinitionException ex) {
|
||||
AutoConfigurationReport report = new AutoConfigurationReport();
|
||||
beanFactory.registerSingleton(BEAN_NAME, report);
|
||||
return report;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides access to a number of {@link ConditionAndOutcome} items.
|
||||
*/
|
||||
public static class ConditionAndOutcomes implements Iterable<ConditionAndOutcome> {
|
||||
|
||||
private List<ConditionAndOutcome> outcomes = new ArrayList<ConditionAndOutcome>();
|
||||
|
||||
public void add(Condition condition, ConditionOutcome outcome) {
|
||||
this.outcomes.add(new ConditionAndOutcome(condition, outcome));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return {@code true} if all outcomes match.
|
||||
*/
|
||||
public boolean isFullMatch() {
|
||||
for (ConditionAndOutcome conditionAndOutcomes : this) {
|
||||
if (!conditionAndOutcomes.getOutcome().isMatch()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<ConditionAndOutcome> iterator() {
|
||||
return Collections.unmodifiableList(this.outcomes).iterator();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides access to a single {@link Condition} and {@link ConditionOutcome}.
|
||||
*/
|
||||
public static class ConditionAndOutcome {
|
||||
|
||||
private final Condition condition;
|
||||
|
||||
private final ConditionOutcome outcome;
|
||||
|
||||
public ConditionAndOutcome(Condition condition, ConditionOutcome outcome) {
|
||||
this.condition = condition;
|
||||
this.outcome = outcome;
|
||||
}
|
||||
|
||||
public Condition getCondition() {
|
||||
return this.condition;
|
||||
}
|
||||
|
||||
public ConditionOutcome getOutcome() {
|
||||
return this.outcome;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,163 @@
|
||||
/*
|
||||
* Copyright 2012-2013 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.SpringApplicationErrorHandler;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurationReport.ConditionAndOutcome;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurationReport.ConditionAndOutcomes;
|
||||
import org.springframework.boot.logging.LogLevel;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* {@link ApplicationContextInitializer} and {@link SpringApplicationErrorHandler} that
|
||||
* writes the {@link AutoConfigurationReport} to the log. Reports are logged at the
|
||||
* {@link LogLevel#DEBUG DEBUG} level unless there was a problem, in which case they are
|
||||
* the {@link LogLevel#INFO INFO} level is used.
|
||||
*
|
||||
* <p>
|
||||
* This initializer is not intended to be shared across multiple application context
|
||||
* instances.
|
||||
*
|
||||
* @author Greg Turnquist
|
||||
* @author Dave Syer
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class AutoConfigurationReportLoggingInitializer implements
|
||||
ApplicationContextInitializer<ConfigurableApplicationContext>,
|
||||
SpringApplicationErrorHandler {
|
||||
|
||||
private static final String LOGGER_BEAN = "autoConfigurationReportLogger";
|
||||
|
||||
private AutoConfigurationReportLogger loggerBean;
|
||||
|
||||
@Override
|
||||
public void initialize(ConfigurableApplicationContext applicationContext) {
|
||||
this.loggerBean = new AutoConfigurationReportLogger(applicationContext);
|
||||
applicationContext.getBeanFactory().registerSingleton(LOGGER_BEAN,
|
||||
this.loggerBean);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleError(SpringApplication application,
|
||||
ConfigurableApplicationContext applicationContext, String[] args,
|
||||
Throwable exception) {
|
||||
if (this.loggerBean != null) {
|
||||
this.loggerBean.logAutoConfigurationReport(true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Spring bean to actually perform the logging.
|
||||
*/
|
||||
public static class AutoConfigurationReportLogger implements
|
||||
ApplicationListener<ContextRefreshedEvent> {
|
||||
|
||||
private final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private final ConfigurableApplicationContext applicationContext;
|
||||
|
||||
private final AutoConfigurationReport report;
|
||||
|
||||
public AutoConfigurationReportLogger(
|
||||
ConfigurableApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
// Get the report early in case the context fails to load
|
||||
this.report = AutoConfigurationReport.get(this.applicationContext
|
||||
.getBeanFactory());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ContextRefreshedEvent event) {
|
||||
if (event.getApplicationContext() == this.applicationContext) {
|
||||
logAutoConfigurationReport();
|
||||
}
|
||||
}
|
||||
|
||||
private void logAutoConfigurationReport() {
|
||||
logAutoConfigurationReport(!this.applicationContext.isActive());
|
||||
}
|
||||
|
||||
void logAutoConfigurationReport(boolean isCrashReport) {
|
||||
if (this.report.getConditionAndOutcomesBySource().size() > 0) {
|
||||
if (isCrashReport && this.logger.isInfoEnabled()) {
|
||||
this.logger.info(getLogMessage(this.report
|
||||
.getConditionAndOutcomesBySource()));
|
||||
}
|
||||
else if (!isCrashReport && this.logger.isDebugEnabled()) {
|
||||
this.logger.debug(getLogMessage(this.report
|
||||
.getConditionAndOutcomesBySource()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private StringBuilder getLogMessage(Map<String, ConditionAndOutcomes> outcomes) {
|
||||
StringBuilder message = new StringBuilder();
|
||||
message.append("\n\n\n");
|
||||
message.append("=========================\n");
|
||||
message.append("AUTO-CONFIGURATION REPORT\n");
|
||||
message.append("=========================\n\n\n");
|
||||
message.append("Positive matches:\n");
|
||||
message.append("-----------------\n");
|
||||
for (Map.Entry<String, ConditionAndOutcomes> entry : outcomes.entrySet()) {
|
||||
if (entry.getValue().isFullMatch()) {
|
||||
addLogMessage(message, entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
message.append("\n\n");
|
||||
message.append("Negative matches:\n");
|
||||
message.append("-----------------\n");
|
||||
for (Map.Entry<String, ConditionAndOutcomes> entry : outcomes.entrySet()) {
|
||||
if (!entry.getValue().isFullMatch()) {
|
||||
addLogMessage(message, entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
message.append("\n\n");
|
||||
return message;
|
||||
}
|
||||
|
||||
private void addLogMessage(StringBuilder message, String source,
|
||||
ConditionAndOutcomes conditionAndOutcomes) {
|
||||
message.append("\n " + ClassUtils.getShortName(source) + "\n");
|
||||
for (ConditionAndOutcome conditionAndOutcome : conditionAndOutcomes) {
|
||||
message.append(" - ");
|
||||
if (StringUtils.hasLength(conditionAndOutcome.getOutcome().getMessage())) {
|
||||
message.append(conditionAndOutcome.getOutcome().getMessage());
|
||||
}
|
||||
else {
|
||||
message.append(conditionAndOutcome.getOutcome().isMatch() ? "matched"
|
||||
: "did not match");
|
||||
}
|
||||
message.append(" (");
|
||||
message.append(ClassUtils.getShortName(conditionAndOutcome.getCondition()
|
||||
.getClass()));
|
||||
message.append(")\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,83 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2013 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.report;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
|
||||
|
||||
/**
|
||||
* Collects details about decision made during autoconfiguration (pass or fail)
|
||||
*
|
||||
* @author Greg Turnquist
|
||||
*/
|
||||
public class AutoConfigurationDecision {
|
||||
|
||||
private final String message;
|
||||
private final String classOrMethodName;
|
||||
private final ConditionOutcome outcome;
|
||||
|
||||
public AutoConfigurationDecision(String message, String classOrMethodName,
|
||||
ConditionOutcome outcome) {
|
||||
this.message = message;
|
||||
this.classOrMethodName = classOrMethodName;
|
||||
this.outcome = outcome;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return this.message;
|
||||
}
|
||||
|
||||
public String getClassOrMethodName() {
|
||||
return this.classOrMethodName;
|
||||
}
|
||||
|
||||
public ConditionOutcome getOutcome() {
|
||||
return this.outcome;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "AutoConfigurationDecision{" + "message='" + this.message + '\''
|
||||
+ ", classOrMethodName='" + this.classOrMethodName + '\'' + ", outcome="
|
||||
+ this.outcome + '}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
if (o == null || getClass() != o.getClass())
|
||||
return false;
|
||||
|
||||
AutoConfigurationDecision decision = (AutoConfigurationDecision) o;
|
||||
|
||||
if (this.message != null ? !this.message.equals(decision.message)
|
||||
: decision.message != null)
|
||||
return false;
|
||||
if (this.outcome != null ? !this.outcome.equals(decision.outcome)
|
||||
: decision.outcome != null)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = this.message != null ? this.message.hashCode() : 0;
|
||||
result = 31 * result + (this.outcome != null ? this.outcome.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
}
|
@ -1,252 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2013 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.report;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
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.ConditionOutcome;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* Bean used to gather auto-configuration decisions, and then generate a collection of
|
||||
* info for beans that were created as well as situations where the conditional outcome
|
||||
* was negative.
|
||||
*
|
||||
* @author Greg Turnquist
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class AutoConfigurationReport implements ApplicationContextAware,
|
||||
ApplicationListener<ContextRefreshedEvent> {
|
||||
|
||||
private static final String AUTO_CONFIGURATION_REPORT = "autoConfigurationReport";
|
||||
|
||||
private static Log logger = LogFactory.getLog(AutoConfigurationReport.class);
|
||||
|
||||
private Set<CreatedBeanInfo> beansCreated = new LinkedHashSet<CreatedBeanInfo>();
|
||||
|
||||
private Map<String, List<AutoConfigurationDecision>> autoconfigurationDecisions = new LinkedHashMap<String, List<AutoConfigurationDecision>>();
|
||||
|
||||
private Map<String, List<String>> positive = new LinkedHashMap<String, List<String>>();
|
||||
|
||||
private Map<String, List<String>> negative = new LinkedHashMap<String, List<String>>();
|
||||
|
||||
private ConfigurableApplicationContext context;
|
||||
|
||||
private boolean initialized = false;
|
||||
|
||||
public static void registerDecision(ConditionContext context, String message,
|
||||
String classOrMethodName, ConditionOutcome outcome) {
|
||||
if (context.getBeanFactory().containsBeanDefinition(AUTO_CONFIGURATION_REPORT)
|
||||
|| context.getBeanFactory().containsSingleton(AUTO_CONFIGURATION_REPORT)) {
|
||||
AutoConfigurationReport autoconfigurationReport = context.getBeanFactory()
|
||||
.getBean(AUTO_CONFIGURATION_REPORT, AutoConfigurationReport.class);
|
||||
autoconfigurationReport.registerDecision(message, classOrMethodName, outcome);
|
||||
}
|
||||
}
|
||||
|
||||
public static AutoConfigurationReport registerReport(
|
||||
ConfigurableApplicationContext applicationContext,
|
||||
ConfigurableListableBeanFactory beanFactory) {
|
||||
if (!beanFactory.containsBean(AutoConfigurationReport.AUTO_CONFIGURATION_REPORT)) {
|
||||
AutoConfigurationReport report = new AutoConfigurationReport();
|
||||
report.setApplicationContext(applicationContext);
|
||||
beanFactory.registerSingleton(
|
||||
AutoConfigurationReport.AUTO_CONFIGURATION_REPORT, report);
|
||||
}
|
||||
return beanFactory.getBean(AutoConfigurationReport.AUTO_CONFIGURATION_REPORT,
|
||||
AutoConfigurationReport.class);
|
||||
}
|
||||
|
||||
private void registerDecision(String message, String classOrMethodName,
|
||||
ConditionOutcome outcome) {
|
||||
AutoConfigurationDecision decision = new AutoConfigurationDecision(message,
|
||||
classOrMethodName, outcome);
|
||||
if (!this.autoconfigurationDecisions.containsKey(classOrMethodName)) {
|
||||
this.autoconfigurationDecisions.put(classOrMethodName,
|
||||
new ArrayList<AutoConfigurationDecision>());
|
||||
}
|
||||
this.autoconfigurationDecisions.get(classOrMethodName).add(decision);
|
||||
}
|
||||
|
||||
public Set<CreatedBeanInfo> getBeansCreated() {
|
||||
return this.beansCreated;
|
||||
}
|
||||
|
||||
public Map<String, List<String>> getNegativeDecisions() {
|
||||
return this.negative;
|
||||
}
|
||||
|
||||
public Set<Class<?>> getBeanTypesCreated() {
|
||||
Set<Class<?>> beanTypesCreated = new HashSet<Class<?>>();
|
||||
for (CreatedBeanInfo bootCreatedBeanInfo : this.getBeansCreated()) {
|
||||
beanTypesCreated.add(bootCreatedBeanInfo.getType());
|
||||
}
|
||||
return beanTypesCreated;
|
||||
}
|
||||
|
||||
public Set<String> getBeanNamesCreated() {
|
||||
Set<String> beanNamesCreated = new HashSet<String>();
|
||||
for (CreatedBeanInfo bootCreatedBeanInfo : this.getBeansCreated()) {
|
||||
beanNamesCreated.add(bootCreatedBeanInfo.getName());
|
||||
}
|
||||
return beanNamesCreated;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext)
|
||||
throws BeansException {
|
||||
this.context = (ConfigurableApplicationContext) applicationContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ContextRefreshedEvent event) {
|
||||
initialize();
|
||||
}
|
||||
|
||||
public void initialize() {
|
||||
if (!this.initialized) {
|
||||
synchronized (this) {
|
||||
if (!this.initialized) {
|
||||
this.initialized = true;
|
||||
try {
|
||||
splitDecisionsIntoPositiveAndNegative();
|
||||
scanPositiveDecisionsForBeansBootCreated();
|
||||
}
|
||||
finally {
|
||||
if (shouldLogReport() && logger.isInfoEnabled()) {
|
||||
logger.info("Created beans:");
|
||||
for (CreatedBeanInfo info : this.beansCreated) {
|
||||
logger.info(info);
|
||||
}
|
||||
logger.info("Negative decisions:");
|
||||
for (String key : this.negative.keySet()) {
|
||||
logger.info(key + ": " + this.negative.get(key));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean shouldLogReport() {
|
||||
String debug = this.context.getEnvironment().getProperty("debug", "false")
|
||||
.toLowerCase().trim();
|
||||
return debug.equals("true") || debug.equals("") //
|
||||
// inactive context is a sign that it crashed
|
||||
|| !this.context.isActive();
|
||||
}
|
||||
|
||||
/**
|
||||
* Scan the list of {@link AutoConfigurationDecision}'s, and if all outcomes true,
|
||||
* then put it on the positive list. Otherwise, put it on the negative list.
|
||||
*/
|
||||
private synchronized void splitDecisionsIntoPositiveAndNegative() {
|
||||
for (String key : this.autoconfigurationDecisions.keySet()) {
|
||||
boolean match = true;
|
||||
for (AutoConfigurationDecision decision : this.autoconfigurationDecisions
|
||||
.get(key)) {
|
||||
if (!decision.getOutcome().isMatch()) {
|
||||
match = false;
|
||||
}
|
||||
}
|
||||
if (match) {
|
||||
if (!this.positive.containsKey(key)) {
|
||||
this.positive.put(key, new ArrayList<String>());
|
||||
}
|
||||
for (AutoConfigurationDecision decision : this.autoconfigurationDecisions
|
||||
.get(key)) {
|
||||
this.positive.get(key).add(decision.getMessage());
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!this.negative.containsKey(key)) {
|
||||
this.negative.put(key, new ArrayList<String>());
|
||||
}
|
||||
for (AutoConfigurationDecision decision : this.autoconfigurationDecisions
|
||||
.get(key)) {
|
||||
this.negative.get(key).add(decision.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Scan all the decisions based on successful outcome, and try to find the
|
||||
* corresponding beans Boot created.
|
||||
*/
|
||||
private synchronized void scanPositiveDecisionsForBeansBootCreated() {
|
||||
for (String key : this.positive.keySet()) {
|
||||
for (AutoConfigurationDecision decision : this.autoconfigurationDecisions
|
||||
.get(key)) {
|
||||
for (String beanName : this.context.getBeanDefinitionNames()) {
|
||||
Object bean = null;
|
||||
if (decision.getMessage().contains(beanName)
|
||||
&& decision.getMessage().contains("matched")) {
|
||||
try {
|
||||
bean = this.context.getBean(beanName);
|
||||
boolean anyMethodsAreBeans = false;
|
||||
for (Method method : bean.getClass().getMethods()) {
|
||||
if (this.context.containsBean(method.getName())) {
|
||||
this.beansCreated.add(new CreatedBeanInfo(method
|
||||
.getName(), method.getReturnType(),
|
||||
this.positive.get(key)));
|
||||
anyMethodsAreBeans = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!anyMethodsAreBeans) {
|
||||
this.beansCreated.add(new CreatedBeanInfo(beanName, bean
|
||||
.getClass(), this.positive.get(key)));
|
||||
}
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
Class<?> type = null;
|
||||
ConfigurableApplicationContext configurable = this.context;
|
||||
String beanClassName = configurable.getBeanFactory()
|
||||
.getBeanDefinition(beanName).getBeanClassName();
|
||||
if (beanClassName != null) {
|
||||
type = ClassUtils.resolveClassName(beanClassName,
|
||||
configurable.getClassLoader());
|
||||
}
|
||||
this.beansCreated.add(new CreatedBeanInfo(beanName, type,
|
||||
this.positive.get(key)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,50 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2013 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.report;
|
||||
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.SpringApplicationErrorHandler;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class AutoConfigurationReportApplicationContextInitializer implements
|
||||
ApplicationContextInitializer<ConfigurableApplicationContext>,
|
||||
SpringApplicationErrorHandler {
|
||||
|
||||
private AutoConfigurationReport report;
|
||||
|
||||
@Override
|
||||
public void initialize(ConfigurableApplicationContext applicationContext) {
|
||||
ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
|
||||
this.report = AutoConfigurationReport.registerReport(applicationContext,
|
||||
beanFactory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleError(SpringApplication application,
|
||||
ConfigurableApplicationContext applicationContext, String[] args,
|
||||
Throwable exception) {
|
||||
if (this.report != null) {
|
||||
this.report.initialize(); // salvage a report if possible
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,82 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2013 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.report;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A collection of data about a bean created by Boot
|
||||
*
|
||||
* @author Greg Turnquist
|
||||
*/
|
||||
public class BootCreatedBeanInfo {
|
||||
|
||||
private final String beanName;
|
||||
private final Class<?> beanType;
|
||||
private final List<String> decisions;
|
||||
|
||||
public BootCreatedBeanInfo(String beanName, Object bean, List<String> decisions) {
|
||||
this.beanName = beanName;
|
||||
this.beanType = bean.getClass();
|
||||
this.decisions = decisions;
|
||||
}
|
||||
|
||||
public BootCreatedBeanInfo(String beanName, Class<?> declaredBeanType, List<String> decisions) {
|
||||
this.beanName = beanName;
|
||||
this.beanType = declaredBeanType;
|
||||
this.decisions = decisions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "BootCreatedBeanInfo{" + "beanName='" + beanName + '\'' + ", beanType=" + beanType
|
||||
+ ", decisions=" + decisions + '}';
|
||||
}
|
||||
|
||||
public String getBeanName() {
|
||||
return beanName;
|
||||
}
|
||||
|
||||
public Class<?> getBeanType() {
|
||||
return beanType;
|
||||
}
|
||||
|
||||
public List<String> getDecisions() {
|
||||
return decisions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
if (o == null || getClass() != o.getClass())
|
||||
return false;
|
||||
|
||||
BootCreatedBeanInfo bootCreatedBeanInfo = (BootCreatedBeanInfo) o;
|
||||
|
||||
if (beanName != null ? !beanName.equals(bootCreatedBeanInfo.beanName)
|
||||
: bootCreatedBeanInfo.beanName != null)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return beanName != null ? beanName.hashCode() : 0;
|
||||
}
|
||||
}
|
@ -1,77 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2013 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.report;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A collection of data about a bean created by Boot
|
||||
*
|
||||
* @author Greg Turnquist
|
||||
*/
|
||||
public class CreatedBeanInfo {
|
||||
|
||||
private final String name;
|
||||
private final Class<?> type;
|
||||
private final List<String> decisions;
|
||||
|
||||
public CreatedBeanInfo(String beanName, Class<?> declaredBeanType,
|
||||
List<String> decisions) {
|
||||
this.name = beanName;
|
||||
this.type = declaredBeanType;
|
||||
this.decisions = decisions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{" + "name='" + this.name + '\'' + ", type=" + this.type + ", decisions="
|
||||
+ this.decisions + '}';
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public Class<?> getType() {
|
||||
return this.type;
|
||||
}
|
||||
|
||||
public List<String> getDecisions() {
|
||||
return this.decisions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
if (o == null || getClass() != o.getClass())
|
||||
return false;
|
||||
|
||||
CreatedBeanInfo bootCreatedBeanInfo = (CreatedBeanInfo) o;
|
||||
|
||||
if (this.name != null ? !this.name.equals(bootCreatedBeanInfo.name)
|
||||
: bootCreatedBeanInfo.name != null)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.name != null ? this.name.hashCode() : 0;
|
||||
}
|
||||
}
|
@ -0,0 +1,166 @@
|
||||
/*
|
||||
* Copyright 2012-2013 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogConfigurationException;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.commons.logging.impl.LogFactoryImpl;
|
||||
import org.apache.commons.logging.impl.NoOpLog;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurationReportLoggingInitializer.AutoConfigurationReportLogger;
|
||||
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.BDDMockito.willAnswer;
|
||||
import static org.mockito.Matchers.anyObject;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link AutoConfigurationReportLoggingInitializer}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class AutoConfigurationReportLoggingInitializerTests {
|
||||
|
||||
private static ThreadLocal<Log> logThreadLocal = new ThreadLocal<Log>();
|
||||
|
||||
private Log log;
|
||||
|
||||
private AutoConfigurationReportLoggingInitializer initializer;
|
||||
|
||||
protected List<String> debugLog = new ArrayList<String>();
|
||||
|
||||
protected List<String> infoLog = new ArrayList<String>();
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
|
||||
this.log = mock(Log.class);
|
||||
logThreadLocal.set(this.log);
|
||||
|
||||
given(this.log.isDebugEnabled()).willReturn(true);
|
||||
willAnswer(new Answer<Object>() {
|
||||
@Override
|
||||
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||
return AutoConfigurationReportLoggingInitializerTests.this.debugLog
|
||||
.add(String.valueOf(invocation.getArguments()[0]));
|
||||
}
|
||||
}).given(this.log).debug(anyObject());
|
||||
|
||||
given(this.log.isInfoEnabled()).willReturn(true);
|
||||
willAnswer(new Answer<Object>() {
|
||||
@Override
|
||||
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||
return AutoConfigurationReportLoggingInitializerTests.this.infoLog
|
||||
.add(String.valueOf(invocation.getArguments()[0]));
|
||||
}
|
||||
}).given(this.log).info(anyObject());
|
||||
|
||||
LogFactory.releaseAll();
|
||||
System.setProperty(LogFactory.FACTORY_PROPERTY, MockLogFactory.class.getName());
|
||||
this.initializer = new AutoConfigurationReportLoggingInitializer();
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
System.clearProperty(LogFactory.FACTORY_PROPERTIES);
|
||||
LogFactory.releaseAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void logsDebugOnContextRefresh() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
this.initializer.initialize(context);
|
||||
context.register(Config.class);
|
||||
context.refresh();
|
||||
assertThat(this.debugLog.size(), not(equalTo(0)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void logsInfoOnError() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
this.initializer.initialize(context);
|
||||
context.register(ErrorConfig.class);
|
||||
try {
|
||||
context.refresh();
|
||||
fail("Did not error");
|
||||
}
|
||||
catch (Exception ex) {
|
||||
this.initializer.handleError(null, context, new String[] {}, ex);
|
||||
}
|
||||
|
||||
assertThat(this.debugLog.size(), equalTo(0));
|
||||
assertThat(this.infoLog.size(), not(equalTo(0)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void logsOutput() throws Exception {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
this.initializer.initialize(context);
|
||||
context.register(Config.class);
|
||||
context.refresh();
|
||||
for (String message : this.debugLog) {
|
||||
System.out.println(message);
|
||||
}
|
||||
// Just basic sanity check, test is for visual inspection
|
||||
String l = this.debugLog.get(0);
|
||||
assertThat(l, containsString("not a web application (OnWebApplicationCondition)"));
|
||||
}
|
||||
|
||||
public static class MockLogFactory extends LogFactoryImpl {
|
||||
@Override
|
||||
public Log getInstance(String name) throws LogConfigurationException {
|
||||
if (AutoConfigurationReportLogger.class.getName().equals(name)) {
|
||||
return logThreadLocal.get();
|
||||
}
|
||||
return new NoOpLog();
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Import(WebMvcAutoConfiguration.class)
|
||||
static class Config {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Import(WebMvcAutoConfiguration.class)
|
||||
static class ErrorConfig {
|
||||
@Bean
|
||||
public String iBreak() {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue