org.infinispan.spring.InfinispanDefaultCacheFactoryBean Maven / Gradle / Ivy
package org.infinispan.spring;
import java.lang.invoke.MethodHandles;
import org.infinispan.Cache;
import org.infinispan.commons.logging.Log;
import org.infinispan.commons.logging.LogFactory;
import org.infinispan.manager.CacheContainer;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
/**
*
* A {@link org.springframework.beans.factory.FactoryBean FactoryBean
} for creating a
* native default Infinispan {@link org.infinispan.Cache org.infinispan.Cache
}
* , delegating to a {@link #setInfinispanCacheContainer(CacheContainer) configurable
}
* {@link org.infinispan.manager.CacheContainer org.infinispan.manager.CacheContainer
}.
* A default Cache
is a Cache
that uses its CacheContainer
's
* default settings. This is contrary to a named Cache
where select settings
* from a CacheContainer
's default configuration may be overridden with settings
* specific to that Cache
.
*
*
* In addition to creating a Cache
this FactoryBean
does also control that
* Cache
's {@link org.infinispan.commons.api.Lifecycle lifecycle} by shutting it down
* when the enclosing Spring application context is closed. It is therefore advisable to
* always use this FactoryBean
when creating a Cache
.
*
*
* @author Olaf Bergner
*
*/
public class InfinispanDefaultCacheFactoryBean implements FactoryBean>,
InitializingBean, DisposableBean {
protected static final Log logger = LogFactory.getLog(MethodHandles.lookup().lookupClass());
private CacheContainer infinispanCacheContainer;
private Cache infinispanCache;
/**
*
* Sets the {@link org.infinispan.manager.CacheContainer
* org.infinispan.manager.CacheContainer
} to be used for creating our
* {@link org.infinispan.Cache Cache
} instance. Note that this is a
* mandatory property.
*
*
* @param infinispanCacheContainer
* The {@link org.infinispan.manager.CacheContainer
* org.infinispan.manager.CacheContainer
} to be used for creating our
* {@link org.infinispan.Cache Cache
} instance
*/
public void setInfinispanCacheContainer(final CacheContainer infinispanCacheContainer) {
this.infinispanCacheContainer = infinispanCacheContainer;
}
/**
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
@Override
public void afterPropertiesSet() throws Exception {
if (this.infinispanCacheContainer == null) {
throw new IllegalStateException("No Infinispan CacheContainer has been set");
}
this.logger.info("Initializing named Infinispan cache ...");
this.infinispanCache = this.infinispanCacheContainer.getCache();
this.logger.info("New Infinispan cache [" + this.infinispanCache + "] initialized");
}
/**
* @see org.springframework.beans.factory.FactoryBean#getObject()
*/
@Override
public Cache getObject() throws Exception {
return this.infinispanCache;
}
/**
* @see org.springframework.beans.factory.FactoryBean#getObjectType()
*/
@Override
public Class extends Cache> getObjectType() {
return this.infinispanCache != null ? this.infinispanCache.getClass() : Cache.class;
}
/**
* Always returns true
.
*
* @return Always true
*
* @see org.springframework.beans.factory.FactoryBean#isSingleton()
*/
@Override
public boolean isSingleton() {
return true;
}
/**
* Shuts down the org.infinispan.Cache
created by this FactoryBean
.
*
* @see org.springframework.beans.factory.DisposableBean#destroy()
* @see org.infinispan.Cache#stop()
*/
@Override
public void destroy() throws Exception {
// Probably being paranoid here ...
if (this.infinispanCache != null) {
this.infinispanCache.stop();
}
}
}