org.infinispan.spring.embedded.InfinispanDefaultCacheFactoryBean Maven / Gradle / Ivy
package org.infinispan.spring.embedded;
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 FactoryBean FactoryBean
} for creating a
* native default Infinispan {@link Cache org.infinispan.Cache
}
* , delegating to a {@link #setInfinispanCacheContainer(CacheContainer) configurable
}
* {@link 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 CacheContainer
* org.infinispan.manager.CacheContainer
} to be used for creating our
* {@link Cache Cache
} instance. Note that this is a
* mandatory property.
*
*
* @param infinispanCacheContainer
* The {@link CacheContainer
* org.infinispan.manager.CacheContainer
} to be used for creating our
* {@link Cache Cache
} instance
*/
public void setInfinispanCacheContainer(final CacheContainer infinispanCacheContainer) {
this.infinispanCacheContainer = infinispanCacheContainer;
}
/**
* @see 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 FactoryBean#getObject()
*/
@Override
public Cache getObject() throws Exception {
return this.infinispanCache;
}
/**
* @see FactoryBean#getObjectType()
*/
@Override
public Class extends Cache> getObjectType() {
return this.infinispanCache != null ? this.infinispanCache.getClass() : Cache.class;
}
/**
* Always returns true
.
*
* @return Always true
*
* @see FactoryBean#isSingleton()
*/
@Override
public boolean isSingleton() {
return true;
}
/**
* Shuts down the org.infinispan.Cache
created by this FactoryBean
.
*
* @see DisposableBean#destroy()
* @see Cache#stop()
*/
@Override
public void destroy() throws Exception {
// Probably being paranoid here ...
if (this.infinispanCache != null) {
this.infinispanCache.stop();
}
}
}