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