001    /**
002     * Copyright (c) 2000-2012 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            public void notifyEntryEvicted(
040                            PortalCache<String, TemplateResource> portalCache, String key,
041                            TemplateResource templateResource)
042                    throws PortalCacheException {
043    
044                    if (templateResource != null) {
045                            _portalCache.remove(templateResource);
046                    }
047            }
048    
049            public void notifyEntryExpired(
050                            PortalCache<String, TemplateResource> portalCache, String key,
051                            TemplateResource templateResource)
052                    throws PortalCacheException {
053    
054                    if (templateResource != null) {
055                            _portalCache.remove(templateResource);
056                    }
057            }
058    
059            public void notifyEntryPut(
060                            PortalCache<String, TemplateResource> portalCache, String key,
061                            TemplateResource templateResource)
062                    throws PortalCacheException {
063            }
064    
065            public void notifyEntryRemoved(
066                            PortalCache<String, TemplateResource> portalCache, String key,
067                            TemplateResource templateResource)
068                    throws PortalCacheException {
069    
070                    if (templateResource != null) {
071                            _portalCache.remove(templateResource);
072                    }
073            }
074    
075            public void notifyEntryUpdated(
076                            PortalCache<String, TemplateResource> portalCache, String key,
077                            TemplateResource templateResource)
078                    throws PortalCacheException {
079    
080                    if (templateResource != null) {
081                            _portalCache.remove(templateResource);
082                    }
083            }
084    
085            public void notifyRemoveAll(
086                            PortalCache<String, TemplateResource> portalCache)
087                    throws PortalCacheException {
088    
089                    _portalCache.removeAll();
090            }
091    
092            private PortalCache<TemplateResource, ?> _portalCache;
093    
094    }