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