001    /**
002     * Copyright (c) 2000-2013 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.cache;
016    
017    import com.liferay.portal.kernel.cache.CacheRegistry;
018    import com.liferay.portal.kernel.cache.CacheRegistryItem;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
022    
023    import java.util.Map;
024    import java.util.concurrent.ConcurrentHashMap;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    @DoPrivileged
030    public class CacheRegistryImpl implements CacheRegistry {
031    
032            public void clear() {
033                    for (Map.Entry<String, CacheRegistryItem> entry :
034                                    _cacheRegistryItems.entrySet()) {
035    
036                            CacheRegistryItem cacheRegistryItem = entry.getValue();
037    
038                            if (_log.isDebugEnabled()) {
039                                    _log.debug(
040                                            "Invalidating " + cacheRegistryItem.getRegistryName());
041                            }
042    
043                            cacheRegistryItem.invalidate();
044                    }
045            }
046    
047            public void clear(String name) {
048                    CacheRegistryItem cacheRegistryItem = _cacheRegistryItems.get(name);
049    
050                    if (cacheRegistryItem != null) {
051                            if (_log.isDebugEnabled()) {
052                                    _log.debug("Invalidating " + name);
053                            }
054    
055                            cacheRegistryItem.invalidate();
056                    }
057                    else {
058                            _log.error("No cache registry found with name " + name);
059                    }
060            }
061    
062            public boolean isActive() {
063                    return _active;
064            }
065    
066            public void register(CacheRegistryItem cacheRegistryItem) {
067                    String name = cacheRegistryItem.getRegistryName();
068    
069                    if (_log.isDebugEnabled()) {
070                            _log.debug("Registering " + name);
071                    }
072    
073                    if (_cacheRegistryItems.containsKey(name)) {
074                            if (_log.isDebugEnabled()) {
075                                    _log.debug("Not registering duplicate " + name);
076                            }
077                    }
078                    else {
079                            _cacheRegistryItems.put(name, cacheRegistryItem);
080                    }
081            }
082    
083            public void setActive(boolean active) {
084                    _active = active;
085    
086                    if (!active) {
087                            clear();
088                    }
089            }
090    
091            public void unregister(String name) {
092                    if (_log.isDebugEnabled()) {
093                            _log.debug("Unregistering " + name);
094                    }
095    
096                    _cacheRegistryItems.remove(name);
097            }
098    
099            private static Log _log = LogFactoryUtil.getLog(CacheRegistryImpl.class);
100    
101            private boolean _active = true;
102            private Map<String, CacheRegistryItem> _cacheRegistryItems =
103                    new ConcurrentHashMap<String, CacheRegistryItem>();
104    
105    }