Search in sources :

Example 11 with CacheException

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;
    }
}
Also used : CacheException(net.sf.ehcache.CacheException) CacheManager(net.sf.ehcache.CacheManager) URL(java.net.URL) CacheException(net.sf.ehcache.CacheException)

Example 12 with CacheException

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());
    }
}
Also used : CacheException(net.sf.ehcache.CacheException) InstrumentedEhcache(com.codahale.metrics.ehcache.InstrumentedEhcache) Ehcache(net.sf.ehcache.Ehcache)

Example 13 with CacheException

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);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CacheException(net.sf.ehcache.CacheException) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) InvocationOnMock(org.mockito.invocation.InvocationOnMock) StateMachineConfig(org.killbill.automaton.StateMachineConfig) Test(org.testng.annotations.Test)

Example 14 with CacheException

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.");
    }
}
Also used : CacheException(net.sf.ehcache.CacheException) Element(net.sf.ehcache.Element)

Example 15 with CacheException

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;
}
Also used : Serializable(java.io.Serializable) CacheException(net.sf.ehcache.CacheException) InputStream(java.io.InputStream) Element(net.sf.ehcache.Element)

Aggregations

CacheException (net.sf.ehcache.CacheException)17 Element (net.sf.ehcache.Element)6 Cache (net.sf.ehcache.Cache)5 CacheManager (net.sf.ehcache.CacheManager)4 Ehcache (net.sf.ehcache.Ehcache)3 InstrumentedEhcache (com.codahale.metrics.ehcache.InstrumentedEhcache)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Serializable (java.io.Serializable)2 Iterator (java.util.Iterator)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 StateMachineConfig (org.killbill.automaton.StateMachineConfig)2 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Test (org.testng.annotations.Test)2 FileInputStream (java.io.FileInputStream)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1