001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 = _multiVMPool.getCache(_CACHE_NAME);
030            }
031    
032            public String getToken(long imageId) {
033                    Long key = imageId;
034    
035                    String token = (String)_portalCache.get(key);
036    
037                    if (token == null) {
038                            token = _createToken(imageId);
039    
040                            _portalCache.put(key, token);
041                    }
042    
043                    return token;
044            }
045    
046            public void resetToken(long imageId) {
047                    _portalCache.remove(imageId);
048    
049                    // Journal content
050    
051                    JournalContentUtil.clearCache();
052    
053                    // Layout cache
054    
055                    CacheUtil.clearCache();
056            }
057    
058            public void setMultiVMPool(MultiVMPool multiVMPool) {
059                    _multiVMPool = multiVMPool;
060            }
061    
062            private String _createToken(long imageId) {
063                    return String.valueOf(System.currentTimeMillis());
064            }
065    
066            private static final String _CACHE_NAME =
067                    WebServerServletToken.class.getName();
068    
069            private MultiVMPool _multiVMPool;
070            private PortalCache _portalCache;
071    
072    }