001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    
022    import java.util.Map;
023    import java.util.concurrent.ConcurrentHashMap;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class CacheRegistryImpl implements CacheRegistry {
029    
030            public void clear() {
031                    for (Map.Entry<String, CacheRegistryItem> entry :
032                                    _cacheRegistryItems.entrySet()) {
033    
034                            CacheRegistryItem cacheRegistryItem = entry.getValue();
035    
036                            if (_log.isDebugEnabled()) {
037                                    _log.debug(
038                                            "Invalidating " + cacheRegistryItem.getRegistryName());
039                            }
040    
041                            cacheRegistryItem.invalidate();
042                    }
043            }
044    
045            public void clear(String name) {
046                    CacheRegistryItem cacheRegistryItem = _cacheRegistryItems.get(name);
047    
048                    if (cacheRegistryItem != null) {
049                            if (_log.isDebugEnabled()) {
050                                    _log.debug("Invalidating " + name);
051                            }
052    
053                            cacheRegistryItem.invalidate();
054                    }
055                    else {
056                            _log.error("No cache registry found with name " + name);
057                    }
058            }
059    
060            public boolean isActive() {
061                    return _active;
062            }
063    
064            public void register(CacheRegistryItem cacheRegistryItem) {
065                    String name = cacheRegistryItem.getRegistryName();
066    
067                    if (_log.isDebugEnabled()) {
068                            _log.debug("Registering " + name);
069                    }
070    
071                    if (_cacheRegistryItems.containsKey(name)) {
072                            if (_log.isDebugEnabled()) {
073                                    _log.debug("Not registering duplicate " + name);
074                            }
075                    }
076                    else {
077                            _cacheRegistryItems.put(name, cacheRegistryItem);
078                    }
079            }
080    
081            public void setActive(boolean active) {
082                    _active = active;
083    
084                    if (!active) {
085                            clear();
086                    }
087            }
088    
089            public void unregister(String name) {
090                    if (_log.isDebugEnabled()) {
091                            _log.debug("Unregistering " + name);
092                    }
093    
094                    _cacheRegistryItems.remove(name);
095            }
096    
097            private static Log _log = LogFactoryUtil.getLog(CacheRegistryImpl.class);
098    
099            private boolean _active = true;
100            private Map<String, CacheRegistryItem> _cacheRegistryItems =
101                    new ConcurrentHashMap<String, CacheRegistryItem>();
102    
103    }