added support for gfe-data:datasource

This commit is contained in:
David Turanski
2013-01-22 08:32:02 -05:00
parent 7b52197f0f
commit 418715a739
13 changed files with 766 additions and 1 deletions

View File

@@ -0,0 +1,180 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.springframework.data.gemfire.client;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.gemfire.function.execution.GemfireFunctionOperations;
import org.springframework.data.gemfire.function.execution.GemfireOnServersFunctionTemplate;
import org.springframework.data.gemfire.repository.support.ListRegionsOnServerFunction;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.client.ClientCache;
import com.gemstone.gemfire.cache.client.ClientCacheFactory;
import com.gemstone.gemfire.cache.client.ClientRegionFactory;
import com.gemstone.gemfire.cache.client.ClientRegionShortcut;
import com.gemstone.gemfire.cache.client.Pool;
import com.gemstone.gemfire.cache.query.QueryService;
import com.gemstone.gemfire.distributed.DistributedSystem;
/**
* @author David Turanski
*
*/
public abstract class AbstractGemfireDataSource {
protected Log logger = LogFactory.getLog(getClass());
protected ClientCache cache;
public AbstractGemfireDataSource(ClientCacheFactory clientCacheFactory) {
this.cache = clientCacheFactory.create();
}
public AbstractGemfireDataSource(String host, int port, Properties properties) {
ClientCacheFactory clientCacheFactory = new ClientCacheFactory();
addRemoteConnection(clientCacheFactory, host, port);
initializeClientCache(clientCacheFactory,properties);
this.cache = clientCacheFactory.create();
createClientRegions();
}
public AbstractGemfireDataSource(List<InetSocketAddress> remoteConnections, Properties properties) {
ClientCacheFactory clientCacheFactory = new ClientCacheFactory();
for (InetSocketAddress remoteConnection: remoteConnections) {
addRemoteConnection(clientCacheFactory, remoteConnection.getHostName(), remoteConnection.getPort());
}
initializeClientCache(clientCacheFactory,properties);
this.cache = clientCacheFactory.create();
createClientRegions();
}
protected abstract void addRemoteConnection(ClientCacheFactory clientCacheFactory, String host, int port);
public void connect() {
this.connect();
}
public String getName() {
return cache.getName();
}
public String getServerGroup() {
return cache.getDefaultPool().getServerGroup();
}
public List<InetSocketAddress> getLocators() {
return cache.getDefaultPool().getLocators();
}
public List<InetSocketAddress> getServers() {
return cache.getDefaultPool().getServers();
}
public QueryService getQueryService() {
return cache.getQueryService();
}
public QueryService getQueryService(String poolName) {
return cache.getQueryService(poolName);
}
public QueryService getLocalQueryService() {
return cache.getLocalQueryService();
}
public void close(boolean keepalive) {
cache.close(keepalive);
}
public void readyForEvents() {
cache.readyForEvents();
}
public Set<InetSocketAddress> getCurrentServers() {
return cache.getCurrentServers();
}
public Pool getDefaultPool() {
return cache.getDefaultPool();
}
public DistributedSystem getDistributedSystem() {
return cache.getDistributedSystem();
}
public <K, V> Region<K, V> getRegion(String path) {
return cache.getRegion(path);
}
public Set<Region<?, ?>> rootRegions() {
return cache.rootRegions();
}
public void close() {
cache.close();
}
public boolean isClosed() {
return cache.isClosed();
}
/**
*
*/
private void createClientRegions() {
GemfireFunctionOperations template = new GemfireOnServersFunctionTemplate(cache);
Iterable<String> regionNames = template.executeAndExtract(new ListRegionsOnServerFunction());
ClientRegionFactory<?,?> clientRegionFactory = null;
if (regionNames !=null && regionNames.iterator().hasNext()) {
clientRegionFactory = cache.createClientRegionFactory(ClientRegionShortcut.PROXY);
}
for (String regionName: regionNames) {
if (logger.isDebugEnabled()) {
logger.debug("creating client region for " + regionName);
clientRegionFactory.create(regionName);
}
}
}
/**
* @param clientCacheFactory
* @param properties
*/
private void initializeClientCache(ClientCacheFactory clientCacheFactory, Properties properties) {
// TODO Auto-generated method stub
}
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.springframework.data.gemfire.client;
import java.util.Set;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.client.Pool;
import com.gemstone.gemfire.cache.query.QueryService;
import com.gemstone.gemfire.distributed.DistributedSystem;
/**
* @author David Turanski
*
*/
public interface GemfireDataSource {
public void connect();
public String getName();
public String getServerGroup();
public QueryService getQueryService();
public QueryService getQueryService(String poolName);
public QueryService getLocalQueryService();
public void readyForEvents();
public Pool getDefaultPool();
public DistributedSystem getDistributedSystem();
public <K, V> Region<K, V> getRegion(String path);
public Set<Region<?, ?>> rootRegions();
public void close();
public void close(boolean keepalive);
public boolean isClosed();
}

View File

@@ -0,0 +1,67 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.springframework.data.gemfire.client;
import org.apache.commons.logging.Log;
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.repository.support.ListRegionsOnServerFunction;
import com.gemstone.gemfire.cache.client.ClientCache;
import com.gemstone.gemfire.cache.client.ClientRegionFactory;
import com.gemstone.gemfire.cache.client.ClientRegionShortcut;
/**
* @author David Turanski
*
*/
public class GemfireDataSourcePostProcessor implements BeanFactoryPostProcessor {
private static Log logger = LogFactory.getLog(GemfireDataSourcePostProcessor.class);
private final ClientCache cache;
public GemfireDataSourcePostProcessor(ClientCache cache) {
this.cache = cache;
}
/* (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);
}
private void createClientRegions(ConfigurableListableBeanFactory beanFactory) {
GemfireFunctionOperations template = new GemfireOnServersFunctionTemplate(cache);
Iterable<String> regionNames = template.executeAndExtract(new ListRegionsOnServerFunction());
ClientRegionFactory<?,?> clientRegionFactory = null;
if (regionNames !=null && regionNames.iterator().hasNext()) {
clientRegionFactory = cache.createClientRegionFactory(ClientRegionShortcut.PROXY);
}
for (String regionName: regionNames) {
if (logger.isDebugEnabled()) {
logger.debug("creating client region for " + regionName);
beanFactory.registerSingleton(regionName,
clientRegionFactory.create(regionName));
}
}
}
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.springframework.data.gemfire.client;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.Properties;
import com.gemstone.gemfire.cache.client.ClientCacheFactory;
/**
* @author David Turanski
*
*/
public class GemfireLocatorDataSource extends AbstractGemfireDataSource {
public GemfireLocatorDataSource(ClientCacheFactory clientCacheFactory) {
super(clientCacheFactory);
}
public GemfireLocatorDataSource(String host, int port, Properties properties) {
super(host, port, properties);
}
public GemfireLocatorDataSource(List<InetSocketAddress> remoteConnections, Properties properties) {
super(remoteConnections, properties);
}
/* (non-Javadoc)
* @see org.springframework.data.gemfire.client.AbstractGemfireDataSource#addRemoteConnection(com.gemstone.gemfire.cache.client.ClientCacheFactory, java.lang.String, int)
*/
@Override
protected void addRemoteConnection(ClientCacheFactory clientCacheFactory, String host, int port) {
clientCacheFactory.addPoolLocator(host, port);
}
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.springframework.data.gemfire.client;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.Properties;
import com.gemstone.gemfire.cache.client.ClientCacheFactory;
/**
* @author David Turanski
*
*/
public class GemfireServerDataSource extends AbstractGemfireDataSource {
public GemfireServerDataSource(ClientCacheFactory clientCacheFactory) {
super(clientCacheFactory);
}
public GemfireServerDataSource(String host, int port, Properties properties) {
super(host, port, properties);
}
public GemfireServerDataSource(List<InetSocketAddress> remoteConnections, Properties properties) {
super(remoteConnections, properties);
}
/* (non-Javadoc)
* @see org.springframework.data.gemfire.client.AbstractGemfireDataSource#addRemoteConnection(com.gemstone.gemfire.cache.client.ClientCacheFactory, java.lang.String, int)
*/
@Override
protected void addRemoteConnection(ClientCacheFactory clientCacheFactory, String host, int port) {
clientCacheFactory.addPoolServer(host, port);
}
}

View File

@@ -37,5 +37,6 @@ class GemfireDataNamespaceHandler extends NamespaceHandlerSupport {
RepositoryConfigurationExtension extension = new GemfireRepositoryConfigurationExtension();
registerBeanDefinitionParser("repositories", new RepositoryBeanDefinitionParser(extension));
registerBeanDefinitionParser("function-executions", new FunctionExecutionBeanDefinitionParser());
registerBeanDefinitionParser("datasource", new GemfireDataSourceParser());
}
}

View File

@@ -0,0 +1,66 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.springframework.data.gemfire.config;
import javax.management.Attribute;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.data.gemfire.client.GemfireDataSourcePostProcessor;
import org.springframework.util.Assert;
import org.w3c.dom.Attr;
import org.w3c.dom.Element;
/**
* @author David Turanski
*
*/
public class GemfireDataSourceParser extends AbstractBeanDefinitionParser {
/* (non-Javadoc)
* @see org.springframework.beans.factory.xml.AbstractBeanDefinitionParser#parseInternal(org.w3c.dom.Element, org.springframework.beans.factory.xml.ParserContext)
*/
@Override
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
AbstractBeanDefinition poolDefinition = (AbstractBeanDefinition) new PoolParser().parse(element, parserContext);
MutablePropertyValues poolProps = poolDefinition.getPropertyValues();
poolProps.add("name", GemfireConstants.DEFAULT_GEMFIRE_POOL_NAME);
parserContext.getRegistry().registerBeanDefinition(GemfireConstants.DEFAULT_GEMFIRE_POOL_NAME,poolDefinition);
AbstractBeanDefinition clientCacheDefinition = (AbstractBeanDefinition)new ClientCacheParser().parse(element, parserContext);
MutablePropertyValues props = clientCacheDefinition.getPropertyValues();
props.add("pool", poolDefinition);
clientCacheDefinition.setPropertyValues(props);
parserContext.getRegistry().registerBeanDefinition(GemfireConstants.DEFAULT_GEMFIRE_CACHE_NAME,clientCacheDefinition);
System.out.println("registered " + GemfireConstants.DEFAULT_GEMFIRE_CACHE_NAME+ ":" + clientCacheDefinition.getBeanClassName());
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(GemfireDataSourcePostProcessor.class);
builder.addConstructorArgReference(GemfireConstants.DEFAULT_GEMFIRE_CACHE_NAME);
BeanDefinitionReaderUtils.registerWithGeneratedName(builder.getBeanDefinition(), parserContext.getRegistry());
return null;
}
}

View File

@@ -0,0 +1,78 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.springframework.data.gemfire.repository.support;
import java.util.ArrayList;
import java.util.List;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.execute.Function;
import com.gemstone.gemfire.cache.execute.FunctionContext;
/**
* @author David Turanski
*
*/
@SuppressWarnings("serial")
public class ListRegionsOnServerFunction implements Function {
/* (non-Javadoc)
* @see com.gemstone.gemfire.cache.execute.Function#hasResult()
*/
@Override
public boolean hasResult() {
return true;
}
/* (non-Javadoc)
* @see com.gemstone.gemfire.cache.execute.Function#execute(com.gemstone.gemfire.cache.execute.FunctionContext)
*/
@Override
public void execute(FunctionContext context) {
Cache cache = CacheFactory.getAnyInstance();
List<String> regionNames = new ArrayList<String>();
for (Region<?,?> region: cache.rootRegions()) {
regionNames.add(region.getName());
}
context.getResultSender().lastResult(regionNames);
}
/* (non-Javadoc)
* @see com.gemstone.gemfire.cache.execute.Function#getId()
*/
@Override
public String getId() {
return this.getClass().getName();
}
/* (non-Javadoc)
* @see com.gemstone.gemfire.cache.execute.Function#optimizeForWrite()
*/
@Override
public boolean optimizeForWrite() {
return false;
}
/* (non-Javadoc)
* @see com.gemstone.gemfire.cache.execute.Function#isHA()
*/
@Override
public boolean isHA() {
return false;
}
}

View File

@@ -0,0 +1,77 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.springframework.data.gemfire.support;
import java.util.ArrayList;
import java.util.List;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.execute.Function;
import com.gemstone.gemfire.cache.execute.FunctionContext;
/**
* @author David Turanski
*
*/
@SuppressWarnings("serial")
public class ListRegionsOnServerFunction implements Function {
/* (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()) {
regionNames.add(region.getName());
}
functionContext.getResultSender().lastResult(regionNames);
}
/* (non-Javadoc)
* @see com.gemstone.gemfire.cache.execute.Function#getId()
*/
@Override
public String getId() {
return this.getClass().getName();
}
/* (non-Javadoc)
* @see com.gemstone.gemfire.cache.execute.Function#hasResult()
*/
@Override
public boolean hasResult() {
return true;
}
/* (non-Javadoc)
* @see com.gemstone.gemfire.cache.execute.Function#isHA()
*/
@Override
public boolean isHA() {
return false;
}
/* (non-Javadoc)
* @see com.gemstone.gemfire.cache.execute.Function#optimizeForWrite()
*/
@Override
public boolean optimizeForWrite() {
return false;
}
}