/** * Returns a deep copy of the map if value-based caching is enabled. * * @param map the mapping of keys to expirable values * @return a deep or shallow copy of the mappings depending on the store by value setting */ protected final Map<K, V> copyMap(Map<K, Expirable<V>> map) { ClassLoader classLoader = cacheManager.getClassLoader(); return map.entrySet().stream().collect(toMap( entry -> copier.copy(entry.getKey(), classLoader), entry -> copier.copy(entry.getValue().get(), classLoader))); }
/** * Returns a copy of the value if value-based caching is enabled. * * @param object the object to be copied * @param <T> the type of object being copied * @return a copy of the object if storing by value or the same instance if by reference */ @SuppressWarnings("NullAway") protected final @NonNull <T> T copyOf(@Nullable T object) { if (object == null) { return null; } T copy = copier.copy(object, cacheManager.getClassLoader()); return requireNonNull(copy); }
/** * Returns a copy of the value if value-based caching is enabled. * * @param expirable the expirable value to be copied * @return a copy of the value if storing by value or the same instance if by reference */ protected final @Nullable V copyValue(@Nullable Expirable<V> expirable) { if (expirable == null) { return null; } V copy = copier.copy(expirable.get(), cacheManager.getClassLoader()); return requireNonNull(copy); }
void remove(final CacheManager mgr) { final ClassLoader classLoader = mgr.getClassLoader(); final Map<URI, CacheManager> mgrs = cacheManagersByLoader.get(classLoader); if (mgrs != null) { mgrs.remove(mgr.getURI()); if (mgrs.isEmpty()) { cacheManagersByLoader.remove(classLoader); } } } }
void remove(final CacheManager mgr) { final ClassLoader classLoader = mgr.getClassLoader(); final Map<URI, CacheManager> mgrs = cacheManagersByLoader.get(classLoader); if (mgrs != null) { mgrs.remove(mgr.getURI()); if (mgrs.isEmpty()) { cacheManagersByLoader.remove(classLoader); } } } }
void remove(final CacheManager mgr) { final ClassLoader classLoader = mgr.getClassLoader(); final Map<URI, CacheManager> mgrs = cacheManagersByLoader.get(classLoader); if (mgrs != null) { mgrs.remove(mgr.getURI()); if (mgrs.isEmpty()) { cacheManagersByLoader.remove(classLoader); } } } }
/** * Returns a copy of the value if value-based caching is enabled. * * @param object the object to be copied * @param <T> the type of object being copied * @return a copy of the object if storing by value or the same instance if by reference */ @SuppressWarnings("NullAway") protected final @Nonnull <T> T copyOf(@Nullable T object) { if (object == null) { return null; } T copy = copier.copy(object, cacheManager.getClassLoader()); return requireNonNull(copy); }
/** * Returns a deep copy of the map if value-based caching is enabled. * * @param map the mapping of keys to expirable values * @return a deep or shallow copy of the mappings depending on the store by value setting */ protected final Map<K, V> copyMap(Map<K, Expirable<V>> map) { ClassLoader classLoader = cacheManager.getClassLoader(); return map.entrySet().stream().collect(toMap( entry -> copier.copy(entry.getKey(), classLoader), entry -> copier.copy(entry.getValue().get(), classLoader))); }
/** * Returns a copy of the value if value-based caching is enabled. * * @param expirable the expirable value to be copied * @return a copy of the value if storing by value or the same instance if by reference */ protected final @Nullable V copyValue(@Nullable Expirable<V> expirable) { if (expirable == null) { return null; } V copy = copier.copy(expirable.get(), cacheManager.getClassLoader()); return requireNonNull(copy); }
@Override public void close(ClassLoader classLoader) { // System.out.println("CacheManager.close() cl=" + classLoader); synchronized (cacheManagersLock) { for( Iterator<CacheManager> it = cacheManagers.iterator(); it.hasNext(); ) { CacheManager cacheManager = it.next(); if (cacheManager.getClassLoader().equals(classLoader)) { cacheManager.close(); // Hint: It may have happened, that cacheManager.close() already removed itrslef cacheManagers. // In that case the next line is a NOP. It is important though, that the underlying Set is a Concurrent Set. it.remove(); } } } }
/** * Returns the CacheManager for the given parameters if it is managed by this TCacheProvider * @param uri The Cache URI * @param classLoader The Classloader * * @return The CacheManager, or null if no matching one is managed by this TCacheProvider */ private CacheManager findCacheManager(URI uri, ClassLoader classLoader) { synchronized (cacheManagersLock) { for (CacheManager cacheManager : cacheManagers) { if (! cacheManager.getURI().equals(uri)) continue; if (! cacheManager.getClassLoader().equals(classLoader)) continue; // Properties are to be ignored for equality check according to the JSR107 Spec return cacheManager; } } return null; }
@Test public void getCacheManager_nullClassLoader() { CachingProvider provider = Caching.getCachingProvider(); final ClassLoader NULL_CLASSLOADER = null; // null classloader is treated as provider.getDefaultClassLoader(). CacheManager manager = provider.getCacheManager(provider.getDefaultURI(), NULL_CLASSLOADER, null); assertNotNull(manager); CacheManager sameManager = provider.getCacheManager(provider.getDefaultURI(), provider.getDefaultClassLoader(), null); assertEquals(sameManager, manager); assertEquals(sameManager.getClassLoader(), manager.getClassLoader()); }