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.Cache;
31  import net.sf.ehcache.CacheManager;
32  import net.sf.ehcache.ObjectExistsException;
33  import net.sf.ehcache.management.ManagementService;
34  
35  import org.springframework.beans.factory.DisposableBean;
36  import org.springframework.beans.factory.InitializingBean;
37  
38  /**
39   * <a href="EhcachePortalCacheManager.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Joseph Shum
42   * @author Raymond Augé
43   * @author Michael C. Han
44   *
45   */
46  public class EhcachePortalCacheManager
47      implements DisposableBean, InitializingBean, 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          Cache cache = _cacheManager.getCache(name);
69  
70          if (cache == null) {
71              try {
72                  _cacheManager.addCache(name);
73              }
74              catch (ObjectExistsException oee) {
75  
76                  // LEP-7122
77  
78              }
79  
80              cache = _cacheManager.getCache(name);
81          }
82  
83          return new EhcachePortalCache(cache);
84      }
85  
86      public void setConfigPropertyKey(String configPropertyKey) {
87          _configPropertyKey = configPropertyKey;
88      }
89  
90      public void setMBeanServer(MBeanServer server) {
91          _mbeanServer = server;
92      }
93  
94      public void setRegisterCacheConfigurations(
95          boolean registerCacheConfigurations) {
96  
97          _registerCacheConfigurations = registerCacheConfigurations;
98      }
99  
100     public void setRegisterCacheManager(boolean registerCacheManager) {
101         _registerCacheManager = registerCacheManager;
102     }
103 
104     public void setRegisterCaches(boolean registerCaches) {
105         _registerCaches = registerCaches;
106     }
107 
108     public void setRegisterCacheStatistics(boolean registerCacheStatistics) {
109         _registerCacheStatistics = registerCacheStatistics;
110     }
111 
112     private String _configPropertyKey;
113     private CacheManager _cacheManager;
114     private MBeanServer _mbeanServer;
115     private boolean _registerCacheManager = true;
116     private boolean _registerCaches = true;
117     private boolean _registerCacheConfigurations = true;
118     private boolean _registerCacheStatistics = true;
119 
120 }