1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.cache.mbean;
35
36 import info.magnolia.cms.util.MBeanUtil;
37 import info.magnolia.commands.CommandsManager;
38 import info.magnolia.commands.chain.Command;
39 import info.magnolia.context.Context;
40 import info.magnolia.context.SimpleContext;
41 import info.magnolia.context.SystemContext;
42 import info.magnolia.module.cache.Cache;
43 import info.magnolia.module.cache.CacheFactory;
44 import info.magnolia.module.cache.CacheModule;
45 import info.magnolia.module.cache.cachekey.DefaultCacheKey;
46 import info.magnolia.module.cache.cachepolicy.Default;
47 import info.magnolia.module.cache.inject.CacheFactoryProvider;
48 import info.magnolia.objectfactory.Components;
49
50 import java.util.HashMap;
51 import java.util.Map;
52
53 import javax.inject.Inject;
54 import javax.inject.Provider;
55 import javax.inject.Singleton;
56
57
58
59
60 @Singleton
61 public class CacheMonitor implements CacheMonitorMBean {
62
63 private final CommandsManager commandsManager;
64 private final CacheFactoryProvider cacheFactoryProvider;
65 private final SystemContext systemContext;
66
67 private int start;
68 private int stop;
69 private Map<String, Integer> calls = new HashMap<String, Integer>();
70 private Map<String, Integer> caches = new HashMap<String, Integer>();
71 private Map<String, Integer> domains = new HashMap<String, Integer>();
72
73
74 @Inject
75 public CacheMonitor(CommandsManager commandsManager, CacheFactoryProvider cacheFactoryProvider, SystemContext systemContext) {
76 MBeanUtil.registerMBean("CacheMonitor", this);
77 this.commandsManager = commandsManager;
78 this.cacheFactoryProvider = cacheFactoryProvider;
79 this.systemContext = systemContext;
80 }
81
82
83
84
85 @Deprecated
86 public CacheMonitor(CommandsManager commandsManager, Provider<CacheModule> cacheModuleProvider, SystemContext systemContext) {
87 MBeanUtil.registerMBean("CacheMonitor", this);
88 this.commandsManager = commandsManager;
89 this.systemContext = systemContext;
90 this.cacheFactoryProvider = Components.getComponent(CacheFactoryProvider.class);
91 }
92
93 private Command getCommand(String commandName) {
94 Command flush = commandsManager.getCommand("cache", commandName);
95 if (flush == null) {
96
97 commandsManager.reload();
98 flush = commandsManager.getCommand("cache", commandName);
99 }
100 if (flush == null) {
101 throw new RuntimeException("Failed to invoke cache " + commandName + " command");
102 }
103 return flush;
104 }
105
106 public void countStart() {
107 start++;
108 }
109
110 public void stop() {
111 stop++;
112 }
113
114
115
116
117 public void logBehavior(String name) {
118 Integer count = calls.get(name);
119 calls.put(name, count == null ? 1 : ++count);
120 }
121
122 public void countFlush(String name) {
123 caches.put(name, caches.get(name) == null ? 1 : caches.get(name) + 1);
124 }
125
126 public void addCache(String name) {
127 caches.put(name, 0);
128 }
129
130 public void logAccess(Object cacheKey) {
131 if (cacheKey == null || !(cacheKey instanceof DefaultCacheKey)) {
132 return;
133 }
134 DefaultCacheKey key = (DefaultCacheKey) cacheKey;
135 Integer count = this.domains.get(key.getDomain());
136 this.domains.put(key.getDomain(), count == null ? 1 : ++count);
137 }
138
139
140 @Override
141 public void flushAll() throws Exception {
142 Command flush = getCommand("flushAll");
143 flush.execute(systemContext);
144 }
145
146 @Override
147 public void flushByUUID(String repository, String uuid) throws Exception {
148 Command flush = getCommand("flushByUUID");
149 Context ctx = new SimpleContext(systemContext);
150 ctx.put("repository", repository);
151 ctx.put("uuid", uuid);
152 flush.execute(ctx);
153 }
154
155
156 @Override
157 public Map<String, Integer> getAll() {
158 return calls;
159 }
160
161 @Override
162 public int getHits() {
163 return calls.get("useCache");
164 }
165
166 @Override
167 public int getBypasses() {
168 return calls.get("bypass");
169 }
170
171 @Override
172 public int getPuts() {
173 return calls.get("store");
174 }
175
176 @Override
177 public int getStopCalls() {
178 return stop;
179 }
180
181 @Override
182 public int getStartCalls() {
183 return start;
184 }
185
186 @Override
187 public Map<String, Integer> getFlushes() {
188 return caches;
189 }
190
191 @Override
192 public Map<String, Integer> getDomainAccesses() {
193 return domains;
194 }
195
196 @Override
197 public int getCachedKeysCount() {
198 int count = 0;
199
200 CacheFactory factory = cacheFactoryProvider.get();
201 for (String name : caches.keySet()) {
202
203 if (Default.UUID_KEY_MAP_KEY.equals(name)) {
204 continue;
205 }
206 Cache cache = factory.getCache(name);
207 count += cache.getSize();
208 }
209 return count;
210 }
211
212 @Override
213 public int getCachedUUIDsCount() {
214 CacheFactory factory = cacheFactoryProvider.get();
215 Cache cache = factory.getCache(Default.UUID_KEY_MAP_KEY);
216 return cache.getSize();
217 }
218 }