001
014
015 package com.liferay.portal.kernel.cache;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.registry.Registry;
020 import com.liferay.registry.RegistryUtil;
021 import com.liferay.registry.ServiceReference;
022 import com.liferay.registry.ServiceTracker;
023 import com.liferay.registry.ServiceTrackerCustomizer;
024
025 import java.util.Map;
026 import java.util.concurrent.ConcurrentHashMap;
027
028
031 public class CacheRegistryUtil {
032
033 public static void clear() {
034 _instance._clear();
035 }
036
037 public static void clear(String name) {
038 _instance._clear(name);
039 }
040
041 public static boolean isActive() {
042 return _instance._isActive();
043 }
044
045 public static void setActive(boolean active) {
046 _instance._setActive(active);
047 }
048
049 private CacheRegistryUtil() {
050 Registry registry = RegistryUtil.getRegistry();
051
052 _serviceTracker = registry.trackServices(
053 CacheRegistryItem.class,
054 new CacheRegistryItemServiceTrackerCustomizer());
055
056 _serviceTracker.open();
057 }
058
059 private void _clear() {
060 for (Map.Entry<String, CacheRegistryItem> entry :
061 _cacheRegistryItems.entrySet()) {
062
063 CacheRegistryItem cacheRegistryItem = entry.getValue();
064
065 if (_log.isDebugEnabled()) {
066 _log.debug(
067 "Invalidating " + cacheRegistryItem.getRegistryName());
068 }
069
070 cacheRegistryItem.invalidate();
071 }
072 }
073
074 private void _clear(String name) {
075 CacheRegistryItem cacheRegistryItem = _cacheRegistryItems.get(name);
076
077 if (cacheRegistryItem != null) {
078 if (_log.isDebugEnabled()) {
079 _log.debug("Invalidating " + name);
080 }
081
082 cacheRegistryItem.invalidate();
083 }
084 else {
085 _log.error("No cache registry found with name " + name);
086 }
087 }
088
089 private boolean _isActive() {
090 return _active;
091 }
092
093 private void _setActive(boolean active) {
094 _active = active;
095
096 if (!active) {
097 _clear();
098 }
099 }
100
101 private static final Log _log = LogFactoryUtil.getLog(
102 CacheRegistryUtil.class);
103
104 private static final CacheRegistryUtil _instance = new CacheRegistryUtil();
105
106 private volatile boolean _active = true;
107 private final Map<String, CacheRegistryItem> _cacheRegistryItems =
108 new ConcurrentHashMap<>();
109 private final ServiceTracker<CacheRegistryItem, CacheRegistryItem>
110 _serviceTracker;
111
112 private class CacheRegistryItemServiceTrackerCustomizer
113 implements ServiceTrackerCustomizer
114 <CacheRegistryItem, CacheRegistryItem> {
115
116 @Override
117 public CacheRegistryItem addingService(
118 ServiceReference<CacheRegistryItem> serviceReference) {
119
120 Registry registry = RegistryUtil.getRegistry();
121
122 CacheRegistryItem cacheRegistryItem = registry.getService(
123 serviceReference);
124
125 _cacheRegistryItems.put(
126 cacheRegistryItem.getRegistryName(), cacheRegistryItem);
127
128 return cacheRegistryItem;
129 }
130
131 @Override
132 public void modifiedService(
133 ServiceReference<CacheRegistryItem> serviceReference,
134 CacheRegistryItem cacheRegistryItem) {
135 }
136
137 @Override
138 public void removedService(
139 ServiceReference<CacheRegistryItem> serviceReference,
140 CacheRegistryItem cacheRegistryItem) {
141
142 _cacheRegistryItems.remove(cacheRegistryItem.getRegistryName());
143 }
144
145 }
146
147 }