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