001    /**
002     * Copyright (c) 2000-present 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.kernel.webserver.WebServerServletToken;
021    import com.liferay.portal.servlet.filters.cache.CacheUtil;
022    import com.liferay.registry.Registry;
023    import com.liferay.registry.RegistryUtil;
024    import com.liferay.registry.dependency.ServiceDependencyListener;
025    import com.liferay.registry.dependency.ServiceDependencyManager;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     * @since  6.1, replaced com.liferay.portal.servlet.ImageServletTokenImpl
030     */
031    @DoPrivileged
032    public class WebServerServletTokenImpl implements WebServerServletToken {
033    
034            public void afterPropertiesSet() {
035                    ServiceDependencyManager serviceDependencyManager =
036                            new ServiceDependencyManager();
037    
038                    serviceDependencyManager.addServiceDependencyListener(
039                            new ServiceDependencyListener() {
040    
041                                    @Override
042                                    public void dependenciesFulfilled() {
043                                            Registry registry = RegistryUtil.getRegistry();
044    
045                                            MultiVMPool multiVMPool = registry.getService(
046                                                    MultiVMPool.class);
047    
048                                            _portalCache =
049                                                    (PortalCache<Long, String>)multiVMPool.getPortalCache(
050                                                            _CACHE_NAME);
051                                    }
052    
053                                    @Override
054                                    public void destroy() {
055                                    }
056    
057                            });
058    
059                    serviceDependencyManager.registerDependencies(MultiVMPool.class);
060            }
061    
062            @Override
063            public String getToken(long imageId) {
064                    Long key = imageId;
065    
066                    String token = _portalCache.get(key);
067    
068                    if (token == null) {
069                            token = _createToken();
070    
071                            _portalCache.put(key, token);
072                    }
073    
074                    return token;
075            }
076    
077            @Override
078            public void resetToken(long imageId) {
079                    _portalCache.remove(imageId);
080    
081                    // Layout cache
082    
083                    CacheUtil.clearCache();
084            }
085    
086            private String _createToken() {
087                    return String.valueOf(System.currentTimeMillis());
088            }
089    
090            private static final String _CACHE_NAME =
091                    WebServerServletToken.class.getName();
092    
093            private PortalCache<Long, String> _portalCache;
094    
095    }