Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.snapshot;
import static com.gemstone.gemfire.cache.snapshot.SnapshotOptions.SnapshotFormat;
import static org.springframework.data.gemfire.snapshot.SnapshotServiceFactoryBean.SnapshotServiceAdapter;
import java.io.Closeable;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationListener;
import org.springframework.data.gemfire.snapshot.event.ExportSnapshotApplicationEvent;
import org.springframework.data.gemfire.snapshot.event.SnapshotApplicationEvent;
import org.springframework.data.gemfire.util.CollectionUtils;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
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 an appropriate GemFire Snapshot Service to perform data import and exports. 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 org.springframework.context.ApplicationListener
* @see org.springframework.data.gemfire.snapshot.SnapshotServiceFactoryBean.SnapshotServiceAdapter
* @see com.gemstone.gemfire.cache.snapshot.CacheSnapshotService
* @see com.gemstone.gemfire.cache.snapshot.RegionSnapshotService
* @since 1.7.0
*/
@SuppressWarnings("unused")
public class SnapshotServiceFactoryBean implements FactoryBean>,
InitializingBean, DisposableBean, ApplicationListener> {
protected static final SnapshotMetadata[] EMPTY_ARRAY = new SnapshotMetadata[0];
private Boolean suppressImportOnInit;
private Cache cache;
private Region region;
private SnapshotMetadata[] exports;
private SnapshotMetadata[] imports;
private SnapshotServiceAdapter snapshotServiceAdapter;
/* (non-Javadoc) */
@SuppressWarnings("unchecked")
static SnapshotMetadata[] nullSafeArray(SnapshotMetadata[] configurations) {
return (configurations != null ? configurations : EMPTY_ARRAY);
}
/* (non-Javadoc) */
static boolean nullSafeIsDirectory(File file) {
return (file != null && file.isDirectory());
}
/* (non-Javadoc) */
static boolean nullSafeIsFile(File file) {
return (file != null && file.isFile());
}
/**
* 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;
}
/**
* Sets the meta-data (location, filter and format) used to create a snapshot from the Cache or Region data.
*
* @param exports an array of snapshot meta-data used for each export.
* @see SnapshotServiceFactoryBean.SnapshotMetadata
*/
public void setExports(SnapshotMetadata[] exports) {
this.exports = exports;
}
/**
* Sets the meta-data (location, filter and format) used to create a snapshot from the Cache or Region data.
*
* @return an array of snapshot meta-data used for each export.
* @see SnapshotServiceFactoryBean.SnapshotMetadata
*/
protected SnapshotMetadata[] getExports() {
return nullSafeArray(exports);
}
/**
* Sets the meta-data (location, filter and format) used to read a data snapshot into an entire Cache
* or individual Region.
*
* @param imports an array of snapshot meta-data used for each import.
* @see SnapshotServiceFactoryBean.SnapshotMetadata
*/
public void setImports(SnapshotMetadata[] imports) {
this.imports = imports;
}
/**
* Gets the meta-data (location, filter and format) used to read a data snapshot into an entire Cache
* or individual Region.
*
* @return an array of snapshot meta-data used for each import.
* @see SnapshotServiceFactoryBean.SnapshotMetadata
*/
protected SnapshotMetadata[] 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 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 getRegion() {
return region;
}
/**
* Sets a boolean condition to indicate whether importing on initialization should be suppressed.
*
* @param suppressImportOnInit a Boolean value to indicate whether importing on initialization should be suppressed.
* @see #getSuppressImportOnInit()
*/
public void setSuppressImportOnInit(Boolean suppressImportOnInit) {
this.suppressImportOnInit = suppressImportOnInit;
}
/**
* Determines whether importing on initialization should be suppressed.
*
* @return a boolean value indicating whether import on initialization should be suppressed.
* @see #setSuppressImportOnInit(Boolean)
* @see #afterPropertiesSet()
*/
protected boolean getSuppressImportOnInit() {
return Boolean.TRUE.equals(suppressImportOnInit);
}
/**
* 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 SnapshotServiceFactoryBean.SnapshotServiceAdapter
*/
@Override
public SnapshotServiceAdapter 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 SnapshotServiceFactoryBean.SnapshotServiceAdapter
* @see SnapshotServiceFactoryBean.CacheSnapshotServiceAdapter
* @see 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.snapshot.SnapshotServiceFactoryBean.SnapshotServiceAdapter
* @see #getSuppressImportOnInit()
* @see #getImports()
* @see #create()
*/
@Override
@SuppressWarnings("unchecked")
public void afterPropertiesSet() throws Exception {
snapshotServiceAdapter = create();
if (!getSuppressImportOnInit()) {
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 region = getRegion();
return (region != null ? wrap(region.getSnapshotService()) : wrap(getCache().getSnapshotService()));
}
/**
* Wraps the GemFire CacheSnapshotService into an appropriate Adapter to uniformly access snapshot operations
* on the Cache and Regions alike.
*
* @param cacheSnapshotService the GemFire CacheSnapshotService to wrap.
* @return a SnapshotServiceAdapter wrapping the GemFire CacheSnapshotService.
* @see SnapshotServiceFactoryBean.SnapshotServiceAdapter
* @see SnapshotServiceFactoryBean.CacheSnapshotServiceAdapter
* @see com.gemstone.gemfire.cache.snapshot.CacheSnapshotService
*/
protected SnapshotServiceAdapter