1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.cache;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  
28  import java.util.Map;
29  import java.util.concurrent.ConcurrentHashMap;
30  
31  /**
32   * <a href="CacheRegistry.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class CacheRegistry {
37  
38      public static void clear() {
39          for (Map.Entry<String, CacheRegistryItem> entry : _items.entrySet()) {
40              CacheRegistryItem item = entry.getValue();
41  
42              if (_log.isDebugEnabled()) {
43                  _log.debug("Invalidating " + item.getRegistryName());
44              }
45  
46              item.invalidate();
47          }
48      }
49  
50      public static void clear(String name) {
51          CacheRegistryItem item = _items.get(name);
52  
53          if (item != null) {
54              if (_log.isDebugEnabled()) {
55                  _log.debug("Invalidating " + name);
56              }
57  
58              item.invalidate();
59          }
60          else {
61              _log.error("No cache registry found with name " + name);
62          }
63      }
64  
65      public static boolean isActive() {
66          return _active;
67      }
68  
69      public static void register(CacheRegistryItem item) {
70          String name = item.getRegistryName();
71  
72          if (_log.isDebugEnabled()) {
73              _log.debug("Registering " + name);
74          }
75  
76          if (_items.containsKey(name)) {
77              if (_log.isDebugEnabled()) {
78                  _log.debug("Not registering duplicate " + name);
79              }
80          }
81          else {
82              _items.put(name, item);
83          }
84      }
85  
86      public static void setActive(boolean active) {
87          _active = active;
88  
89          if (!active) {
90              clear();
91          }
92      }
93  
94      public static void unregister(String name) {
95          if (_log.isDebugEnabled()) {
96              _log.debug("Unregistering " + name);
97          }
98  
99          _items.remove(name);
100     }
101 
102     private static Log _log = LogFactoryUtil.getLog(CacheRegistry.class);
103 
104     private static boolean _active = true;
105     private static Map<String, CacheRegistryItem> _items =
106         new ConcurrentHashMap<String, CacheRegistryItem>();
107 
108 }