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.util.PropsUtil;
26  
27  import com.opensymphony.oscache.base.CacheEntry;
28  import com.opensymphony.oscache.base.NeedsRefreshException;
29  import com.opensymphony.oscache.general.GeneralCacheAdministrator;
30  
31  import java.util.Map;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  import org.hibernate.cache.Cache;
37  import org.hibernate.cache.CacheException;
38  import org.hibernate.cache.Timestamper;
39  
40  /**
41   * <a href="OSCache.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Mathias Bogaert
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class OSCache implements Cache {
48  
49      public OSCache(int refreshPeriod, String cron, String region) {
50          _refreshPeriod = refreshPeriod;
51          _cron = cron;
52          _regionName = region;
53          _regionGroups = new String[] {region};
54      }
55  
56      public void clear() throws CacheException {
57          _cache.flushGroup(_regionName);
58      }
59  
60      public void destroy() throws CacheException {
61          synchronized (_cache) {
62              _cache.destroy();
63          }
64      }
65  
66      public Object get(Object key) throws CacheException {
67          String keyString = _encodeKey(key);
68  
69          try {
70              return _cache.getFromCache(keyString, _refreshPeriod, _cron);
71          }
72          catch (NeedsRefreshException nre) {
73              _cache.cancelUpdate(keyString);
74  
75              return null;
76          }
77      }
78  
79      public long getElementCountOnDisk() {
80          return -1;
81      }
82  
83      public long getElementCountInMemory() {
84          return -1;
85      }
86  
87      public String getRegionName() {
88          return _regionName;
89      }
90  
91      public long getSizeInMemory() {
92          return -1;
93      }
94  
95      public int getTimeout() {
96          return CacheEntry.INDEFINITE_EXPIRY;
97      }
98  
99      public void lock(Object key) throws CacheException {
100     }
101 
102     public long nextTimestamp() {
103         return Timestamper.next();
104     }
105 
106     public void put(Object key, Object value) throws CacheException {
107         _cache.putInCache(_encodeKey(key), value, _regionGroups);
108     }
109 
110     public Object read(Object key) throws CacheException {
111         return get(key);
112     }
113 
114     public void remove(Object key) throws CacheException {
115         _cache.flushEntry(_encodeKey(key));
116     }
117 
118     public Map toMap() {
119         return null;
120     }
121 
122     public void unlock(Object key) throws CacheException {
123     }
124 
125     public void update(Object key, Object value) throws CacheException {
126         _cache.flushEntry(_encodeKey(key));
127 
128         put(key, value);
129     }
130 
131     private String _encodeKey(Object key) {
132         String keyString = String.valueOf(key);
133 
134         if (_log.isDebugEnabled()) {
135             _log.debug("Key " + keyString);
136         }
137 
138         return keyString;
139     }
140 
141     private static Log _log = LogFactory.getLog(OSCache.class);
142 
143     private static GeneralCacheAdministrator _cache =
144         new GeneralCacheAdministrator(PropsUtil.getProperties());
145 
146     private int _refreshPeriod;
147     private String _cron;
148     private String _regionName;
149     private String[] _regionGroups;
150 
151 }