1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.cache.ehcache;
16  
17  import com.liferay.portal.kernel.cache.BlockingPortalCache;
18  import com.liferay.portal.kernel.cache.PortalCache;
19  import com.liferay.portal.kernel.cache.PortalCacheManager;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  import com.liferay.portal.util.PropsUtil;
23  import com.liferay.portal.util.PropsValues;
24  
25  import java.net.URL;
26  
27  import javax.management.MBeanServer;
28  
29  import net.sf.ehcache.CacheManager;
30  import net.sf.ehcache.Ehcache;
31  import net.sf.ehcache.ObjectExistsException;
32  import net.sf.ehcache.management.ManagementService;
33  
34  /**
35   * <a href="EhcachePortalCacheManager.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Joseph Shum
38   * @author Raymond Augé
39   * @author Michael C. Han
40   */
41  public class EhcachePortalCacheManager implements PortalCacheManager {
42  
43      public void afterPropertiesSet() {
44          URL url = getClass().getResource(PropsUtil.get(_configPropertyKey));
45  
46          _cacheManager = new CacheManager(url);
47  
48          if (PropsValues.EHCACHE_PORTAL_CACHE_MANAGER_JMX_ENABLED) {
49              ManagementService.registerMBeans(
50                  _cacheManager, _mbeanServer, _registerCacheManager,
51                  _registerCaches, _registerCacheConfigurations,
52                  _registerCacheStatistics);
53          }
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              synchronized (_cacheManager) {
73                  cache = _cacheManager.getEhcache(name);
74  
75                  if (cache == null) {
76                      try {
77                          _cacheManager.addCache(name);
78                      }
79                      catch (ObjectExistsException oee) {
80  
81                          // LEP-7122
82  
83                      }
84  
85                      cache = _cacheManager.getEhcache(name);
86  
87                      if (_log.isInfoEnabled()) {
88                          _log.info(
89                              "Cache name " + name + " is using implementation " +
90                                  cache.getClass().getName());
91                      }
92                  }
93              }
94          }
95  
96          PortalCache portalCache = new EhcachePortalCache(cache);
97  
98          if (PropsValues.EHCACHE_BLOCKING_CACHE_ALLOWED && blocking) {
99              portalCache = new BlockingPortalCache(portalCache);
100         }
101 
102         return portalCache;
103     }
104 
105     public CacheManager getEhcacheManager() {
106         return _cacheManager;
107     }
108 
109     public void setConfigPropertyKey(String configPropertyKey) {
110         _configPropertyKey = configPropertyKey;
111     }
112 
113     public void setMBeanServer(MBeanServer server) {
114         _mbeanServer = server;
115     }
116 
117     public void setRegisterCacheConfigurations(
118         boolean registerCacheConfigurations) {
119 
120         _registerCacheConfigurations = registerCacheConfigurations;
121     }
122 
123     public void setRegisterCacheManager(boolean registerCacheManager) {
124         _registerCacheManager = registerCacheManager;
125     }
126 
127     public void setRegisterCaches(boolean registerCaches) {
128         _registerCaches = registerCaches;
129     }
130 
131     public void setRegisterCacheStatistics(boolean registerCacheStatistics) {
132         _registerCacheStatistics = registerCacheStatistics;
133     }
134 
135     private static Log _log = LogFactoryUtil.getLog(
136         EhcachePortalCacheManager.class);
137 
138     private String _configPropertyKey;
139     private CacheManager _cacheManager;
140     private MBeanServer _mbeanServer;
141     private boolean _registerCacheManager = true;
142     private boolean _registerCaches = true;
143     private boolean _registerCacheConfigurations = true;
144     private boolean _registerCacheStatistics = true;
145 
146 }