KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portlets > jmx > renderer > html > ViewMBeanRenderer


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.portlets.jmx.renderer.html;
6
7 import java.io.IOException JavaDoc;
8 import java.util.ResourceBundle JavaDoc;
9
10 import javax.faces.component.UIComponent;
11 import javax.faces.context.FacesContext;
12 import javax.faces.context.ResponseWriter;
13 import javax.management.MBeanAttributeInfo JavaDoc;
14 import javax.management.MBeanConstructorInfo JavaDoc;
15 import javax.management.MBeanInfo JavaDoc;
16 import javax.management.MBeanOperationInfo JavaDoc;
17 import javax.management.MBeanParameterInfo JavaDoc;
18 import javax.management.MBeanServer JavaDoc;
19 import javax.management.ObjectName JavaDoc;
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 /**
27  * Jul 29, 2004
28  * @author: Tuan Nguyen
29  * @email: tuan08@users.sourceforge.net
30  * @version: $Id: ViewMBeanRenderer.java,v 1.3 2004/08/28 18:59:20 tuan08 Exp $
31  */

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 JavaDoc {
36         UIMBean uiMBean = (UIMBean) component ;
37     ResourceBundle JavaDoc res = getApplicationResourceBundle(context.getExternalContext()) ;
38         MBeanInfo JavaDoc minfo = uiMBean.getMBeanInfo() ;
39         ResponseWriter w = context.getResponseWriter() ;
40         MBeanAttributeInfo JavaDoc[] attrs = minfo.getAttributes();
41     ObjectName JavaDoc name = uiMBean.getObjectName() ;
42     MBeanServer JavaDoc 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 JavaDoc 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 JavaDoc[] 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 JavaDoc x) {
81             x.printStackTrace();
82         }
83     }
84   
85     private void renderAttribute(ResponseWriter w,
86                                MBeanAttributeInfo JavaDoc attr, Object JavaDoc value) throws Exception JavaDoc {
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 JavaDoc method, ResourceBundle JavaDoc res) throws Exception JavaDoc {
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 JavaDoc[] 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 JavaDoc getDependencies(MBeanInfo JavaDoc minfo) {
128     if(!minfo.getClassName().startsWith("exo")) return "" ;
129     StringBuffer JavaDoc b = new StringBuffer JavaDoc() ;
130     MBeanConstructorInfo JavaDoc[] constructors = minfo.getConstructors() ;
131     MBeanConstructorInfo JavaDoc 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 JavaDoc[] 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