KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > console > web > taglib > MBeanServerContentsTag


1 /**
2  *
3  * Copyright 2003-2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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
18 package org.apache.geronimo.console.web.taglib;
19
20 import java.io.IOException JavaDoc;
21 import java.net.URLDecoder JavaDoc;
22 import java.net.URLEncoder JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.management.MBeanServer JavaDoc;
29 import javax.management.MalformedObjectNameException JavaDoc;
30 import javax.management.ObjectInstance JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32 import javax.management.QueryExp JavaDoc;
33 import javax.servlet.jsp.JspWriter JavaDoc;
34
35 import org.apache.geronimo.console.web.util.ObjectInstanceComparator;
36
37 /**
38  * This class displays the contents of the MBeanServer, arranged in groups, in
39  * alphabetical order by MBean domain and then by the MBean's canonical name.
40  *
41  */

42 public final class MBeanServerContentsTag extends MBeanServerContextSupport {
43     private MBeanServerContextTag ctx;
44     private MBeanServer JavaDoc server;
45
46     public int doStartTag() {
47         ctx = getMBeanServerContext();
48         server = ctx.getMBeanServer();
49         JspWriter JavaDoc out = pageContext.getOut();
50
51         try {
52             if (server != null) {
53
54                 ObjectName JavaDoc objectName = new ObjectName JavaDoc(ctx.getObjectNameFilter());
55                 QueryExp JavaDoc query = null;
56                 Set JavaDoc results = server.queryMBeans(objectName, query);
57                 List JavaDoc mbeans = toList(results);
58                 printMBeanStack(out, mbeans);
59             }
60         } catch (MalformedObjectNameException JavaDoc e) {
61             try {
62                 String JavaDoc s = "Your query string was improperly formatted. " +
63                         "Please try another query.";
64                 out.println("<div class='paragraphHead'> " +
65                         "Invalid Query String </div>");
66                 out.println("<p>" + s + "</p>");
67             } catch (IOException JavaDoc ex) {
68                 e.printStackTrace();
69             }
70         } catch (IOException JavaDoc e) {
71             e.printStackTrace();
72         }
73
74         return EVAL_BODY_INCLUDE;
75     }
76
77     public int doEndTag() {
78         return EVAL_PAGE;
79     }
80
81     private void printMBeanStack(JspWriter JavaDoc out, List JavaDoc mbeans)
82             throws IOException JavaDoc {
83         Iterator JavaDoc iter = mbeans.iterator();
84         String JavaDoc currentDomain = "";
85         int i = 0;
86         while (iter.hasNext()) {
87             ObjectInstance JavaDoc instance = (ObjectInstance JavaDoc) iter.next();
88             ObjectName JavaDoc name = instance.getObjectName();
89
90             if (!(name.getDomain().equals(currentDomain))) {
91                 if (i != 0) {
92                     out.println("</ul>\n");
93                 }
94                 currentDomain = name.getDomain();
95                 out.println(
96                         "\n<div class='paragraphHead'>" + currentDomain + "</div>");
97                 out.println("<ul class='mbeanList'>");
98
99             }
100
101             String JavaDoc cName = name.getCanonicalName();
102             String JavaDoc encodedName = URLEncoder.encode(cName, "UTF-8");
103             String JavaDoc output = cName.substring(cName.indexOf(":") + 1);
104
105             out.println("<li><a HREF=\"mbeanInfo.jsp?MBeanName=" +
106                     encodedName + "\">" + URLDecoder.decode(output, "UTF-8") + "</a></li>");
107
108             i++;
109         }
110
111         out.println("</ul>\n");
112         out.println("<br/> Number of MBeans == " + i);
113     }
114
115     /*
116      * The idea behind this method is to build a tree structure in the list
117      * of MBeans and sort out the objects by subgroups. This would make them
118      * a lot easier to read on the screen.
119      *
120      * Unfortunately, this method isn't ready yet.
121      */

122     private void printCascadingDefinition(JspWriter JavaDoc out, String JavaDoc output) {
123         //TODO: Format the JSR77 stuff so it's more readable.
124
}
125
126     private List JavaDoc toList(Set JavaDoc set) {
127         List JavaDoc list = new ArrayList JavaDoc();
128         list.addAll(set);
129         ObjectInstanceComparator comparator = new ObjectInstanceComparator();
130         Collections.sort(list, comparator);
131         return list;
132     }
133
134 }
Popular Tags