@SuppressWarnings("unchecked") private CompleteConfiguration<?, ?> configuration() { return cache.getConfiguration(CompleteConfiguration.class); } }
@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) { 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; }
/** {@inheritDoc} */ @Override public <K, V> Cache<K, V> getCache(String cacheName, Class<K> keyType, Class<V> valType) { kernalGateway.readLock(); try { Cache<K, V> cache = getCache0(cacheName); if (cache != null) { if(!keyType.isAssignableFrom(cache.getConfiguration(Configuration.class).getKeyType())) throw new ClassCastException(); if(!valType.isAssignableFrom(cache.getConfiguration(Configuration.class).getValueType())) throw new ClassCastException(); } return cache; } finally { kernalGateway.readUnlock(); } }
private void checkConfiguration(Supplier<Cache<?, ?>> cacheSupplier, long expectedValue) { Cache<?, ?> cache = cacheSupplier.get(); @SuppressWarnings("unchecked") CaffeineConfiguration<?, ?> configuration = cache.getConfiguration(CaffeineConfiguration.class); assertThat(configuration.getMaximumSize(), is(OptionalLong.of(expectedValue))); } }
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))); } }
@Test public void getCache() { Cache<Integer, Integer> cache = Caching.getCachingProvider() .getCacheManager().getCache("test-cache"); assertThat(cache, is(not(nullValue()))); @SuppressWarnings("unchecked") CaffeineConfiguration<Integer, Integer> config = cache.getConfiguration(CaffeineConfiguration.class); checkTestCache(config); }
@Test public void testTypeOverriding() throws Exception { CachingProvider provider = Caching.getCachingProvider(); javax.cache.CacheManager cacheManager = provider.getCacheManager(this.getClass().getResource("/ehcache-107-types.xml").toURI(), getClass().getClassLoader()); MutableConfiguration<Long, String> cache1Conf = new MutableConfiguration<>(); cache1Conf.setTypes(Long.class, String.class); javax.cache.Cache<Long, String> cache = cacheManager.createCache("defaultCache", cache1Conf); @SuppressWarnings("unchecked") Configuration<Long, String> cache1CompleteConf = cache.getConfiguration(Configuration.class); assertThat(cache1CompleteConf.getKeyType(), is(equalTo(Long.class))); assertThat(cache1CompleteConf.getValueType(), is(equalTo(String.class))); }
@SuppressWarnings("unchecked") public static void testJsr107EhcacheOsgi() throws Exception { CachingProvider cachingProvider = Caching.getCachingProvider("org.ehcache.jsr107.EhcacheCachingProvider", TestMethods.class.getClassLoader()); CacheManager cacheManager = cachingProvider.getCacheManager(TestMethods.class.getResource("/org/ehcache/osgi/ehcache-107-osgi.xml").toURI(), TestMethods.class.getClassLoader()); Cache<Long, Person> personCache = cacheManager.getCache("personCache", Long.class, Person.class); assertEquals(Person.class, personCache.getConfiguration(javax.cache.configuration.Configuration.class).getValueType()); }
@Test public void testEnabledAtCacheManagerLevel() throws Exception { CacheManager cacheManager = provider.getCacheManager(getClass().getResource("/org/ehcache/docs/ehcache-107-mbeans-cache-manager-config.xml") .toURI(), provider.getDefaultClassLoader()); Cache<String, String> cache = cacheManager.getCache("stringCache", String.class, String.class); @SuppressWarnings("unchecked") Eh107Configuration<String, String> configuration = cache.getConfiguration(Eh107Configuration.class); assertThat(configuration.isManagementEnabled(), is(true)); assertThat(configuration.isStatisticsEnabled(), is(true)); assertThat(isMbeanRegistered("stringCache", MBEAN_MANAGEMENT_TYPE), is(true)); assertThat(isMbeanRegistered("stringCache", MBEAN_STATISTICS_TYPE), is(true)); }
@Test public void testCacheLevelOverridesCacheManagerLevel() throws Exception { CacheManager cacheManager = provider.getCacheManager(getClass().getResource("/org/ehcache/docs/ehcache-107-mbeans-cache-manager-config.xml") .toURI(), provider.getDefaultClassLoader()); Cache<String, String> cache = cacheManager.getCache("overrideCache", String.class, String.class); @SuppressWarnings("unchecked") Eh107Configuration<String, String> configuration = cache.getConfiguration(Eh107Configuration.class); assertThat(configuration.isManagementEnabled(), is(false)); assertThat(configuration.isStatisticsEnabled(), is(false)); assertThat(isMbeanRegistered("overrideCache", MBEAN_MANAGEMENT_TYPE), is(false)); assertThat(isMbeanRegistered("overrideCache", MBEAN_STATISTICS_TYPE), is(false)); }
@Test public void testRunTimeTypeLaxity() throws Exception { CachingProvider provider = Caching.getCachingProvider(); javax.cache.CacheManager cacheManager = provider.getCacheManager(this.getClass().getResource("/ehcache-107-types.xml").toURI(), getClass().getClassLoader()); MutableConfiguration<Long, String> cache1Conf = new MutableConfiguration<>(); cache1Conf.setTypes(Long.class, String.class); javax.cache.Cache<Long, String> cache = cacheManager.createCache("cache1", cache1Conf); @SuppressWarnings("unchecked") Configuration<Long, String> cache1CompleteConf = cache.getConfiguration(Configuration.class); assertThat(cache1CompleteConf.getKeyType(), is(equalTo(Long.class))); assertThat(cache1CompleteConf.getValueType(), is(equalTo(String.class))); try { cacheManager.getCache("cache1"); } finally { cacheManager.destroyCache("cache1"); cacheManager.close(); } }
@Test public void testCacheLevelOnlyOneOverridesCacheManagerLevel() throws Exception { CacheManager cacheManager = provider.getCacheManager(getClass().getResource("/org/ehcache/docs/ehcache-107-mbeans-cache-manager-config.xml") .toURI(), provider.getDefaultClassLoader()); Cache<String, String> cache = cacheManager.getCache("overrideOneCache", String.class, String.class); @SuppressWarnings("unchecked") Eh107Configuration<String, String> configuration = cache.getConfiguration(Eh107Configuration.class); assertThat(configuration.isManagementEnabled(), is(true)); assertThat(configuration.isStatisticsEnabled(), is(false)); assertThat(isMbeanRegistered("overrideOneCache", MBEAN_MANAGEMENT_TYPE), is(true)); assertThat(isMbeanRegistered("overrideOneCache", MBEAN_STATISTICS_TYPE), is(false)); }
@Test @SuppressWarnings("unchecked") public void testCompileTimeTypeSafety() throws Exception { CachingProvider provider = Caching.getCachingProvider(); javax.cache.CacheManager cacheManager = provider.getCacheManager(this.getClass().getResource("/ehcache-107-types.xml").toURI(), getClass().getClassLoader()); MutableConfiguration<Long, String> cache1Conf = new MutableConfiguration<>(); javax.cache.Cache<Long, String> cache = cacheManager.createCache("cache1", cache1Conf); cache.put(1l, "one"); cache.put(2l, "two"); Configuration<Object, Object> cache1CompleteConf = cache.getConfiguration(Configuration.class); //This ensures that we have compile time type safety, i.e when configuration does not have types defined but // what you get cache as should work. assertThat(cache1CompleteConf.getKeyType(), is(equalTo(Object.class))); assertThat(cache1CompleteConf.getValueType(), is(equalTo(Object.class))); assertThat(cache.get(1l), is(equalTo("one"))); assertThat(cache.get(2l), is(equalTo("two"))); javax.cache.Cache<String, String> second = cacheManager.getCache("cache1"); second.put("3","three"); assertThat(second.get("3"), is(equalTo("three"))); cacheManager.destroyCache("cache1"); cacheManager.close(); }
@Test public void testEnabledAtCacheLevel() throws Exception { CacheManager cacheManager = provider.getCacheManager(getClass().getResource("/ehcache-107-mbeans-cache-config.xml") .toURI(), provider.getDefaultClassLoader()); Cache<String, String> cache = cacheManager.getCache("stringCache", String.class, String.class); @SuppressWarnings("unchecked") Eh107Configuration<String, String> configuration = cache.getConfiguration(Eh107Configuration.class); assertThat(configuration.isManagementEnabled(), is(true)); assertThat(configuration.isStatisticsEnabled(), is(true)); assertThat(isMbeanRegistered("stringCache", MBEAN_MANAGEMENT_TYPE), is(true)); assertThat(isMbeanRegistered("stringCache", MBEAN_STATISTICS_TYPE), is(true)); }
@Test @SuppressWarnings("unchecked") public void testUsingEhcacheConfiguration() throws Exception { // tag::ehcacheBasedConfigurationExample[] CacheConfiguration<Long, String> cacheConfiguration = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, ResourcePoolsBuilder.heap(10)).build(); // <1> Cache<Long, String> cache = cacheManager.createCache("myCache", Eh107Configuration.fromEhcacheCacheConfiguration(cacheConfiguration)); // <2> Eh107Configuration<Long, String> configuration = cache.getConfiguration(Eh107Configuration.class); configuration.unwrap(CacheConfiguration.class); // <3> configuration.unwrap(CacheRuntimeConfiguration.class); // <4> try { cache.getConfiguration(CompleteConfiguration.class); // <5> throw new AssertionError("IllegalArgumentException expected"); } catch (IllegalArgumentException iaex) { // Expected } // end::ehcacheBasedConfigurationExample[] }
@Test public void testManagementDisabledOverriddenFromTemplate() throws Exception { CacheManager cacheManager = provider.getCacheManager(getClass().getResource("/ehcache-107-mbeans-template-config.xml") .toURI(), provider.getDefaultClassLoader()); MutableConfiguration<Long, String> configuration = new MutableConfiguration<>(); configuration.setTypes(Long.class, String.class); configuration.setManagementEnabled(false); configuration.setStatisticsEnabled(false); Cache<Long, String> cache = cacheManager.createCache("enables-mbeans", configuration); @SuppressWarnings("unchecked") Eh107Configuration<Long, String> eh107Configuration = cache.getConfiguration(Eh107Configuration.class); assertThat(eh107Configuration.isManagementEnabled(), is(true)); assertThat(eh107Configuration.isStatisticsEnabled(), is(true)); assertThat(isMbeanRegistered("enables-mbeans", MBEAN_MANAGEMENT_TYPE), is(true)); assertThat(isMbeanRegistered("enables-mbeans", MBEAN_STATISTICS_TYPE), is(true)); }
@Test public void testManagementEnabledOverriddenFromTemplate() throws Exception { CacheManager cacheManager = provider.getCacheManager(getClass().getResource("/ehcache-107-mbeans-template-config.xml") .toURI(), provider.getDefaultClassLoader()); MutableConfiguration<Long, String> configuration = new MutableConfiguration<>(); configuration.setTypes(Long.class, String.class); configuration.setManagementEnabled(true); configuration.setStatisticsEnabled(true); Cache<Long, String> cache = cacheManager.createCache("disables-mbeans", configuration); @SuppressWarnings("unchecked") Eh107Configuration<Long, String> eh107Configuration = cache.getConfiguration(Eh107Configuration.class); assertThat(eh107Configuration.isManagementEnabled(), is(false)); assertThat(eh107Configuration.isStatisticsEnabled(), is(false)); assertThat(isMbeanRegistered("disables-mbeans", MBEAN_MANAGEMENT_TYPE), is(false)); assertThat(isMbeanRegistered("disables-mbeans", MBEAN_STATISTICS_TYPE), is(false)); }
@Test public void testEnableCacheLevelProgrammatic() throws Exception { CacheManager cacheManager = provider.getCacheManager(); CacheConfigurationBuilder<Long, String> configurationBuilder = newCacheConfigurationBuilder(Long.class, String.class, heap(10)) .add(new Jsr107CacheConfiguration(ConfigurationElementState.ENABLED, ConfigurationElementState.ENABLED)); Cache<Long, String> cache = cacheManager.createCache("test", Eh107Configuration.fromEhcacheCacheConfiguration(configurationBuilder)); @SuppressWarnings("unchecked") Eh107Configuration<Long, String> configuration = cache.getConfiguration(Eh107Configuration.class); assertThat(configuration.isManagementEnabled(), is(true)); assertThat(configuration.isStatisticsEnabled(), is(true)); assertThat(isMbeanRegistered("test", MBEAN_MANAGEMENT_TYPE), is(true)); assertThat(isMbeanRegistered("test", MBEAN_STATISTICS_TYPE), is(true)); }
@Test public void basicJsr107StillWorks() throws Exception { CacheManager cacheManager = provider.getCacheManager(); MutableConfiguration<Long, String> configuration = new MutableConfiguration<>(); configuration.setTypes(Long.class, String.class); configuration.setManagementEnabled(true); configuration.setStatisticsEnabled(true); Cache<Long, String> cache = cacheManager.createCache("cache", configuration); @SuppressWarnings("unchecked") Eh107Configuration<Long, String> eh107Configuration = cache.getConfiguration(Eh107Configuration.class); assertThat(eh107Configuration.isManagementEnabled(), is(true)); assertThat(eh107Configuration.isStatisticsEnabled(), is(true)); assertThat(isMbeanRegistered("cache", MBEAN_MANAGEMENT_TYPE), is(true)); assertThat(isMbeanRegistered("cache", MBEAN_STATISTICS_TYPE), is(true)); } }