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     */
025    public class WebServerServletTokenImpl implements WebServerServletToken {
026    
027            public void afterPropertiesSet() {
028                    _portalCache = _multiVMPool.getCache(_CACHE_NAME);
029            }
030    
031            public String getToken(long imageId) {
032                    Long key = imageId;
033    
034                    String token = (String)_portalCache.get(key);
035    
036                    if (token == null) {
037                            token = _createToken(imageId);
038    
039                            _portalCache.put(key, token);
040                    }
041    
042                    return token;
043            }
044    
045            public void resetToken(long imageId) {
046                    _portalCache.remove(imageId);
047    
048                    // Journal content
049    
050                    JournalContentUtil.clearCache();
051    
052                    // Layout cache
053    
054                    CacheUtil.clearCache();
055            }
056    
057            public void setMultiVMPool(MultiVMPool multiVMPool) {
058                    _multiVMPool = multiVMPool;
059            }
060    
061            private String _createToken(long imageId) {
062                    return String.valueOf(System.currentTimeMillis());
063            }
064    
065            private static final String _CACHE_NAME =
066                    WebServerServletToken.class.getName();
067    
068            private MultiVMPool _multiVMPool;
069            private PortalCache _portalCache;
070    
071    }