diff --git a/pom.xml b/pom.xml index 0cdfad47..49f7695d 100644 --- a/pom.xml +++ b/pom.xml @@ -162,7 +162,7 @@ limitations under the License. org.apache.maven.plugins maven-surefire-plugin - 2.4.3 + 2.7.2 org.apache.maven.plugins @@ -224,7 +224,6 @@ limitations under the License. org.apache.maven.plugins maven-surefire-plugin - 2.7.2 always diff --git a/src/main/java/org/springframework/data/gemfire/CacheFactoryBean.java b/src/main/java/org/springframework/data/gemfire/CacheFactoryBean.java index 8e805dd2..6137c649 100644 --- a/src/main/java/org/springframework/data/gemfire/CacheFactoryBean.java +++ b/src/main/java/org/springframework/data/gemfire/CacheFactoryBean.java @@ -62,17 +62,21 @@ public class CacheFactoryBean implements BeanNameAware, BeanFactoryAware, BeanCl private Properties properties; private DistributedSystem system; private ClassLoader beanClassLoader; - private GemfireBeanFactoryLocator factoryLocator = new GemfireBeanFactoryLocator(); + private GemfireBeanFactoryLocator factoryLocator; private BeanFactory beanFactory; private String beanName; + private boolean useBeanFactoryLocator = true; + public void afterPropertiesSet() throws Exception { // initialize locator - factoryLocator.setBeanFactory(beanFactory); - factoryLocator.setBeanName(beanName); - factoryLocator.afterPropertiesSet(); - + if (useBeanFactoryLocator) { + factoryLocator = new GemfireBeanFactoryLocator(); + factoryLocator.setBeanFactory(beanFactory); + factoryLocator.setBeanName(beanName); + factoryLocator.afterPropertiesSet(); + } Properties cfgProps = mergeProperties(); system = DistributedSystem.connect(cfgProps); @@ -128,7 +132,10 @@ public class CacheFactoryBean implements BeanNameAware, BeanFactoryAware, BeanCl } system = null; - factoryLocator.destroy(); + if (factoryLocator != null) { + factoryLocator.destroy(); + factoryLocator = null; + } } public DataAccessException translateExceptionIfPossible(RuntimeException ex) { @@ -187,4 +194,15 @@ public class CacheFactoryBean implements BeanNameAware, BeanFactoryAware, BeanCl public void setCacheXml(Resource cacheXml) { this.cacheXml = cacheXml; } + + /** + * Indicates whether a bean factory locator is enabled (default) for this cache definition or not. The locator stores + * the enclosing bean factory reference to allow auto-wiring of Spring beans into GemFire managed classes. Usually disabled + * when the same cache is used in multiple application context/bean factories inside the same VM. + * + * @param usage true if the bean factory locator is used underneath or not + */ + public void setUseBeanFactoryLocator(boolean usage) { + this.useBeanFactoryLocator = usage; + } } \ No newline at end of file diff --git a/src/main/java/org/springframework/data/gemfire/GemfireBeanFactoryLocator.java b/src/main/java/org/springframework/data/gemfire/GemfireBeanFactoryLocator.java index 0e62916a..2b0eeca2 100644 --- a/src/main/java/org/springframework/data/gemfire/GemfireBeanFactoryLocator.java +++ b/src/main/java/org/springframework/data/gemfire/GemfireBeanFactoryLocator.java @@ -112,7 +112,8 @@ public class GemfireBeanFactoryLocator implements BeanFactoryLocator, BeanFactor if (log.isDebugEnabled()) log.debug("adding key=" + name + " w/ reference=" + beanFactory); - if (beanFactories.containsKey(name) || beanFactories.putIfAbsent(name, beanFactory) != null) { + if (beanFactories.containsKey(name) && !beanFactory.equals(beanFactories.get(name)) + || beanFactories.putIfAbsent(name, beanFactory) != null) { throw new IllegalArgumentException("a beanFactoryReference already exists for key " + factoryName); } } @@ -120,10 +121,11 @@ public class GemfireBeanFactoryLocator implements BeanFactoryLocator, BeanFactor } public void destroy() { - for (String name : names) { - beanFactories.remove(name); + if (names != null) { + for (String name : names) { + beanFactories.remove(name); + } } - if (beanFactory == defaultFactory) { synchronized (GemfireBeanFactoryLocator.class) { defaultFactory = null; diff --git a/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.1.xsd b/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.1.xsd index 184e391d..eb43844f 100644 --- a/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.1.xsd +++ b/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.1.xsd @@ -51,6 +51,15 @@ consider using a dedicated utility such as the namespace and its 'prop ]]> + + + + + diff --git a/src/test/java/org/springframework/data/gemfire/CacheIntegrationTest.java b/src/test/java/org/springframework/data/gemfire/CacheIntegrationTest.java index d2bfc72b..f01ada81 100644 --- a/src/test/java/org/springframework/data/gemfire/CacheIntegrationTest.java +++ b/src/test/java/org/springframework/data/gemfire/CacheIntegrationTest.java @@ -61,4 +61,5 @@ public class CacheIntegrationTest extends RecreatingContextTest{ public void testCacheWithXml() throws Exception { Cache cache = ctx.getBean("cache-with-xml", Cache.class); } -} + +} \ No newline at end of file diff --git a/src/test/java/org/springframework/data/gemfire/config/CacheNamespaceTest.java b/src/test/java/org/springframework/data/gemfire/config/CacheNamespaceTest.java index 675392ff..16287627 100644 --- a/src/test/java/org/springframework/data/gemfire/config/CacheNamespaceTest.java +++ b/src/test/java/org/springframework/data/gemfire/config/CacheNamespaceTest.java @@ -17,12 +17,14 @@ package org.springframework.data.gemfire.config; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import org.junit.Test; import org.springframework.core.io.Resource; import org.springframework.data.gemfire.CacheFactoryBean; +import org.springframework.data.gemfire.GemfireBeanFactoryLocator; import org.springframework.data.gemfire.RecreatingContextTest; import org.springframework.data.gemfire.TestUtils; @@ -61,4 +63,17 @@ public class CacheNamespaceTest extends RecreatingContextTest { assertEquals(ctx.getBean("props"), TestUtils.readField("properties", cfb)); } + @Test(expected = IllegalArgumentException.class) + public void testNoBeanFactory() throws Exception { + assertTrue(ctx.containsBean("no-bl")); + CacheFactoryBean cfb = (CacheFactoryBean) ctx.getBean("&no-bl"); + GemfireBeanFactoryLocator locator = new GemfireBeanFactoryLocator(); + try { + assertNotNull(locator.useBeanFactory("cache-with-name")); + locator.useBeanFactory("no-bl"); + } finally { + locator.destroy(); + } + } + } \ No newline at end of file diff --git a/src/test/resources/org/springframework/data/gemfire/config/cache-ns.xml b/src/test/resources/org/springframework/data/gemfire/config/cache-ns.xml index c3e03f48..e80adb57 100644 --- a/src/test/resources/org/springframework/data/gemfire/config/cache-ns.xml +++ b/src/test/resources/org/springframework/data/gemfire/config/cache-ns.xml @@ -21,4 +21,6 @@ false + + \ No newline at end of file