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