001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.servlet;
016    
017    import com.liferay.portal.kernel.cache.MultiVMPool;
018    import com.liferay.portal.kernel.cache.PortalCache;
019    import com.liferay.portal.kernel.servlet.ImageServletToken;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.servlet.filters.cache.CacheUtil;
022    import com.liferay.portlet.journalcontent.util.JournalContentUtil;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     */
027    public class ImageServletTokenImpl implements ImageServletToken {
028    
029            public static final String CACHE_NAME = ImageServletToken.class.getName();
030    
031            public void afterPropertiesSet() {
032                    _portalCache = _multiVMPool.getCache(CACHE_NAME);
033            }
034    
035            public String getToken(long imageId) {
036                    String key = _encodeKey(imageId);
037    
038                    String token = (String)_portalCache.get(key);
039    
040                    if (token == null) {
041                            token = _createToken(imageId);
042    
043                            _portalCache.put(key, token);
044                    }
045    
046                    return token;
047            }
048    
049            public void resetToken(long imageId) {
050                    String key = _encodeKey(imageId);
051    
052                    _portalCache.remove(key);
053    
054                    // Journal content
055    
056                    JournalContentUtil.clearCache();
057    
058                    // Layout cache
059    
060                    CacheUtil.clearCache();
061            }
062    
063            public void setMultiVMPool(MultiVMPool multiVMPool) {
064                    _multiVMPool = multiVMPool;
065            }
066    
067            private String _createToken(long imageId) {
068                    return String.valueOf(System.currentTimeMillis());
069            }
070    
071            private String _encodeKey(long imageId) {
072                    return CACHE_NAME.concat(StringPool.POUND).concat(
073                            String.valueOf(imageId));
074            }
075    
076            private MultiVMPool _multiVMPool;
077            private PortalCache _portalCache;
078    
079    }