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 if (PropsValues.EHCACHE_PORTAL_CACHE_MANAGER_JMX_ENABLED) {
57 ManagementService.registerMBeans(
58 _cacheManager, _mbeanServer, _registerCacheManager,
59 _registerCaches, _registerCacheConfigurations,
60 _registerCacheStatistics);
61 }
62 }
63
64 public void clearAll() {
65 _cacheManager.clearAll();
66 }
67
68 public void destroy() throws Exception {
69 _cacheManager.shutdown();
70 }
71
72 public PortalCache getCache(String name) {
73 return getCache(name, false);
74 }
75
76 public PortalCache getCache(String name, boolean blocking) {
77 Ehcache cache = _cacheManager.getEhcache(name);
78
79 if (cache == null) {
80 try {
81 _cacheManager.addCache(name);
82 }
83 catch (ObjectExistsException oee) {
84
85
87 }
88
89 cache = _cacheManager.getEhcache(name);
90
91 if (_log.isInfoEnabled()) {
92 _log.info(
93 "Cache name " + name + " is using implementation " +
94 cache.getClass().getName());
95 }
96 }
97
98 PortalCache portalCache = new EhcachePortalCache(cache);
99
100 if (PropsValues.EHCACHE_BLOCKING_CACHE_ALLOWED && blocking) {
101 portalCache = new BlockingPortalCache(portalCache);
102 }
103
104 return portalCache;
105 }
106
107 public void setConfigPropertyKey(String configPropertyKey) {
108 _configPropertyKey = configPropertyKey;
109 }
110
111 public void setMBeanServer(MBeanServer server) {
112 _mbeanServer = server;
113 }
114
115 public void setRegisterCacheConfigurations(
116 boolean registerCacheConfigurations) {
117
118 _registerCacheConfigurations = registerCacheConfigurations;
119 }
120
121 public void setRegisterCacheManager(boolean registerCacheManager) {
122 _registerCacheManager = registerCacheManager;
123 }
124
125 public void setRegisterCaches(boolean registerCaches) {
126 _registerCaches = registerCaches;
127 }
128
129 public void setRegisterCacheStatistics(boolean registerCacheStatistics) {
130 _registerCacheStatistics = registerCacheStatistics;
131 }
132
133 private static Log _log =
134 LogFactoryUtil.getLog(EhcachePortalCacheManager.class);
135
136 private String _configPropertyKey;
137 private CacheManager _cacheManager;
138 private MBeanServer _mbeanServer;
139 private boolean _registerCacheManager = true;
140 private boolean _registerCaches = true;
141 private boolean _registerCacheConfigurations = true;
142 private boolean _registerCacheStatistics = true;
143
144 }