Browse Source

EhCacheManagerFactoryBean applies cacheManagerName ahead of creation (for EHCache 2.5 compatibility)

Issue: SPR-9171
pull/216/head
Juergen Hoeller 12 years ago
parent
commit
944e1c95e6
  1. 54
      spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheManagerFactoryBean.java
  2. 3
      spring-context-support/src/test/java/org/springframework/cache/ehcache/EhCacheSupportTests.java

54
spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheManagerFactoryBean.java vendored

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2013 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.
@ -18,9 +18,12 @@ package org.springframework.cache.ehcache; @@ -18,9 +18,12 @@ package org.springframework.cache.ehcache;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.config.Configuration;
import net.sf.ehcache.config.ConfigurationFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -28,6 +31,8 @@ import org.springframework.beans.factory.DisposableBean; @@ -28,6 +31,8 @@ import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
/**
* {@link FactoryBean} that exposes an EHCache {@link net.sf.ehcache.CacheManager}
@ -54,6 +59,10 @@ import org.springframework.core.io.Resource; @@ -54,6 +59,10 @@ import org.springframework.core.io.Resource;
*/
public class EhCacheManagerFactoryBean implements FactoryBean<CacheManager>, InitializingBean, DisposableBean {
// Check whether EHCache 2.1+ CacheManager.create(Configuration) method is available...
private static final Method createWithConfiguration =
ClassUtils.getMethodIfAvailable(CacheManager.class, "create", Configuration.class);
protected final Log logger = LogFactory.getLog(getClass());
private Resource configLocation;
@ -98,20 +107,41 @@ public class EhCacheManagerFactoryBean implements FactoryBean<CacheManager>, Ini @@ -98,20 +107,41 @@ public class EhCacheManagerFactoryBean implements FactoryBean<CacheManager>, Ini
public void afterPropertiesSet() throws IOException, CacheException {
logger.info("Initializing EHCache CacheManager");
if (this.configLocation != null) {
InputStream is = this.configLocation.getInputStream();
try {
this.cacheManager = (this.shared ? CacheManager.create(is) : new CacheManager(is));
InputStream is = (this.configLocation != null ? this.configLocation.getInputStream() : null);
try {
// A bit convoluted for EHCache 1.x/2.0 compatibility.
// To be much simpler once we require EHCache 2.1+
if (this.cacheManagerName != null) {
if (this.shared && createWithConfiguration == null) {
// No CacheManager.create(Configuration) method available before EHCache 2.1;
// can only set CacheManager name after creation.
this.cacheManager = (is != null ? CacheManager.create(is) : CacheManager.create());
this.cacheManager.setName(this.cacheManagerName);
}
else {
Configuration configuration = (is != null ? ConfigurationFactory.parseConfiguration(is) :
ConfigurationFactory.parseConfiguration());
configuration.setName(this.cacheManagerName);
if (this.shared) {
this.cacheManager = (CacheManager) ReflectionUtils.invokeMethod(createWithConfiguration, null, configuration);
}
else {
this.cacheManager = new CacheManager(configuration);
}
}
}
finally {
is.close();
// For strict backwards compatibility: use simplest possible constructors...
else if (this.shared) {
this.cacheManager = (is != null ? CacheManager.create(is) : CacheManager.create());
}
else {
this.cacheManager = (is != null ? new CacheManager(is) : new CacheManager());
}
}
else {
this.cacheManager = (this.shared ? CacheManager.create() : new CacheManager());
}
if (this.cacheManagerName != null) {
this.cacheManager.setName(this.cacheManagerName);
finally {
if (is != null) {
is.close();
}
}
}

3
spring-context-support/src/test/java/org/springframework/cache/ehcache/EhCacheSupportTests.java vendored

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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.
@ -38,6 +38,7 @@ public class EhCacheSupportTests extends TestCase { @@ -38,6 +38,7 @@ public class EhCacheSupportTests extends TestCase {
public void testLoadingBlankCacheManager() throws Exception {
EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean();
cacheManagerFb.setCacheManagerName("myCacheManager");
assertEquals(CacheManager.class, cacheManagerFb.getObjectType());
assertTrue("Singleton property", cacheManagerFb.isSingleton());
cacheManagerFb.afterPropertiesSet();

Loading…
Cancel
Save