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.PortalCache;
26  import com.liferay.portal.kernel.cache.PortalCacheManager;
27  import com.liferay.portal.util.PropsUtil;
28  
29  import java.net.URL;
30  
31  import javax.management.MBeanServer;
32  
33  import net.sf.ehcache.CacheManager;
34  import net.sf.ehcache.Ehcache;
35  import net.sf.ehcache.ObjectExistsException;
36  import net.sf.ehcache.constructs.blocking.BlockingCache;
37  import net.sf.ehcache.management.ManagementService;
38  
39  /**
40   * <a href="EhcachePortalCacheManager.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Joseph Shum
43   * @author Raymond Augé
44   * @author Michael C. Han
45   *
46   */
47  public class EhcachePortalCacheManager implements PortalCacheManager {
48  
49      public void afterPropertiesSet() {
50          URL url = getClass().getResource(PropsUtil.get(_configPropertyKey));
51  
52          _cacheManager = new CacheManager(url);
53  
54          ManagementService.registerMBeans(
55              _cacheManager, _mbeanServer, _registerCacheManager, _registerCaches,
56              _registerCacheConfigurations, _registerCacheStatistics);
57      }
58  
59      public void clearAll() {
60          _cacheManager.clearAll();
61      }
62  
63      public void destroy() throws Exception {
64          _cacheManager.shutdown();
65      }
66  
67      public PortalCache getCache(String name) {
68          return getCache(name, false);
69      }
70  
71      public PortalCache getCache(String name, boolean blocking) {
72          Ehcache cache = _cacheManager.getEhcache(name);
73  
74          if (cache == null) {
75              try {
76                  _cacheManager.addCache(name);
77              }
78              catch (ObjectExistsException oee) {
79  
80                  // LEP-7122
81  
82              }
83  
84              cache = _cacheManager.getEhcache(name);
85  
86              if (blocking) {
87                  cache = replaceCacheWithDecoratedCache(cache);
88              }
89          }
90  
91          return new EhcachePortalCache(cache);
92      }
93  
94      public void setConfigPropertyKey(String configPropertyKey) {
95          _configPropertyKey = configPropertyKey;
96      }
97  
98      public void setMBeanServer(MBeanServer server) {
99          _mbeanServer = server;
100     }
101 
102     public void setRegisterCacheConfigurations(
103         boolean registerCacheConfigurations) {
104 
105         _registerCacheConfigurations = registerCacheConfigurations;
106     }
107 
108     public void setRegisterCacheManager(boolean registerCacheManager) {
109         _registerCacheManager = registerCacheManager;
110     }
111 
112     public void setRegisterCaches(boolean registerCaches) {
113         _registerCaches = registerCaches;
114     }
115 
116     public void setRegisterCacheStatistics(boolean registerCacheStatistics) {
117         _registerCacheStatistics = registerCacheStatistics;
118     }
119 
120     protected Ehcache replaceCacheWithDecoratedCache(Ehcache cache) {
121         if (cache instanceof BlockingCache) {
122             return cache;
123         }
124 
125         Ehcache decoratedCache = new BlockingCache(cache);
126 
127         _cacheManager.replaceCacheWithDecoratedCache(
128             cache, decoratedCache);
129 
130         return decoratedCache;
131     }
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 }