KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > web > jmx > JMXWriter


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.web.jmx;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 import javax.management.AttributeNotFoundException JavaDoc;
23 import javax.management.InstanceNotFoundException JavaDoc;
24 import javax.management.JMException JavaDoc;
25 import javax.management.MBeanAttributeInfo JavaDoc;
26 import javax.management.MBeanException JavaDoc;
27 import javax.management.MBeanInfo JavaDoc;
28 import javax.management.MBeanServer JavaDoc;
29 import javax.management.ObjectInstance JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31 import javax.management.ReflectionException JavaDoc;
32
33 import java.io.IOException JavaDoc;
34 import java.io.PrintWriter JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Collection JavaDoc;
37 import java.util.HashSet JavaDoc;
38 import java.util.Hashtable JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.List JavaDoc;
41 import java.util.Map JavaDoc;
42 import java.util.Set JavaDoc;
43 import java.util.TreeMap JavaDoc;
44 import java.util.TreeSet JavaDoc;
45 import java.util.Map.Entry;
46
47 /**
48  * A useful class for turning JMX statistics into XML and XHTML
49  *
50  * @version $Revision: 356269 $
51  */

52 public class JMXWriter {
53     private static final Log log = LogFactory.getLog(JMXWriter.class);
54
55     private PrintWriter JavaDoc writer;
56     private ManagementContext managementContext;
57     private String JavaDoc unknownValue = "Unknown";
58
59     public JMXWriter(PrintWriter JavaDoc writer, ManagementContext context) {
60         this.writer = writer;
61         managementContext = context;
62     }
63
64     public MBeanServer JavaDoc getMBeanServer() {
65         return managementContext.getMBeanServer();
66     }
67
68     public ManagementContext getManagementContext() {
69         return managementContext;
70     }
71
72     public void setManagementContext(ManagementContext managementContext) {
73         this.managementContext = managementContext;
74     }
75
76     public void outputHtmlNamesByDomain(Collection JavaDoc names) throws IOException JavaDoc {
77         Map JavaDoc map = new TreeMap JavaDoc();
78         for (Iterator JavaDoc iter = names.iterator(); iter.hasNext();) {
79             ObjectName JavaDoc name = (ObjectName JavaDoc) iter.next();
80             String JavaDoc domain = name.getDomain();
81             List JavaDoc list = (List JavaDoc) map.get(domain);
82             if (list == null) {
83                 list = new ArrayList JavaDoc();
84                 map.put(domain, list);
85             }
86             list.add(name);
87         }
88         for (Iterator JavaDoc iter = map.entrySet().iterator(); iter.hasNext();) {
89             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
90             String JavaDoc domain = (String JavaDoc) entry.getKey();
91             names = (List JavaDoc) entry.getValue();
92
93             writer.print("<li>");
94             writer.print(domain);
95
96             writer.print("<ul>");
97             outputHtmlNamesByProperty(names, "Type");
98             writer.print("</ul>");
99             writer.print("</li>");
100         }
101     }
102
103     public void outputHtmlNamesByProperty(Collection JavaDoc names, String JavaDoc property) throws IOException JavaDoc {
104         Map JavaDoc map = new TreeMap JavaDoc();
105         for (Iterator JavaDoc iter = names.iterator(); iter.hasNext();) {
106             ObjectName JavaDoc name = (ObjectName JavaDoc) iter.next();
107             String JavaDoc propertyValue = name.getKeyProperty(property);
108             if (propertyValue == null) {
109                 propertyValue = unknownValue;
110             }
111             List JavaDoc list = (List JavaDoc) map.get(propertyValue);
112             if (list == null) {
113                 list = new ArrayList JavaDoc();
114                 map.put(propertyValue, list);
115             }
116             list.add(name);
117         }
118         for (Iterator JavaDoc iter = map.entrySet().iterator(); iter.hasNext();) {
119             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
120             String JavaDoc propertyValue = (String JavaDoc) entry.getKey();
121             names = (List JavaDoc) entry.getValue();
122
123             if (names.size() > 1) {
124                 writer.print("<li><a HREF='");
125                 printDetailURL(property, propertyValue);
126                 writer.print("'>");
127                 writer.print(propertyValue);
128                 writer.print("</a><ul>");
129
130                 // outputHtmlNames(names);
131
outputHtmlNamesSortedByShortName(names);
132                 writer.print("</ul>");
133                 writer.print("</li>");
134             }
135             else if (names.size() == 1) {
136                 ObjectName JavaDoc name = (ObjectName JavaDoc) ((List JavaDoc) names).get(0);
137                 outputHtmlName(name, propertyValue);
138             }
139         }
140     }
141
142     public void outputHtmlNamesSortedByShortName(Collection JavaDoc names) throws IOException JavaDoc {
143         Map JavaDoc map = new TreeMap JavaDoc();
144         for (Iterator JavaDoc iter = names.iterator(); iter.hasNext();) {
145             ObjectName JavaDoc name = (ObjectName JavaDoc) iter.next();
146             String JavaDoc propertyValue = getShortName(name);
147             map.put(propertyValue, name);
148         }
149         for (Iterator JavaDoc iter = map.entrySet().iterator(); iter.hasNext();) {
150             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
151             String JavaDoc description = (String JavaDoc) entry.getKey();
152             ObjectName JavaDoc name = (ObjectName JavaDoc) entry.getValue();
153             outputHtmlName(name, description);
154         }
155     }
156
157     public void outputHtmlNames(Collection JavaDoc names) throws IOException JavaDoc {
158         for (Iterator JavaDoc iter = names.iterator(); iter.hasNext();) {
159             outputHtmlNames((ObjectName JavaDoc) iter.next());
160         }
161     }
162
163     public void outputHtmlNames(ObjectName JavaDoc name) throws IOException JavaDoc {
164         outputHtmlName(name, getShortName(name));
165     }
166
167     public void outputHtmlName(ObjectName JavaDoc name, String JavaDoc shortName) throws IOException JavaDoc {
168         writer.print("<li><a HREF='");
169         printDetailURL(name);
170
171         /*
172          * Map properties = name.getKeyPropertyList(); for (Iterator iter =
173          * properties.entrySet().iterator(); iter.hasNext();) { Map.Entry entry =
174          * (Map.Entry) iter.next(); writer.print("<property name='");
175          * writer.print(entry.getKey()); writer.print("'>");
176          * writer.print(entry.getValue()); writer.println("</property>"); }
177          */

178
179         writer.print("'>");
180         // writer.print(name.getCanonicalKeyPropertyListString());
181
writer.print(shortName);
182         writer.print("</a></li>");
183     }
184
185     /**
186      * Returns a short descriptive name of the ObjectName without the domain
187      */

188     protected String JavaDoc getShortName(ObjectName JavaDoc name) {
189         // TODO Auto-generated method stub
190
String JavaDoc answer = name.toString();
191         int idx = answer.indexOf(':');
192         if (idx >= 0) {
193             return answer.substring(idx + 1);
194         }
195         return answer;
196     }
197
198     public void outputNames(Collection JavaDoc names) throws IOException JavaDoc {
199         for (Iterator JavaDoc iter = names.iterator(); iter.hasNext();) {
200             outputNames((ObjectName JavaDoc) iter.next());
201         }
202     }
203
204     public void outputNames(ObjectName JavaDoc name) throws IOException JavaDoc {
205         writer.print("<mbean name='");
206         writer.print(name.getCanonicalName());
207         writer.print("' domain='");
208         writer.print(name.getDomain());
209         writer.println("'>");
210         Map JavaDoc properties = name.getKeyPropertyList();
211         for (Iterator JavaDoc iter = properties.entrySet().iterator(); iter.hasNext();) {
212             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
213             writer.print("<property name='");
214             writer.print(entry.getKey());
215             writer.print("'>");
216             writer.print(entry.getValue());
217             writer.println("</property>");
218         }
219         writer.println("</mbean>");
220     }
221
222     public void outputDetail(Set JavaDoc names) throws IOException JavaDoc, JMException JavaDoc {
223
224         for (Iterator JavaDoc iter = names.iterator(); iter.hasNext();) {
225             outputDetail((ObjectName JavaDoc) iter.next());
226         }
227     }
228
229     public void outputDetail(ObjectName JavaDoc name) throws JMException JavaDoc, IOException JavaDoc {
230
231         MBeanServer JavaDoc beanServer = getMBeanServer();
232         MBeanInfo JavaDoc beanInfo = beanServer.getMBeanInfo(name);
233         writer.print("<mbean name='");
234         writer.print(name.getCanonicalName());
235         writer.print("' domain='");
236         writer.print(name.getDomain());
237         writer.println("'>");
238
239         MBeanAttributeInfo JavaDoc[] attributes = beanInfo.getAttributes();
240         for (int i = 0; i < attributes.length; i++) {
241             MBeanAttributeInfo JavaDoc info = attributes[i];
242             if (info.isReadable()) {
243                 String JavaDoc attributeName = info.getName();
244                 Object JavaDoc value = getAttributeValue(name, attributeName);
245                 if (value != null) {
246                     writer.print("<attribute name='");
247                     writer.print(attributeName);
248                     writer.print("' type='");
249                     writer.print(info.getType());
250                     writer.print("'>");
251                     printEncodedValue(value);
252                     writer.println("</attribute>");
253                 }
254             }
255         }
256
257         writer.println("</mbean>");
258     }
259
260     public void outputHtmlProperties(Set JavaDoc names) throws JMException JavaDoc, IOException JavaDoc {
261         if (names.size() <= 1) {
262         for (Iterator JavaDoc iter = names.iterator(); iter.hasNext();) {
263             outputHtmlProperties((ObjectName JavaDoc) iter.next());
264         }
265         }else {
266             outputHtmlPropertiesGrid(names);
267         }
268     }
269
270     public void outputHtmlPropertiesGrid(Set JavaDoc names) throws JMException JavaDoc {
271         Set JavaDoc propertyNames = new TreeSet JavaDoc();
272         Map JavaDoc[] propertyNamesPerMBeanArray = new Map JavaDoc[names.size()];
273         int beanCounter = 0;
274         for (Iterator JavaDoc iter = names.iterator(); iter.hasNext();) {
275             ObjectName JavaDoc name = (ObjectName JavaDoc) iter.next();
276             Hashtable JavaDoc keyMap = name.getKeyPropertyList();
277             propertyNamesPerMBeanArray[beanCounter++] = keyMap;
278             propertyNames.addAll(keyMap.keySet());
279         }
280
281         writer.println("<table>");
282         writer.println("<tr>");
283         writer.print("<th>Domain</th>");
284         for (Iterator JavaDoc iter = propertyNames.iterator(); iter.hasNext();) {
285             writer.print("<th>");
286             writer.print(iter.next());
287             writer.print("</th>");
288         }
289         writer.println();
290         writer.println("</tr>");
291
292         beanCounter = 0;
293         for (Iterator JavaDoc iter = names.iterator(); iter.hasNext();) {
294             ObjectName JavaDoc name = (ObjectName JavaDoc) iter.next();
295
296             writer.print("<tr><td class='domainName'>");
297             writer.print(name.getDomain());
298             writer.print("</td>");
299
300             for (Iterator JavaDoc iter2 = propertyNames.iterator(); iter2.hasNext();) {
301                 String JavaDoc propertyName = (String JavaDoc) iter2.next();
302
303                 if (propertyNamesPerMBeanArray[beanCounter].containsKey(propertyName)) {
304                     String JavaDoc value = name.getKeyProperty(propertyName);
305                     writer.print("<td class='");
306                     writer.print(propertyName);
307                     writer.print("'>");
308                     if (value != null) {
309                         printEncodedValue(value);
310                     }
311                     writer.print("</td>");
312                 }
313             }
314             writer.print("</tr>");
315             beanCounter++;
316         }
317         writer.println("</table>");
318     }
319
320
321     public void outputHtmlProperties(ObjectName JavaDoc name) throws JMException JavaDoc, IOException JavaDoc {
322         writer.println("<table>");
323         writer.println("<tr>");
324         writer.println("<th>Property</th><th>Value</th>");
325         writer.println("</tr>");
326
327         writer.print("<tr><td>Domain</td><td>");
328         String JavaDoc domain = name.getDomain();
329         if (domain != null) {
330             printEncodedValue(domain);
331         }
332         writer.print("</td></tr>");
333
334         Map JavaDoc map = name.getKeyPropertyList();
335         for (Iterator JavaDoc iter = map.entrySet().iterator(); iter.hasNext();) {
336             Map.Entry JavaDoc entry = (Entry) iter.next();
337             String JavaDoc attributeName = (String JavaDoc) entry.getKey();
338             String JavaDoc value = (String JavaDoc) entry.getValue();
339
340             writer.print("<tr><td>");
341             writer.print(attributeName);
342             writer.print("</td><td>");
343             if (value != null) {
344                 printEncodedValue(value);
345             }
346             writer.print("</td></tr>");
347         }
348
349         writer.println("</table>");
350     }
351
352     public void outputHtmlAttributes(Set JavaDoc names) throws IOException JavaDoc, JMException JavaDoc {
353         if (names.size() <= 1) {
354             for (Iterator JavaDoc iter = names.iterator(); iter.hasNext();) {
355                 outputHtmlAttributes((ObjectName JavaDoc) iter.next());
356             }
357         }
358         else {
359             outputHtmlAttributeGrid(names);
360         }
361     }
362
363     public void outputHtmlAttributeGrid(Set JavaDoc names) throws JMException JavaDoc {
364         MBeanServer JavaDoc beanServer = getMBeanServer();
365         Set JavaDoc attributeNames = new TreeSet JavaDoc();
366         Set JavaDoc[] attributeNamesPerMBeanArray = new Set JavaDoc[names.size()];
367         int beanCounter = 0;
368         for (Iterator JavaDoc iter = names.iterator(); iter.hasNext();) {
369             ObjectName JavaDoc name = (ObjectName JavaDoc) iter.next();
370             MBeanInfo JavaDoc beanInfo = beanServer.getMBeanInfo(name);
371             MBeanAttributeInfo JavaDoc[] attributes = beanInfo.getAttributes();
372
373             Set JavaDoc availableNamesPerMBean = new HashSet JavaDoc();
374             attributeNamesPerMBeanArray[beanCounter++] = availableNamesPerMBean;
375             for (int i = 0; i < attributes.length; i++) {
376                 MBeanAttributeInfo JavaDoc info = attributes[i];
377                 if (info.isReadable()) {
378                     String JavaDoc attributeName = info.getName();
379                     availableNamesPerMBean.add(attributeName);
380                     attributeNames.add(attributeName);
381                 }
382             }
383         }
384
385         writer.println("<table>");
386         writer.println("<tr>");
387         writer.print("<th>MBean</th>");
388         for (Iterator JavaDoc iter = attributeNames.iterator(); iter.hasNext();) {
389             writer.print("<th>");
390             writer.print(iter.next());
391             writer.print("</th>");
392         }
393         writer.println();
394         writer.println("</tr>");
395
396         beanCounter = 0;
397         for (Iterator JavaDoc iter = names.iterator(); iter.hasNext();) {
398             ObjectName JavaDoc name = (ObjectName JavaDoc) iter.next();
399
400             writer.print("<tr><td class='mbeanName'>");
401             writer.print(name);
402             writer.print("</td>");
403
404             for (Iterator JavaDoc iter2 = attributeNames.iterator(); iter2.hasNext();) {
405                 String JavaDoc attributeName = (String JavaDoc) iter2.next();
406
407                 if (attributeNamesPerMBeanArray[beanCounter].contains(attributeName)) {
408                     Object JavaDoc value = getAttributeValue(name, attributeName);
409                     writer.print("<td class='");
410                     writer.print(attributeName);
411                     writer.print("'>");
412                     if (value != null) {
413                         printEncodedValue(value);
414                     }
415                     writer.print("</td>");
416                 }
417             }
418             writer.print("</tr>");
419             beanCounter++;
420         }
421         writer.println("</table>");
422     }
423
424     public void outputHtmlAttributes(ObjectName JavaDoc name) throws JMException JavaDoc, IOException JavaDoc {
425         MBeanServer JavaDoc beanServer = getMBeanServer();
426         MBeanInfo JavaDoc beanInfo = beanServer.getMBeanInfo(name);
427         writer.println("<table>");
428         writer.println("<tr>");
429         writer.println("<th>Attribute</th><th>Value</th><th>Type</th>");
430         writer.println("</tr>");
431
432         MBeanAttributeInfo JavaDoc[] attributes = beanInfo.getAttributes();
433         for (int i = 0; i < attributes.length; i++) {
434             MBeanAttributeInfo JavaDoc info = attributes[i];
435             if (info.isReadable()) {
436                 String JavaDoc attributeName = info.getName();
437
438                 Object JavaDoc value = getAttributeValue(name, attributeName);
439                 writer.print("<tr><td>");
440                 writer.print(attributeName);
441                 writer.print("</td><td>");
442                 if (value != null) {
443                     printEncodedValue(value);
444                 }
445                 writer.print("</td><td>");
446                 writer.print(info.getType());
447                 writer.print("</td></tr>");
448             }
449         }
450
451         writer.println("</table>");
452     }
453
454     public void outputMBeans(Collection JavaDoc names) throws IOException JavaDoc {
455         for (Iterator JavaDoc iter = names.iterator(); iter.hasNext();) {
456             outputMBeans((ObjectName JavaDoc) iter.next());
457         }
458     }
459
460     public void outputMBeans(ObjectName JavaDoc name) throws IOException JavaDoc {
461         Map JavaDoc properties = name.getKeyPropertyList();
462         for (Iterator JavaDoc iter = properties.entrySet().iterator(); iter.hasNext();) {
463             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
464             writer.print("<mbean name='");
465             writer.print(entry.getKey());
466             ObjectInstance JavaDoc objectInstance = (ObjectInstance JavaDoc) entry.getValue();
467             if (objectInstance != null) {
468                 writer.print("' className='");
469                 writer.print(objectInstance.getClassName());
470             }
471             writer.println("'/>");
472         }
473     }
474
475     public void outputHeader() throws IOException JavaDoc {
476         writer.println("<?xml version='1.0'?>");
477         writer.println("<mbeans>");
478     }
479
480     public void outputFooter() throws IOException JavaDoc {
481         writer.println("</mbeans>");
482     }
483
484     /**
485      * Encodes the value as a String and ensures that there are no bad XML
486      * characters like < or > which are encoded.
487      */

488     public void printEncodedValue(Object JavaDoc value) {
489         if (value != null) {
490             String JavaDoc text = value.toString();
491             for (int i = 0, size = text.length(); i < size; i++) {
492                 char ch = text.charAt(i);
493                 switch (ch) {
494                 case '<':
495                     writer.print("&lt;");
496                     break;
497
498                 case '>':
499                     writer.print("&gt;");
500                     break;
501
502                 case '&':
503                     writer.print("&amp;");
504                     break;
505
506                 // used in ObjectName
507
case ',':
508                     writer.print("%2C");
509                     break;
510
511                 case ':':
512                     writer.print("%3A");
513                     break;
514
515                 case '=':
516                     writer.print("%3D");
517                     break;
518
519                 default:
520                     writer.print(ch);
521                 }
522             }
523         }
524     }
525
526     /**
527      * Prints a HTTP encoded ObjectName suitable for use inside URLs
528      */

529     public void printEncodedObjectName(ObjectName JavaDoc name) {
530         printEncodedValue(name);
531     }
532
533     /**
534      * Outputs a URL to the detail JMX stats view
535      */

536     protected void printDetailURL(ObjectName JavaDoc name) {
537         writer.print("mbeanDetail.jsp?view=detail&style=html&name=");
538         printEncodedObjectName(name);
539     }
540
541     /**
542      * Outputs a URL to the detail JMX stats view
543      */

544     protected void printDetailURL(String JavaDoc propertyName, String JavaDoc propertyValue) {
545         writer.print("mbeanDetail.jsp?view=detail&style=html&query=");
546         printEncodedValue("*:" + propertyName + "=" + propertyValue + ",*");
547     }
548
549     protected Object JavaDoc getAttributeValue(ObjectName JavaDoc name, String JavaDoc attributeName) throws MBeanException JavaDoc {
550         MBeanServer JavaDoc beanServer = getMBeanServer();
551         Object JavaDoc value = null;
552         try {
553             value = beanServer.getAttribute(name, attributeName);
554         }
555         catch (AttributeNotFoundException JavaDoc e) {
556             log.warn("Caught: " + e, e);
557         }
558         catch (InstanceNotFoundException JavaDoc e) {
559             log.warn("Caught: " + e, e);
560         }
561         catch (ReflectionException JavaDoc e) {
562             log.warn("Caught: " + e, e);
563         }
564         return value;
565     }
566
567 }
568
Popular Tags