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            @Override
033            public void clear() {
034                    for (Map.Entry<String, CacheRegistryItem> entry :
035                                    _cacheRegistryItems.entrySet()) {
036    
037                            CacheRegistryItem cacheRegistryItem = entry.getValue();
038    
039                            if (_log.isDebugEnabled()) {
040                                    _log.debug(
041                                            "Invalidating " + cacheRegistryItem.getRegistryName());
042                            }
043    
044                            cacheRegistryItem.invalidate();
045                    }
046            }
047    
048            @Override
049            public void clear(String name) {
050                    CacheRegistryItem cacheRegistryItem = _cacheRegistryItems.get(name);
051    
052                    if (cacheRegistryItem != null) {
053                            if (_log.isDebugEnabled()) {
054                                    _log.debug("Invalidating " + name);
055                            }
056    
057                            cacheRegistryItem.invalidate();
058                    }
059                    else {
060                            _log.error("No cache registry found with name " + name);
061                    }
062            }
063    
064            @Override
065            public boolean isActive() {
066                    return _active;
067            }
068    
069            @Override
070            public void register(CacheRegistryItem cacheRegistryItem) {
071                    String name = cacheRegistryItem.getRegistryName();
072    
073                    if (_log.isDebugEnabled()) {
074                            _log.debug("Registering " + name);
075                    }
076    
077                    if (_cacheRegistryItems.containsKey(name)) {
078                            if (_log.isDebugEnabled()) {
079                                    _log.debug("Not registering duplicate " + name);
080                            }
081                    }
082                    else {
083                            _cacheRegistryItems.put(name, cacheRegistryItem);
084                    }
085            }
086    
087            @Override
088            public void setActive(boolean active) {
089                    _active = active;
090    
091                    if (!active) {
092                            clear();
093                    }
094            }
095    
096            @Override
097            public void unregister(String name) {
098                    if (_log.isDebugEnabled()) {
099                            _log.debug("Unregistering " + name);
100                    }
101    
102                    _cacheRegistryItems.remove(name);
103            }
104    
105            private static Log _log = LogFactoryUtil.getLog(CacheRegistryImpl.class);
106    
107            private boolean _active = true;
108            private Map<String, CacheRegistryItem> _cacheRegistryItems =
109                    new ConcurrentHashMap<String, CacheRegistryItem>();
110    
111    }