1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.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  /**
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 (_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 }