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.webcache;
21  
22  import com.liferay.portal.kernel.cache.PortalCache;
23  import com.liferay.portal.kernel.cache.SingleVMPool;
24  import com.liferay.portal.kernel.log.Log;
25  import com.liferay.portal.kernel.log.LogFactoryUtil;
26  import com.liferay.portal.kernel.util.Time;
27  import com.liferay.portal.kernel.webcache.WebCacheException;
28  import com.liferay.portal.kernel.webcache.WebCacheItem;
29  import com.liferay.portal.kernel.webcache.WebCachePool;
30  
31  /**
32   * <a href="WebCachePoolImpl.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   *
36   */
37  public class WebCachePoolImpl implements WebCachePool {
38  
39      public static final String CACHE_NAME = WebCachePool.class.getName();
40  
41      public void afterPropertiesSet() {
42          _cache = _singleVMPool.getCache(CACHE_NAME);
43      }
44  
45      public void clear() {
46          _cache.removeAll();
47      }
48  
49      public Object get(String key, WebCacheItem wci) {
50          Object obj = _singleVMPool.get(_cache, key);
51  
52          if (obj == null) {
53              try {
54                  obj = wci.convert(key);
55  
56                  int timeToLive = (int)(wci.getRefreshTime() / Time.SECOND);
57  
58                  _cache.put(key, obj, timeToLive);
59              }
60              catch (WebCacheException wce) {
61                  if (_log.isWarnEnabled()) {
62                      Throwable cause = wce.getCause();
63  
64                      if (cause != null) {
65                          _log.warn(cause, cause);
66                      }
67                      else {
68                          _log.warn(wce, wce);
69                      }
70                  }
71              }
72          }
73  
74          return obj;
75      }
76  
77      public void remove(String key) {
78          _singleVMPool.remove(_cache, key);
79      }
80  
81      public void setSingleVMPool(SingleVMPool singleVMPool) {
82          _singleVMPool = singleVMPool;
83      }
84  
85      private static Log _log = LogFactoryUtil.getLog(WebCachePoolImpl.class);
86  
87      private SingleVMPool _singleVMPool;
88      private PortalCache _cache;
89  
90  }