+ add flag to disable bean-factory-locator
This commit is contained in:
Costin Leau
2011-07-06 11:50:55 +03:00
parent fff04a8777
commit e261ecf6bd
7 changed files with 59 additions and 13 deletions

View File

@@ -162,7 +162,7 @@ limitations under the License.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.3</version>
<version>2.7.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
@@ -224,7 +224,6 @@ limitations under the License.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7.2</version>
<configuration>
<forkMode>always</forkMode>
<systemPropertyVariables>

View File

@@ -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;
}
}

View File

@@ -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;

View File

@@ -51,6 +51,15 @@ consider using a dedicated utility such as the <util:*/> namespace and its 'prop
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="use-bean-factory-locator" type="xsd:string" default="true" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[
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.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
</xsd:element>

View File

@@ -61,4 +61,5 @@ public class CacheIntegrationTest extends RecreatingContextTest{
public void testCacheWithXml() throws Exception {
Cache cache = ctx.getBean("cache-with-xml", Cache.class);
}
}
}

View File

@@ -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();
}
}
}

View File

@@ -21,4 +21,6 @@
<util:properties id="props">
<prop key="disable-tcp">false</prop>
</util:properties>
<gfe:cache id="no-bl" use-bean-factory-locator="false" />
</beans>