org.apache.openejb.webadmin.clienttools.ViewClassBean Maven / Gradle / Ivy
The newest version!
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.openejb.webadmin.clienttools;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import org.apache.openejb.webadmin.HttpRequest;
import org.apache.openejb.webadmin.HttpResponse;
import org.apache.openejb.webadmin.WebAdminBean;
import org.apache.openejb.webadmin.HttpHome;
import javax.ejb.Stateless;
import javax.ejb.RemoteHome;
/**
* @author David Blevins
*/
@Stateless(name = "ClientTools/ViewClass")
@RemoteHome(HttpHome.class)
public class ViewClassBean extends WebAdminBean implements Constants {
boolean hasMethods;
public void preProcess(HttpRequest request, HttpResponse response)
throws IOException {
}
public void postProcess(HttpRequest request, HttpResponse response)
throws IOException {
}
public void writeHtmlTitle(PrintWriter out) throws IOException {
out.write("Client Tools -- JNDI Viewer");
}
public void writePageTitle(PrintWriter out) throws IOException {
out.write("JNDI Viewer");
}
public void writeBody(PrintWriter out) throws IOException {
try {
String className = request.getQueryParameter("class");
if (className == null) {
out.print("Enter a class name to browse:");
out.print(
"");
out.print("Or browse one of these fun classes:
");
out.print(tab + getClassRef("javax.ejb.EJBHome") + "
");
out.print(tab + getClassRef("javax.ejb.EJBObject") + "
");
out.print(
tab + getClassRef("javax.ejb.EnterpriseBean") + "
");
out.print(tab + getClassRef("javax.ejb.SessionBean") + "
");
out.print(tab + getClassRef("javax.ejb.EntityBean") + "
");
out.print(
tab
+ getClassRef("javax.servlet.http.HttpServlet")
+ "
");
out.print(
tab
+ getClassRef("javax.servlet.http.HttpServletRequest")
+ "
");
out.print(
tab
+ getClassRef("javax.servlet.http.HttpServletResponse")
+ "
");
out.print(
tab
+ getClassRef("javax.servlet.http.HttpSession")
+ "
");
out.print(
tab + getClassRef("javax.naming.InitialContext") + "
");
out.print(tab + getClassRef("javax.naming.Context") + "
");
} else {
Class clazz = this.getClass().forName(className);
printClass(clazz, out);
}
} catch (Exception e) {
out.println("FAIL");
return;
}
out.print("
");
if (hasMethods) {
out.print("* Public ");
out.print("* Private ");
out.print("* Protected ");
out.print("* Default ");
out.print("
");
}
}
public void printClass(Class clazz, PrintWriter out)
throws Exception {
out.print("" + clazz.getName() + "
");
Method[] methods = clazz.getDeclaredMethods();
hasMethods = (methods.length > 0);
for (int i = 0; i < methods.length; i++) {
printMethod(methods[i], out);
}
/*
* //out.print(" Public Methods:
*
"); for (int i=0; i < methods.length; i++){ if
* (Modifier.isPublic(methods[i].getModifiers())){ printMethod(
* methods[i], out ); } } //out.print("
* Private Methods:
"); for (int i=0; i
* < methods.length; i++){ if
* (Modifier.isPrivate(methods[i].getModifiers())){ printMethod(
* methods[i], out ); } } for (int i=0; i < methods.length; i++){ if
* (Modifier.isProtected(methods[i].getModifiers())){ printMethod(
* methods[i], out ); } } for (int i=0; i < methods.length; i++){ if
* (!Modifier.isSrict(methods[i].getModifiers())){ printMethod(
* methods[i], out ); } }
*/
Class sup = clazz.getSuperclass();
if (sup != null) {
out.print("
Extends:
");
out.print(tab + getClassRef(sup) + "
");
}
Class[] intf = clazz.getInterfaces();
if (intf.length > 0) {
out.print("
Implements:
");
for (int i = 0; i < intf.length; i++) {
out.print(tab + getClassRef(intf[i]) + "
");
}
}
}
public void printMethod(Method m, PrintWriter out)
throws Exception {
out.print(tab);
out.print(" " + getModifier(m));
out.print(" " + getShortClassRef(m.getReturnType()) + " ");
out.print("" + m.getName() + " ");
Class[] params = m.getParameterTypes();
out.print("(");
for (int j = 0; j < params.length; j++) {
out.print(getShortClassRef(params[j]));
if (j != params.length - 1) {
out.print(", ");
}
}
out.print(")");
Class[] excp = m.getExceptionTypes();
if (excp.length > 0) {
out.print(" throws ");
for (int j = 0; j < excp.length; j++) {
out.print(getShortClassRef(excp[j]));
if (j != excp.length - 1) {
out.print(", ");
}
}
}
out.print("
");
}
public String getModifier(Method m) throws Exception {
int mod = m.getModifiers();
String color = "";
if (Modifier.isPublic(mod)) {
color = "green";
} else if (Modifier.isPrivate(mod)) {
color = "red";
} else if (Modifier.isProtected(mod)) {
color = "blue";
} else {
color = "yellow";
}
return "*";
}
public String getClassRef(Class clazz) throws Exception {
String name = clazz.getName();
return "" + name + "";
}
public String getClassRef(String name) throws Exception {
return "" + name + "";
}
public String getShortClassRef(Class clazz) throws Exception {
if (clazz.isPrimitive()) {
return "" + clazz.getName() + "";
} else if (clazz.isArray() && clazz.getComponentType().isPrimitive()) {
return ""
+ clazz.getComponentType()
+ "[]";
} else if (clazz.isArray()) {
String name = clazz.getComponentType().getName();
int dot = name.lastIndexOf(".") + 1;
String shortName = name.substring(dot, name.length());
return ""
+ shortName
+ "[]";
} else {
String name = clazz.getName();
int dot = name.lastIndexOf(".") + 1;
String shortName = name.substring(dot, name.length());
return ""
+ shortName
+ "";
}
}
}