1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.cache;
21  
22  import com.liferay.portal.kernel.cache.PortalCache;
23  import com.liferay.portal.kernel.cache.PortalCacheManager;
24  import com.liferay.portal.util.PropsUtil;
25  
26  import java.net.URL;
27  
28  import javax.management.MBeanServer;
29  
30  import net.sf.ehcache.CacheManager;
31  import net.sf.ehcache.Ehcache;
32  import net.sf.ehcache.ObjectExistsException;
33  import net.sf.ehcache.constructs.blocking.BlockingCache;
34  import net.sf.ehcache.management.ManagementService;
35  
36  /**
37   * <a href="EhcachePortalCacheManager.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Joseph Shum
40   * @author Raymond Augé
41   * @author Michael C. Han
42   *
43   */
44  public class EhcachePortalCacheManager implements PortalCacheManager {
45  
46      public void afterPropertiesSet() {
47          URL url = getClass().getResource(PropsUtil.get(_configPropertyKey));
48  
49          _cacheManager = new CacheManager(url);
50  
51          ManagementService.registerMBeans(
52              _cacheManager, _mbeanServer, _registerCacheManager, _registerCaches,
53              _registerCacheConfigurations, _registerCacheStatistics);
54      }
55  
56      public void clearAll() {
57          _cacheManager.clearAll();
58      }
59  
60      public void destroy() throws Exception {
61          _cacheManager.shutdown();
62      }
63  
64      public PortalCache getCache(String name) {
65          return getCache(name, false);
66      }
67  
68      public PortalCache getCache(String name, boolean blocking) {
69          Ehcache cache = _cacheManager.getEhcache(name);
70  
71          if (cache == null) {
72              try {
73                  _cacheManager.addCache(name);
74              }
75              catch (ObjectExistsException oee) {
76  
77                  // LEP-7122
78  
79              }
80  
81              cache = _cacheManager.getEhcache(name);
82  
83              if (blocking) {
84                  cache = replaceCacheWithDecoratedCache(cache);
85              }
86          }
87  
88          return new EhcachePortalCache(cache);
89      }
90  
91      public void setConfigPropertyKey(String configPropertyKey) {
92          _configPropertyKey = configPropertyKey;
93      }
94  
95      public void setMBeanServer(MBeanServer server) {
96          _mbeanServer = server;
97      }
98  
99      public void setRegisterCacheConfigurations(
100         boolean registerCacheConfigurations) {
101 
102         _registerCacheConfigurations = registerCacheConfigurations;
103     }
104 
105     public void setRegisterCacheManager(boolean registerCacheManager) {
106         _registerCacheManager = registerCacheManager;
107     }
108 
109     public void setRegisterCaches(boolean registerCaches) {
110         _registerCaches = registerCaches;
111     }
112 
113     public void setRegisterCacheStatistics(boolean registerCacheStatistics) {
114         _registerCacheStatistics = registerCacheStatistics;
115     }
116 
117     protected Ehcache replaceCacheWithDecoratedCache(Ehcache cache) {
118         if (cache instanceof BlockingCache) {
119             return cache;
120         }
121 
122         Ehcache decoratedCache = new BlockingCache(cache);
123 
124         _cacheManager.replaceCacheWithDecoratedCache(
125             cache, decoratedCache);
126 
127         return decoratedCache;
128     }
129 
130     private String _configPropertyKey;
131     private CacheManager _cacheManager;
132     private MBeanServer _mbeanServer;
133     private boolean _registerCacheManager = true;
134     private boolean _registerCaches = true;
135     private boolean _registerCacheConfigurations = true;
136     private boolean _registerCacheStatistics = true;
137 
138 }