001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.webadmin.clienttools;
017:
018: import java.io.IOException;
019: import java.io.PrintWriter;
020: import java.lang.reflect.Method;
021: import java.lang.reflect.Modifier;
022:
023: import org.apache.openejb.webadmin.HttpRequest;
024: import org.apache.openejb.webadmin.HttpResponse;
025: import org.apache.openejb.webadmin.WebAdminBean;
026: import org.apache.openejb.webadmin.HttpHome;
027:
028: import javax.ejb.Stateless;
029: import javax.ejb.RemoteHome;
030:
031: /**
032: * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
033: */
034: @Stateless(name="ClientTools/ViewClass")
035: @RemoteHome(HttpHome.class)
036: public class ViewClassBean extends WebAdminBean implements Constants {
037:
038: boolean hasMethods;
039:
040: public void preProcess(HttpRequest request, HttpResponse response)
041: throws IOException {
042: }
043:
044: public void postProcess(HttpRequest request, HttpResponse response)
045: throws IOException {
046: }
047:
048: public void writeHtmlTitle(PrintWriter out) throws IOException {
049: out.write("Client Tools -- JNDI Viewer");
050: }
051:
052: public void writePageTitle(PrintWriter out) throws IOException {
053: out.write("JNDI Viewer");
054: }
055:
056: public void writeBody(PrintWriter out) throws IOException {
057: try {
058: String className = request.getQueryParameter("class");
059: if (className == null) {
060: out.print("<b>Enter a class name to browse:</b>");
061: out.print("<FORM NAME='view' METHOD='GET' ACTION='"
062: + VIEW_CLASS + "'>");
063: out
064: .print("<INPUT type='text' NAME='class' size='40' VALUE=''>");
065: out
066: .print("<INPUT type='SUBMIT' NAME='view' value='View'>");
067: out.print("</form>");
068: out
069: .print("<b>Or browse one of these fun classes:</b><br><br>");
070: out.print(tab + getClassRef("javax.ejb.EJBHome")
071: + "<br>");
072: out.print(tab + getClassRef("javax.ejb.EJBObject")
073: + "<br>");
074: out.print(tab + getClassRef("javax.ejb.EnterpriseBean")
075: + "<br>");
076: out.print(tab + getClassRef("javax.ejb.SessionBean")
077: + "<br>");
078: out.print(tab + getClassRef("javax.ejb.EntityBean")
079: + "<br>");
080: out.print(tab
081: + getClassRef("javax.servlet.http.HttpServlet")
082: + "<br>");
083: out
084: .print(tab
085: + getClassRef("javax.servlet.http.HttpServletRequest")
086: + "<br>");
087: out
088: .print(tab
089: + getClassRef("javax.servlet.http.HttpServletResponse")
090: + "<br>");
091: out.print(tab
092: + getClassRef("javax.servlet.http.HttpSession")
093: + "<br>");
094: out.print(tab
095: + getClassRef("javax.naming.InitialContext")
096: + "<br>");
097: out.print(tab + getClassRef("javax.naming.Context")
098: + "<br>");
099:
100: } else {
101: Class clazz = this .getClass().forName(className);
102: printClass(clazz, out);
103: }
104: } catch (Exception e) {
105: out.println("FAIL");
106: return;
107: }
108: out.print("<BR><BR>");
109: if (hasMethods) {
110: out
111: .print("<font color='green'>*</font> Public ");
112: out.print("<font color='red'>*</font> Private ");
113: out
114: .print("<font color='blue'>*</font> Protected ");
115: out.print("<font color='yellow'>*</font> Default ");
116: out.print("<BR>");
117: }
118: }
119:
120: public void printClass(Class clazz, PrintWriter out)
121: throws Exception {
122: out.print("<b>" + clazz.getName() + "</b><br>");
123: Method[] methods = clazz.getDeclaredMethods();
124: hasMethods = (methods.length > 0);
125: for (int i = 0; i < methods.length; i++) {
126: printMethod(methods[i], out);
127: }
128:
129: /*
130: * //out.print(" <font color='gray'><u> Public Methods:
131: * </u></font><br> "); for (int i=0; i < methods.length; i++){ if
132: * (Modifier.isPublic(methods[i].getModifiers())){ printMethod(
133: * methods[i], out ); } } //out.print(" <font color='gray'>
134: * <u> Private Methods: </u></font><br> "); for (int i=0; i
135: * < methods.length; i++){ if
136: * (Modifier.isPrivate(methods[i].getModifiers())){ printMethod(
137: * methods[i], out ); } } for (int i=0; i < methods.length; i++){ if
138: * (Modifier.isProtected(methods[i].getModifiers())){ printMethod(
139: * methods[i], out ); } } for (int i=0; i < methods.length; i++){ if
140: * (!Modifier.isSrict(methods[i].getModifiers())){ printMethod(
141: * methods[i], out ); } }
142: */
143: Class sup = clazz.getSuperclass();
144: if (sup != null) {
145: out.print("<br><b>Extends:</b><br>");
146: out.print(tab + getClassRef(sup) + "<br>");
147: }
148:
149: Class[] intf = clazz.getInterfaces();
150:
151: if (intf.length > 0) {
152: out.print("<br><b>Implements:</b><br>");
153: for (int i = 0; i < intf.length; i++) {
154: out.print(tab + getClassRef(intf[i]) + "<br>");
155: }
156: }
157: }
158:
159: public void printMethod(Method m, PrintWriter out) throws Exception {
160: out.print(tab);
161: out.print(" " + getModifier(m));
162:
163: out.print(" " + getShortClassRef(m.getReturnType())
164: + " ");
165:
166: out.print("" + m.getName() + " ");
167: Class[] params = m.getParameterTypes();
168: out.print("<font color='gray'>(</font>");
169: for (int j = 0; j < params.length; j++) {
170: out.print(getShortClassRef(params[j]));
171: if (j != params.length - 1) {
172: out.print(", ");
173: }
174: }
175: out.print("<font color='gray'>)</font>");
176:
177: Class[] excp = m.getExceptionTypes();
178: if (excp.length > 0) {
179: out.print(" <font color='gray'>throws</font> ");
180: for (int j = 0; j < excp.length; j++) {
181: out.print(getShortClassRef(excp[j]));
182: if (j != excp.length - 1) {
183: out.print(", ");
184: }
185: }
186: }
187: out.print("<br>");
188: }
189:
190: public String getModifier(Method m) throws Exception {
191: int mod = m.getModifiers();
192: String color = "";
193:
194: if (Modifier.isPublic(mod)) {
195: color = "green";
196: } else if (Modifier.isPrivate(mod)) {
197: color = "red";
198: } else if (Modifier.isProtected(mod)) {
199: color = "blue";
200: } else {
201: color = "yellow";
202: }
203: return "<font color='" + color + "'>*</font>";
204: }
205:
206: public String getClassRef(Class clazz) throws Exception {
207: String name = clazz.getName();
208: return "<a href='" + VIEW_CLASS + "?class=" + name + "'>"
209: + name + "</a>";
210: }
211:
212: public String getClassRef(String name) throws Exception {
213: return "<a href='" + VIEW_CLASS + "?class=" + name + "'>"
214: + name + "</a>";
215: }
216:
217: public String getShortClassRef(Class clazz) throws Exception {
218: if (clazz.isPrimitive()) {
219: return "<font color='gray'>" + clazz.getName() + "</font>";
220: } else if (clazz.isArray()
221: && clazz.getComponentType().isPrimitive()) {
222: return "<font color='gray'>" + clazz.getComponentType()
223: + "[]</font>";
224: } else if (clazz.isArray()) {
225: String name = clazz.getComponentType().getName();
226: int dot = name.lastIndexOf(".") + 1;
227: String shortName = name.substring(dot, name.length());
228: return "<a href='" + VIEW_CLASS + "?class=" + name + "'>"
229: + shortName + "[]</a>";
230: } else {
231: String name = clazz.getName();
232: int dot = name.lastIndexOf(".") + 1;
233: String shortName = name.substring(dot, name.length());
234: return "<a href='" + VIEW_CLASS + "?class=" + name + "'>"
235: + shortName + "</a>";
236: }
237: }
238: }
|