1
22
23 package com.liferay.portal.cache;
24
25 import com.liferay.portal.kernel.cache.BlockingPortalCache;
26 import com.liferay.portal.kernel.cache.PortalCache;
27 import com.liferay.portal.kernel.cache.PortalCacheManager;
28 import com.liferay.portal.kernel.log.Log;
29 import com.liferay.portal.kernel.log.LogFactoryUtil;
30 import com.liferay.portal.util.PropsUtil;
31 import com.liferay.portal.util.PropsValues;
32
33 import java.net.URL;
34
35 import javax.management.MBeanServer;
36
37 import net.sf.ehcache.CacheManager;
38 import net.sf.ehcache.Ehcache;
39 import net.sf.ehcache.ObjectExistsException;
40 import net.sf.ehcache.management.ManagementService;
41
42
49 public class EhcachePortalCacheManager implements PortalCacheManager {
50
51 public void afterPropertiesSet() {
52 URL url = getClass().getResource(PropsUtil.get(_configPropertyKey));
53
54 _cacheManager = new CacheManager(url);
55
56 ManagementService.registerMBeans(
57 _cacheManager, _mbeanServer, _registerCacheManager, _registerCaches,
58 _registerCacheConfigurations, _registerCacheStatistics);
59 }
60
61 public void clearAll() {
62 _cacheManager.clearAll();
63 }
64
65 public void destroy() throws Exception {
66 _cacheManager.shutdown();
67 }
68
69 public PortalCache getCache(String name) {
70 return getCache(name, false);
71 }
72
73 public PortalCache getCache(String name, boolean blocking) {
74 Ehcache cache = _cacheManager.getEhcache(name);
75
76 if (cache == null) {
77 try {
78 _cacheManager.addCache(name);
79 }
80 catch (ObjectExistsException oee) {
81
82
84 }
85
86 cache = _cacheManager.getEhcache(name);
87
88 if (_log.isInfoEnabled()) {
89 _log.info(
90 "Cache name " + name + " is using implementation " +
91 cache.getClass().getName());
92 }
93 }
94
95 PortalCache portalCache = new EhcachePortalCache(cache);
96
97 if (PropsValues.EHCACHE_BLOCKING_CACHE_ALLOWED && blocking) {
98 portalCache = new BlockingPortalCache(portalCache);
99 }
100
101 return portalCache;
102 }
103
104 public void setConfigPropertyKey(String configPropertyKey) {
105 _configPropertyKey = configPropertyKey;
106 }
107
108 public void setMBeanServer(MBeanServer server) {
109 _mbeanServer = server;
110 }
111
112 public void setRegisterCacheConfigurations(
113 boolean registerCacheConfigurations) {
114
115 _registerCacheConfigurations = registerCacheConfigurations;
116 }
117
118 public void setRegisterCacheManager(boolean registerCacheManager) {
119 _registerCacheManager = registerCacheManager;
120 }
121
122 public void setRegisterCaches(boolean registerCaches) {
123 _registerCaches = registerCaches;
124 }
125
126 public void setRegisterCacheStatistics(boolean registerCacheStatistics) {
127 _registerCacheStatistics = registerCacheStatistics;
128 }
129
130 private static Log _log =
131 LogFactoryUtil.getLog(EhcachePortalCacheManager.class);
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 }