Refine search
/** * Gets the value attribute of the Element object. * * @return The value which must be {@code Serializable}. If not use {@link #getObjectValue}. * @throws CacheException if the value is not {@code Serializable}. * @deprecated Please use {@link #getObjectValue()} instead. */ @Deprecated public final Serializable getValue() throws CacheException { try { return (Serializable) getObjectValue(); } catch (ClassCastException e) { throw new CacheException("The value " + getObjectValue() + " for key " + getObjectKey() + " is not Serializable. Consider using Element.getObjectValue()"); } }
@Override public Object extractPrincipal(X509Certificate cert) { try { Element element = cache.get(cert); if (element != null) { return element.getObjectValue(); } } catch (CacheException cacheException) { throw new DataRetrievalFailureException("Cache failure: " + cacheException.getMessage()); } final Object principal = delegate.extractPrincipal(cert); cache.put(new Element(cert, principal)); return principal; } }
/** * {@inheritDoc} */ @Override public RuntimeException getCause() { return (RuntimeException)super.getCause(); } }
Element element = get(key); if (element != null) { return element; element = getQuiet(key); if (element != null) { return element; long cacheLoaderTimeoutMillis = configuration.getCacheLoaderTimeoutMillis(); final Object value; if (cacheLoaderTimeoutMillis > 0) { final Future<AtomicReference<Object>> future = asynchronousLoad(key, loader, loaderArgument); value = future.get(cacheLoaderTimeoutMillis, TimeUnit.MILLISECONDS).get(); } else { return getQuiet(key); } else { Element newElement = new Element(key, value); put(newElement, false); Element fromCache = getQuiet(key); throw new LoaderTimeoutException("Timeout on load for key " + key, e); } catch (Exception e) { throw new CacheException("Exception on load for key " + key, e);
/** * Returns the active configuration text for the input cacheName * * @param cacheName * @return Returns the active configuration text for the input cacheName * @throws CacheException if the cache with <code>cacheName</code> does not exist */ public String getActiveConfigurationText(String cacheName) throws CacheException { boolean decoratedCache = false; Ehcache cache = getCache(cacheName); if (cache == null) { cache = getEhcache(cacheName); decoratedCache = true; } CacheConfiguration actualConfig = cache != null ? cache.getCacheConfiguration() : null; if (actualConfig == null) { throw new CacheException("Cache with name '" + cacheName + "' does not exist"); } CacheConfiguration config = decoratedCache ? actualConfig.clone().name(cacheName) : actualConfig; return ConfigurationUtil.generateCacheConfigurationText(runtimeCfg.getConfiguration(), config); }
/** * Calls the CacheLoader and puts the result in the Cache */ public void run() throws CacheException { try { //Test to see if it has turned up in the meantime boolean existsOnRun = isKeyInCache(key); if (!existsOnRun) { Object value = loadValueUsingLoader(key, specificLoader, argument); if (value != null) { put(new Element(key, value), false); } } } catch (RuntimeException e) { if (LOG.isDebugEnabled()) { LOG.debug("Problem during load. Load will not be completed. Cause was " + e.getCause(), e); } throw new CacheException("Problem during load. Load will not be completed. Cause was " + e.getCause(), 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); } }
public UserDetails getUserFromCache(String username) { Element element = null; try { element = cache.get(username); } catch (CacheException cacheException) { throw new DataRetrievalFailureException("Cache failure: " + cacheException.getMessage()); } if (logger.isDebugEnabled()) { logger.debug("Cache hit: " + (element != null) + "; username: " + username); } if (element == null) { return null; } else { return (UserDetails) element.getValue(); } }
Element element = get(key); map.put(key, element.getObjectValue()); Future future = asynchronousLoadAll(missingKeys, loaderArgument); long cacheLoaderTimeoutMillis = configuration.getCacheLoaderTimeoutMillis(); if (cacheLoaderTimeoutMillis > 0) { try { Element element = get(key); if (element != null) { map.put(key, element.getObjectValue()); } else { map.put(key, null); throw new CacheException(e.getMessage() + " for key " + key, e); } catch (ExecutionException e) { throw new CacheException(e.getMessage() + " for key " + key, e); Element element = get(key); if (element != null) { map.put(key, element.getObjectValue()); } else { map.put(key, null);
/** * Both CacheEntryFactory can return an Element rather than just a regular value * this method test this, making a fresh Element otherwise. It also enforces * the rule that the CacheEntryFactory must provide the same key (via equals() * not necessarily same object) if it is returning an Element. * * @param key * @param value * @return the Element to be put back in the cache * @throws CacheException for various illegal states which could be harmful */ protected static Element makeAndCheckElement(Object key, Object value) throws CacheException { //simply build a new element using the supplied key if (!(value instanceof Element)) { return new Element(key, value); } //It is already an element - perform sanity checks Element element = (Element) value; if ((element.getObjectKey() == null) && (key == null)) { return element; } else if (element.getObjectKey() == null) { throw new CacheException("CacheEntryFactory returned an Element with a null key"); } else if (!element.getObjectKey().equals(key)) { throw new CacheException("CacheEntryFactory returned an Element with a different key: " + element.getObjectKey() + " compared to the key that was requested: " + key); } else { return element; } }
ObjectOutputStream oos = null; if (value.getObjectValue() == null) { return duplicateElementWithNewValue(value, null); oos.writeObject(value.getObjectValue()); } catch (Exception e) { throw new CacheException("When configured copyOnRead or copyOnWrite, a Store will only accept Serializable values", e); } finally { try {
@Override public UserDetails getUserFromCache(X509Certificate userCert) { Element element = null; try { element = cache.get(userCert); } catch (CacheException cacheException) { throw new DataRetrievalFailureException("Cache failure: " + cacheException.getMessage()); } if (logger.isDebugEnabled()) { String subjectDN = "unknown"; if ((userCert != null) && (userCert.getSubjectDN() != null)) { subjectDN = userCert.getSubjectDN().toString(); } logger.debug("X.509 Cache hit. SubjectDN: " + subjectDN); } if (element == null) { return null; } else { return (UserDetails) element.getObjectValue(); } }
/** * Check if a transaction has begun on the current thread if the cache is configured as * transactional, otherwise always return false. * @param cache the cache to check if a transaction started for * @return true if the cache is transactional and a transaction started, false otherwise * @throws CacheException if anything wrong happens */ public static boolean isTransactionStarted(Ehcache cache) throws CacheException { try { switch (cache.getCacheConfiguration().getTransactionalMode()) { case LOCAL: TransactionController ctrl = cache.getCacheManager().getTransactionController(); return ctrl.getCurrentTransactionContext() != null; case XA: case XA_STRICT: Object tm = ((net.sf.ehcache.Cache) cache).getTransactionManagerLookup().getTransactionManager(); return ((Integer) tm.getClass().getMethod("getStatus").invoke(tm)) != XA_STATUS_NO_TRANSACTION; case OFF: default: return false; } } catch (Exception e) { e.printStackTrace(); throw new CacheException("error checking if transaction started: " + e); } }
/** * Looks up an entry. creating it if not found. */ @Override public Element get(final Object key) throws LockTimeoutException { Element element = super.get(key); if (element == null) { try { // Value not cached - fetch it Object value = factory.createEntry(key); element = makeAndCheckElement(key, value); } catch (final Throwable throwable) { // Could not fetch - Ditch the entry from the cache and rethrow // release the lock you acquired element = new Element(key, null); throw new CacheException("Could not fetch object for cache entry with key \"" + key + "\".", throwable); } finally { put(element); } } return element; }
final ClassLoader loader = getCacheConfiguration().getClassLoader(); if (loader != cacheManager.getConfiguration().getClassLoader()) { if (!getName().startsWith(CacheManager.LOCAL_CACHE_NAME_PREFIX)) { throw new CacheException("This cache (" + getName() + ") is configurated with a different classloader reference than its containing cache manager"); throw new IllegalStateException("Cannot initialise the " + configuration.getName() + " cache because its status is not STATUS_UNINITIALISED"); if (featuresManager == null) { if (configuration.isOverflowToOffHeap()) { throw new CacheException("Cache " + configuration.getName() + " cannot be configured because the enterprise features manager could not be found. " + "You must use an enterprise version of Ehcache to successfully enable overflowToOffHeap."); PersistenceConfiguration persistence = configuration.getPersistenceConfiguration(); if (persistence != null && Strategy.LOCALRESTARTABLE.equals(persistence.getStrategy())) { throw new CacheException("Cache " + configuration.getName() + " cannot be configured because the enterprise features manager could not be found. " + "You must use an enterprise version of Ehcache to successfully enable enterprise persistence.");
boolean isXa = element.getObjectValue() instanceof SoftLockID; SoftLockID sl = (SoftLockID) element.getObjectValue(); element = sl.getOldElement(); for (Map.Entry<String, ?> attr : dynAttrs.entrySet()) { if (!AttributeType.isSupportedType(attr.getValue())) { throw new CacheException(String.format("Unsupported attribute type specified %s for dynamically extracted attribute %s", attr.getClass().getName(), attr.getKey())); String name = entry.getKey(); SearchAttribute sa = config.getSearchAttributes().get(name); Class<?> c = ConfigurationHelper.getSearchAttributeType(sa, cache.getCacheConfiguration().getClassLoader()); if (c == null) { continue; }
private void doLoad(final Ehcache cache) { CacheManager manager = cache.getCacheManager(); if (manager == null) { throw new CacheException("Cache must belong to a cache manager to bootstrap"); } DiskStorePathManager pathManager = diskStorePathManager != null ? diskStorePathManager : cache.getCacheManager() .getDiskStorePathManager(); final RotatingSnapshotFile snapshotFile = new RotatingSnapshotFile(pathManager, cache.getName(), manager.getConfiguration().getClassLoader()); try { final Set<Object> keys = snapshotFile.readAll(); int loaded = 0; for (Object key : keys) { if (isInMemoryLimitReached(cache, loaded)) { break; } cache.get(key); loaded++; } LOG.info("Finished loading {} keys (of {} on disk) from previous snapshot for Cache '{}'", new Object[] {Integer.valueOf(loaded), keys.size(), cache.getName()}); } catch (IOException e) { LOG.error("Couldn't load keySet for Cache '{}'", cache.getName(), e); } if (doKeySnapshot) { keySnapshotter = new KeySnapshotter(cache, interval, doKeySnapshotOnDedicatedThread, snapshotFile); } }
this.classLoader = cache.getCacheConfiguration().getClassLoader(); this.diskStorePathManager = cache.getCacheManager().getDiskStorePathManager(); this.file = diskStorePathManager.getFile(cache.getName(), ".data"); this.indexFile = diskStorePathManager.getFile(cache.getName(), ".index"); this.pinningEnabled = determineCachePinned(cache.getCacheConfiguration()); this.diskPersistent = cache.getCacheConfiguration().isDiskPersistent(); dataAccess = allocateRandomAccessFiles(file, cache.getCacheConfiguration().getDiskAccessStripes()); } catch (FileNotFoundException e) { throw new CacheException(e);
/** * Initialize the given {@link Ehcache} without adding it to the {@link CacheManager}. * * @param cache * @param registerCacheConfig */ void initializeEhcache(final Ehcache cache, final boolean registerCacheConfig) { if (!registerCacheConfig) { cache.getCacheConfiguration().setupFor(this, registerCacheConfig, getParentCacheName(cache)); } else { cache.getCacheConfiguration().setupFor(this); } cache.setCacheManager(this); cache.setTransactionManagerLookup(transactionManagerLookup); cache.initialise(); if (!runtimeCfg.allowsDynamicCacheConfig()) { cache.disableDynamicFeatures(); } if (!registerCacheConfig) { associateShadowCache(cache); } try { cache.bootstrap(); } catch (CacheException e) { LOG.warn("Cache " + cache.getName() + "requested bootstrap but a CacheException occured. " + e.getMessage(), 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); } }