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.MBeanUtil;
38 import info.magnolia.module.cache.Cache;
39 import info.magnolia.module.cache.CacheFactory;
40
41 import java.util.Arrays;
42 import java.util.List;
43
44 import javax.management.MBeanServer;
45
46 import info.magnolia.module.cache.mbean.CacheMonitor;
47 import net.sf.ehcache.CacheManager;
48 import net.sf.ehcache.Ehcache;
49 import net.sf.ehcache.config.CacheConfiguration;
50 import net.sf.ehcache.config.Configuration;
51 import net.sf.ehcache.config.ConfigurationFactory;
52 import net.sf.ehcache.constructs.blocking.BlockingCache;
53 import net.sf.ehcache.management.ManagementService;
54
55
56
57
58
59
60
61 public class EhCacheFactory implements CacheFactory {
62 private final CacheMonitor cacheMonitor;
63
64 private CacheManager cacheManager;
65 private CacheConfiguration defaultCacheConfiguration;
66 private String diskStorePath;
67
68 private int blockingTimeout = 10000;
69
70 public EhCacheFactory(CacheMonitor cacheMonitor) {
71 this.cacheMonitor = cacheMonitor;
72 diskStorePath = Path.getCacheDirectoryPath();
73 }
74
75 public CacheConfiguration getDefaultCacheConfiguration() {
76 return defaultCacheConfiguration;
77 }
78
79 public void setDefaultCacheConfiguration(CacheConfiguration defaultCacheConfiguration) {
80 this.defaultCacheConfiguration = defaultCacheConfiguration;
81 }
82
83 public String getDiskStorePath() {
84 return diskStorePath;
85 }
86
87 public void setDiskStorePath(String diskStorePath) {
88 this.diskStorePath = diskStorePath;
89 }
90
91 public int getBlockingTimeout() {
92 return blockingTimeout;
93 }
94
95 public void setBlockingTimeout(int blockingTimeout) {
96 this.blockingTimeout = blockingTimeout;
97 }
98
99 public List getCacheNames() {
100 return Arrays.asList(cacheManager.getCacheNames());
101 }
102
103 @Override
104 public Cache getCache(String name) {
105 final Ehcache ehcache = cacheManager.getEhcache(name);
106 if (ehcache == null) {
107 createCache(name);
108 return getCache(name);
109 }
110 return new EhCacheWrapper(ehcache, cacheMonitor, name);
111 }
112
113 protected void createCache(String name) {
114 synchronized (this.getClass()) {
115 cacheManager.addCache(name);
116
117 final Ehcache cache = cacheManager.getEhcache(name);
118
119 final BlockingCache newBlockingCache = new BlockingCache(cache);
120 newBlockingCache.setTimeoutMillis(getBlockingTimeout());
121 cacheManager.replaceCacheWithDecoratedCache(cache, newBlockingCache);
122 }
123 }
124
125 @Override
126 public void start() {
127 final Configuration cfg = ConfigurationFactory.parseConfiguration();
128 cfg.setSource("ehcache defaults");
129 if (defaultCacheConfiguration != null) {
130 cfg.setDefaultCacheConfiguration(defaultCacheConfiguration);
131 cfg.setSource(cfg.getConfigurationSource() + " + Magnolia-based defaultCacheConfiguration");
132 }
133 if (diskStorePath != null) {
134 cfg.getDiskStoreConfiguration().setPath(diskStorePath);
135 cfg.setSource(cfg.getConfigurationSource() + " + Magnolia-based diskStorePath");
136 }
137 cacheManager = new CacheManager(cfg);
138
139
140
141 final MBeanServer mBeanServer = MBeanUtil.getMBeanServer();
142 ManagementService.registerMBeans(cacheManager, mBeanServer, true, true, true, true);
143 }
144
145 @Override
146 public void stop() {
147 cacheManager.shutdown();
148 }
149
150 public CacheManager getWrappedCacheManager() {
151 return cacheManager;
152 }
153 }