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.template;
016    
017    import com.liferay.portal.kernel.cache.PortalCache;
018    import com.liferay.portal.kernel.cache.PortalCacheException;
019    import com.liferay.portal.kernel.cache.PortalCacheListener;
020    import com.liferay.portal.kernel.cache.SingleVMPoolUtil;
021    import com.liferay.portal.kernel.template.TemplateResource;
022    import com.liferay.portal.kernel.util.StringPool;
023    
024    /**
025     * @author Tina Tian
026     */
027    public class TemplateResourcePortalCacheListener
028            implements PortalCacheListener<String, TemplateResource> {
029    
030            public TemplateResourcePortalCacheListener(
031                    String templateResourceLoaderName) {
032    
033                    String portalCacheName = TemplateResource.class.getName();
034    
035                    portalCacheName = portalCacheName.concat(StringPool.POUND).concat(
036                            templateResourceLoaderName);
037    
038                    _portalCache = SingleVMPoolUtil.getPortalCache(portalCacheName);
039            }
040    
041            @Override
042            public void dispose() {
043            }
044    
045            @Override
046            public void notifyEntryEvicted(
047                            PortalCache<String, TemplateResource> portalCache, String key,
048                            TemplateResource templateResource, int timeToLive)
049                    throws PortalCacheException {
050    
051                    if (templateResource != null) {
052                            _portalCache.remove(templateResource);
053                    }
054            }
055    
056            @Override
057            public void notifyEntryExpired(
058                            PortalCache<String, TemplateResource> portalCache, String key,
059                            TemplateResource templateResource, int timeToLive)
060                    throws PortalCacheException {
061    
062                    if (templateResource != null) {
063                            _portalCache.remove(templateResource);
064                    }
065            }
066    
067            @Override
068            public void notifyEntryPut(
069                            PortalCache<String, TemplateResource> portalCache, String key,
070                            TemplateResource templateResource, int timeToLive)
071                    throws PortalCacheException {
072            }
073    
074            @Override
075            public void notifyEntryRemoved(
076                            PortalCache<String, TemplateResource> portalCache, String key,
077                            TemplateResource templateResource, int timeToLive)
078                    throws PortalCacheException {
079    
080                    if (templateResource != null) {
081                            _portalCache.remove(templateResource);
082                    }
083            }
084    
085            @Override
086            public void notifyEntryUpdated(
087                            PortalCache<String, TemplateResource> portalCache, String key,
088                            TemplateResource templateResource, int timeToLive)
089                    throws PortalCacheException {
090    
091                    if (templateResource != null) {
092                            _portalCache.remove(templateResource);
093                    }
094            }
095    
096            @Override
097            public void notifyRemoveAll(
098                            PortalCache<String, TemplateResource> portalCache)
099                    throws PortalCacheException {
100    
101                    _portalCache.removeAll();
102            }
103    
104            private final PortalCache<TemplateResource, ?> _portalCache;
105    
106    }