Skip to content

Commit 0d0d713

Browse files
committed
Convenient EhCache CacheManager bootstrapping within @bean methods
Also introducing default CacheManager setup for EhCacheCacheManager, analogous to JCacheCacheManager. Issue: SPR-12093
1 parent b910ff3 commit 0d0d713

File tree

4 files changed

+163
-47
lines changed

4 files changed

+163
-47
lines changed

spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheCacheManager.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,25 @@ public net.sf.ehcache.CacheManager getCacheManager() {
6969
return this.cacheManager;
7070
}
7171

72+
@Override
73+
public void afterPropertiesSet() {
74+
if (getCacheManager() == null) {
75+
setCacheManager(EhCacheManagerUtils.buildCacheManager());
76+
}
77+
super.afterPropertiesSet();
78+
}
79+
7280

7381
@Override
7482
protected Collection<Cache> loadCaches() {
75-
net.sf.ehcache.CacheManager cacheManager = getCacheManager();
76-
Assert.notNull(cacheManager, "A backing EhCache CacheManager is required");
77-
Status status = cacheManager.getStatus();
83+
Status status = getCacheManager().getStatus();
7884
Assert.isTrue(Status.STATUS_ALIVE.equals(status),
7985
"An 'alive' EhCache CacheManager is required - current cache is " + status.toString());
8086

81-
String[] names = cacheManager.getCacheNames();
87+
String[] names = getCacheManager().getCacheNames();
8288
Collection<Cache> caches = new LinkedHashSet<Cache>(names.length);
8389
for (String name : names) {
84-
caches.add(new EhCacheCache(cacheManager.getEhcache(name)));
90+
caches.add(new EhCacheCache(getCacheManager().getEhcache(name)));
8591
}
8692
return caches;
8793
}

spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheFactoryBean.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.cache.ehcache;
1818

19-
import java.io.IOException;
2019
import java.lang.reflect.Method;
2120
import java.util.Set;
2221

