1 |
|
|
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 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
package info.magnolia.module.cache.ehcache; |
35 |
|
|
36 |
|
import info.magnolia.cms.core.Path; |
37 |
|
import info.magnolia.cms.util.DeprecationUtil; |
38 |
|
import info.magnolia.cms.util.MBeanUtil; |
39 |
|
import info.magnolia.init.MagnoliaConfigurationProperties; |
40 |
|
import info.magnolia.init.MagnoliaInitPaths; |
41 |
|
import info.magnolia.module.cache.Cache; |
42 |
|
import info.magnolia.module.cache.CacheFactory; |
43 |
|
import info.magnolia.module.cache.CacheModule; |
44 |
|
import info.magnolia.module.cache.mbean.CacheMonitor; |
45 |
|
import info.magnolia.objectfactory.Components; |
46 |
|
|
47 |
|
import java.util.Arrays; |
48 |
|
import java.util.HashMap; |
49 |
|
import java.util.List; |
50 |
|
import java.util.Map; |
51 |
|
|
52 |
|
import javax.inject.Inject; |
53 |
|
import javax.management.MBeanServer; |
54 |
|
|
55 |
|
import org.apache.commons.lang3.StringUtils; |
56 |
|
|
57 |
|
import net.sf.ehcache.CacheManager; |
58 |
|
import net.sf.ehcache.Ehcache; |
59 |
|
import net.sf.ehcache.config.Configuration; |
60 |
|
import net.sf.ehcache.config.DiskStoreConfiguration; |
61 |
|
import net.sf.ehcache.config.generator.ConfigurationSource; |
62 |
|
import net.sf.ehcache.constructs.blocking.BlockingCache; |
63 |
|
import net.sf.ehcache.management.ManagementService; |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
@deprecated@link |
69 |
|
|
70 |
|
@Deprecated |
|
|
| 82.5% |
Uncovered Elements: 17 (97) |
Complexity: 25 |
Complexity Density: 0.42 |
|
71 |
|
public class EhCacheFactory implements CacheFactory { |
72 |
|
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(EhCacheFactory.class); |
73 |
|
|
74 |
|
|
75 |
|
protected static final String CACHE_MANAGER_ID_INVALID_CHARS = "*?=:,\n\""; |
76 |
|
protected static final String MAGNOLIA_PROPERTY_CACHE_MANAGER_ID = "magnolia.cache.manager.id"; |
77 |
|
|
78 |
|
private CacheManager cacheManager; |
79 |
|
private Map<String, EhCacheConfiguration> caches = new HashMap<String, EhCacheConfiguration>(); |
80 |
|
|
81 |
|
private int blockingTimeout = 10000; |
82 |
|
private String diskStorePath; |
83 |
|
|
84 |
|
private final CacheMonitor cacheMonitor; |
85 |
|
private final CacheModule cacheModule; |
86 |
|
private final MagnoliaInitPaths magnoliaInitPaths; |
87 |
|
private final String cacheManagerId; |
88 |
|
private final MagnoliaConfigurationProperties magnoliaConfigurationProperties; |
89 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
90 |
34 |
@Inject... |
91 |
|
public EhCacheFactory(CacheMonitor cacheMonitor, CacheModule cacheModule, MagnoliaInitPaths magnoliaInitPaths, MagnoliaConfigurationProperties magnoliaConfigurationProperties) { |
92 |
34 |
this.cacheMonitor = cacheMonitor; |
93 |
34 |
this.cacheModule = cacheModule; |
94 |
34 |
this.magnoliaInitPaths = magnoliaInitPaths; |
95 |
34 |
this.magnoliaConfigurationProperties = magnoliaConfigurationProperties; |
96 |
34 |
this.diskStorePath = Path.getCacheDirectoryPath(); |
97 |
34 |
this.cacheManagerId = this.createCacheManagerIdentifier(); |
98 |
|
} |
99 |
|
|
100 |
|
|
101 |
|
@deprecated@link |
102 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
103 |
0 |
@Deprecated... |
104 |
|
public EhCacheFactory(CacheMonitor cacheMonitor, CacheModule cacheModule, MagnoliaInitPaths magnoliaInitPaths) { |
105 |
0 |
this(cacheMonitor, cacheModule, magnoliaInitPaths, Components.getComponent(MagnoliaConfigurationProperties.class)); |
106 |
|
} |
107 |
|
|
108 |
|
|
109 |
|
@deprecated@link |
110 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
111 |
0 |
public EhCacheFactory(CacheMonitor cacheMonitor) {... |
112 |
0 |
this(cacheMonitor, Components.getComponent(CacheModule.class), Components.getComponent(MagnoliaInitPaths.class), Components.getComponent(MagnoliaConfigurationProperties.class)); |
113 |
|
} |
114 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
115 |
|
@Deprecated... |
116 |
|
public EhCacheConfiguration getDefaultCacheConfiguration() { |
117 |
|
DeprecationUtil.isDeprecated("Code should not be calling info.magnolia.module.cache.ehcache.EhCacheFactory#getDefaultCacheConfiguration"); |
118 |
|
return getCaches().get(DEFAULT_CACHE_NAME); |
119 |
|
} |
120 |
|
|
121 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
122 |
0 |
@Deprecated... |
123 |
|
public void setDefaultCacheConfiguration(EhCacheConfiguration defaultCacheConfiguration) { |
124 |
0 |
if (!isDefaultCacheName(defaultCacheConfiguration.getName())) { |
125 |
0 |
log.warn("Calling setDefaultCacheConfiguration with a CacheConfiguration of name " + defaultCacheConfiguration.getName() + "; it should be unnamed."); |
126 |
|
} |
127 |
0 |
if (!getCaches().containsKey(DEFAULT_CACHE_NAME)) { |
128 |
0 |
DeprecationUtil.isDeprecated("Configure your default cache settings under modules/cache/configuration/cacheFactory/caches/default rather than modules/cache/configuration/cacheFactory/defaultCacheConfiguration"); |
129 |
0 |
getCaches().put(DEFAULT_CACHE_NAME, defaultCacheConfiguration); |
130 |
|
} else { |
131 |
0 |
log.warn("You already have a configuration at modules/cache/configuration/cacheFactory/caches/default, ignoring modules/cache/configuration/cacheFactory/defaultCacheConfiguration"); |
132 |
|
} |
133 |
|
} |
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
138 |
|
public Map<String, EhCacheConfiguration> getCaches() {... |
139 |
|
return caches; |
140 |
|
} |
141 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
142 |
|
public void setCaches(Map<String, EhCacheConfiguration> caches) {... |
143 |
|
this.caches = caches; |
144 |
|
} |
145 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
146 |
|
public String getDiskStorePath() {... |
147 |
|
return diskStorePath; |
148 |
|
} |
149 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
150 |
|
public void setDiskStorePath(String diskStorePath) {... |
151 |
|
this.diskStorePath = diskStorePath; |
152 |
|
} |
153 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
154 |
|
public int getBlockingTimeout() {... |
155 |
|
return blockingTimeout; |
156 |
|
} |
157 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
158 |
|
public void setBlockingTimeout(int blockingTimeout) {... |
159 |
|
this.blockingTimeout = blockingTimeout; |
160 |
|
} |
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
165 |
|
@Override... |
166 |
|
public List getCacheNames() { |
167 |
|
return Arrays.asList(cacheManager.getCacheNames()); |
168 |
|
} |
169 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (15) |
Complexity: 4 |
Complexity Density: 0.44 |
|
170 |
46 |
@Override... |
171 |
|
public Cache getCache(String name) { |
172 |
46 |
if (isDefaultCacheName(name)) { |
173 |
1 |
throw new IllegalArgumentException("'" + name + "' is not a valid cache name."); |
174 |
|
} |
175 |
45 |
Ehcache ehcache = cacheManager.getEhcache(name); |
176 |
|
|
177 |
|
|
178 |
45 |
if (ehcache == null) { |
179 |
|
|
180 |
21 |
createCache(name); |
181 |
21 |
return getCache(name); |
182 |
|
} |
183 |
|
|
184 |
|
|
185 |
24 |
if (!(ehcache instanceof BlockingCache)) { |
186 |
23 |
ehcache = decorateAndSubstitute(ehcache); |
187 |
|
} |
188 |
24 |
return wrap(name, ehcache); |
189 |
|
} |
190 |
|
|
191 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
192 |
22 |
protected void createCache(String name) {... |
193 |
22 |
synchronized (this.getClass()) { |
194 |
22 |
cacheManager.addCache(name); |
195 |
|
} |
196 |
|
} |
197 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
198 |
23 |
protected BlockingCache decorateAndSubstitute(Ehcache ehcache) {... |
199 |
23 |
synchronized (this.getClass()) { |
200 |
23 |
BlockingCache newBlockingCache = new BlockingCache(ehcache); |
201 |
23 |
newBlockingCache.setTimeoutMillis(getBlockingTimeout()); |
202 |
23 |
cacheManager.replaceCacheWithDecoratedCache(ehcache, newBlockingCache); |
203 |
23 |
return newBlockingCache; |
204 |
|
} |
205 |
|
} |
206 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
207 |
24 |
protected EhCacheWrapper wrap(String name, Ehcache ehcache) {... |
208 |
24 |
return new EhCacheWrapper(cacheModule, ehcache, cacheMonitor, name); |
209 |
|
} |
210 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
211 |
67 |
private boolean isDefaultCacheName(String name) {... |
212 |
67 |
return DEFAULT_CACHE_NAME.equals(name) || name == null; |
213 |
|
} |
214 |
|
|
|
|
| 96.9% |
Uncovered Elements: 1 (32) |
Complexity: 6 |
Complexity Density: 0.27 |
|
215 |
28 |
@Override... |
216 |
|
public void start(boolean isRestart) { |
217 |
28 |
final Configuration cfg = new Configuration(); |
218 |
|
|
219 |
|
|
220 |
28 |
for (EhCacheConfiguration cacheConfig : caches.values()) { |
221 |
|
|
222 |
21 |
if (!isDefaultCacheName(cacheConfig.getName())) { |
223 |
2 |
cfg.addCache(cacheConfig); |
224 |
|
} |
225 |
|
} |
226 |
|
|
227 |
28 |
EhCacheConfiguration defaultCacheConfiguration = caches.get(DEFAULT_CACHE_NAME); |
228 |
28 |
if (defaultCacheConfiguration != null) { |
229 |
|
|
230 |
19 |
defaultCacheConfiguration.resetName(); |
231 |
|
} else { |
232 |
9 |
log.warn("Default cache configuration is not set."); |
233 |
9 |
defaultCacheConfiguration = new EhCacheConfiguration(); |
234 |
9 |
defaultCacheConfiguration.setMaxEntriesLocalHeap(10000); |
235 |
|
} |
236 |
28 |
cfg.setDefaultCacheConfiguration(defaultCacheConfiguration); |
237 |
|
|
238 |
|
|
239 |
28 |
if (!caches.isEmpty()) { |
240 |
20 |
cfg.setSource(new DescribingConfigurationSource("Magnolia-based cache configuration")); |
241 |
|
} |
242 |
28 |
if (diskStorePath != null) { |
243 |
28 |
cfg.diskStore(new DiskStoreConfiguration().path(diskStorePath)); |
244 |
28 |
cfg.setSource(new DescribingConfigurationSource("Magnolia-based diskStorePath")); |
245 |
|
} |
246 |
28 |
cfg.setName(cacheManagerId); |
247 |
28 |
cacheManager = CacheManager.getCacheManager(cfg.getName()); |
248 |
28 |
if (shouldRegisterCacheManager()) { |
249 |
26 |
cacheManager = new CacheManager(cfg); |
250 |
26 |
final MBeanServer mBeanServer = MBeanUtil.getMBeanServer(); |
251 |
26 |
ManagementService.registerMBeans(cacheManager, mBeanServer, true, true, true, true, false); |
252 |
|
} |
253 |
|
} |
254 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
255 |
24 |
protected boolean shouldRegisterCacheManager() {... |
256 |
24 |
return cacheManager == null; |
257 |
|
} |
258 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
259 |
34 |
protected String createCacheManagerIdentifier() {... |
260 |
34 |
if (magnoliaConfigurationProperties.hasProperty(MAGNOLIA_PROPERTY_CACHE_MANAGER_ID)) { |
261 |
2 |
return magnoliaConfigurationProperties.getProperty(MAGNOLIA_PROPERTY_CACHE_MANAGER_ID); |
262 |
|
} else { |
263 |
32 |
return StringUtils.replaceChars(magnoliaInitPaths.getRootPath(), CACHE_MANAGER_ID_INVALID_CHARS, "-") + "#" + getClass().getName() + "#cacheManager"; |
264 |
|
} |
265 |
|
} |
266 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
267 |
21 |
@Override... |
268 |
|
public void stop(boolean isRestart) { |
269 |
21 |
if (!isRestart) { |
270 |
21 |
cacheManager.shutdown(); |
271 |
|
} |
272 |
|
} |
273 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
274 |
|
public CacheManager getWrappedCacheManager() {... |
275 |
|
return cacheManager; |
276 |
|
} |
277 |
|
|
|
|
| 33.3% |
Uncovered Elements: 4 (6) |
Complexity: 3 |
Complexity Density: 1 |
|
278 |
|
private static final class DescribingConfigurationSource extends ConfigurationSource { |
279 |
|
private final String description; |
280 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
281 |
48 |
private DescribingConfigurationSource(String description) {... |
282 |
48 |
this.description = description; |
283 |
|
} |
284 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
285 |
0 |
@Override... |
286 |
|
public Configuration createConfiguration() { |
287 |
0 |
throw new IllegalStateException("not implemented - not needed"); |
288 |
|
} |
289 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
290 |
0 |
@Override... |
291 |
|
public String toString() { |
292 |
0 |
return description; |
293 |
|
} |
294 |
|
} |
295 |
|
} |