use of org.exoplatform.services.cache.ExoCache in project kernel by exoplatform.
the class ExoCacheFactoryImpl method createCache.
/**
* To create a new cache instance according to the given configuration, we follow the steps below:
*
* We first try to find if a specific location of the cache configuration has been defined thanks
* to an external component plugin of type ExoCacheFactoryConfigPlugin. If so we use the default cache
* configuration defined in this file otherwise we use the default cache configuration defined in
* "${CACHE_CONFIG_TEMPLATE_KEY}"
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public ExoCache<Serializable, Object> createCache(final ExoCacheConfig config) throws ExoCacheInitException {
final String region = config.getName();
String CacheConfig = mappingCacheNameConfig.get(region);
if (CacheConfig == null && config.isAsync() && asyncCacheTemplate != null && asyncCacheManager == null) {
// use async template if cache use async mode
this.asyncCacheManager = initCacheManager(asyncCacheTemplate);
}
final String customConfig = CacheConfig;
final ExoCache<Serializable, Object> eXoCache;
final DefaultCacheManager cacheManager;
try {
final ConfigurationBuilder confBuilder = new ConfigurationBuilder();
if (customConfig != null) {
try {
cacheManager = SecurityHelper.doPrivilegedExceptionAction(new PrivilegedExceptionAction<DefaultCacheManager>() {
public DefaultCacheManager run() throws Exception {
// A custom configuration has been set
if (LOG.isInfoEnabled())
LOG.info("A custom configuration has been set for the cache '" + region + "'.");
ParserRegistry parser = new ParserRegistry(Thread.currentThread().getContextClassLoader());
// Load the configuration
ConfigurationBuilderHolder holder = parser.parse(configManager.getInputStream(customConfig));
GlobalConfigurationBuilder configBuilder = holder.getGlobalConfigurationBuilder();
// Configure JGroups and JMX since it could affect the state of the Global Config
configureCacheManager(configBuilder);
GlobalConfiguration gc = configBuilder.build();
// Check if a CacheManager with the same GlobalConfiguration exists
DefaultCacheManager currentCacheManager = mappingGlobalConfigCacheManager.get(gc.transport().clusterName());
if (currentCacheManager == null) {
// Use a different cache manager name to prevent naming conflict
configBuilder.globalJmxStatistics().cacheManagerName(gc.globalJmxStatistics().cacheManagerName() + "_" + region + "_" + ctx.getName());
// No cache manager has been defined so far for this Cache Configuration
currentCacheManager = new DefaultCacheManager(configBuilder.build(), holder.getDefaultConfigurationBuilder().build(), false);
for (Entry<String, ConfigurationBuilder> entry : holder.getNamedConfigurationBuilders().entrySet()) {
currentCacheManager.defineConfiguration(entry.getKey(), entry.getValue().build());
}
currentCacheManager.start();
// We register this new cache manager
mappingGlobalConfigCacheManager.put(gc.transport().clusterName(), currentCacheManager);
}
return currentCacheManager;
}
});
} catch (PrivilegedActionException e) {
Throwable cause = e.getCause();
if (cause instanceof Exception) {
// NOSONAR
throw (Exception) cause;
} else {
throw new Exception(e);
}
}
confBuilder.read(cacheManager.getDefaultCacheConfiguration());
} else if (config.isDistributed()) {
// We expect a distributed cache
if (distributedCacheManager == null) {
throw new IllegalArgumentException("The DistributedCacheManager has not been defined in the configuration," + " please configure it at root container level if you want to use a distributed cache.");
}
return new DistributedExoCache(ctx, config, distributedCacheManager.getCache(DistributedExoCache.CACHE_NAME));
} else {
cacheManager = (config.isAsync() && asyncCacheManager != null) ? this.asyncCacheManager : this.cacheManager;
// No custom configuration has been found, a configuration template will be used
if (LOG.isInfoEnabled())
LOG.info("The configuration template will be used for the cache '" + region + "'.");
confBuilder.read(cacheManager.getDefaultCacheConfiguration());
if (!config.isRepicated()) {
// The cache is local
confBuilder.clustering().cacheMode(CacheMode.LOCAL);
}
}
// Reset the configuration to avoid conflicts
resetConfiguration(confBuilder);
final ExoCacheCreator creator = getExoCacheCreator(config);
// Create the cache
eXoCache = creator.create(config, confBuilder, new Callable<Cache<Serializable, Object>>() {
public Cache<Serializable, Object> call() throws Exception {
try {
return SecurityHelper.doPrivilegedExceptionAction(new PrivilegedExceptionAction<Cache<Serializable, Object>>() {
public Cache<Serializable, Object> run() throws Exception {
// Define the configuration
cacheManager.defineConfiguration(region, confBuilder.build());
// create and start the cache
return cacheManager.getCache(region);
}
});
} catch (PrivilegedActionException e) {
Throwable cause = e.getCause();
if (cause instanceof Exception) {
// NOSONAR
throw (Exception) cause;
} else {
throw new Exception(e);
}
}
}
});
} catch (// NOSONAR
Exception e) {
throw new ExoCacheInitException("The cache '" + region + "' could not be initialized", e);
}
return eXoCache;
}
use of org.exoplatform.services.cache.ExoCache in project kernel by exoplatform.
the class TestExoCacheFactoryImpl method testCacheFactory.
public void testCacheFactory() {
ExoCache cache = service_.getCacheInstance("myCache");
assertTrue("expect an instance of AbstractExoCache", cache instanceof AbstractExoCache);
AbstractExoCache aCache = (AbstractExoCache) cache;
assertTrue("expect a local cache", aCache.cache.getCacheConfiguration().clustering().cacheMode() == CacheMode.LOCAL);
aCache.cache.stop();
cache = service_.getCacheInstance("cacheDistributed");
assertTrue("expect an instance of AbstractExoCache", cache instanceof AbstractExoCache);
aCache = (AbstractExoCache) cache;
assertTrue("expect a distributed cache", aCache.cache.getCacheConfiguration().clustering().cacheMode() == CacheMode.REPL_SYNC);
aCache.cache.stop();
cache = service_.getCacheInstance("myCustomCache");
assertTrue("expect an instance of AbstractExoCache", cache instanceof AbstractExoCache);
aCache = (AbstractExoCache) cache;
assertTrue("expect a distributed cache", aCache.cache.getCacheConfiguration().clustering().cacheMode() == CacheMode.REPL_SYNC);
aCache.cache.stop();
}
use of org.exoplatform.services.cache.ExoCache in project kernel by exoplatform.
the class TestDistributedExoCache method testSelect.
public void testSelect() throws Exception {
cache.put(new MyKey("a"), 1);
cache.put(new MyKey("b"), 2);
cache.put(new MyKey("c"), 3);
final AtomicInteger count = new AtomicInteger();
CachedObjectSelector<Serializable, Object> selector = new CachedObjectSelector<Serializable, Object>() {
public void onSelect(ExoCache<? extends Serializable, ? extends Object> cache, Serializable key, ObjectCacheInfo<? extends Object> ocinfo) throws Exception {
assertTrue(key.equals(new MyKey("a")) || key.equals(new MyKey("b")) || key.equals(new MyKey("c")));
assertTrue(ocinfo.get().equals(1) || ocinfo.get().equals(2) || ocinfo.get().equals(3));
count.incrementAndGet();
}
public boolean select(Serializable key, ObjectCacheInfo<? extends Object> ocinfo) {
return true;
}
};
cache.select(selector);
assertEquals(3, count.intValue());
}
use of org.exoplatform.services.cache.ExoCache in project kernel by exoplatform.
the class TestMCExoCache method testSelect.
public void testSelect() throws Exception {
cache.put(new MyKey("a"), 1);
cache.put(new MyKey("b"), 2);
cache.put(new MyKey("c"), 3);
final AtomicInteger count = new AtomicInteger();
CachedObjectSelector<Serializable, Object> selector = new CachedObjectSelector<Serializable, Object>() {
public void onSelect(ExoCache<? extends Serializable, ? extends Object> cache, Serializable key, ObjectCacheInfo<? extends Object> ocinfo) throws Exception {
assertTrue(key.equals(new MyKey("a")) || key.equals(new MyKey("b")) || key.equals(new MyKey("c")));
assertTrue(ocinfo.get().equals(1) || ocinfo.get().equals(2) || ocinfo.get().equals(3));
count.incrementAndGet();
}
public boolean select(Serializable key, ObjectCacheInfo<? extends Object> ocinfo) {
return true;
}
};
try {
cache.select(selector);
assertEquals(3, count.intValue());
} catch (UnsupportedOperationException e) {
// OK
}
}
Aggregations