org.infinispan.spring.provider.ContainerCacheManagerFactoryBean Maven / Gradle / Ivy
Go to download
The Infinispan Spring Integration project provides Spring
integration for Infinispan, a high performance distributed cache.
Its primary features are
* An implementation of org.springframework.cache.CacheManager,
Spring's central caching abstraction, backed by Infinispan's
EmbeddedCacheManager. To be used if your Spring-powered
application and Infinispan are colocated, i.e. running within
the same VM.
* An implementation of org.springframework.cache.CacheManager
backed by Infinispan's RemoteCacheManager. To bes used if your
Spring-powered application accesses Infinispan remotely, i.e.
over the network.
* An implementation of org.springframework.cache.CacheManager
backed by a CacheContainer reference. To be used if your Spring-
powered application needs access to a CacheContainer defined
outside the application (e.g. retrieved from JNDI)
* Spring namespace support allowing shortcut definitions for all the
components above
In addition, Infinispan Spring Integration offers various FactoryBeans
for facilitating creation of Infinispan core classes - Cache, CacheManager,
... - within a Spring context.
package org.infinispan.spring.provider;
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.manager.CacheContainer;
import org.infinispan.manager.EmbeddedCacheManager;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.cache.CacheManager;
import org.springframework.util.Assert;
/**
* {@link FactoryBean} for creating a {@link CacheManager} for a pre-defined {@link org.infinispan.manager.CacheContainer}.
*
* Useful when the cache container is defined outside the application (e.g. provided by the application server)
*
* @author Marius Bogoevici
*/
public class ContainerCacheManagerFactoryBean implements FactoryBean {
private CacheContainer cacheContainer;
public ContainerCacheManagerFactoryBean(CacheContainer cacheContainer) {
Assert.notNull(cacheContainer, "CacheContainer cannot be null");
if (!(cacheContainer instanceof EmbeddedCacheManager ||
cacheContainer instanceof RemoteCacheManager)) {
throw new IllegalArgumentException("CacheContainer must be either an EmbeddedCacheManager or a RemoteCacheManager ");
}
this.cacheContainer = cacheContainer;
}
@Override
public CacheManager getObject() throws Exception {
if (this.cacheContainer instanceof EmbeddedCacheManager) {
return new SpringEmbeddedCacheManager((EmbeddedCacheManager) this.cacheContainer);
} else if (this.cacheContainer instanceof RemoteCacheManager) {
return new SpringRemoteCacheManager((RemoteCacheManager) this.cacheContainer);
} else {
throw new IllegalArgumentException("CacheContainer must be either an EmbeddedCacheManager or a RemoteCacheManager ");
}
}
@Override
public Class> getObjectType() {
return CacheManager.class;
}
@Override
public boolean isSingleton() {
return true;
}
}