1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.servlet;
16  
17  import com.liferay.portal.kernel.cache.MultiVMPool;
18  import com.liferay.portal.kernel.cache.PortalCache;
19  import com.liferay.portal.kernel.servlet.ImageServletToken;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.servlet.filters.cache.CacheUtil;
22  import com.liferay.portlet.journalcontent.util.JournalContentUtil;
23  
24  /**
25   * <a href="ImageServletTokenImpl.java.html"><b><i>View Source</i></b></a>
26   *
27   * @author Brian Wing Shun Chan
28   */
29  public class ImageServletTokenImpl implements ImageServletToken {
30  
31      public static final String CACHE_NAME = ImageServletToken.class.getName();
32  
33      public void afterPropertiesSet() {
34          _cache = _multiVMPool.getCache(CACHE_NAME);
35      }
36  
37      public String getToken(long imageId) {
38          String key = _encodeKey(imageId);
39  
40          String token = (String)_cache.get(key);
41  
42          if (token == null) {
43              token = _createToken(imageId);
44  
45              _cache.put(key, token);
46          }
47  
48          return token;
49      }
50  
51      public void resetToken(long imageId) {
52          String key = _encodeKey(imageId);
53  
54          _cache.remove(key);
55  
56          // Journal content
57  
58          JournalContentUtil.clearCache();
59  
60          // Layout cache
61  
62          CacheUtil.clearCache();
63      }
64  
65      public void setMultiVMPool(MultiVMPool multiVMPool) {
66          _multiVMPool = multiVMPool;
67      }
68  
69      private String _createToken(long imageId) {
70          return String.valueOf(System.currentTimeMillis());
71      }
72  
73      private String _encodeKey(long imageId) {
74          return CACHE_NAME.concat(StringPool.POUND).concat(
75              String.valueOf(imageId));
76      }
77  
78      private MultiVMPool _multiVMPool;
79      private PortalCache _cache;
80  
81  }