Skip to content

Commit

Permalink
EhCacheManagerFactoryBean applies cacheManagerName ahead of creation …
Browse files Browse the repository at this point in the history
…(for EHCache 2.5 compatibility)

Issue: SPR-9171
  • Loading branch information
jhoeller committed Jan 18, 2013
1 parent cca255b commit 944e1c9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -18,16 +18,21 @@

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;

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}
Expand All @@ -54,6 +59,10 @@
*/
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;
Expand Down Expand Up @@ -98,20 +107,41 @@ public void setCacheManagerName(String cacheManagerName) {

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();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 944e1c9

Please sign in to comment.