parent
ef02cc9bff
commit
64bf33038d
@ -1,38 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.actuate.metrics.cache;
|
||||
|
||||
import io.micrometer.core.instrument.Tag;
|
||||
import io.micrometer.core.instrument.binder.MeterBinder;
|
||||
import io.micrometer.core.instrument.binder.cache.EhCache2Metrics;
|
||||
|
||||
import org.springframework.cache.ehcache.EhCacheCache;
|
||||
|
||||
/**
|
||||
* {@link CacheMeterBinderProvider} implementation for EhCache2.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public class EhCache2CacheMeterBinderProvider implements CacheMeterBinderProvider<EhCacheCache> {
|
||||
|
||||
@Override
|
||||
public MeterBinder getMeterBinder(EhCacheCache cache, Iterable<Tag> tags) {
|
||||
return new EhCache2Metrics(cache.getNativeCache(), tags);
|
||||
}
|
||||
|
||||
}
|
@ -1,57 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.actuate.metrics.cache;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import io.micrometer.core.instrument.binder.MeterBinder;
|
||||
import io.micrometer.core.instrument.binder.cache.EhCache2Metrics;
|
||||
import net.sf.ehcache.Cache;
|
||||
import net.sf.ehcache.CacheManager;
|
||||
import net.sf.ehcache.config.CacheConfiguration;
|
||||
import net.sf.ehcache.config.Configuration;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.cache.ehcache.EhCacheCache;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link EhCache2CacheMeterBinderProvider}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
class EhCache2CacheMeterBinderProviderTests {
|
||||
|
||||
@Test
|
||||
void ehCache2CacheProvider() {
|
||||
CacheManager cacheManager = new CacheManager(
|
||||
new Configuration().name("EhCacheCacheTests").defaultCache(new CacheConfiguration("default", 100)));
|
||||
try {
|
||||
Cache nativeCache = new Cache(new CacheConfiguration("test", 100));
|
||||
cacheManager.addCache(nativeCache);
|
||||
EhCacheCache cache = new EhCacheCache(nativeCache);
|
||||
MeterBinder meterBinder = new EhCache2CacheMeterBinderProvider().getMeterBinder(cache,
|
||||
Collections.emptyList());
|
||||
assertThat(meterBinder).isInstanceOf(EhCache2Metrics.class);
|
||||
}
|
||||
finally {
|
||||
cacheManager.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,74 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.cache;
|
||||
|
||||
import net.sf.ehcache.Cache;
|
||||
import net.sf.ehcache.CacheManager;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ResourceCondition;
|
||||
import org.springframework.cache.ehcache.EhCacheCacheManager;
|
||||
import org.springframework.cache.ehcache.EhCacheManagerUtils;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
/**
|
||||
* EhCache cache configuration. Only kick in if a configuration file location is set or if
|
||||
* a default configuration file exists.
|
||||
*
|
||||
* @author Eddú Meléndez
|
||||
* @author Stephane Nicoll
|
||||
* @author Madhura Bhave
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnClass({ Cache.class, EhCacheCacheManager.class })
|
||||
@ConditionalOnMissingBean(org.springframework.cache.CacheManager.class)
|
||||
@Conditional({ CacheCondition.class, EhCacheCacheConfiguration.ConfigAvailableCondition.class })
|
||||
class EhCacheCacheConfiguration {
|
||||
|
||||
@Bean
|
||||
EhCacheCacheManager cacheManager(CacheManagerCustomizers customizers, CacheManager ehCacheCacheManager) {
|
||||
return customizers.customize(new EhCacheCacheManager(ehCacheCacheManager));
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
CacheManager ehCacheCacheManager(CacheProperties cacheProperties) {
|
||||
Resource location = cacheProperties.resolveConfigLocation(cacheProperties.getEhcache().getConfig());
|
||||
if (location != null) {
|
||||
return EhCacheManagerUtils.buildCacheManager(location);
|
||||
}
|
||||
return EhCacheManagerUtils.buildCacheManager();
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the EhCache configuration is available. This either kick in if a
|
||||
* default configuration has been found or if property referring to the file to use
|
||||
* has been set.
|
||||
*/
|
||||
static class ConfigAvailableCondition extends ResourceCondition {
|
||||
|
||||
ConfigAvailableCondition() {
|
||||
super("EhCache", "spring.cache.ehcache.config", "classpath:/ehcache.xml");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,81 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.cache;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.autoconfigure.cache.CacheAutoConfigurationTests.DefaultCacheAndCustomizersConfiguration;
|
||||
import org.springframework.boot.autoconfigure.cache.CacheAutoConfigurationTests.DefaultCacheConfiguration;
|
||||
import org.springframework.boot.autoconfigure.cache.CacheAutoConfigurationTests.EhCacheCustomCacheManager;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
|
||||
import org.springframework.cache.ehcache.EhCacheCacheManager;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link CacheAutoConfiguration} with EhCache 2.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
@ClassPathExclusions("ehcache-3*.jar")
|
||||
class EhCache2CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(CacheAutoConfiguration.class));
|
||||
|
||||
@Test
|
||||
void ehCacheWithCaches() {
|
||||
this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class)
|
||||
.withPropertyValues("spring.cache.type=ehcache").run((context) -> {
|
||||
EhCacheCacheManager cacheManager = getCacheManager(context, EhCacheCacheManager.class);
|
||||
assertThat(cacheManager.getCacheNames()).containsOnly("cacheTest1", "cacheTest2");
|
||||
assertThat(context.getBean(net.sf.ehcache.CacheManager.class))
|
||||
.isEqualTo(cacheManager.getCacheManager());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void ehCacheWithCustomizers() {
|
||||
this.contextRunner.withUserConfiguration(DefaultCacheAndCustomizersConfiguration.class)
|
||||
.withPropertyValues("spring.cache.type=ehcache")
|
||||
.run(verifyCustomizers("allCacheManagerCustomizer", "ehcacheCacheManagerCustomizer"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void ehCacheWithConfig() {
|
||||
this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class)
|
||||
.withPropertyValues("spring.cache.type=ehcache",
|
||||
"spring.cache.ehcache.config=cache/ehcache-override.xml")
|
||||
.run((context) -> {
|
||||
EhCacheCacheManager cacheManager = getCacheManager(context, EhCacheCacheManager.class);
|
||||
assertThat(cacheManager.getCacheNames()).containsOnly("cacheOverrideTest1", "cacheOverrideTest2");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void ehCacheWithExistingCacheManager() {
|
||||
this.contextRunner.withUserConfiguration(EhCacheCustomCacheManager.class)
|
||||
.withPropertyValues("spring.cache.type=ehcache").run((context) -> {
|
||||
EhCacheCacheManager cacheManager = getCacheManager(context, EhCacheCacheManager.class);
|
||||
assertThat(cacheManager.getCacheManager()).isEqualTo(context.getBean("customEhCacheCacheManager"));
|
||||
});
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue