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.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          if (PropsValues.EHCACHE_PORTAL_CACHE_MANAGER_JMX_ENABLED) {
57              ManagementService.registerMBeans(
58                  _cacheManager, _mbeanServer, _registerCacheManager,
59                  _registerCaches, _registerCacheConfigurations,
60                  _registerCacheStatistics);
61          }
62      }
63  
64      public void clearAll() {
65          _cacheManager.clearAll();
66      }
67  
68      public void destroy() throws Exception {
69          _cacheManager.shutdown();
70      }
71  
72      public PortalCache getCache(String name) {
73          return getCache(name, false);
74      }
75  
76      public PortalCache getCache(String name, boolean blocking) {
77          Ehcache cache = _cacheManager.getEhcache(name);
78  
79          if (cache == null) {
80              try {
81                  _cacheManager.addCache(name);
82              }
83              catch (ObjectExistsException oee) {
84  
85                  // LEP-7122
86  
87              }
88  
89              cache = _cacheManager.getEhcache(name);
90  
91              if (_log.isInfoEnabled()) {
92                  _log.info(
93                      "Cache name " + name + " is using implementation " +
94                          cache.getClass().getName());
95              }
96          }
97  
98          PortalCache portalCache = new EhcachePortalCache(cache);
99  
100         if (PropsValues.EHCACHE_BLOCKING_CACHE_ALLOWED && blocking) {
101             portalCache = new BlockingPortalCache(portalCache);
102         }
103 
104         return portalCache;
105     }
106 
107     public void setConfigPropertyKey(String configPropertyKey) {
108         _configPropertyKey = configPropertyKey;
109     }
110 
111     public void setMBeanServer(MBeanServer server) {
112         _mbeanServer = server;
113     }
114 
115     public void setRegisterCacheConfigurations(
116         boolean registerCacheConfigurations) {
117 
118         _registerCacheConfigurations = registerCacheConfigurations;
119     }
120 
121     public void setRegisterCacheManager(boolean registerCacheManager) {
122         _registerCacheManager = registerCacheManager;
123     }
124 
125     public void setRegisterCaches(boolean registerCaches) {
126         _registerCaches = registerCaches;
127     }
128 
129     public void setRegisterCacheStatistics(boolean registerCacheStatistics) {
130         _registerCacheStatistics = registerCacheStatistics;
131     }
132 
133     private static Log _log =
134         LogFactoryUtil.getLog(EhcachePortalCacheManager.class);
135 
136     private String _configPropertyKey;
137     private CacheManager _cacheManager;
138     private MBeanServer _mbeanServer;
139     private boolean _registerCacheManager = true;
140     private boolean _registerCaches = true;
141     private boolean _registerCacheConfigurations = true;
142     private boolean _registerCacheStatistics = true;
143 
144 }