1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package ch.elca.el4j.services.statistics.light;
19
20 import java.text.MessageFormat;
21
22 import javax.management.MBeanServer;
23 import javax.management.ObjectName;
24
25 import org.springframework.beans.factory.DisposableBean;
26 import org.springframework.beans.factory.InitializingBean;
27
28 import ch.elca.el4j.services.monitoring.jmx.display.DisplayManager;
29 import ch.elca.el4j.services.monitoring.jmx.display.HtmlDisplayManager;
30 import ch.elca.el4j.services.monitoring.jmx.display.HtmlTabulator;
31 import ch.elca.el4j.services.monitoring.jmx.display.Section;
32
33 import com.jamonapi.MonitorFactory;
34
35
36
37
38
39
40
41
42 public class LightStatisticsReporter
43 implements LightStatisticsReporterMBean, InitializingBean,
44 DisposableBean {
45
46
47 public static final String NAME = "Performance:key=lightStatisticsReporter";
48
49
50 private static String[] CAPTIONS = {
51 "Target", "Hits", "Avg",
52 "Total", "StdDev", "LastValue",
53 "Min", "Max", "Active",
54 "AvgActive", "MaxActive", "FirstAccess",
55 "LastAccess", "Enabled", "Primary", "HasListeners"
56 };
57
58
59
60 private String m_formatString;
61
62
63 private MBeanServer m_server;
64
65
66 private boolean m_fullyQualified = false;
67
68
69
70
71 public String getFormatString() {
72 return m_formatString;
73 }
74
75
76
77
78 public void setFormatString(String formatString) {
79 this.m_formatString = formatString;
80 }
81
82
83
84
85 public boolean isFullyQualified() {
86 return m_fullyQualified;
87 }
88
89
90
91
92 public void setFullyQualified(boolean fullyQualified) {
93 this.m_fullyQualified = fullyQualified;
94 }
95
96
97
98
99
100 public MBeanServer getServer() {
101 return m_server;
102 }
103
104
105
106
107
108 public void setServer(MBeanServer beanServer) {
109 m_server = beanServer;
110 }
111
112
113
114
115 public void resetMonitor() {
116 MonitorFactory.reset();
117 }
118
119
120
121
122 public void enableMonitor() {
123 MonitorFactory.setEnabled(true);
124 }
125
126
127
128
129 public void disableMonitor() {
130 MonitorFactory.setEnabled(false);
131 }
132
133
134
135
136 public String[] getData() {
137 Object[][] dataAsObj = MonitorFactory.getRootMonitor().getBasicData();
138
139 if (dataAsObj == null) {
140 return new String[] {" "};
141 }
142
143
144 String[][] data;
145 data = new String[dataAsObj.length][];
146 for (int i = 0; i < dataAsObj.length; i++) {
147 String[] line = new String[dataAsObj[i].length];
148 for (int j = 0; j < dataAsObj[i].length; j++) {
149 line[j] = (dataAsObj[i][j]).toString();
150 }
151 data[i] = line;
152 }
153
154
155 String[] paddedCaptions = new String[CAPTIONS.length];
156 String[] result = new String[data.length + 1];
157 MessageFormat format = new MessageFormat(m_formatString);
158
159 int[] lengths = prepareData(data);
160
161
162 for (int i = 0; i < CAPTIONS.length; i++) {
163 paddedCaptions[i] = padString(CAPTIONS[i], lengths[i]);
164 }
165
166
167 result[0] = format.format(paddedCaptions);
168
169
170 if (data[0].length >= CAPTIONS.length) {
171
172 for (int i = 0; i < data.length; i++) {
173 for (int j = 0; j < CAPTIONS.length; j++) {
174 data[i][j] = padString(data[i][j], lengths[j]);
175 }
176 }
177
178
179 for (int i = 0; i < data.length; i++) {
180 result[i + 1] = format.format(data[i]);
181 }
182 }
183
184 return result;
185 }
186
187
188
189
190
191
192
193
194 protected int[] prepareData(String[][] data) {
195 int[] lengths = new int[CAPTIONS.length];
196
197 for (int i = 0; i < CAPTIONS.length; i++) {
198 lengths[i] = CAPTIONS[i].length();
199 }
200
201
202 if (data[0].length == 1) {
203 return lengths;
204 }
205
206
207 for (int j = 0; j < data.length; j++) {
208 data[j][0] = convertClassName(data[j][0]);
209 int len = data[j][0].length();
210 if (len > lengths[0]) {
211 lengths[0] = len;
212 }
213 }
214
215 for (int i = 1; i < CAPTIONS.length; i++) {
216 for (int j = 0; j < data.length; j++) {
217
218 data[j][i] = data[j][i].replaceAll(" ", " ");
219
220
221 int len = data[j][i].length();
222 if (len > lengths[i]) {
223 lengths[i] = len;
224 }
225 }
226 }
227 return lengths;
228 }
229
230
231
232
233
234
235
236 private String padString(String s, int len) {
237 StringBuffer buffer = new StringBuffer(s);
238 for (int i = s.length(); i < len; i++) {
239 buffer.append("_");
240 }
241 return buffer.toString();
242 }
243
244
245
246
247
248
249
250
251 private String convertClassName(String className) {
252 if (m_fullyQualified) {
253 return className;
254
255 } else {
256 int pos = className.lastIndexOf('.');
257 String fqClassName = className.substring(0, pos);
258 pos = fqClassName.lastIndexOf('.');
259 return className.substring(pos + 1);
260 }
261 }
262
263
264
265
266 public void afterPropertiesSet() throws Exception {
267 if (m_server == null) {
268 throw new IllegalStateException("m_beanServer has not been set!");
269 }
270
271 m_server.registerMBean(this, new ObjectName(NAME));
272 }
273
274
275
276
277 public void destroy() throws Exception {
278 if (m_server != null) {
279 m_server.unregisterMBean(new ObjectName(NAME));
280 }
281 }
282
283
284 public String report() {
285 DisplayManager manager = new HtmlDisplayManager();
286 manager.setTitle("Light Statistics");
287 Section section = new Section("Light Statistics");
288 HtmlTabulator table = new HtmlTabulator(CAPTIONS);
289 Object[][] data = MonitorFactory.getRootMonitor().getBasicData();
290
291 if (data == null) {
292 section.addWarning("No data.");
293 manager.addSection(section);
294 return manager.getPage();
295 }
296
297 for (Object[] row : data) {
298 String[] thisRow = new String[row.length];
299 for (int i = 0; i < row.length; i++) {
300 thisRow[i] = row[i].toString();
301 }
302 table.addRow(thisRow);
303 }
304 section.add(table.tabulate());
305 manager.addSection(section);
306
307 return manager.getPage();
308 }
309 }