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