1
19
20 package com.liferay.portal.cache;
21
22 import com.liferay.portal.kernel.cache.PortalCache;
23 import com.liferay.portal.kernel.cache.PortalCacheManager;
24 import com.liferay.portal.util.PropsUtil;
25
26 import java.net.URL;
27
28 import javax.management.MBeanServer;
29
30 import net.sf.ehcache.CacheManager;
31 import net.sf.ehcache.Ehcache;
32 import net.sf.ehcache.ObjectExistsException;
33 import net.sf.ehcache.constructs.blocking.BlockingCache;
34 import net.sf.ehcache.management.ManagementService;
35
36
44 public class EhcachePortalCacheManager implements PortalCacheManager {
45
46 public void afterPropertiesSet() {
47 URL url = getClass().getResource(PropsUtil.get(_configPropertyKey));
48
49 _cacheManager = new CacheManager(url);
50
51 ManagementService.registerMBeans(
52 _cacheManager, _mbeanServer, _registerCacheManager, _registerCaches,
53 _registerCacheConfigurations, _registerCacheStatistics);
54 }
55
56 public void clearAll() {
57 _cacheManager.clearAll();
58 }
59
60 public void destroy() throws Exception {
61 _cacheManager.shutdown();
62 }
63
64 public PortalCache getCache(String name) {
65 return getCache(name, false);
66 }
67
68 public PortalCache getCache(String name, boolean blocking) {
69 Ehcache cache = _cacheManager.getEhcache(name);
70
71 if (cache == null) {
72 try {
73 _cacheManager.addCache(name);
74 }
75 catch (ObjectExistsException oee) {
76
77
79 }
80
81 cache = _cacheManager.getEhcache(name);
82
83 if (blocking) {
84 cache = replaceCacheWithDecoratedCache(cache);
85 }
86 }
87
88 return new EhcachePortalCache(cache);
89 }
90
91 public void setConfigPropertyKey(String configPropertyKey) {
92 _configPropertyKey = configPropertyKey;
93 }
94
95 public void setMBeanServer(MBeanServer server) {
96 _mbeanServer = server;
97 }
98
99 public void setRegisterCacheConfigurations(
100 boolean registerCacheConfigurations) {
101
102 _registerCacheConfigurations = registerCacheConfigurations;
103 }
104
105 public void setRegisterCacheManager(boolean registerCacheManager) {
106 _registerCacheManager = registerCacheManager;
107 }
108
109 public void setRegisterCaches(boolean registerCaches) {
110 _registerCaches = registerCaches;
111 }
112
113 public void setRegisterCacheStatistics(boolean registerCacheStatistics) {
114 _registerCacheStatistics = registerCacheStatistics;
115 }
116
117 protected Ehcache replaceCacheWithDecoratedCache(Ehcache cache) {
118 if (cache instanceof BlockingCache) {
119 return cache;
120 }
121
122 Ehcache decoratedCache = new BlockingCache(cache);
123
124 _cacheManager.replaceCacheWithDecoratedCache(
125 cache, decoratedCache);
126
127 return decoratedCache;
128 }
129
130 private String _configPropertyKey;
131 private CacheManager _cacheManager;
132 private MBeanServer _mbeanServer;
133 private boolean _registerCacheManager = true;
134 private boolean _registerCaches = true;
135 private boolean _registerCacheConfigurations = true;
136 private boolean _registerCacheStatistics = true;
137
138 }