use of net.sf.ehcache.CacheException in project simplejpa by appoxy.
the class EhCacheFactory method init.
public synchronized void init(Map properties) {
if (manager != null) {
log.warning("Attempt to restart an already started CacheFactory. Using previously created EhCacheFactory.");
return;
}
initializing = true;
try {
String configurationResourceName = null;
if (properties != null) {
configurationResourceName = (String) properties.get(NET_SF_EHCACHE_CONFIGURATION_RESOURCE_NAME);
}
if (configurationResourceName == null || configurationResourceName.length() == 0) {
manager = new CacheManager();
} else {
if (!configurationResourceName.startsWith("/")) {
configurationResourceName = "/" + configurationResourceName;
if (log.isLoggable(Level.FINE)) {
log.fine("prepending / to " + configurationResourceName + ". It should be placed in the root" + "of the classpath rather than in a package.");
}
}
URL url = loadResource(configurationResourceName);
manager = new CacheManager(url);
}
} catch (net.sf.ehcache.CacheException e) {
if (e.getMessage().startsWith("Cannot parseConfiguration CacheManager. Attempt to create a new instance of " + "CacheManager using the diskStorePath")) {
throw new CacheException("Could not init EhCacheFactory.", e);
} else {
throw e;
}
} finally {
initializing = false;
}
}
use of net.sf.ehcache.CacheException in project killbill by killbill.
the class ShiroEhCacheInstrumentor method instrument.
public void instrument(final String cacheName) {
// Initialize the cache, if it doesn't exist yet
// Note: Shiro's cache manager is not thread safe. Concurrent requests on startup
// can throw org.apache.shiro.cache.CacheException: net.sf.ehcache.ObjectExistsException: Cache shiro-activeSessionCache already exists
shiroEhCacheManager.getCache(cacheName);
final Ehcache shiroEhcache = ehCacheCacheManager.getEhcache(cacheName);
final Ehcache decoratedCache = InstrumentedEhcache.instrument(metricRegistry, shiroEhcache);
try {
ehCacheCacheManager.replaceCacheWithDecoratedCache(shiroEhcache, decoratedCache);
} catch (final CacheException e) {
logger.warn("Unable to instrument cache {}: {}", shiroEhcache.getName(), e.getMessage());
}
}
use of net.sf.ehcache.CacheException in project killbill by killbill.
the class TestStateMachineConfigCacheInvalidationCallback method testInvalidation.
@Test(groups = "fast")
public void testInvalidation() throws Exception {
final String pluginName = UUID.randomUUID().toString();
final StateMachineConfig defaultPaymentStateMachineConfig = stateMachineConfigCache.getPaymentStateMachineConfig(UUID.randomUUID().toString(), internalCallContext);
Assert.assertNotNull(defaultPaymentStateMachineConfig);
final AtomicBoolean shouldThrow = new AtomicBoolean(false);
Mockito.when(tenantInternalApi.getPluginPaymentStateMachineConfig(Mockito.eq(pluginName), Mockito.any(InternalTenantContext.class))).thenAnswer(new Answer<String>() {
@Override
public String answer(final InvocationOnMock invocation) throws Throwable {
if (shouldThrow.get()) {
throw new RuntimeException();
}
return null;
}
});
// Prime caches
Assert.assertEquals(stateMachineConfigCache.getPaymentStateMachineConfig(pluginName, internalCallContext), defaultPaymentStateMachineConfig);
Assert.assertEquals(stateMachineConfigCache.getPaymentStateMachineConfig(pluginName, otherMultiTenantContext), defaultPaymentStateMachineConfig);
shouldThrow.set(true);
// No exception (cached)
Assert.assertEquals(stateMachineConfigCache.getPaymentStateMachineConfig(pluginName, internalCallContext), defaultPaymentStateMachineConfig);
cacheInvalidationCallback.invalidateCache(TenantKey.PLUGIN_PAYMENT_STATE_MACHINE_, pluginName, multiTenantContext);
try {
stateMachineConfigCache.getPaymentStateMachineConfig(pluginName, multiTenantContext);
Assert.fail();
} catch (final CacheException exception) {
Assert.assertTrue(exception.getCause() instanceof RuntimeException);
}
// No exception (cached)
Assert.assertEquals(stateMachineConfigCache.getPaymentStateMachineConfig(pluginName, otherMultiTenantContext), defaultPaymentStateMachineConfig);
}
use of net.sf.ehcache.CacheException in project camel by apache.
the class CacheProducer method performCacheOperation.
private void performCacheOperation(Exchange exchange, String operation, String key) throws Exception {
if (checkIsEqual(operation, CacheConstants.CACHE_OPERATION_URL_ADD)) {
LOG.debug("Adding an element with key {} into the Cache", key);
Element element = createElementFromBody(key, exchange, CacheConstants.CACHE_OPERATION_ADD);
cache.put(element);
} else if (checkIsEqual(operation, CacheConstants.CACHE_OPERATION_URL_UPDATE)) {
LOG.debug("Updating an element with key {} into the Cache", key);
Element element = createElementFromBody(key, exchange, CacheConstants.CACHE_OPERATION_UPDATE);
cache.put(element);
} else if (checkIsEqual(operation, CacheConstants.CACHE_OPERATION_URL_DELETEALL)) {
LOG.debug("Deleting All elements from the Cache");
cache.removeAll();
} else if (checkIsEqual(operation, CacheConstants.CACHE_OPERATION_URL_DELETE)) {
LOG.debug("Deleting an element with key {} into the Cache", key);
cache.remove(key);
} else if (checkIsEqual(operation, CacheConstants.CACHE_OPERATION_URL_GET)) {
LOG.debug("Quering an element with key {} from the Cache", key);
Element element = cache.get(key);
if (element != null) {
exchange.getIn().setHeader(CacheConstants.CACHE_ELEMENT_WAS_FOUND, true);
exchange.getIn().setBody(element.getObjectValue());
} else {
exchange.getIn().removeHeader(CacheConstants.CACHE_ELEMENT_WAS_FOUND);
}
} else if (checkIsEqual(operation, CacheConstants.CACHE_OPERATION_URL_CHECK)) {
LOG.debug("Querying an element with key {} from the Cache", key);
// getQuiet checks for element expiry
Element element = cache.getQuiet(key);
if (element != null) {
exchange.getIn().setHeader(CacheConstants.CACHE_ELEMENT_WAS_FOUND, true);
} else {
exchange.getIn().removeHeader(CacheConstants.CACHE_ELEMENT_WAS_FOUND);
}
} else {
throw new CacheException(CacheConstants.CACHE_OPERATION + " " + operation + " is not supported.");
}
}
use of net.sf.ehcache.CacheException in project camel by apache.
the class CacheProducer method createElementFromBody.
private Element createElementFromBody(String key, Exchange exchange, String cacheOperation) throws NoTypeConversionAvailableException {
Element element;
Object body = exchange.getIn().getBody();
if (body == null) {
throw new CacheException("Body cannot be null for operation " + cacheOperation);
} else if (body instanceof Serializable) {
element = new Element(key, body);
} else if (config.isObjectCache()) {
element = new Element(key, body);
} else {
InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, body);
// Read InputStream into a byte[] buffer
element = new Element(key, exchange.getContext().getTypeConverter().mandatoryConvertTo(byte[].class, is));
}
// set overrides for the cache expiration and such
final Integer ttl = exchange.getIn().getHeader(CacheConstants.CACHE_ELEMENT_EXPIRY_TTL, Integer.class);
if (ttl != null) {
element.setTimeToLive(ttl);
}
final Integer idle = exchange.getIn().getHeader(CacheConstants.CACHE_ELEMENT_EXPIRY_IDLE, Integer.class);
if (idle != null) {
element.setTimeToIdle(idle);
}
final Boolean flag = exchange.getIn().getHeader(CacheConstants.CACHE_ELEMENT_EXPIRY_ETERNAL, Boolean.class);
if (flag != null) {
element.setEternal(flag);
}
return element;
}
Aggregations