SGF-409 - Modify the GemfireDataSourcePostProcessor (basis for <gfe:datasource>) to not assume a GemFire Server was configured and bootstrapped with Spring and subsequently that the SDG ListRegionsOnServerFunction was registered.

This commit is contained in:
John Blum
2015-07-15 18:21:06 -07:00
parent dd572d0b36
commit 96bb8b3d60
5 changed files with 332 additions and 38 deletions

View File

@@ -17,15 +17,17 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.data.gemfire.function.execution.GemfireFunctionOperations;
import org.springframework.data.gemfire.function.execution.GemfireOnServersFunctionTemplate;
import org.springframework.data.gemfire.support.ListRegionsOnServerFunction;
import org.springframework.data.gemfire.util.CollectionUtils;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.client.ClientCache;
import com.gemstone.gemfire.cache.client.ClientRegionFactory;
import com.gemstone.gemfire.cache.client.ClientRegionShortcut;
import com.gemstone.gemfire.management.internal.cli.functions.ListFunctionFunction;
/**
* A {@link BeanFactoryPostProcessor} to register a Client Region bean, if necessary, for each Region accessible
@@ -44,47 +46,74 @@ public class GemfireDataSourcePostProcessor implements BeanFactoryPostProcessor
this.cache = cache;
}
/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.beans.factory.config.BeanFactoryPostProcessor#postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)
*/
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
createClientRegions(beanFactory);
if (isFunctionAvailable(ListRegionsOnServerFunction.ID)) {
createClientRegions(beanFactory);
}
}
private void createClientRegions(ConfigurableListableBeanFactory beanFactory) {
GemfireFunctionOperations functionTemplate = new GemfireOnServersFunctionTemplate(cache);
// TODO what happens when the GemFire cluster contains a mix of "pure" GemFire Servers (non-Spring configured)
// as well as Spring configured/bootstrapped GemFire Servers?
boolean isFunctionAvailable(String targetFunctionId) {
GemfireOnServersFunctionTemplate functionTemplate = new GemfireOnServersFunctionTemplate(cache);
Iterable<String> regionNames = functionTemplate.executeAndExtract(new ListRegionsOnServerFunction());
Iterable<String> functionIds = CollectionUtils.nullSafeIterable(
functionTemplate.<Iterable<String>>executeAndExtract(new ListFunctionFunction()));
ClientRegionFactory<?, ?> clientRegionFactory = null;
if (regionNames != null && regionNames.iterator().hasNext()) {
clientRegionFactory = cache.createClientRegionFactory(ClientRegionShortcut.PROXY);
for (String functionId : functionIds) {
if (functionId.equals(targetFunctionId)) {
return true;
}
}
for (String regionName : regionNames) {
boolean createRegion = true;
return false;
}
if (beanFactory.containsBean(regionName)) {
Object existingBean = beanFactory.getBean(regionName);
Assert.isTrue(existingBean instanceof Region, String.format(
"Cannot create a ClientRegion bean named '%1$s'. A bean with this name of type '%2$s' already exists.",
regionName, existingBean.getClass().getName()));
createRegion = false;
}
/* (non-Javadoc) */
void createClientRegions(ConfigurableListableBeanFactory beanFactory) {
GemfireOnServersFunctionTemplate functionTemplate = new GemfireOnServersFunctionTemplate(cache);
if (createRegion) {
if (logger.isDebugEnabled()) {
logger.debug(String.format("Creating Client Region bean with name '%s'...", regionName));
Iterable<String> regionNames = CollectionUtils.nullSafeIterable(
functionTemplate.<Iterable<String>>executeAndExtract(new ListRegionsOnServerFunction()));
if (regionNames.iterator().hasNext()) {
ClientRegionFactory<?, ?> clientRegionFactory = cache.createClientRegionFactory(ClientRegionShortcut.PROXY);
for (String regionName : regionNames) {
boolean createRegion = true;
if (beanFactory.containsBean(regionName)) {
Object existingBean = beanFactory.getBean(regionName);
Assert.isTrue(existingBean instanceof Region, String.format(
"Cannot create a client Region bean named '%1$s'. A bean with this name of type '%2$s' already exists.",
regionName, ObjectUtils.nullSafeClassName(existingBean)));
createRegion = false;
}
beanFactory.registerSingleton(regionName, clientRegionFactory.create(regionName));
} else {
if (logger.isDebugEnabled()) {
logger.debug(String.format("A Region named '%s' is already defined.", regionName));
if (createRegion) {
log("Creating Region bean with name '%s'...", regionName);
beanFactory.registerSingleton(regionName, clientRegionFactory.create(regionName));
}
else {
log("A Region with name '%s' is already defined.", regionName);
}
}
}
}
/* (non-Javadoc) */
private void log(String message, Object... arguments) {
if (logger.isDebugEnabled()) {
logger.debug(String.format(message, arguments));
}
}
}

View File

@@ -22,27 +22,40 @@ import com.gemstone.gemfire.cache.execute.Function;
import com.gemstone.gemfire.cache.execute.FunctionContext;
/**
* @author David Turanski
* ListRegionsOnServerFunction is a GemFire Function class that returns a List of names for all Regions
* defined in the GemFire cluster.
*
* @author David Turanski
* @author John Blum
* @see com.gemstone.gemfire.cache.execute.Function
*/
@SuppressWarnings("serial")
public class ListRegionsOnServerFunction implements Function {
/* (non-Javadoc)
public static final String ID = ListRegionsOnServerFunction.class.getName();
/*
* (non-Javadoc)
* @see com.gemstone.gemfire.cache.execute.Function#execute(com.gemstone.gemfire.cache.execute.FunctionContext)
*/
@Override
public void execute(FunctionContext functionContext) {
Cache cache = CacheFactory.getAnyInstance();
List<String> regionNames = new ArrayList<String>();
for (Region<?, ?> region: cache.rootRegions()) {
for (Region<?, ?> region : getCache().rootRegions()) {
regionNames.add(region.getName());
}
functionContext.getResultSender().lastResult(regionNames);
}
/* (non-Javadoc)
/* (non-Javadoc) */
Cache getCache() {
return CacheFactory.getAnyInstance();
}
/*
* (non-Javadoc)
* @see com.gemstone.gemfire.cache.execute.Function#getId()
*/
@Override
@@ -50,7 +63,8 @@ public class ListRegionsOnServerFunction implements Function {
return this.getClass().getName();
}
/* (non-Javadoc)
/*
* (non-Javadoc)
* @see com.gemstone.gemfire.cache.execute.Function#hasResult()
*/
@Override
@@ -58,7 +72,8 @@ public class ListRegionsOnServerFunction implements Function {
return true;
}
/* (non-Javadoc)
/*
* (non-Javadoc)
* @see com.gemstone.gemfire.cache.execute.Function#isHA()
*/
@Override
@@ -66,7 +81,8 @@ public class ListRegionsOnServerFunction implements Function {
return false;
}
/* (non-Javadoc)
/*
* (non-Javadoc)
* @see com.gemstone.gemfire.cache.execute.Function#optimizeForWrite()
*/
@Override

View File

@@ -18,6 +18,8 @@ package org.springframework.data.gemfire.util;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* The CollectionUtils class is a utility class for working with Java Collections Framework and classes.
@@ -32,7 +34,37 @@ import java.util.Collections;
public abstract class CollectionUtils extends org.springframework.util.CollectionUtils {
/**
* A null-safe operation returning the original Collection is non-null or an empty Collection
* A null-safe operation returning the original Iterable object if non-null or a default, empty Iterable
* implementation if null.
*
* @param <T> the class type of the iterable elements.
* @param iterable the Iterable object evaluated for a null reference.
* @return the Iterable object if not null or a default, empty Iterable implementation otherwise.
* @see java.lang.Iterable
* @see java.util.Iterator
*/
public static <T> Iterable<T> nullSafeIterable(Iterable<T> iterable) {
return (iterable != null ? iterable : new Iterable<T>() {
@Override public Iterator<T> iterator() {
return new Iterator<T>() {
@Override public boolean hasNext() {
return false;
}
@Override public T next() {
throw new NoSuchElementException("no elements in this Iterator");
}
@Override public void remove() {
throw new UnsupportedOperationException("operation not supported");
}
};
}
});
}
/**
* A null-safe operation returning the original Collection if non-null or an empty Collection
* (implemented with List) if null.
*
* @param <T> the element class type of the Collection.