1 5 package org.exoplatform.portlets.jmx.renderer.html; 6 7 import java.io.IOException ; 8 import java.util.ResourceBundle ; 9 10 import javax.faces.component.UIComponent; 11 import javax.faces.context.FacesContext; 12 import javax.faces.context.ResponseWriter; 13 import javax.management.MBeanAttributeInfo ; 14 import javax.management.MBeanConstructorInfo ; 15 import javax.management.MBeanInfo ; 16 import javax.management.MBeanOperationInfo ; 17 import javax.management.MBeanParameterInfo ; 18 import javax.management.MBeanServer ; 19 import javax.management.ObjectName ; 20 21 import org.exoplatform.faces.core.component.model.Parameter; 22 import org.exoplatform.faces.core.renderer.html.HtmlBasicRenderer; 23 import org.exoplatform.portlets.jmx.component.UIMBean; 24 25 26 32 public class ViewMBeanRenderer extends HtmlBasicRenderer { 33 private static Parameter executeParam = new Parameter(ACTION , UIMBean.EXECUTE_ACTION) ; 34 35 public void encodeChildren( FacesContext context, UIComponent component ) throws IOException { 36 UIMBean uiMBean = (UIMBean) component ; 37 ResourceBundle res = getApplicationResourceBundle(context.getExternalContext()) ; 38 MBeanInfo minfo = uiMBean.getMBeanInfo() ; 39 ResponseWriter w = context.getResponseWriter() ; 40 MBeanAttributeInfo [] attrs = minfo.getAttributes(); 41 ObjectName name = uiMBean.getObjectName() ; 42 MBeanServer server = uiMBean.getMBeanServer(); 43 try { 44 w.write("<table class='"); w.write(uiMBean.getClazz()); w.write("'>") ; 45 w. write("<tr>") ; 46 w. write("<th colspan='2'>") ; w.write(res.getString("UIMBean.header.mbean")); w.write("</th>") ; 47 w. write("</tr>") ; 48 w. write("<tr>") ; 49 w. write("<td>") ; w.write(res.getString("UIMBean.label.type")); w.write("</td>") ; 50 w. write("<td>") ; w.write(minfo.getClassName()); w.write("</td>") ; 51 w. write("</tr>") ; 52 w. write("<tr>") ; 53 w. write("<td>") ; w.write(res.getString("UIMBean.label.description")); w.write("</td>") ; 54 w. write("<td>") ; w.write(ft_.format(minfo.getDescription())); w.write("</td>") ; 55 w. write("</tr>") ; 56 w. write("<tr>") ; 57 w. write("<td>") ; w.write(res.getString("UIMBean.label.dependencies")); w.write("</td>") ; 58 w. write("<td>") ; w.write(getDependencies(minfo)); w.write("</td>") ; 59 w. write("</tr>") ; 60 w. write("<tr>") ; 61 w. write("<td colspan='2'>") ; 62 for (int i = 0; i < attrs.length; i++) { 63 Object value = "" ; 64 if (attrs[i].isReadable()) { 65 value = server.getAttribute(name, attrs[i].getName()); 66 } 67 renderAttribute(w, attrs[i], value) ; 68 } 69 w. write("</td>") ; 70 w. write("</tr>") ; 71 MBeanOperationInfo [] methods = minfo.getOperations(); 72 w. write("<tr>") ; 73 w. write("<td colspan='2'>") ; 74 for (int j = 0; j < methods.length; j++) { 75 renderOperation(w, uiMBean, methods[j], res) ; 76 } 77 w. write("</td>") ; 78 w. write("</tr>") ; 79 w.write("</table>") ; 80 } catch (Exception x) { 81 x.printStackTrace(); 82 } 83 } 84 85 private void renderAttribute(ResponseWriter w, 86 MBeanAttributeInfo attr, Object value) throws Exception { 87 if(value == null) value = "" ; 88 w.write("<div>") ; 89 w. write("Attribute Name: "); w.write(attr.getName()) ; 90 w. write("<br/>Attribute Type: "); w.write(attr.getType()) ; 91 w. write("<br/>Attribute Readable: "); w.write(Boolean.toString(attr.isReadable())) ; 92 w. write("<br/>Attribute Writable: "); w.write(Boolean.toString(attr.isWritable())) ; 93 w. write("<br/>Attribute Value: "); w.write(value.toString()) ; 94 w.write("</div>") ; 95 } 96 97 private void renderOperation(ResponseWriter w, UIMBean uiMBean, 98 MBeanOperationInfo method, ResourceBundle res) throws Exception { 99 w.write("<table class='method'>") ; 100 w. write("<tr>") ; 101 w. write("<td>" + res.getString("UIMBean.label.method")); w.write(method.getName()) ;w.write("</td>") ; 102 w. write("<td style='text-align: right; border: none'>"); 103 if(uiMBean.canExecute(method)) { 104 Parameter[] params = {executeParam, new Parameter("methodId" , Integer.toString(method.hashCode()))} ; 105 linkRenderer_.render(w, uiMBean, res.getString("UIMBean.button.execute"), params) ; 106 } else { 107 w. write(res.getString("UIMBean.label.na")); 108 } 109 w. write("</td>") ; 110 w. write("</tr>") ; 111 w. write("<tr>") ; 112 w. write("<td colspan='2'>" + res.getString("UIMBean.label.parameter") + ":") ; 113 MBeanParameterInfo [] params = method.getSignature() ; 114 for(int k = 0 ; k < params.length; k++) { 115 w.write("<li>"); 116 w.write(params[k].getName()); w.write(" - ") ;w.write(params[k].getType()) ; 117 w.write("</li>"); 118 } 119 w. write("</td>") ; 120 w. write("</tr>") ; 121 w. write("<tr>") ; 122 w. write("<td colspan='2'>" + res.getString("UIMBean.label.return-type")); w.write(method.getReturnType()) ; w.write("</td>") ; 123 w. write("</tr>") ; 124 w.write("</table>") ; 125 } 126 127 private String getDependencies(MBeanInfo minfo) { 128 if(!minfo.getClassName().startsWith("exo")) return "" ; 129 StringBuffer b = new StringBuffer () ; 130 MBeanConstructorInfo [] constructors = minfo.getConstructors() ; 131 MBeanConstructorInfo constructor = null ; 132 for(int i = 0; i < constructors.length; i++) { 133 if(constructor == null) constructor = constructors[i] ; 134 if(constructor.getSignature().length < constructors[i].getSignature().length) { 135 constructor = constructors[i] ; 136 } 137 } 138 if(constructor == null) return "" ; 139 MBeanParameterInfo [] params = constructor.getSignature() ; 140 for(int i = 0 ; i < params.length; i++) { 141 b.append(params[i].getType()).append(" ") ; 142 } 143 return b.toString() ; 144 } 145 } | Popular Tags |