1 package fr.ifremer.quadrige3.core.dao.technical.ehcache;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 import net.sf.ehcache.CacheException;
28 import net.sf.ehcache.CacheManager;
29 import net.sf.ehcache.config.Configuration;
30 import net.sf.ehcache.config.ConfigurationFactory;
31 import net.sf.ehcache.config.DiskStoreConfiguration;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.springframework.beans.factory.DisposableBean;
35 import org.springframework.beans.factory.FactoryBean;
36 import org.springframework.beans.factory.InitializingBean;
37 import org.springframework.core.io.Resource;
38
39 import java.io.File;
40 import java.io.IOException;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 public class EhCacheManagerFactoryBean implements FactoryBean<CacheManager>, InitializingBean, DisposableBean {
73 protected final Log logger = LogFactory.getLog(getClass());
74
75 private String diskStorePath = null;
76
77 private Resource configLocation;
78
79 private boolean shared = false;
80
81 private String cacheManagerName;
82
83 private CacheManager cacheManager;
84
85
86
87
88
89
90 public String getDiskStorePath() {
91 return diskStorePath;
92 }
93
94
95
96
97
98
99 public void setDiskStorePath(String diskStorePath) {
100 this.diskStorePath = diskStorePath;
101 }
102
103
104
105
106
107
108
109
110
111
112 public void setConfigLocation(Resource configLocation) {
113 this.configLocation = configLocation;
114 }
115
116
117
118
119
120
121
122
123
124
125 public void setShared(boolean shared) {
126 this.shared = shared;
127 }
128
129
130
131
132
133
134
135 public void setCacheManagerName(String cacheManagerName) {
136 this.cacheManagerName = cacheManagerName;
137 }
138
139
140 @Override
141 public void afterPropertiesSet() throws IOException, CacheException {
142
143 Configuration config;
144
145 if (this.configLocation != null) {
146 config = ConfigurationFactory.parseConfiguration(this.configLocation.getInputStream());
147 } else {
148 config = ConfigurationFactory.parseConfiguration();
149 }
150
151 DiskStoreConfiguration diskStoreConfiguration = config.getDiskStoreConfiguration();
152 boolean noDiskStoreConfiguration = diskStoreConfiguration == null;
153 if (noDiskStoreConfiguration) {
154 diskStoreConfiguration = new DiskStoreConfiguration();
155 }
156
157
158 File diskStore = new File(getDiskStorePath());
159 if (!diskStore.isDirectory() || !diskStore.exists()) {
160 logger.info("Using EHCache disk store at " + getDiskStorePath() + " - directory not exists yet");
161 }
162 else if (logger.isDebugEnabled()) {
163 logger.debug("Using EHCache disk store at " + getDiskStorePath() + " (replace old config: " + diskStoreConfiguration.getOriginalPath()
164 + ")");
165 }
166
167 diskStoreConfiguration.setPath(getDiskStorePath());
168 if (noDiskStoreConfiguration) {
169 config.addDiskStore(diskStoreConfiguration);
170 }
171 if (this.cacheManagerName != null) {
172 config.setName(this.cacheManagerName);
173 }
174 if (this.shared) {
175 this.cacheManager = CacheManager.create(config);
176 } else {
177 this.cacheManager = new CacheManager(config);
178 }
179 }
180
181
182 @Override
183 public CacheManager getObject() {
184 return this.cacheManager;
185 }
186
187
188 @Override
189 public Class<? extends CacheManager> getObjectType() {
190 return (this.cacheManager != null ? this.cacheManager.getClass() : CacheManager.class);
191 }
192
193
194 @Override
195 public boolean isSingleton() {
196 return true;
197 }
198
199
200 @Override
201 public void destroy() {
202 logger.debug("Shutting down EHCache CacheManager");
203 this.cacheManager.shutdown();
204 }
205 }