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