001    /**
002     * Copyright (c) 2000-2012 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.webserver;
016    
017    import com.liferay.portal.kernel.cache.MultiVMPool;
018    import com.liferay.portal.kernel.cache.PortalCache;
019    import com.liferay.portal.servlet.filters.cache.CacheUtil;
020    import com.liferay.portlet.journalcontent.util.JournalContentUtil;
021    
022    /**
023     * @author Brian Wing Shun Chan
024     * @since  6.1, replaced com.liferay.portal.servlet.ImageServletTokenImpl
025     */
026    public class WebServerServletTokenImpl implements WebServerServletToken {
027    
028            public void afterPropertiesSet() {
029                    _portalCache = (PortalCache<Long, String>)_multiVMPool.getCache(
030                            _CACHE_NAME);
031            }
032    
033            public String getToken(long imageId) {
034                    Long key = imageId;
035    
036                    String token = _portalCache.get(key);
037    
038                    if (token == null) {
039                            token = _createToken(imageId);
040    
041                            _portalCache.put(key, token);
042                    }
043    
044                    return token;
045            }
046    
047            public void resetToken(long imageId) {
048                    _portalCache.remove(imageId);
049    
050                    // Journal content
051    
052                    JournalContentUtil.clearCache();
053    
054                    // Layout cache
055    
056                    CacheUtil.clearCache();
057            }
058    
059            public void setMultiVMPool(MultiVMPool multiVMPool) {
060                    _multiVMPool = multiVMPool;
061            }
062    
063            private String _createToken(long imageId) {
064                    return String.valueOf(System.currentTimeMillis());
065            }
066    
067            private static final String _CACHE_NAME =
068                    WebServerServletToken.class.getName();
069    
070            private MultiVMPool _multiVMPool;
071            private PortalCache<Long, String> _portalCache;
072    
073    }