Refine search
/** * Constructs a {@link MutableConfiguration} based on another * {@link CompleteConfiguration}. * * @param configuration the {@link CompleteConfiguration} */ public MutableConfiguration(CompleteConfiguration<K, V> configuration) { this.keyType = configuration.getKeyType(); this.valueType = configuration.getValueType(); listenerConfigurations = new HashSet<CacheEntryListenerConfiguration<K, V>>(); for (CacheEntryListenerConfiguration<K, V> definition : configuration .getCacheEntryListenerConfigurations()) { addCacheEntryListenerConfiguration(definition); } this.cacheLoaderFactory = configuration.getCacheLoaderFactory(); this.cacheWriterFactory = configuration.getCacheWriterFactory(); if (configuration.getExpiryPolicyFactory() == null) { this.expiryPolicyFactory = EternalExpiryPolicy.factoryOf(); } else { this.expiryPolicyFactory = configuration.getExpiryPolicyFactory(); } this.isReadThrough = configuration.isReadThrough(); this.isWriteThrough = configuration.isWriteThrough(); this.isStatisticsEnabled = configuration.isStatisticsEnabled(); this.isStoreByValue = configuration.isStoreByValue(); this.isManagementEnabled = configuration.isManagementEnabled(); }
@Override public <K, V> Cache<K, V> getCache(String cacheName) { checkNotClosed(); Cache<K, V> cache = (Cache<K, V>) getCache(cacheName, Object.class, Object.class); if (cache != null) { if (cache.getConfiguration(CompleteConfiguration.class).getKeyType() != Object.class) { throw new IllegalArgumentException("Wrong type of key for " + cacheName); } if (cache.getConfiguration(CompleteConfiguration.class).getValueType() != Object.class) { throw new IllegalArgumentException("Wrong type of value for " + cacheName); } } return cache; }
@Override public <K, V> Cache<K, V> getCache(String cacheName, Class<K> keyType, Class<V> valueType) { checkNotClosed(); if (cacheName == null) { throw new NullPointerException(); } if (keyType == null) { throw new NullPointerException(); } if (valueType == null) { throw new NullPointerException(); } JCache<?, ?> cache = caches.get(cacheName); if (cache == null) { return null; } if (!keyType.isAssignableFrom(cache.getConfiguration(CompleteConfiguration.class).getKeyType())) { throw new ClassCastException("Wrong type of key for " + cacheName); } if (!valueType.isAssignableFrom(cache.getConfiguration(CompleteConfiguration.class).getValueType())) { throw new ClassCastException("Wrong type of value for " + cacheName); } return (Cache<K, V>) cache; }
this.isReadThrough = completeConfig.isReadThrough(); this.isWriteThrough = completeConfig.isWriteThrough(); this.isStatisticsEnabled = completeConfig.isStatisticsEnabled(); this.isManagementEnabled = completeConfig.isManagementEnabled(); this.cacheWriterFactory = createThrowingFactory(); } else { this.cacheLoaderFactory = completeConfig.getCacheLoaderFactory(); this.cacheWriterFactory = completeConfig.getCacheWriterFactory(); tempExpiryPolicyFactory = completeConfig.getExpiryPolicyFactory(); for (CacheEntryListenerConfiguration<K, V> listenerConfig : completeConfig.getCacheEntryListenerConfigurations()) { cacheEntryListenerConfigs.add(listenerConfig);
private void checkConfigurationJmx(Supplier<Cache<?, ?>> cacheSupplier) throws Exception { Cache<?, ?> cache = cacheSupplier.get(); @SuppressWarnings("unchecked") CompleteConfiguration<?, ?> configuration = cache.getConfiguration(CompleteConfiguration.class); assertThat(configuration.isManagementEnabled(), is(true)); assertThat(configuration.isStatisticsEnabled(), is(true)); String name = "javax.cache:Cache=%s,CacheManager=%s,type=CacheStatistics"; ManagementFactory.getPlatformMBeanServer().getObjectInstance( new ObjectName(String.format(name, cache.getName(), PROVIDER_NAME))); } }
private <K, V> Jsr107CacheLoaderWriter<K, V> initCacheLoaderWriter(CompleteConfiguration<K, V> config) { Factory<CacheLoader<K, V>> cacheLoaderFactory = config.getCacheLoaderFactory(); @SuppressWarnings("unchecked") Factory<CacheWriter<K, V>> cacheWriterFactory = (Factory<CacheWriter<K, V>>) (Object) config.getCacheWriterFactory(); if (config.isReadThrough() && cacheLoaderFactory == null) { throw new IllegalArgumentException("read-through enabled without a CacheLoader factory provided"); } if (config.isWriteThrough() && cacheWriterFactory == null) { throw new IllegalArgumentException("write-through enabled without a CacheWriter factory provided"); } CacheLoader<K, V> cacheLoader = cacheLoaderFactory == null ? null : cacheLoaderFactory.create(); CacheWriter<K, V> cacheWriter; try { cacheWriter = cacheWriterFactory == null ? null : cacheWriterFactory.create(); } catch (Throwable t) { throw closeAllAfter(new CacheException(t), cacheLoader); } if (cacheLoader == null && cacheWriter == null) { return null; } else { return new Eh107CacheLoaderWriter<>(cacheLoader, config.isReadThrough(), cacheWriter, config.isWriteThrough()); } }
@Override protected void configure() { for (Map.Entry<String, Cache> cacheEntry : this.caches.entrySet()) { bind(Cache.class).annotatedWith(Names.named(cacheEntry.getKey())).toInstance(cacheEntry.getValue()); Factory expiryPolicyFactory = ((CompleteConfiguration)cacheEntry.getValue().getConfiguration(CompleteConfiguration.class)).getExpiryPolicyFactory(); if (expiryPolicyFactory != null) { requestInjection(expiryPolicyFactory); } Factory cacheLoaderFactory = ((CompleteConfiguration)cacheEntry.getValue().getConfiguration(CompleteConfiguration.class)).getCacheLoaderFactory(); if (cacheLoaderFactory != null) { requestInjection(cacheLoaderFactory); } Factory cacheWriterFactory = ((CompleteConfiguration)cacheEntry.getValue().getConfiguration(CompleteConfiguration.class)).getCacheWriterFactory(); if (cacheWriterFactory != null) { requestInjection(cacheWriterFactory); } } install(new CacheAnnotationsModule()); } }
@Override public boolean isManagementEnabled() { return cache.getConfiguration(CompleteConfiguration.class).isManagementEnabled(); }
@Override public boolean isStatisticsEnabled() { return cache.getConfiguration(CompleteConfiguration.class).isStatisticsEnabled(); }
@Override public boolean isWriteThrough() { return cache.getConfiguration(CompleteConfiguration.class).isWriteThrough(); }
@Override public boolean isReadThrough() { return cache.getConfiguration(CompleteConfiguration.class).isReadThrough(); }
@Test public void test107CacheCanReturnCompleteConfigurationWhenNonePassedIn() { CacheManager cacheManager = cachingProvider.getCacheManager(); Cache<Long, String> cache = cacheManager.createCache("cacheWithoutCompleteConfig", new Configuration<Long, String>() { private static final long serialVersionUID = 1L; @Override public Class<Long> getKeyType() { return Long.class; } @Override public Class<String> getValueType() { return String.class; } @Override public boolean isStoreByValue() { return true; } }); @SuppressWarnings("unchecked") CompleteConfiguration<Long, String> configuration = cache.getConfiguration(CompleteConfiguration.class); assertThat(configuration, notNullValue()); assertThat(configuration.isStoreByValue(), is(true)); // Respects defaults assertThat(configuration.getExpiryPolicyFactory(), equalTo(EternalExpiryPolicy.factoryOf())); }
private void buildAdapterCache() { createdCache = new JCacheAdapter<K, V>( manager, new InternalCache2kBuilder<K,V>(cache2kConfiguration, manager.getCache2kManager()).buildAsIs(), keyType.getType(), valueType.getType(), config.isStoreByValue(), config.isReadThrough(), config.getCacheLoaderFactory() != null, eventHandling ); }
@Test public void testDynamicRegistration() { assertEquals(1, getConfigurationCacheEntryListenerConfigurationSize(cache)); MyCacheEntryListener secondListener = new MyCacheEntryListener<Long, String>(oldValueRequired); MutableCacheEntryListenerConfiguration<Long, String> listenerConfiguration = new MutableCacheEntryListenerConfiguration(FactoryBuilder.factoryOf(secondListener), null, oldValueRequired, true); cache.registerCacheEntryListener(listenerConfiguration); assertEquals(2,getConfigurationCacheEntryListenerConfigurationSize(cache)); CompleteConfiguration<Long, String> cacheConfig = (CompleteConfiguration)cache.getConfiguration(CompleteConfiguration.class); for (CacheEntryListenerConfiguration<Long, String> config : cacheConfig.getCacheEntryListenerConfigurations()) { config.hashCode(); config.isOldValueRequired(); config.isSynchronous(); } //Can only register the same configuration once try { cache.registerCacheEntryListener(listenerConfiguration); fail(); } catch (IllegalArgumentException e) { //expected } }
private <K, V> Map<CacheEntryListenerConfiguration<K, V>, ListenerResources<K, V>> initCacheEventListeners(CompleteConfiguration<K, V> config) { Map<CacheEntryListenerConfiguration<K, V>, ListenerResources<K, V>> listenerResources = new ConcurrentHashMap<>(); for (CacheEntryListenerConfiguration<K, V> listenerConfig : config.getCacheEntryListenerConfigurations()) { listenerResources.put(listenerConfig, ListenerResources.createListenerResources(listenerConfig)); } return listenerResources; }
CacheRuntimeConfiguration<Long, Client> ehcacheConfig = (CacheRuntimeConfiguration<Long, Client>)anyCache.getConfiguration( Eh107Configuration.class).unwrap(CacheRuntimeConfiguration.class); // <3> assertFalse(anotherCache.getConfiguration(Configuration.class).isStoreByValue()); // <5> CacheRuntimeConfiguration<Long, Client> foosEhcacheConfig = (CacheRuntimeConfiguration<Long, Client>)foosCache.getConfiguration( Eh107Configuration.class).unwrap(CacheRuntimeConfiguration.class); Client client1 = new Client("client1", 1); final Factory<ExpiryPolicy> expiryPolicyFactory = foosConfig.getExpiryPolicyFactory();
private <K, V> Eh107Expiry<K, V> initExpiryPolicy(CompleteConfiguration<K, V> config) { return new ExpiryPolicyToEhcacheExpiry<>(config.getExpiryPolicyFactory().create()); }
private int getConfigurationCacheEntryListenerConfigurationSize(Cache cache) { int i = 0; CompleteConfiguration<Long, String> cacheConfig = (CompleteConfiguration)cache.getConfiguration(CompleteConfiguration.class); for (CacheEntryListenerConfiguration<Long, String> listenerConfig : cacheConfig.getCacheEntryListenerConfigurations()) { i++; } return i; }
@Override public String getValueType() { return cache.getConfiguration(CompleteConfiguration.class).getValueType().getName(); }
@Override public String getKeyType() { return cache.getConfiguration(CompleteConfiguration.class).getKeyType().getName(); }