1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.cache.ehcache;
16  
17  import com.liferay.portal.kernel.cache.BlockingPortalCache;
18  import com.liferay.portal.kernel.cache.PortalCache;
19  import com.liferay.portal.kernel.cache.PortalCacheManager;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  import com.liferay.portal.util.PropsUtil;
23  import com.liferay.portal.util.PropsValues;
24  
25  import java.net.URL;
26  
27  import javax.management.MBeanServer;
28  
29  import net.sf.ehcache.CacheManager;
30  import net.sf.ehcache.Ehcache;
31  import net.sf.ehcache.ObjectExistsException;
32  import net.sf.ehcache.management.ManagementService;
33  
34  /**
35   * <a href="EhcachePortalCacheManager.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Joseph Shum
38   * @author Raymond Augé
39   * @author Michael C. Han
40   */
41  public class EhcachePortalCacheManager implements PortalCacheManager {
42  
43      public void afterPropertiesSet() {
44          URL url = getClass().getResource(PropsUtil.get(_configPropertyKey));
45  
46          _cacheManager = new CacheManager(url);
47  
48          if (PropsValues.EHCACHE_PORTAL_CACHE_MANAGER_JMX_ENABLED) {
49              _managementService = new ManagementService(
50                  _cacheManager, _mBeanServer, _registerCacheManager,
51                  _registerCaches, _registerCacheConfigurations,
52                  _registerCacheStatistics);
53  
54              _managementService.init();
55          }
56      }
57  
58      public void clearAll() {
59          _cacheManager.clearAll();
60      }
61  
62      public void destroy() throws Exception {
63          try {
64              _cacheManager.shutdown();
65          }
66          finally {
67              if (_managementService != null) {
68                  _managementService.dispose();
69              }
70          }
71      }
72  
73      public PortalCache getCache(String name) {
74          return getCache(name, false);
75      }
76  
77      public PortalCache getCache(String name, boolean blocking) {
78          Ehcache cache = _cacheManager.getEhcache(name);
79  
80          if (cache == null) {
81              synchronized (_cacheManager) {
82                  cache = _cacheManager.getEhcache(name);
83  
84                  if (cache == null) {
85                      try {
86                          _cacheManager.addCache(name);
87                      }
88                      catch (ObjectExistsException oee) {
89  
90                          // LEP-7122
91  
92                      }
93  
94                      cache = _cacheManager.getEhcache(name);
95  
96                      if (_log.isInfoEnabled()) {
97                          _log.info(
98                              "Cache name " + name + " is using implementation " +
99                                  cache.getClass().getName());
100                     }
101                 }
102             }
103         }
104 
105         PortalCache portalCache = new EhcachePortalCache(cache);
106 
107         portalCache.setDebug(_debug);
108 
109         if (PropsValues.EHCACHE_BLOCKING_CACHE_ALLOWED && blocking) {
110             portalCache = new BlockingPortalCache(portalCache);
111         }
112 
113         return portalCache;
114     }
115 
116     public CacheManager getEhcacheManager() {
117         return _cacheManager;
118     }
119 
120     public void setConfigPropertyKey(String configPropertyKey) {
121         _configPropertyKey = configPropertyKey;
122     }
123 
124     public void setDebug(boolean debug) {
125         _debug = debug;
126     }
127 
128     public void setMBeanServer(MBeanServer mBeanServer) {
129         _mBeanServer = mBeanServer;
130     }
131 
132     public void setRegisterCacheConfigurations(
133         boolean registerCacheConfigurations) {
134 
135         _registerCacheConfigurations = registerCacheConfigurations;
136     }
137 
138     public void setRegisterCacheManager(boolean registerCacheManager) {
139         _registerCacheManager = registerCacheManager;
140     }
141 
142     public void setRegisterCaches(boolean registerCaches) {
143         _registerCaches = registerCaches;
144     }
145 
146     public void setRegisterCacheStatistics(boolean registerCacheStatistics) {
147         _registerCacheStatistics = registerCacheStatistics;
148     }
149 
150     private static Log _log = LogFactoryUtil.getLog(
151         EhcachePortalCacheManager.class);
152 
153     private String _configPropertyKey;
154     private CacheManager _cacheManager;
155     private boolean _debug;
156     private ManagementService _managementService;
157     private MBeanServer _mBeanServer;
158     private boolean _registerCacheManager = true;
159     private boolean _registerCaches = true;
160     private boolean _registerCacheConfigurations = true;
161     private boolean _registerCacheStatistics = true;
162 
163 }