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;
21 import java.net.URLDecoder;
22 import java.net.URLEncoder;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Set;
28 import javax.management.MBeanServer;
29 import javax.management.MalformedObjectNameException;
30 import javax.management.ObjectInstance;
31 import javax.management.ObjectName;
32 import javax.management.QueryExp;
33 import javax.servlet.jsp.JspWriter;
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 server;
45
46 public int doStartTag() {
47 ctx = getMBeanServerContext();
48 server = ctx.getMBeanServer();
49 JspWriter out = pageContext.getOut();
50
51 try {
52 if (server != null) {
53
54 ObjectName objectName = new ObjectName(ctx.getObjectNameFilter());
55 QueryExp query = null;
56 Set results = server.queryMBeans(objectName, query);
57 List mbeans = toList(results);
58 printMBeanStack(out, mbeans);
59 }
60 } catch (MalformedObjectNameException e) {
61 try {
62 String 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 ex) {
68 e.printStackTrace();
69 }
70 } catch (IOException 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 out, List mbeans)
82 throws IOException {
83 Iterator iter = mbeans.iterator();
84 String currentDomain = "";
85 int i = 0;
86 while (iter.hasNext()) {
87 ObjectInstance instance = (ObjectInstance) iter.next();
88 ObjectName 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 cName = name.getCanonicalName();
102 String encodedName = URLEncoder.encode(cName, "UTF-8");
103 String 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 out, String output) {
123 //TODO: Format the JSR77 stuff so it's more readable.
124 }
125
126 private List toList(Set set) {
127 List list = new ArrayList();
128 list.addAll(set);
129 ObjectInstanceComparator comparator = new ObjectInstanceComparator();
130 Collections.sort(list, comparator);
131 return list;
132 }
133
134 }