SGF-408 - Provide support in the SDG XML namespace to load a pre-defined data set using GemFire Snapshot Service for development and testing purposes.

Initial implementation and unit tests.
This commit is contained in:
John Blum
2015-08-19 22:29:31 -07:00
parent dff74e694c
commit 022c68cdd1
6 changed files with 957 additions and 4 deletions

View File

@@ -0,0 +1,497 @@
/*
* Copyright 2010-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;
import static com.gemstone.gemfire.cache.snapshot.SnapshotOptions.SnapshotFormat;
import java.io.File;
import java.util.Arrays;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.snapshot.CacheSnapshotService;
import com.gemstone.gemfire.cache.snapshot.RegionSnapshotService;
import com.gemstone.gemfire.cache.snapshot.SnapshotFilter;
import com.gemstone.gemfire.cache.snapshot.SnapshotOptions;
/**
* The SnapshotServiceFactoryBean class is a Spring FactoryBean used to configure and create an instance
* of the appropriate GemFire Snapshot Service. A CacheSnapshotService is created if the Region is not specified,
* otherwise a RegionSnapshotService is used based on the configured Region.
*
* @author John Blum
* @see org.springframework.beans.factory.DisposableBean
* @see org.springframework.beans.factory.FactoryBean
* @see org.springframework.beans.factory.InitializingBean
* @see com.gemstone.gemfire.cache.snapshot.CacheSnapshotService
* @see com.gemstone.gemfire.cache.snapshot.RegionSnapshotService
* @since 1.7.0
*/
@SuppressWarnings("unused")
public class SnapshotServiceFactoryBean<K, V> implements FactoryBean<SnapshotServiceFactoryBean.SnapshotServiceAdapter<K, V>>,
InitializingBean, DisposableBean {
protected static final SnapshotMetadata[] EMPTY_ARRAY = new SnapshotMetadata[0];
private Cache cache;
private Region<K, V> region;
private SnapshotServiceAdapter<K, V> snapshotServiceAdapter;
private SnapshotMetadata<K, V>[] exports;
private SnapshotMetadata<K, V>[] imports;
/**
* Sets a reference to the GemFire Cache for which the snapshot will be taken.
*
* @param cache the GemFire Cache used to create an instance of CacheSnapshotService.
* @throws IllegalArgumentException if the Cache reference is null.
* @see com.gemstone.gemfire.cache.Cache
* @see #getCache()
*/
public void setCache(Cache cache) {
Assert.notNull(cache, "The GemFire Cache must not be null");
this.cache = cache;
}
/**
* Gets a reference to the GemFire Cache for which the snapshot will be taken.
*
* @return the GemFire Cache used to create an instance of CacheSnapshotService.
* @throws IllegalStateException if the Cache argument is null.
* @see com.gemstone.gemfire.cache.Cache
* @see #setCache(Cache)
*/
protected Cache getCache() {
Assert.state(cache != null, "The GemFire Cache was not properly initialized");
return cache;
}
public void setExports(SnapshotMetadata<K, V>[] exports) {
this.exports = exports;
}
protected SnapshotMetadata<K, V>[] getExports() {
return nullSafeArray(exports);
}
public void setImports(SnapshotMetadata<K, V>[] imports) {
this.imports = imports;
}
protected SnapshotMetadata<K, V>[] getImports() {
return nullSafeArray(imports);
}
/**
* Sets a reference to the GemFire Region for which the snapshot will be taken.
*
* @param region the GemFire Region used to create an instance of the RegionSnapshotService.
* @see com.gemstone.gemfire.cache.Region
* @see #getRegion()
*/
public void setRegion(Region<K, V> region) {
this.region = region;
}
/**
* Gets a reference to the GemFire Region for which the snapshot will be taken.
*
* @return the GemFire Region used to create an instance of the RegionSnapshotService.
* @see com.gemstone.gemfire.cache.Region
* @see #getRegion()
*/
protected Region<K, V> getRegion() {
return region;
}
/**
* Gets the reference to the GemFire Snapshot Service created by this FactoryBean.
*
* @return the GemFire Snapshot Service created by this FactoryBean.
* @throws Exception if the GemFire Snapshot Service failed to be created.
* @see org.springframework.data.gemfire.SnapshotServiceFactoryBean.SnapshotServiceAdapter
*/
@Override
@SuppressWarnings("unchecked")
public SnapshotServiceAdapter<K, V> getObject() throws Exception {
return snapshotServiceAdapter;
}
/**
* Gets the type of Snapshot Service created by this FactoryBean.
*
* @return a Class object representing the type of Snapshot Service created by this FactoryBean.
* @see org.springframework.data.gemfire.SnapshotServiceFactoryBean.SnapshotServiceAdapter
* @see org.springframework.data.gemfire.SnapshotServiceFactoryBean.CacheSnapshotServiceAdapter
* @see org.springframework.data.gemfire.SnapshotServiceFactoryBean.RegionSnapshotServiceAdapter
*/
@Override
public Class<?> getObjectType() {
return (snapshotServiceAdapter != null ? snapshotServiceAdapter.getClass() : SnapshotServiceAdapter.class);
}
/**
* Determines this this FactoryBean creates single GemFire Snapshot Service instances.
*
* @return true.
*/
@Override
public boolean isSingleton() {
return true;
}
/**
* Constructs and initializes the GemFire Snapshot Service used to take a snapshot of the configured Cache
* or Region if initialized. In addition, this initialization method will perform the actual import.
*
* @throws Exception if the construction and initialization of the GemFire Snapshot Service fails.
* @see org.springframework.data.gemfire.SnapshotServiceFactoryBean.SnapshotServiceAdapter#doImport(SnapshotMetadata[])
* @see #getImports()
* @see #create()
*/
@Override
@SuppressWarnings("unchecked")
public void afterPropertiesSet() throws Exception {
snapshotServiceAdapter = create();
snapshotServiceAdapter.doImport(getImports());
}
/**
* Constructs an appropriate instance of the SnapshotServiceAdapter based on the FactoryBean configuration. If
* a Region has not been specified, then a GemFire Snapshot Service for the Cache is constructed, otherwise
* the GemFire Snapshot Service for the configured Region is used.
*
* @return a SnapshotServiceAdapter wrapping the appropriate GemFire Snapshot Service (either Cache or Region)
* depending on the FactoryBean configuration.
* @see #wrap(CacheSnapshotService)
* @see #wrap(RegionSnapshotService)
* @see #getRegion()
*/
protected SnapshotServiceAdapter create() {
Region<K, V> region = getRegion();
return (region != null ? wrap(region.getSnapshotService()) : wrap(getCache().getSnapshotService()));
}
/**
* Wraps the GemFire CacheSnapshotService into an appropriate Adapter to ...
*
* @param cacheSnapshotService the GemFire CacheSnapshotService to wrap.
* @return a SnapshotServiceAdapter wrapping the GemFire CacheSnapshotService.
* @see org.springframework.data.gemfire.SnapshotServiceFactoryBean.SnapshotServiceAdapter
* @see org.springframework.data.gemfire.SnapshotServiceFactoryBean.CacheSnapshotServiceAdapter
* @see com.gemstone.gemfire.cache.snapshot.CacheSnapshotService
*/
protected SnapshotServiceAdapter<Object, Object> wrap(CacheSnapshotService cacheSnapshotService) {
return new CacheSnapshotServiceAdapter(cacheSnapshotService);
}
/**
* Wraps the GemFire RegionSnapshotService into an appropriate Adapter to ...
*
* @param regionSnapshotService the GemFire RegionSnapshotService to wrap.
* @return a SnapshotServiceAdapter wrapping the GemFire RegionSnapshotService.
* @see org.springframework.data.gemfire.SnapshotServiceFactoryBean.SnapshotServiceAdapter
* @see org.springframework.data.gemfire.SnapshotServiceFactoryBean.RegionSnapshotServiceAdapter
* @see com.gemstone.gemfire.cache.snapshot.RegionSnapshotService
*/
protected SnapshotServiceAdapter<K, V> wrap(RegionSnapshotService<K, V> regionSnapshotService) {
return new RegionSnapshotServiceAdapter(regionSnapshotService);
}
/* (non-Javadoc) */
@SuppressWarnings("unchecked")
protected SnapshotMetadata<K, V>[] nullSafeArray(SnapshotMetadata<K, V>[] configurations) {
return (configurations != null ? configurations : EMPTY_ARRAY);
}
/**
* Performs an export of the GemFire Cache or Region if configured.
*
* @throws Exception if the Cache/Region data export operation fails.
* @see org.springframework.data.gemfire.SnapshotServiceFactoryBean.SnapshotServiceAdapter#doExport(SnapshotMetadata[])
* @see #getExports()
* @see #getObject()
*/
@Override
public void destroy() throws Exception {
getObject().doExport(getExports());
}
public interface SnapshotServiceAdapter<K, V> {
SnapshotOptions<K, V> createOptions();
void doExport(SnapshotMetadata<K, V>[] configurations);
void doImport(SnapshotMetadata<K, V>[] configurations);
void load(File directory, SnapshotFormat format);
void load(SnapshotFormat format, SnapshotOptions<K, V> options, File... snapshots);
void save(File location, SnapshotFormat format);
void save(File location, SnapshotFormat format, SnapshotOptions<K, V> options);
}
protected class CacheSnapshotServiceAdapter implements SnapshotServiceAdapter<Object, Object> {
private final CacheSnapshotService snapshotService;
public CacheSnapshotServiceAdapter(CacheSnapshotService snapshotService) {
Assert.notNull(snapshotService, "The backing CacheSnapshotService must not be null");
this.snapshotService = snapshotService;
}
protected CacheSnapshotService getSnapshotService() {
return snapshotService;
}
@Override
public SnapshotOptions<Object, Object> createOptions() {
return getSnapshotService().createOptions();
}
protected SnapshotOptions<Object, Object> createOptions(SnapshotFilter<Object, Object> filter) {
return createOptions().setFilter(filter);
}
@Override
public void doExport(SnapshotMetadata<Object, Object>[] configurations) {
if (!ObjectUtils.isEmpty(configurations)) {
for (SnapshotMetadata<Object, Object> configuration : configurations) {
save(configuration.getLocation(), configuration.getFormat(),
createOptions(configuration.getFilter()));
}
}
}
@Override
public void doImport(SnapshotMetadata<Object, Object>[] configurations) {
if (!ObjectUtils.isEmpty(configurations)) {
for (SnapshotMetadata<Object, Object> configuration : configurations) {
if (configuration.isDirectory()) {
load(configuration.getLocation(), configuration.getFormat());
}
else {
load(configuration.getFormat(), createOptions(configuration.getFilter()),
configuration.getLocation());
}
}
}
}
@Override
public void load(File directory, SnapshotFormat format) {
try {
getSnapshotService().load(directory, format);
}
catch (Throwable t) {
throw new RuntimeException(String.format(
"Failed to load snapshots from directory (%1$s) in format (%2$s)", directory, format), t);
}
}
@Override
public void load(SnapshotFormat format, SnapshotOptions<Object, Object> options,
File... snapshots) {
try {
getSnapshotService().load(snapshots, format, options);
}
catch (Throwable t) {
throw new RuntimeException(String.format(
"Failed to load snapshots (%1$s) in format (%2$s) with options (%3$s)",
Arrays.toString(snapshots), format, options), t);
}
}
@Override
public void save(File directory, SnapshotFormat format) {
try {
getSnapshotService().save(directory, format);
}
catch (Throwable t) {
throw new RuntimeException(String.format(
"Failed to save snapshots to directory (%1$s) using format (%2$s)", directory, format), t);
}
}
@Override
public void save(File directory, SnapshotFormat format,
SnapshotOptions<Object, Object> options) {
try {
getSnapshotService().save(directory, format, options);
}
catch (Throwable t) {
throw new RuntimeException(String.format(
"Failed to save snapshots to directory (%1$s) using format (%2$s) with options (%3$s)",
directory, format, options), t);
}
}
}
protected class RegionSnapshotServiceAdapter implements SnapshotServiceAdapter<K, V> {
private final RegionSnapshotService<K, V> snapshotService;
public RegionSnapshotServiceAdapter(RegionSnapshotService<K, V> snapshotService) {
Assert.notNull(snapshotService, "The backing RegionSnapshotService must not be null");
this.snapshotService = snapshotService;
}
protected RegionSnapshotService<K, V> getSnapshotService() {
return snapshotService;
}
@Override
public SnapshotOptions<K, V> createOptions() {
return getSnapshotService().createOptions();
}
protected SnapshotOptions<K, V> createOptions(SnapshotFilter<K, V> filter) {
return createOptions().setFilter(filter);
}
@Override
public void doExport(final SnapshotMetadata<K, V>[] configurations) {
if (!ObjectUtils.isEmpty(configurations)) {
for (SnapshotMetadata<K, V> configuration : configurations) {
Assert.isTrue(configuration.isFile(), String.format("The export location (%1$s) must be a file",
configuration.getLocation()));
save(configuration.getLocation(), configuration.getFormat(), createOptions(configuration.getFilter()));
}
}
}
@Override
public void doImport(final SnapshotMetadata<K, V>[] configurations) {
if (!ObjectUtils.isEmpty(configurations)) {
for (SnapshotMetadata<K, V> configuration : configurations) {
Assert.isTrue(configuration.isFile(), String.format("The import location (%1$s) must be a file",
configuration.getLocation()));
load(configuration.getFormat(), createOptions(configuration.getFilter()), configuration.getLocation());
}
}
}
@Override
public void load(File snapshot, SnapshotFormat format) {
try {
getSnapshotService().load(snapshot, format);
}
catch (Throwable t) {
throw new RuntimeException(String.format(
"Failed to load snapshot from file (%1$s) in format (%2$s)", snapshot, format), t);
}
}
@Override
public void load(SnapshotFormat format, SnapshotOptions<K, V> options, File... snapshots) {
try {
for (File snapshot : snapshots) {
getSnapshotService().load(snapshot, format, options);
}
}
catch (Throwable t) {
throw new RuntimeException(String.format(
"Failed to load snapshots (%1$s) in format (%2$s) with options (%3$s)",
Arrays.toString(snapshots), format, options), t);
}
}
@Override
public void save(File snapshot, SnapshotFormat format) {
try {
getSnapshotService().save(snapshot, format);
}
catch (Throwable t) {
throw new RuntimeException(String.format(
"Failed to save snapshots to directory (%1$s) using format (%2$s)", snapshot, format), t);
}
}
@Override
public void save(File snapshot, SnapshotFormat format, SnapshotOptions<K, V> options) {
try {
getSnapshotService().save(snapshot, format, options);
}
catch (Throwable t) {
throw new RuntimeException(String.format(
"Failed to save snapshots to directory (%1$s) using format (%2$s) with options (%3$s)",
snapshot, format, options), t);
}
}
}
public static class SnapshotMetadata<K, V> {
private final File location;
private final SnapshotFilter<K, V> filter;
private final SnapshotFormat format;
public SnapshotMetadata(File location, SnapshotFilter<K, V> filter, SnapshotFormat format) {
Assert.isTrue(location != null && location.exists(), String.format(
"The File location (%1$s) must exist", location));
this.location = location;
this.filter = filter;
this.format = format;
}
public boolean isDirectory() {
return getLocation().isDirectory();
}
public boolean isFile() {
return getLocation().isFile();
}
public File getLocation() {
return location;
}
public boolean isFilterPresent() {
return (getFilter() != null);
}
public SnapshotFilter<K, V> getFilter() {
return filter;
}
public SnapshotFormat getFormat() {
return (format != null ? format : SnapshotFormat.GEMFIRE);
}
@Override
public String toString() {
return String.format("{ @type = %1$s, location = %2$s, filter = %2$s, format = %4$s }",
getClass().getName(), getLocation().getAbsolutePath(), getFilter(), getFormat());
}
}
}

