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.context.Context;
39 import info.magnolia.context.MgnlContext;
40 import info.magnolia.context.SimpleContext;
41 import info.magnolia.module.ModuleRegistry;
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.DefaultCacheKey;
46 import info.magnolia.module.cache.cachepolicy.Default;
47
48 import java.util.HashMap;
49 import java.util.Map;
50
51 import org.apache.commons.chain.Command;
52
53
54
55
56
57 public class CacheMonitor implements CacheMonitorMBean {
58
59 private static CacheMonitor instance = new CacheMonitor();
60 private int start;
61 private int stop;
62 private Map<String, Integer> calls = new HashMap<String, Integer>();
63 private Map<String, Integer> caches = new HashMap<String, Integer>();
64 private Map<String, Integer> domains = new HashMap<String, Integer>();
65 private CommandsManager commandsManager;
66
67 public CacheMonitor() {
68 MBeanUtil.registerMBean("CacheMonitor", this);
69 commandsManager = CommandsManager.getInstance();
70 }
71
72 private CacheFactory getCacheFactory() {
73 CacheFactory factory = ModuleRegistry.Factory.getInstance().getModuleInstance(CacheModule.class).getCacheFactory();
74 return factory;
75 }
76
77 private Command getCommand(String commandName) {
78 Command flush = commandsManager.getCommand("cache", commandName);
79 if (flush == null) {
80
81 commandsManager.reload();
82 flush = commandsManager.getCommand("cache", commandName);
83 }
84 if (flush == null) {
85 throw new RuntimeException("Failed to invoke cache " + commandName + " command");
86 }
87 return flush;
88 }
89
90 public static CacheMonitor getInstance() {
91 return instance;
92 }
93
94 public void countStart() {
95 start++;
96 }
97
98 public void stop() {
99 stop++;
100 }
101
102
103
104
105 public void logBehavior(String name) {
106 Integer count = calls.get(name);
107 calls.put(name, count == null ? 1 : ++count);
108 }
109
110 public void countFlush(String name) {
111 caches.put(name, caches.get(name) + 1);
112 }
113
114 public void addCache(String name) {
115 caches.put(name, 0);
116 }
117
118 public void logAccess(Object cacheKey) {
119 if (cacheKey == null || !(cacheKey instanceof DefaultCacheKey)) {
120 return;
121 }
122 DefaultCacheKey key = (DefaultCacheKey) cacheKey;
123 Integer count = this.domains.get(key.getDomain());
124 this.domains.put(key.getDomain(), count == null ? 1 : ++count);
125 }
126
127
128 public void flushAll() throws Exception {
129 Command flush = getCommand("flushAll");
130 flush.execute(MgnlContext.getSystemContext());
131 }
132
133 public void flushByUUID(String repository, String uuid) throws Exception {
134 Command flush = getCommand("flushByUUID");
135 Context ctx = new SimpleContext(MgnlContext.getSystemContext());
136 ctx.put("repository", repository);
137 ctx.put("uuid", uuid);
138 flush.execute(ctx);
139 }
140
141
142 public Map<String, Integer> getAll() {
143 return calls;
144 }
145
146 public int getHits() {
147 return calls.get("useCache");
148 }
149
150 public int getBypasses() {
151 return calls.get("bypass");
152 }
153
154 public int getPuts() {
155 return calls.get("store");
156 }
157
158 public int getStopCalls() {
159 return stop;
160 }
161
162 public int getStartCalls() {
163 return start;
164 }
165
166 public Map<String, Integer> getFlushes() {
167 return caches;
168 }
169
170 public Map<String, Integer> getDomainAccesses() {
171 return domains;
172 }
173
174 public int getCachedKeysCount() {
175 int count = 0;
176
177 CacheFactory factory = getCacheFactory();
178 for (String name : caches.keySet()) {
179
180 if (Default.UUID_KEY_MAP_KEY.equals(name)) {
181 continue;
182 }
183 Cache cache = factory.getCache(name);
184 count += cache.getSize();
185 }
186 return count;
187 }
188
189 public int getCachedUUIDsCount() {
190 CacheFactory factory = getCacheFactory();
191 Cache cache = factory.getCache(Default.UUID_KEY_MAP_KEY);
192 return cache.getSize();
193 }
194 }