All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.data.gemfire.LookupRegionFactoryBean Maven / Gradle / Ivy

There is a newer version: 2.3.9.RELEASE
Show newest version
/*
 * 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 org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;

import com.gemstone.gemfire.cache.AttributesMutator;
import com.gemstone.gemfire.cache.CacheListener;
import com.gemstone.gemfire.cache.CacheLoader;
import com.gemstone.gemfire.cache.CacheWriter;
import com.gemstone.gemfire.cache.CustomExpiry;
import com.gemstone.gemfire.cache.EvictionAttributesMutator;
import com.gemstone.gemfire.cache.ExpirationAttributes;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.asyncqueue.AsyncEventQueue;
import com.gemstone.gemfire.cache.wan.GatewaySender;

/**
 * The LookupRegionFactoryBean class is a concrete implementation of RegionLookupFactoryBean for handling
 * >gfe:lookup-region/< SDG XML namespace (XSD) elements.
 *
 * @author John Blum
 * @see org.springframework.data.gemfire.RegionLookupFactoryBean
 * @see com.gemstone.gemfire.cache.AttributesMutator
 * @since 1.6.0
 */
@SuppressWarnings("unused")
public class LookupRegionFactoryBean extends RegionLookupFactoryBean {

	private Boolean cloningEnabled;
	private Boolean enableStatistics;

	private AsyncEventQueue[] asyncEventQueues;

	private CacheListener[] cacheListeners;

	private CacheLoader cacheLoader;

	private CacheWriter cacheWriter;

	private CustomExpiry customEntryIdleTimeout;
	private CustomExpiry customEntryTimeToLive;

	private ExpirationAttributes entryIdleTimeout;
	private ExpirationAttributes entryTimeToLive;
	private ExpirationAttributes regionIdleTimeout;
	private ExpirationAttributes regionTimeToLive;

	private GatewaySender[] gatewaySenders;

	private Integer evictionMaximum;

	@Override
	public void afterPropertiesSet() throws Exception {
		super.afterPropertiesSet();

		AttributesMutator attributesMutator = getRegion().getAttributesMutator();

		if (!ObjectUtils.isEmpty(asyncEventQueues)) {
			for (AsyncEventQueue asyncEventQueue : asyncEventQueues) {
				attributesMutator.addAsyncEventQueueId(asyncEventQueue.getId());
			}
		}

		if (!ObjectUtils.isEmpty(cacheListeners)) {
			for (CacheListener cacheListener : cacheListeners) {
				attributesMutator.addCacheListener(cacheListener);
			}
		}

		if (cacheLoader != null) {
			attributesMutator.setCacheLoader(cacheLoader);
		}

		if (cacheWriter != null) {
			attributesMutator.setCacheWriter(cacheWriter);
		}

		if (cloningEnabled != null) {
			attributesMutator.setCloningEnabled(cloningEnabled);
		}

		if (isStatisticsEnabled()) {
			assertStatisticsEnabled();

			if (customEntryIdleTimeout != null) {
				attributesMutator.setCustomEntryIdleTimeout(customEntryIdleTimeout);
			}

			if (customEntryTimeToLive != null) {
				attributesMutator.setCustomEntryTimeToLive(customEntryTimeToLive);
			}

			if (entryIdleTimeout != null) {
				attributesMutator.setEntryIdleTimeout(entryIdleTimeout);
			}

			if (entryTimeToLive != null) {
				attributesMutator.setEntryTimeToLive(entryTimeToLive);
			}

			if (regionIdleTimeout != null) {
				attributesMutator.setRegionIdleTimeout(regionIdleTimeout);
			}

			if (regionTimeToLive != null) {
				attributesMutator.setRegionTimeToLive(regionTimeToLive);
			}
		}

		if (evictionMaximum != null) {
			EvictionAttributesMutator evictionAttributesMutator = attributesMutator.getEvictionAttributesMutator();
			evictionAttributesMutator.setMaximum(evictionMaximum);
		}

		if (!ObjectUtils.isEmpty(gatewaySenders)) {
			for (GatewaySender gatewaySender : gatewaySenders) {
				attributesMutator.addGatewaySenderId(gatewaySender.getId());
			}
		}
	}

	@Override
	final boolean isLookupEnabled() {
		return true;
	}

	/* (non-Javadoc) */
	public void setAsyncEventQueues(AsyncEventQueue[] asyncEventQueues) {
		this.asyncEventQueues = asyncEventQueues;
	}

	/* (non-Javadoc) */
	public void setCacheListeners(CacheListener[] cacheListeners) {
		this.cacheListeners = cacheListeners;
	}

	/* (non-Javadoc) */
	public void setCacheLoader(CacheLoader cacheLoader) {
		this.cacheLoader = cacheLoader;
	}

	/* (non-Javadoc) */
	public void setCacheWriter(CacheWriter cacheWriter) {
		this.cacheWriter = cacheWriter;
	}

	/* (non-Javadoc) */
	public void setCloningEnabled(Boolean cloningEnabled) {
		this.cloningEnabled = cloningEnabled;
	}

	/* (non-Javadoc) */
	public void setCustomEntryIdleTimeout(CustomExpiry customEntryIdleTimeout) {
		setStatisticsEnabled(customEntryIdleTimeout != null);
		this.customEntryIdleTimeout = customEntryIdleTimeout;
	}

	/* (non-Javadoc) */
	public void setCustomEntryTimeToLive(CustomExpiry customEntryTimeToLive) {
		setStatisticsEnabled(customEntryTimeToLive != null);
		this.customEntryTimeToLive = customEntryTimeToLive;
	}

	/* (non-Javadoc) */
	public void setEntryIdleTimeout(ExpirationAttributes entryIdleTimeout) {
		setStatisticsEnabled(entryIdleTimeout != null);
		this.entryIdleTimeout = entryIdleTimeout;
	}

	/* (non-Javadoc) */
	public void setEntryTimeToLive(ExpirationAttributes entryTimeToLive) {
		setStatisticsEnabled(entryTimeToLive != null);
		this.entryTimeToLive = entryTimeToLive;
	}

	/* (non-Javadoc) */
	public void setEvictionMaximum(final Integer evictionMaximum) {
		this.evictionMaximum = evictionMaximum;
	}

	/* (non-Javadoc) */
	public void setGatewaySenders(GatewaySender[] gatewaySenders) {
		this.gatewaySenders = gatewaySenders;
	}

	/* (non-Javadoc) */
	public void setRegionIdleTimeout(ExpirationAttributes regionIdleTimeout) {
		setStatisticsEnabled(regionIdleTimeout != null);
		this.regionIdleTimeout = regionIdleTimeout;
	}

	/* (non-Javadoc) */
	public void setRegionTimeToLive(ExpirationAttributes regionTimeToLive) {
		setStatisticsEnabled(regionTimeToLive != null);
		this.regionTimeToLive = regionTimeToLive;
	}

	/* (non-Javadoc) */
	public void setStatisticsEnabled(Boolean enableStatistics) {
		this.enableStatistics = enableStatistics;
	}

	/* (non-Javadoc) */
	protected boolean isStatisticsEnabled() {
		return Boolean.TRUE.equals(this.enableStatistics);
	}

	/* (non-Javadoc) */
	private void assertStatisticsEnabled() {
		Region localRegion = getRegion();
		Assert.state(localRegion.getAttributes().getStatisticsEnabled(), String.format(
			"Statistics for Region '%1$s' must be enabled to change Entry & Region TTL/TTI Expiration settings",
				localRegion.getFullPath()));
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy