1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
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.kernel.util.StringUtil;
22  import com.liferay.portal.servlet.filters.cache.CacheUtil;
23  import com.liferay.portlet.journalcontent.util.JournalContentUtil;
24  
25  /**
26   * <a href="ImageServletTokenImpl.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Brian Wing Shun Chan
29   */
30  public class ImageServletTokenImpl implements ImageServletToken {
31  
32      public static final String CACHE_NAME = ImageServletToken.class.getName();
33  
34      public void afterPropertiesSet() {
35          _portalCache = _multiVMPool.getCache(CACHE_NAME);
36      }
37  
38      public String getToken(long imageId) {
39          String key = _encodeKey(imageId);
40  
41          String token = (String)_portalCache.get(key);
42  
43          if (token == null) {
44              token = _createToken(imageId);
45  
46              _portalCache.put(key, token);
47          }
48  
49          return token;
50      }
51  
52      public void resetToken(long imageId) {
53          String key = _encodeKey(imageId);
54  
55          _portalCache.remove(key);
56  
57          // Journal content
58  
59          JournalContentUtil.clearCache();
60  
61          // Layout cache
62  
63          CacheUtil.clearCache();
64      }
65  
66      public void setMultiVMPool(MultiVMPool multiVMPool) {
67          _multiVMPool = multiVMPool;
68      }
69  
70      private String _createToken(long imageId) {
71          return String.valueOf(System.currentTimeMillis());
72      }
73  
74      private String _encodeKey(long imageId) {
75          return CACHE_NAME.concat(StringPool.POUND).concat(
76              StringUtil.toHexString(imageId));
77      }
78  
79      private MultiVMPool _multiVMPool;
80      private PortalCache _portalCache;
81  
82  }