KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Set JavaDoc;
27 import javax.management.MBeanAttributeInfo JavaDoc;
28 import javax.management.MBeanInfo JavaDoc;
29 import javax.management.MBeanServer JavaDoc;
30 import javax.management.MalformedObjectNameException JavaDoc;
31 import javax.management.ObjectInstance JavaDoc;
32 import javax.management.ObjectName JavaDoc;
33 import javax.management.QueryExp JavaDoc;
34 import javax.servlet.jsp.JspWriter JavaDoc;
35
36 import org.apache.geronimo.console.web.util.MBeanAttributesComparator;
37
38 /**
39  * This tag will display the contents of an MBean in a simple table of
40  * name/value pairs. The style of the table can be controlled with CSS.
41  */

42 public final class MBeanAttributesTag
43         extends MBeanServerContextSupport {
44     private Hashtable JavaDoc properties;
45     private MBeanServerContextTag ctx;
46     private ObjectInstance JavaDoc instance;
47     private Object JavaDoc[] keys;
48     private Set JavaDoc keySet;
49     private ObjectName JavaDoc name;
50     private MBeanServer JavaDoc server;
51
52     public int doEndTag() {
53         return EVAL_PAGE;
54     }
55
56     public int doStartTag() {
57         ctx = getMBeanServerContext();
58         server = ctx.getMBeanServer();
59         JspWriter JavaDoc out = pageContext.getOut();
60
61         printMBeanProperties(out);
62         printMBeanAttributes(out);
63
64
65         return EVAL_BODY_INCLUDE;
66     }
67
68     /**
69      * This seems like a very backwards way to do this. I don't know
70      * that creating an ObjectName, using it to get an ObjectInstance
71      * then creating another ObjectName is necessarily the way to go.
72      *
73      */

74     private String JavaDoc getDomain() {
75         try {
76             ObjectName JavaDoc mbeanName = new ObjectName JavaDoc(getMBeanName());
77             QueryExp JavaDoc query = null;
78             Set JavaDoc results = server.queryMBeans(mbeanName, query);
79
80             instance = (ObjectInstance JavaDoc) results.iterator().next();
81             name = instance.getObjectName();
82             return name.getDomain();
83
84         } catch (MalformedObjectNameException JavaDoc e) {
85             return "No object to introspect. Choose one from the MBean Stack View.";
86         }
87     }
88
89     /**
90      * This gets the value of the MBeanName request parameter. If it
91      * Doesn't find anything, it returns null.
92      */

93     private String JavaDoc getMBeanName() {
94         String JavaDoc s =
95                 pageContext.getRequest().getParameter("MBeanName");
96         if (s == null || s == "") {
97             return null;
98         }
99         return s;
100     }
101
102     private void printMBeanProperties(JspWriter JavaDoc out) {
103         try {
104             //String mbeanName;
105
//out.println("<strong>MBean Name </strong>" + getMBeanName());
106
out.println("<table cellpadding=\"0\" cellspacing=\"0\">");
107
108             out.println("\t<tr class=\"head\">");
109             out.println("\t\t<td class=\"head\" colspan=\"3\">" +
110                     "MBean Properties</td>");
111             out.println("\t</tr>");
112
113             out.println("\t<tr class=\"one\">");
114             out.println("\t\t<td class=\"name\">MBean Domain</td>");
115             out.println("\t\t<td class=\"center\">=</td>");
116             out.println("\t\t<td class=\"value\">" + getDomain() + "</td>");
117             out.println("\t</tr>");
118
119             printMBeanPropertiesStack(out);
120
121             out.println("</table>");
122         } catch (IOException JavaDoc e) {
123             e.printStackTrace();
124         }
125     }
126
127     private void printMBeanPropertiesStack(JspWriter JavaDoc out) {
128         properties = name.getKeyPropertyList();
129         keySet = properties.keySet();
130         keys = toList(keySet).toArray();
131
132         try {
133             //out.println("Hello");
134

135             String JavaDoc key;
136             String JavaDoc property;
137             String JavaDoc trClass = "one";
138             for (int i = 0; i < keys.length; i++) {
139                 key = (String JavaDoc) keys[i];
140                 property = (String JavaDoc) name.getKeyProperty(key);
141                 if (i % 2 == 0) {
142                     trClass = "two";
143                 } else if (i % 2 == 1) {
144                     trClass = "one";
145                 }
146
147                 out.println("\t<tr class=\"" + trClass + "\">");
148                 out.println("\t\t<td class=\"name\">" + key + "</td>");
149                 out.println("\t\t<td class=\"center\">=</td>");
150                 out.println("\t\t<td class=\"value\">" +
151                         URLDecoder.decode(property, "UTF-8") + "</td>");
152                 out.println("\t</tr>");
153
154             }
155         } catch (IOException JavaDoc e) {
156             e.printStackTrace();
157         }
158     }
159
160     private void printMBeanAttributes(JspWriter JavaDoc out) {
161         try {
162             //String mbeanName;
163
//out.println("<strong>MBean Name </strong>" + getMBeanName());
164
out.println("<table cellpadding=\"0\" cellspacing=\"0\">");
165
166             out.println("\t<tr class=\"head\">");
167             out.println("\t\t<td class=\"head\" colspan=\"3\">" +
168                     "MBean Attributes & Info</td>");
169             out.println("\t</tr>");
170
171             printMBeanAttributesStack(out);
172
173
174             out.println("</table>");
175         } catch (IOException JavaDoc e) {
176             e.printStackTrace();
177         }
178     }
179
180     private void printMBeanAttributesStack(JspWriter JavaDoc out) {
181         try {
182             MBeanInfo JavaDoc info = server.getMBeanInfo(name);
183             MBeanAttributeInfo JavaDoc[] attributes = info.getAttributes();
184             String JavaDoc className = info.getClassName();
185             String JavaDoc description = info.getDescription();
186
187             out.println("\t<tr class=\"one\">");
188             out.println("\t\t<td class=\"name\">Class Name</td>");
189             out.println("\t\t<td class=\"center\">=</td>");
190             out.println("\t\t<td class=\"value\">" + className + "</td>");
191             out.println("\t</tr>");
192
193             out.println("\t<tr class=\"two\">");
194             out.println("\t\t<td class=\"name\">Description</td>");
195             out.println("\t\t<td class=\"center\">=</td>");
196             out.println("\t\t<td class=\"value\">" + description + "</td>");
197             out.println("\t</tr>");
198
199             String JavaDoc attributeName = "name";
200             String JavaDoc value = "value";
201             String JavaDoc trClass = "one";
202             for (int i = 0; i < attributes.length; i++) {
203
204                 attributeName = attributes[i].getName();
205                 //value = attributes[i].toString();
206

207                 if (attributes[i].isReadable()) {
208                     Object JavaDoc attrObj = server.getAttribute(name, attributeName);
209                     if ( attrObj == null ) {
210                         continue;
211                     }
212                     value = attrObj.toString();
213
214                     if (i % 2 == 0) {
215                         trClass = "one";
216                     } else if (i % 2 == 1) {
217                         trClass = "two";
218                     }
219                     out.println("\t<tr class=\"" + trClass + "\">");
220                     out.println("\t\t<td class=\"name\">" + attributeName + "</td>");
221                     out.println("\t\t<td class=\"center\">=</td>");
222                     out.println("\t\t<td class=\"value\">" +
223                         URLDecoder.decode(value, "UTF-8") + "</td>");
224                     out.println("\t</tr>");
225                 }
226             }
227         } catch (Exception JavaDoc e) {
228             e.printStackTrace();
229         }
230     }
231
232     private List JavaDoc toList(Set JavaDoc set) {
233         List JavaDoc list = new ArrayList JavaDoc();
234         MBeanAttributesComparator comp = new MBeanAttributesComparator();
235         list.addAll(set);
236         Collections.sort(list, comp);
237         return list;
238     }
239 }
240
Popular Tags