Add support for parent hierarchy in AUtoConfigurationReport

pull/138/head
Dave Syer 11 years ago
parent 47d079d937
commit 20bede21ad

@ -39,6 +39,7 @@ import com.fasterxml.jackson.annotation.JsonPropertyOrder;
*
* @author Greg Turnquist
* @author Phillip Webb
* @author Dave Syer
*/
@ConfigurationProperties(name = "endpoints.autoconfig", ignoreUnknownFields = false)
public class AutoConfigurationReportEndpoint extends AbstractEndpoint<Report> {
@ -65,6 +66,8 @@ public class AutoConfigurationReportEndpoint extends AbstractEndpoint<Report> {
private MultiValueMap<String, MessageAndCondition> negativeMatches;
private Report parent;
public Report(AutoConfigurationReport report) {
this.positiveMatches = new LinkedMultiValueMap<String, MessageAndCondition>();
this.negativeMatches = new LinkedMultiValueMap<String, MessageAndCondition>();
@ -74,7 +77,9 @@ public class AutoConfigurationReportEndpoint extends AbstractEndpoint<Report> {
: this.negativeMatches, entry.getKey(), entry.getValue());
}
if (report.getParent() != null) {
this.parent = new Report(report.getParent());
}
}
private void dunno(MultiValueMap<String, MessageAndCondition> map, String source,
@ -93,6 +98,10 @@ public class AutoConfigurationReportEndpoint extends AbstractEndpoint<Report> {
return this.negativeMatches;
}
public Report getParent() {
return this.parent;
}
}
/**

@ -24,6 +24,7 @@ import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
@ -42,6 +43,8 @@ public class AutoConfigurationReport {
private final SortedMap<String, ConditionAndOutcomes> outcomes = new TreeMap<String, ConditionAndOutcomes>();
private AutoConfigurationReport parent;
/**
* Private constructor.
* @see #get(ConfigurableListableBeanFactory)
@ -70,6 +73,15 @@ public class AutoConfigurationReport {
return Collections.unmodifiableMap(this.outcomes);
}
/**
* The parent report (from a parent BeanFactory if there is one).
*
* @return the parent report (or null if there isn't one)
*/
public AutoConfigurationReport getParent() {
return this.parent;
}
/**
* Obtain a {@link AutoConfigurationReport} for the specified bean factory.
* @param beanFactory the bean factory
@ -77,14 +89,24 @@ public class AutoConfigurationReport {
*/
public static AutoConfigurationReport get(ConfigurableListableBeanFactory beanFactory) {
synchronized (beanFactory) {
AutoConfigurationReport report;
try {
return beanFactory.getBean(BEAN_NAME, AutoConfigurationReport.class);
report = beanFactory.getBean(BEAN_NAME, AutoConfigurationReport.class);
}
catch (NoSuchBeanDefinitionException ex) {
AutoConfigurationReport report = new AutoConfigurationReport();
report = new AutoConfigurationReport();
beanFactory.registerSingleton(BEAN_NAME, report);
return report;
}
locateParent(beanFactory.getParentBeanFactory(), report);
return report;
}
}
private static void locateParent(BeanFactory beanFactory,
AutoConfigurationReport report) {
if (beanFactory != null && report.parent == null
&& beanFactory.containsBean(BEAN_NAME)) {
report.parent = beanFactory.getBean(BEAN_NAME, AutoConfigurationReport.class);
}
}

@ -24,6 +24,7 @@ import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.autoconfigure.AutoConfigurationReport.ConditionAndOutcome;
import org.springframework.boot.autoconfigure.AutoConfigurationReport.ConditionAndOutcomes;
@ -84,6 +85,17 @@ public class AutoConfigurationReportTests {
sameInstance(AutoConfigurationReport.get(this.beanFactory)));
}
@Test
public void parent() throws Exception {
this.beanFactory.setParentBeanFactory(new DefaultListableBeanFactory());
AutoConfigurationReport.get((ConfigurableListableBeanFactory) this.beanFactory
.getParentBeanFactory());
assertThat(this.report,
sameInstance(AutoConfigurationReport.get(this.beanFactory)));
assertThat(this.report, not(nullValue()));
assertThat(this.report.getParent(), not(nullValue()));
}
@Test
public void recordConditionEvaluations() throws Exception {
this.report.recordConditionEvaluation("a", this.condition1, this.outcome1);

Loading…
Cancel
Save