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