001    /**
002     * Copyright (c) 2000-2013 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.kernel.security.pacl.DoPrivileged;
020    import com.liferay.portal.servlet.filters.cache.CacheUtil;
021    import com.liferay.portlet.journalcontent.util.JournalContentUtil;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     * @since  6.1, replaced com.liferay.portal.servlet.ImageServletTokenImpl
026     */
027    @DoPrivileged
028    public class WebServerServletTokenImpl implements WebServerServletToken {
029    
030            public void afterPropertiesSet() {
031                    _portalCache = (PortalCache<Long, String>)_multiVMPool.getCache(
032                            _CACHE_NAME);
033            }
034    
035            @Override
036            public String getToken(long imageId) {
037                    Long key = imageId;
038    
039                    String token = _portalCache.get(key);
040    
041                    if (token == null) {
042                            token = _createToken(imageId);
043    
044                            _portalCache.put(key, token);
045                    }
046    
047                    return token;
048            }
049    
050            @Override
051            public void resetToken(long imageId) {
052                    _portalCache.remove(imageId);
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 static final String _CACHE_NAME =
072                    WebServerServletToken.class.getName();
073    
074            private MultiVMPool _multiVMPool;
075            private PortalCache<Long, String> _portalCache;
076    
077    }