1 17 18 package org.apache.geronimo.console.web.taglib; 19 20 import java.io.IOException ; 21 import java.net.URLDecoder ; 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.Hashtable ; 25 import java.util.List ; 26 import java.util.Set ; 27 import javax.management.MBeanAttributeInfo ; 28 import javax.management.MBeanInfo ; 29 import javax.management.MBeanServer ; 30 import javax.management.MalformedObjectNameException ; 31 import javax.management.ObjectInstance ; 32 import javax.management.ObjectName ; 33 import javax.management.QueryExp ; 34 import javax.servlet.jsp.JspWriter ; 35 36 import org.apache.geronimo.console.web.util.MBeanAttributesComparator; 37 38 42 public final class MBeanAttributesTag 43 extends MBeanServerContextSupport { 44 private Hashtable properties; 45 private MBeanServerContextTag ctx; 46 private ObjectInstance instance; 47 private Object [] keys; 48 private Set keySet; 49 private ObjectName name; 50 private MBeanServer 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 out = pageContext.getOut(); 60 61 printMBeanProperties(out); 62 printMBeanAttributes(out); 63 64 65 return EVAL_BODY_INCLUDE; 66 } 67 68 74 private String getDomain() { 75 try { 76 ObjectName mbeanName = new ObjectName (getMBeanName()); 77 QueryExp query = null; 78 Set results = server.queryMBeans(mbeanName, query); 79 80 instance = (ObjectInstance ) results.iterator().next(); 81 name = instance.getObjectName(); 82 return name.getDomain(); 83 84 } catch (MalformedObjectNameException e) { 85 return "No object to introspect. Choose one from the MBean Stack View."; 86 } 87 } 88 89 93 private String getMBeanName() { 94 String 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 out) { 103 try { 104 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 e) { 123 e.printStackTrace(); 124 } 125 } 126 127 private void printMBeanPropertiesStack(JspWriter out) { 128 properties = name.getKeyPropertyList(); 129 keySet = properties.keySet(); 130 keys = toList(keySet).toArray(); 131 132 try { 133 135 String key; 136 String property; 137 String trClass = "one"; 138 for (int i = 0; i < keys.length; i++) { 139 key = (String ) keys[i]; 140 property = (String ) 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 e) { 156 e.printStackTrace(); 157 } 158 } 159 160 private void printMBeanAttributes(JspWriter out) { 161 try { 162 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 e) { 176 e.printStackTrace(); 177 } 178 } 179 180 private void printMBeanAttributesStack(JspWriter out) { 181 try { 182 MBeanInfo info = server.getMBeanInfo(name); 183 MBeanAttributeInfo [] attributes = info.getAttributes(); 184 String className = info.getClassName(); 185 String 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 attributeName = "name"; 200 String value = "value"; 201 String trClass = "one"; 202 for (int i = 0; i < attributes.length; i++) { 203 204 attributeName = attributes[i].getName(); 205 207 if (attributes[i].isReadable()) { 208 Object 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 e) { 228 e.printStackTrace(); 229 } 230 } 231 232 private List toList(Set set) { 233 List list = new ArrayList (); 234 MBeanAttributesComparator comp = new MBeanAttributesComparator(); 235 list.addAll(set); 236 Collections.sort(list, comp); 237 return list; 238 } 239 } 240 | Popular Tags |