Refactored the logic in the CacheParser adding the PdxDiskStoreAwareBeanFactoryPostProcessor to the ApplicationContext by registering the PdxDiskStoreAwareBeanFactoryPostProcessor as a BeanDefinition in the BeanDefinitionRegistry (ConfigurableApplicationContext).

This commit is contained in:
John Blum
2013-11-01 15:07:40 -07:00
parent 415f23216f
commit a602254e21
2 changed files with 22 additions and 25 deletions

View File

@@ -21,12 +21,11 @@ import java.util.List;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.data.gemfire.CacheFactoryBean;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
@@ -54,6 +53,7 @@ class CacheParser extends AbstractSimpleBeanDefinitionParser {
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
super.doParse(element, builder);
ParsingUtils.setPropertyValue(element, builder, "cache-xml-location", "cacheXml");
ParsingUtils.setPropertyReference(element, builder, "properties-ref", "properties");
ParsingUtils.setPropertyReference(element, builder, "pdx-serializer-ref", "pdxSerializer");
@@ -111,15 +111,23 @@ class CacheParser extends AbstractSimpleBeanDefinitionParser {
final String pdxDiskStoreName = element.getAttribute("pdx-disk-store");
if (!StringUtils.isEmpty(pdxDiskStoreName)) {
final BeanDefinitionRegistry registry = parserContext.getRegistry();
if (registry instanceof ConfigurableApplicationContext) {
((ConfigurableApplicationContext) registry).addBeanFactoryPostProcessor(
new PdxDiskStoreAwareBeanFactoryPostProcessor(pdxDiskStoreName));
}
registerPdxDiskStoreAwareBeanFactoryPostProcessor(parserContext, pdxDiskStoreName);
}
}
private void registerPdxDiskStoreAwareBeanFactoryPostProcessor(ParserContext parserContext, String pdxDiskStoreName) {
BeanDefinitionReaderUtils.registerWithGeneratedName(
createPdxDiskStoreAwareBeanFactoryPostProcessorBeanDefinition(pdxDiskStoreName),
parserContext.getRegistry());
}
private AbstractBeanDefinition createPdxDiskStoreAwareBeanFactoryPostProcessorBeanDefinition(String pdxDiskStoreName) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(
PdxDiskStoreAwareBeanFactoryPostProcessor.class);
builder.addConstructorArgValue(pdxDiskStoreName);
return builder.getBeanDefinition();
}
private void parseDynamicRegionFactory(Element element, BeanDefinitionBuilder builder) {
Element dynamicRegionFactory = DomUtils.getChildElementByTagName(element, "dynamic-region-factory");
if (dynamicRegionFactory != null) {

View File

@@ -20,14 +20,11 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.data.gemfire.CacheFactoryBean;
import org.springframework.data.gemfire.test.GemfireTestApplicationContextInitializer;
import org.springframework.test.context.ContextConfiguration;
@@ -55,23 +52,15 @@ public class CacheUsingPdxNamespaceTest {
@Autowired
private ApplicationContext context;
protected PdxDiskStoreAwareBeanFactoryPostProcessor getPdxDiskStoreAwareBeanFactoryPostProcessor(AbstractApplicationContext context) {
for (BeanFactoryPostProcessor postProcessor : context.getBeanFactoryPostProcessors()) {
if (postProcessor instanceof PdxDiskStoreAwareBeanFactoryPostProcessor) {
return (PdxDiskStoreAwareBeanFactoryPostProcessor) postProcessor;
}
}
return null;
}
@Test
public void testApplicationContextHasPdxDiskStoreAwareBeanFactoryPostProcessor() {
assumeTrue(context instanceof AbstractApplicationContext);
final PdxDiskStoreAwareBeanFactoryPostProcessor postProcessor = getPdxDiskStoreAwareBeanFactoryPostProcessor(
(AbstractApplicationContext) context);
PdxDiskStoreAwareBeanFactoryPostProcessor postProcessor = context.getBean(
PdxDiskStoreAwareBeanFactoryPostProcessor.class);
// NOTE the postProcessor reference will not be null as the ApplicationContext.getBean(:Class) method (getting
// a bean by Class type) will throw a NoSuchBeanDefinitionException if no bean of type
// PdxDiskStoreAwareBeanFactoryPostProcessor could be found, or throw a NoUniqueBeanDefinitionException if
// our PdxDiskStoreAwareBeanFactoryPostProcessor bean is not unique!
assertNotNull(postProcessor);
assertEquals("pdxStore", postProcessor.getPdxDiskStoreName());
}