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.dao.orm.hibernate;
21  
22  import com.liferay.portal.kernel.cache.CacheRegistry;
23  import com.liferay.portal.kernel.cache.CacheRegistryItem;
24  import com.liferay.portal.kernel.log.Log;
25  import com.liferay.portal.kernel.log.LogFactoryUtil;
26  
27  import java.util.Map;
28  
29  import org.hibernate.cache.Cache;
30  import org.hibernate.cache.CacheException;
31  
32  /**
33   * <a href="CacheWrapper.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   *
37   */
38  public class CacheWrapper implements Cache, CacheRegistryItem {
39  
40      public CacheWrapper(Cache cache) {
41          _cache = cache;
42          _registryName = cache.getRegionName();
43  
44          if (_log.isDebugEnabled()) {
45              _log.debug("Creating cache for " + _registryName);
46          }
47  
48          CacheRegistry.register(this);
49      }
50  
51      public void clear() throws CacheException {
52          _cache.clear();
53      }
54  
55      public void destroy() throws CacheException {
56          _cache.destroy();
57      }
58  
59      public Object get(Object key) throws CacheException {
60          return _cache.get(key);
61      }
62  
63      public long getElementCountInMemory() {
64          return _cache.getElementCountInMemory();
65      }
66  
67      public long getElementCountOnDisk() {
68          return _cache.getElementCountOnDisk();
69      }
70  
71      public String getRegionName() {
72          return _cache.getRegionName();
73      }
74  
75      public String getRegistryName() {
76          return _registryName;
77      }
78  
79      public long getSizeInMemory() {
80          return _cache.getSizeInMemory();
81      }
82  
83      public int getTimeout() {
84          return _cache.getTimeout();
85      }
86  
87      public void lock(Object key) throws CacheException {
88          _cache.lock(key);
89      }
90  
91      public long nextTimestamp() {
92          return _cache.nextTimestamp();
93      }
94  
95      public void put(Object key, Object value) throws CacheException {
96          if (CacheRegistry.isActive()) {
97              _cache.put(key, value);
98          }
99      }
100 
101     public Object read(Object key) throws CacheException {
102         return _cache.read(key);
103     }
104 
105     public void remove(Object key) throws CacheException {
106         _cache.remove(key);
107     }
108 
109     public Map toMap() {
110         return _cache.toMap();
111     }
112 
113     public void unlock(Object key) throws CacheException {
114         _cache.unlock(key);
115     }
116 
117     public void update(Object key, Object value) throws CacheException {
118         if (CacheRegistry.isActive()) {
119             _cache.update(key, value);
120         }
121     }
122 
123     public void invalidate() {
124         if (_log.isDebugEnabled()) {
125             _log.debug("Invalidating cache for " + _registryName);
126         }
127 
128         _cache.clear();
129     }
130 
131     private static Log _log = LogFactoryUtil.getLog(CacheWrapper.class);
132 
133     private Cache _cache;
134     private String _registryName;
135 
136 }