KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > webadmin > clienttools > ViewClassBean


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact dev@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://www.openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: ViewClassBean.java 1912 2005-06-16 22:29:56Z jlaskowski $
44  */

45 package org.openejb.webadmin.clienttools;
46
47 import java.io.IOException JavaDoc;
48 import java.io.PrintWriter JavaDoc;
49 import java.lang.reflect.Method JavaDoc;
50 import java.lang.reflect.Modifier JavaDoc;
51
52 import org.openejb.webadmin.HttpRequest;
53 import org.openejb.webadmin.HttpResponse;
54 import org.openejb.webadmin.WebAdminBean;
55
56 /**
57  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
58  */

59 public class ViewClassBean extends WebAdminBean implements Constants {
60
61     boolean hasMethods;
62
63     public void preProcess(HttpRequest request, HttpResponse response)
64         throws IOException JavaDoc {
65     }
66
67     public void postProcess(HttpRequest request, HttpResponse response)
68         throws IOException JavaDoc {
69     }
70
71     public void writeHtmlTitle(PrintWriter JavaDoc out) throws IOException JavaDoc {
72         out.write("Client Tools -- JNDI Viewer");
73     }
74
75     public void writePageTitle(PrintWriter JavaDoc out) throws IOException JavaDoc {
76         out.write("JNDI Viewer");
77     }
78
79     public void writeBody(PrintWriter JavaDoc out) throws IOException JavaDoc {
80         try {
81             String JavaDoc className = request.getQueryParameter("class");
82             if (className == null) {
83                 out.print("<b>Enter a class name to browse:</b>");
84                 out.print(
85                     "<FORM NAME='view' METHOD='GET' ACTION='"+VIEW_CLASS+"'>");
86                 out.print(
87                     "<INPUT type='text' NAME='class' size='40' VALUE=''>");
88                 out.print("<INPUT type='SUBMIT' NAME='view' value='View'>");
89                 out.print("</form>");
90                 out.print("<b>Or browse one of these fun classes:</b><br><br>");
91                 out.print(tab + getClassRef("javax.ejb.EJBHome") + "<br>");
92                 out.print(tab + getClassRef("javax.ejb.EJBObject") + "<br>");
93                 out.print(
94                     tab + getClassRef("javax.ejb.EnterpriseBean") + "<br>");
95                 out.print(tab + getClassRef("javax.ejb.SessionBean") + "<br>");
96                 out.print(tab + getClassRef("javax.ejb.EntityBean") + "<br>");
97                 out.print(
98                     tab
99                         + getClassRef("javax.servlet.http.HttpServlet")
100                         + "<br>");
101                 out.print(
102                     tab
103                         + getClassRef("javax.servlet.http.HttpServletRequest")
104                         + "<br>");
105                 out.print(
106                     tab
107                         + getClassRef("javax.servlet.http.HttpServletResponse")
108                         + "<br>");
109                 out.print(
110                     tab
111                         + getClassRef("javax.servlet.http.HttpSession")
112                         + "<br>");
113                 out.print(
114                     tab + getClassRef("javax.naming.InitialContext") + "<br>");
115                 out.print(tab + getClassRef("javax.naming.Context") + "<br>");
116
117             } else {
118                 Class JavaDoc clazz = this.getClass().forName(className);
119                 printClass(clazz, out);
120             }
121         } catch (Exception JavaDoc e) {
122             out.println("FAIL");
123             return;
124         }
125         out.print("<BR><BR>");
126         if (hasMethods) {
127             out.print("<font color='green'>*</font>&nbsp;Public &nbsp; ");
128             out.print("<font color='red'>*</font>&nbsp;Private &nbsp;");
129             out.print("<font color='blue'>*</font>&nbsp;Protected &nbsp;");
130             out.print("<font color='yellow'>*</font>&nbsp;Default ");
131             out.print("<BR>");
132         }
133     }
134
135
136     public void printClass(Class JavaDoc clazz, PrintWriter JavaDoc out)
137         throws Exception JavaDoc {
138         out.print("<b>" + clazz.getName() + "</b><br>");
139         Method JavaDoc[] methods = clazz.getDeclaredMethods();
140         hasMethods = (methods.length > 0);
141         for (int i = 0; i < methods.length; i++) {
142             printMethod(methods[i], out);
143         }
144
145         /*
146          * //out.print("&nbsp;&nbsp; <font color='gray'><u> Public Methods:
147          * </u></font><br> "); for (int i=0; i < methods.length; i++){ if
148          * (Modifier.isPublic(methods[i].getModifiers())){ printMethod(
149          * methods[i], out ); } } //out.print("&nbsp;&nbsp; <font color='gray'>
150          * <u> Private Methods: </u></font><br> "); for (int i=0; i
151          * < methods.length; i++){ if
152          * (Modifier.isPrivate(methods[i].getModifiers())){ printMethod(
153          * methods[i], out ); } } for (int i=0; i < methods.length; i++){ if
154          * (Modifier.isProtected(methods[i].getModifiers())){ printMethod(
155          * methods[i], out ); } } for (int i=0; i < methods.length; i++){ if
156          * (!Modifier.isSrict(methods[i].getModifiers())){ printMethod(
157          * methods[i], out ); } }
158          */

159         Class JavaDoc sup = clazz.getSuperclass();
160         if (sup != null) {
161             out.print("<br><b>Extends:</b><br>");
162             out.print(tab + getClassRef(sup) + "<br>");
163         }
164
165         Class JavaDoc[] intf = clazz.getInterfaces();
166
167         if (intf.length > 0) {
168             out.print("<br><b>Implements:</b><br>");
169             for (int i = 0; i < intf.length; i++) {
170                 out.print(tab + getClassRef(intf[i]) + "<br>");
171             }
172         }
173     }
174
175     public void printMethod(Method JavaDoc m, PrintWriter JavaDoc out)
176         throws Exception JavaDoc {
177         out.print(tab);
178         out.print(" " + getModifier(m));
179
180         out.print(" " + getShortClassRef(m.getReturnType()) + "&nbsp;&nbsp;");
181
182         out.print("" + m.getName() + "&nbsp;");
183         Class JavaDoc[] params = m.getParameterTypes();
184         out.print("<font color='gray'>(</font>");
185         for (int j = 0; j < params.length; j++) {
186             out.print(getShortClassRef(params[j]));
187             if (j != params.length - 1) {
188                 out.print(",&nbsp;");
189             }
190         }
191         out.print("<font color='gray'>)</font>");
192
193         Class JavaDoc[] excp = m.getExceptionTypes();
194         if (excp.length > 0) {
195             out.print(" <font color='gray'>throws</font>&nbsp;&nbsp;");
196             for (int j = 0; j < excp.length; j++) {
197                 out.print(getShortClassRef(excp[j]));
198                 if (j != excp.length - 1) {
199                     out.print(",&nbsp;");
200                 }
201             }
202         }
203         out.print("<br>");
204     }
205
206     public String JavaDoc getModifier(Method JavaDoc m) throws Exception JavaDoc {
207         int mod = m.getModifiers();
208         String JavaDoc color = "";
209
210         if (Modifier.isPublic(mod)) {
211             color = "green";
212         } else if (Modifier.isPrivate(mod)) {
213             color = "red";
214         } else if (Modifier.isProtected(mod)) {
215             color = "blue";
216         } else {
217             color = "yellow";
218         }
219         return "<font color='" + color + "'>*</font>";
220     }
221
222     public String JavaDoc getClassRef(Class JavaDoc clazz) throws Exception JavaDoc {
223         String JavaDoc name = clazz.getName();
224         return "<a HREF='"+VIEW_CLASS+"?class=" + name + "'>" + name + "</a>";
225     }
226
227     public String JavaDoc getClassRef(String JavaDoc name) throws Exception JavaDoc {
228         return "<a HREF='"+VIEW_CLASS+"?class=" + name + "'>" + name + "</a>";
229     }
230
231     public String JavaDoc getShortClassRef(Class JavaDoc clazz) throws Exception JavaDoc {
232         if (clazz.isPrimitive()) {
233             return "<font color='gray'>" + clazz.getName() + "</font>";
234         } else if (clazz.isArray() && clazz.getComponentType().isPrimitive()) {
235             return "<font color='gray'>"
236                 + clazz.getComponentType()
237                 + "[]</font>";
238         } else if (clazz.isArray()) {
239             String JavaDoc name = clazz.getComponentType().getName();
240             int dot = name.lastIndexOf(".") + 1;
241             String JavaDoc shortName = name.substring(dot, name.length());
242             return "<a HREF='"+VIEW_CLASS+"?class="
243                 + name
244                 + "'>"
245                 + shortName
246                 + "[]</a>";
247         } else {
248             String JavaDoc name = clazz.getName();
249             int dot = name.lastIndexOf(".") + 1;
250             String JavaDoc shortName = name.substring(dot, name.length());
251             return "<a HREF='"+VIEW_CLASS+"?class="
252                 + name
253                 + "'>"
254                 + shortName
255                 + "</a>";
256         }
257     }
258 }
259
Popular Tags