@@ -231,7 +230,7 @@ public void setBeanName(String name) {
231230

232231

233232
@Override
234-
public void afterPropertiesSet() throws CacheException, IOException {
233+
public void afterPropertiesSet() throws CacheException {
235234
// If no cache name given, use bean name as cache name.
236235
String cacheName = getName();
237236
if (cacheName == null) {

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

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616

1717
package org.springframework.cache.ehcache;
1818

19-
import java.io.IOException;
20-
import java.io.InputStream;
21-
2219
import net.sf.ehcache.CacheException;
2320
import net.sf.ehcache.CacheManager;
2421
import net.sf.ehcache.config.Configuration;
@@ -97,9 +94,6 @@ public void setCacheManagerName(String cacheManagerName) {
9794
* but will simply work with the default CacheManager name if none specified.
9895
* All references to the same CacheManager name (or the same default) in the
9996
* same ClassLoader space will share the specified CacheManager then.
100-
* <p><b>NOTE:</b> This feature requires EhCache 2.5 or higher. In contrast to
101-
* the {@link #setShared "shared"} flag, it supports controlled shutdown of the
102-
* CacheManager by the EhCacheManagerFactoryBean that actually created it.
10397
* @see #setCacheManagerName
10498
* #see #setShared
10599
* @see net.sf.ehcache.CacheManager#getCacheManager(String)
@@ -131,44 +125,36 @@ public void setShared(boolean shared) {
131125

132126

133127
@Override
134-
public void afterPropertiesSet() throws IOException, CacheException {
128+
public void afterPropertiesSet() throws CacheException {
135129
logger.info("Initializing EhCache CacheManager");
136-
InputStream is = (this.configLocation != null ? this.configLocation.getInputStream() : null);
137-
try {
138-
Configuration configuration = (is != null ?
139-
ConfigurationFactory.parseConfiguration(is) : ConfigurationFactory.parseConfiguration());
140-
if (this.cacheManagerName != null) {
141-
configuration.setName(this.cacheManagerName);
142-
}
143-
if (this.shared) {
144-
// Old-school EhCache singleton sharing...
145-
// No way to find out whether we actually created a new CacheManager
146-
// or just received an existing singleton reference.
147-
this.cacheManager = CacheManager.create(configuration);
148-
}
149-
else if (this.acceptExisting) {
150-
// EhCache 2.5+: Reusing an existing CacheManager of the same name.
151-
// Basically the same code as in CacheManager.getInstance(String),
152-
// just storing whether we're dealing with an existing instance.
153-
synchronized (CacheManager.class) {
154-
this.cacheManager = CacheManager.getCacheManager(this.cacheManagerName);
155-
if (this.cacheManager == null) {
156-
this.cacheManager = new CacheManager(configuration);
157-
}
158-
else {
159-
this.locallyManaged = false;
160-
}
130+
Configuration configuration = (this.configLocation != null ?
131+
EhCacheManagerUtils.parseConfiguration(this.configLocation) : ConfigurationFactory.parseConfiguration());
132+
if (this.cacheManagerName != null) {
133+
configuration.setName(this.cacheManagerName);
134+
}
135+
if (this.shared) {
136+
// Old-school EhCache singleton sharing...
137+
// No way to find out whether we actually created a new CacheManager
138+
// or just received an existing singleton reference.
139+
this.cacheManager = CacheManager.create(configuration);
140+
}
141+
else if (this.acceptExisting) {
142+
// EhCache 2.5+: Reusing an existing CacheManager of the same name.
143+
// Basically the same code as in CacheManager.getInstance(String),
144+
// just storing whether we're dealing with an existing instance.
145+
synchronized (CacheManager.class) {
146+
this.cacheManager = CacheManager.getCacheManager(this.cacheManagerName);
147+
if (this.cacheManager == null) {
148+
this.cacheManager = new CacheManager(configuration);
149+
}
150+
else {
151+
this.locallyManaged = false;
161152
}
162-
}
163-
else {
164-
// Throwing an exception if a CacheManager of the same name exists already...
165-
this.cacheManager = new CacheManager(configuration);
166153
}
167154
}
168-
finally {
169-
if (is != null) {
170-
is.close();
171-
}
155+
else {
156+
// Throwing an exception if a CacheManager of the same name exists already...
157+
this.cacheManager = new CacheManager(configuration);
172158
}
173159
}
174160

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Copyright 2002-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cache.ehcache;
18+
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
22+
import net.sf.ehcache.CacheException;
23+
import net.sf.ehcache.CacheManager;
24+
import net.sf.ehcache.config.Configuration;
25+
import net.sf.ehcache.config.ConfigurationFactory;
26+
27+
import org.springframework.core.io.Resource;
28+
29+
/**
30+
* Convenient builder methods for EhCache 2.5+ {@link CacheManager} setup,
31+
* providing easy programmatic bootstrapping from a Spring-provided resource.
32+
* This is primarily intended for use within {@code @Bean} methods in a
33+
* Spring configuration class.
34+
*
35+
* <p>These methods are a simple alternative to custom {@link CacheManager} setup
36+
* code. For any advanced purposes, consider using {@link #parseConfiguration},
37+
* customizing the configuration object, and then calling the
38+
* {@link CacheManager#CacheManager(Configuration)} constructor.
39+
*
40+
* @author Juergen Hoeller
41+
* @since 4.1
42+
*/
43+
public abstract class EhCacheManagerUtils {
44+
45+
/**
46+
* Build an EhCache {@link CacheManager} from the default configuration.
47+
* <p>The CacheManager will be configured from "ehcache.xml" in the root of the class path
48+
* (that is, default EhCache initialization - as defined in the EhCache docs - will apply).
49+
* If no configuration file can be found, a fail-safe fallback configuration will be used.
50+
* @return the new EhCache CacheManager
51+
* @throws CacheException in case of configuration parsing failure
52+
*/
53+
public static CacheManager buildCacheManager() throws CacheException {
54+
return new CacheManager(ConfigurationFactory.parseConfiguration());
55+
}
56+
57+
/**
58+
* Build an EhCache {@link CacheManager} from the default configuration.
59+
* <p>The CacheManager will be configured from "ehcache.xml" in the root of the class path
60+
* (that is, default EhCache initialization - as defined in the EhCache docs - will apply).
61+
* If no configuration file can be found, a fail-safe fallback configuration will be used.
62+
* @param name the desired name of the cache manager
63+
* @return the new EhCache CacheManager
64+
* @throws CacheException in case of configuration parsing failure
65+
*/
66+
public static CacheManager buildCacheManager(String name) throws CacheException {
67+
Configuration configuration = ConfigurationFactory.parseConfiguration();
68+
configuration.setName(name);
69+
return new CacheManager(configuration);
70+
}
71+
72+
/**
73+
* Build an EhCache {@link CacheManager} from the given configuration resource.
74+
* @param configLocation the location of the configuration file (as a Spring resource)
75+
* @return the new EhCache CacheManager
76+
* @throws CacheException in case of configuration parsing failure
77+
*/
78+
public static CacheManager buildCacheManager(Resource configLocation) throws CacheException {
79+
return new CacheManager(parseConfiguration(configLocation));
80+
}
81+
82+
/**
83+
* Build an EhCache {@link CacheManager} from the given configuration resource.
84+
* @param name the desired name of the cache manager
85+
* @param configLocation the location of the configuration file (as a Spring resource)
86+
* @return the new EhCache CacheManager
87+
* @throws CacheException in case of configuration parsing failure
88+
*/
89+
public static CacheManager buildCacheManager(String name, Resource configLocation) throws CacheException {
90+
Configuration configuration = parseConfiguration(configLocation);
91+
configuration.setName(name);
92+
return new CacheManager(configuration);
93+
}
94+
95+
/**
96+
* Parse EhCache configuration from the given resource, for further use with
97+
* custom {@link CacheManager} creation.
98+
* @param configLocation the location of the configuration file (as a Spring resource)
99+
* @return the EhCache Configuration handle
100+
* @throws CacheException in case of configuration parsing failure
101+
* @see CacheManager#CacheManager(Configuration)
102+
* @see CacheManager#create(Configuration)
103+
*/
104+
public static Configuration parseConfiguration(Resource configLocation) throws CacheException {
105+
InputStream is = null;
106+
try {
107+
is = configLocation.getInputStream();
108+
return ConfigurationFactory.parseConfiguration(is);
109+
}
110+
catch (IOException ex) {
111+
throw new CacheException("Failed to parse EhCache configuration resource", ex);
112+
}
113+
finally {
114+
if (is != null) {
115+
try {
116+
is.close();
117+
}
118+
catch (IOException ex) {
119+
// ignore
120+
}
121+
}
122+
}
123+
}
124+
125+
}

0 commit comments

Comments
 (0)