View File

@@ -40,5 +40,6 @@ class GemfireDataNamespaceHandler extends NamespaceHandlerSupport {
registerBeanDefinitionParser("function-executions", new FunctionExecutionBeanDefinitionParser());
registerBeanDefinitionParser("datasource", new GemfireDataSourceParser());
registerBeanDefinitionParser("json-region-autoproxy", new GemfireRegionAutoProxyParser());
registerBeanDefinitionParser("snapshot-service", new SnapshotServiceParser());
}
}

View File

@@ -51,6 +51,9 @@ abstract class ParsingUtils {
private static final Log log = LogFactory.getLog(ParsingUtils.class);
protected static final String CACHE_PROPERTY_NAME = "cache";
protected static final String CACHE_REF_ATTRIBUTE_NAME = "cache-ref";
static void setPropertyReference(Element element, BeanDefinitionBuilder builder, String attributeName,
String propertyName) {
@@ -416,7 +419,7 @@ abstract class ParsingUtils {
return false;
}
public static void parseCompressor(ParserContext parserContext, Element element,
static void parseCompressor(ParserContext parserContext, Element element,
BeanDefinitionBuilder regionAttributesBuilder) {
Element compressorElement = DomUtils.getChildElementByTagName(element, "compressor");
@@ -427,8 +430,16 @@ abstract class ParsingUtils {
}
}
static String resolveCacheReference(final String cacheRef) {
return (StringUtils.hasText(cacheRef) ? cacheRef : GemfireConstants.DEFAULT_GEMFIRE_CACHE_NAME);
static void setCacheReference(Element element, BeanDefinitionBuilder builder) {
builder.addPropertyReference(CACHE_PROPERTY_NAME, resolveCacheReference(element));
}
static String resolveCacheReference(Element element) {
return resolveCacheReference(element.getAttribute(CACHE_REF_ATTRIBUTE_NAME));
}
static String resolveCacheReference(String cacheReference) {
return (StringUtils.hasText(cacheReference) ? cacheReference : GemfireConstants.DEFAULT_GEMFIRE_CACHE_NAME);
}
}

View File

@@ -0,0 +1,90 @@
/*
* Copyright 2010-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 java.util.List;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.data.gemfire.SnapshotServiceFactoryBean;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;
/**
* Parser for &lt;gfe-data:snapshot-service&gt; bean definition elements in Spring XML config.
*
* @author John Blum
* @see org.springframework.beans.factory.config.BeanDefinition
* @see org.springframework.beans.factory.support.BeanDefinitionBuilder
* @see org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser
* @see org.springframework.beans.factory.xml.ParserContext
* @see org.springframework.data.gemfire.SnapshotServiceFactoryBean
* @since 1.7.0
*/
class SnapshotServiceParser extends AbstractSingleBeanDefinitionParser {
@Override
protected Class<?> getBeanClass(final Element element) {
return SnapshotServiceFactoryBean.class;
}
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
super.doParse(element, parserContext, builder);
ParsingUtils.setCacheReference(element, builder);
ParsingUtils.setPropertyReference(element, builder, "region-ref", "region");
builder.addPropertyValue("exports", parseExports(element, parserContext));
builder.addPropertyValue("imports", parseImports(element, parserContext));
}
private ManagedList<BeanDefinition> parseExports(Element element, ParserContext parserContext) {
return parseSnapshots(element, parserContext, "export-snapshot");
}
private ManagedList<BeanDefinition> parseImports(Element element, ParserContext parserContext) {
return parseSnapshots(element, parserContext, "import-snapshot");
}
private ManagedList<BeanDefinition> parseSnapshots(Element element, ParserContext parserContext,
String childTagName) {
ManagedList<BeanDefinition> snapshotBeans = new ManagedList<BeanDefinition>();
for (Element childElement : DomUtils.getChildElementsByTagName(element, childTagName)) {
snapshotBeans.add(parseSnapshotMetadata(childElement, parserContext));
}
return snapshotBeans;
}
private BeanDefinition parseSnapshotMetadata(Element snapshotMetadataElement, ParserContext parserContext) {
BeanDefinitionBuilder snapshotMetadataBuilder = BeanDefinitionBuilder.genericBeanDefinition(
SnapshotServiceFactoryBean.SnapshotMetadata.class);
ParsingUtils.setPropertyValue(snapshotMetadataElement, snapshotMetadataBuilder, "location");
ParsingUtils.parseRefOrNestedBeanDeclaration(parserContext, snapshotMetadataElement, snapshotMetadataBuilder,
"filter-ref");
ParsingUtils.setPropertyValue(snapshotMetadataElement, snapshotMetadataBuilder, "format");
return snapshotMetadataBuilder.getBeanDefinition();
}
}