void rejectNullKey(K key) { if (key == null) { throw new CacheException(new NullPointerException("Null keys are not supported")); } }
@Nonnull @Override public List<UserHistoryItem> getHistory(@Nonnull final UserHistoryItem.Type type, @Nonnull final String userKey) { notNull("userKey", userKey); notNull("type", type); try { return ImmutableList.copyOf(cache.get(new Key(userKey, type))); } catch (CacheException e) { if (e.getCause() instanceof DataAccessException) { //JRADEV-15907, JRA-30247: We don't want a database error here to cause other operations to fail. If the user //loses a bit of history then so be it. Log an move on. log.debug("Unable to get user history items. Returning empty list.", e); return emptyList(); } else { throw e; } } }
@Override public PropertyEntry getEntry(String entityName, long entityId, String propertyKey) { try { return entries.get(new CacheKey(entityName, entityId, propertyKey)).getValue(); } catch (CacheException ex) { final Throwable cause = ex.getCause(); if (cause instanceof RuntimeException) { throw (RuntimeException)cause; } throw new PropertyImplementationException((cause != null) ? cause : ex); } }
@Nonnull @Override public Collection<K> getKeys() { try { return internalCache.asMap().keySet(); } catch (Exception e) { throw new CacheException(e); } }
@Test public void testExceptionHandling() throws Exception { Cache<String, Long> cache = makeExceptionalCache(); // Add some entries using the builder assertThat(cache.get("1"), equalTo(1L)); assertThat(cache.get("2"), equalTo(2L)); assertThat(cache.get("3"), equalTo(3L)); try { cache.get("George"); fail("Should throw CacheException when the loader throws an exception"); } catch (CacheException ex) { assertThat("This exception should wrap the original Exception", ex.getCause(), notNullValue()); } try { Long v = cache.get(null); fail("Should throw CacheException when the Key is a null"); } catch (CacheException ex) { // Just want a CacheException } assertSize(cache, (3)); }
@Override public boolean replace(@Nonnull K key, @Nonnull V oldValue, @Nonnull V newValue) { try { return internalCache.asMap().replace(key, oldValue, newValue); } catch (Exception e) { throw new CacheException(e); } }
@Override public void reset() { try { hazelcastMap.remove(REFERENCE_KEY); } catch (RuntimeException e) { throw new CacheException(e); } }
@Override public void remove(@Nonnull final K key) { try { delegate.remove(wrap(key)); } catch (Exception e) { throw new CacheException(e); } }
@Override public boolean remove(@Nonnull K key, @Nonnull V value) { try { return internalCache.asMap().remove(key, value); } catch (Exception e) { throw new CacheException(e); } finally { if (isCollectorStatisticsEnabled()) { collector.remove(); } } }
@Nonnull @SuppressWarnings("unchecked") @Override public Collection<K> getKeys() { try { return unwrapAllKeys(delegate.getKeys(), valueProcessor); } catch (Exception e) { throw new CacheException(e); } }
@Override public void put(@Nonnull final K key, @Nonnull final V value) { try { delegate.put(new Element(wrap(key), wrap(value))); } catch (Exception e) { throw new CacheException(e); } }
@Override public void removeAll() { removeAllLock.lock(); try { internalCache.invalidateAll(); eventLogger.info("Cache {} was flushed", getName()); if (stacktraceLogger.isInfoEnabled()) { stacktraceLogger.info("Cache {} was flushed. Stacktrace:", getName(), new Exception()); } } catch (Exception e) { throw new CacheException(e); } finally { removeAllLock.unlock(); } }
@Override public boolean remove(@Nonnull K key, @Nonnull V value) { try { return delegate.removeElement(new Element(wrap(key), wrap(value))); } catch (Exception e) { throw new CacheException(e); } }
@SuppressWarnings("unchecked") @Nullable @Override public V get(@Nonnull final K key) { try { Element element = unwrap(delegate.get(wrap(key))); return element == null ? null : (V) element.getObjectValue(); } catch (net.sf.ehcache.CacheException e) { throw new CacheException(e.getCause()); } catch (Exception e) { throw new CacheException(e); } }
@Override public void removeAll() { try { delegate.removeAll(); eventLogger.info("Cache {} was flushed", delegate.getName()); if (stacktraceLogger.isInfoEnabled()) { stacktraceLogger.info("Cache {} was flushed. Stacktrace:", delegate.getName(), new Exception()); } } catch (Exception e) { throw new CacheException(e); } }
@Override public boolean replace(@Nonnull K key, @Nonnull V oldValue, @Nonnull V newValue) { try { return delegate.replace(new Element(wrap(key), wrap(oldValue)), new Element(wrap(key), wrap(newValue))); } catch (Exception e) { throw new CacheException(e); } }
@SuppressWarnings("unchecked") @Nonnull @Override public V get(@Nonnull final K key, @Nonnull final com.atlassian.cache.Supplier<? extends V> valueSupplier) { try { Element element = unwrap(delegate.getWithLoader(wrap(key), getCacheLoader(valueSupplier), null)); return (V) element.getObjectValue(); } catch (net.sf.ehcache.CacheException e) { throw new CacheException(e.getCause()); } catch (Exception e) { throw new CacheException(e); } }
@Nonnull @SuppressWarnings("unchecked") @Override public V get() { try { return (V) unwrap(delegate.get(wrap(ReferenceKey.KEY))).getObjectValue(); } catch (net.sf.ehcache.CacheException e) { throw new CacheException(e.getCause()); } catch (Exception e) { throw new CacheException(e); } }
@Nullable @Override @SuppressWarnings("unchecked") public V putIfAbsent(@Nonnull K key, @Nonnull V value) { try { Element previous = unwrap(delegate.putIfAbsent(new Element(wrap(key), wrap(value)))); if (previous != null) { return (V) previous.getObjectValue(); } else { return null; } } catch (Exception e) { throw new CacheException(e); } }
@Override public void put(@Nonnull final K key, @Nonnull final V value) { try { V oldValue = internalCache.asMap().put(key, value); if (isCollectorStatisticsEnabled()) { collector.put(); } if (oldValue == null) { // Here we care only for the case when oldValue was null, e.g. missing as in the other // cases DelegatingRemovalListener will be called with REPLACED notification listenerSupport.notifyAdd(key, value); } } catch (Exception e) { throw new CacheException(e); } }