1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.cache;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  
20  import java.util.Map;
21  import java.util.concurrent.ConcurrentHashMap;
22  
23  /**
24   * <a href="CacheRegistry.java.html"><b><i>View Source</i></b></a>
25   *
26   * @author Brian Wing Shun Chan
27   */
28  public class CacheRegistry {
29  
30      public static void clear() {
31          for (Map.Entry<String, CacheRegistryItem> entry : _items.entrySet()) {
32              CacheRegistryItem item = entry.getValue();
33  
34              if (_log.isDebugEnabled()) {
35                  _log.debug("Invalidating " + item.getRegistryName());
36              }
37  
38              item.invalidate();
39          }
40      }
41  
42      public static void clear(String name) {
43          CacheRegistryItem item = _items.get(name);
44  
45          if (item != null) {
46              if (_log.isDebugEnabled()) {
47                  _log.debug("Invalidating " + name);
48              }
49  
50              item.invalidate();
51          }
52          else {
53              _log.error("No cache registry found with name " + name);
54          }
55      }
56  
57      public static boolean isActive() {
58          return _active;
59      }
60  
61      public static void register(CacheRegistryItem item) {
62          String name = item.getRegistryName();
63  
64          if (_log.isDebugEnabled()) {
65              _log.debug("Registering " + name);
66          }
67  
68          if (_items.containsKey(name)) {
69              if (_log.isDebugEnabled()) {
70                  _log.debug("Not registering duplicate " + name);
71              }
72          }
73          else {
74              _items.put(name, item);
75          }
76      }
77  
78      public static void setActive(boolean active) {
79          _active = active;
80  
81          if (!active) {
82              clear();
83          }
84      }
85  
86      public static void unregister(String name) {
87          if (_log.isDebugEnabled()) {
88              _log.debug("Unregistering " + name);
89          }
90  
91          _items.remove(name);
92      }
93  
94      private static Log _log = LogFactoryUtil.getLog(CacheRegistry.class);
95  
96      private static boolean _active = true;
97      private static Map<String, CacheRegistryItem> _items =
98          new ConcurrentHashMap<String, CacheRegistryItem>();
99  
100 }