1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.util.PropsUtil;
30  import com.liferay.portal.util.PropsValues;
31  
32  import java.net.URL;
33  
34  import javax.management.MBeanServer;
35  
36  import net.sf.ehcache.CacheManager;
37  import net.sf.ehcache.Ehcache;
38  import net.sf.ehcache.ObjectExistsException;
39  import net.sf.ehcache.constructs.blocking.BlockingCache;
40  import net.sf.ehcache.management.ManagementService;
41  
42  /**
43   * <a href="EhcachePortalCacheManager.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Joseph Shum
46   * @author Raymond Augé
47   * @author Michael C. Han
48   */
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                  // LEP-7122
83  
84              }
85  
86              cache = _cacheManager.getEhcache(name);
87  
88              if (PropsValues.EHCACHE_BLOCKING_CACHE_ALLOWED && blocking) {
89                  cache = replaceCacheWithDecoratedCache(cache);
90              }
91  
92              if (_log.isInfoEnabled()) {
93                  _log.info(
94                      "Cache name " + name + " is using implementation " +
95                          cache.getClass().getName());
96              }
97          }
98  
99          return new EhcachePortalCache(cache);
100     }
101 
102     public void setConfigPropertyKey(String configPropertyKey) {
103         _configPropertyKey = configPropertyKey;
104     }
105 
106     public void setMBeanServer(MBeanServer server) {
107         _mbeanServer = server;
108     }
109 
110     public void setRegisterCacheConfigurations(
111         boolean registerCacheConfigurations) {
112 
113         _registerCacheConfigurations = registerCacheConfigurations;
114     }
115 
116     public void setRegisterCacheManager(boolean registerCacheManager) {
117         _registerCacheManager = registerCacheManager;
118     }
119 
120     public void setRegisterCaches(boolean registerCaches) {
121         _registerCaches = registerCaches;
122     }
123 
124     public void setRegisterCacheStatistics(boolean registerCacheStatistics) {
125         _registerCacheStatistics = registerCacheStatistics;
126     }
127 
128     protected Ehcache replaceCacheWithDecoratedCache(Ehcache cache) {
129         if (cache instanceof BlockingCache) {
130             return cache;
131         }
132 
133         Ehcache decoratedCache = new BlockingCache(cache);
134 
135         _cacheManager.replaceCacheWithDecoratedCache(
136             cache, decoratedCache);
137 
138         return decoratedCache;
139     }
140 
141     private static Log _log =
142         LogFactoryUtil.getLog(EhcachePortalCacheManager.class);
143 
144     private String _configPropertyKey;
145     private CacheManager _cacheManager;
146     private MBeanServer _mbeanServer;
147     private boolean _registerCacheManager = true;
148     private boolean _registerCaches = true;
149     private boolean _registerCacheConfigurations = true;
150     private boolean _registerCacheStatistics = true;
151 
152 }