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