1 8 package mx4j.tools.adaptor.http; 9 10 import java.io.IOException ; 11 import java.lang.reflect.Constructor ; 12 import java.util.Arrays ; 13 import javax.management.JMException ; 14 import javax.management.loading.DefaultLoaderRepository ; 15 16 import org.w3c.dom.Document ; 17 import org.w3c.dom.Element ; 18 19 25 public class ConstructorsCommandProcessor extends HttpCommandProcessorAdaptor 26 { 27 public ConstructorsCommandProcessor() 28 { 29 } 30 31 public Document executeRequest(HttpInputStream in) throws IOException , JMException 32 { 33 Document document = builder.newDocument(); 34 35 String classname = in.getVariable("classname"); 36 if (classname == null || classname.trim().length() == 0) 37 { 38 return createException(document, "", "classname parameter required"); 39 } 40 else 41 { 42 Class targetClass = null; 44 try 45 { 46 targetClass = DefaultLoaderRepository.loadClass(classname); 47 } 48 catch (ClassNotFoundException e) 49 { 50 } 51 try 52 { 53 if (targetClass == null) 54 { 55 targetClass = ClassLoader.getSystemClassLoader().loadClass(classname); 56 } 57 } 58 catch (ClassNotFoundException e) 59 { 60 } 61 try 62 { 63 if (targetClass == null) 64 { 65 targetClass = getClass().getClassLoader().loadClass(classname); 66 } 67 } 68 catch (ClassNotFoundException e) 69 { 70 } 71 72 if (targetClass == null) 73 { 74 return createException(document, classname, "class " + classname + " not found"); 75 } 76 77 Element root = document.createElement("Class"); 78 root.setAttribute("classname", classname); 79 document.appendChild(root); 80 Constructor [] constructors = targetClass.getConstructors(); 81 Arrays.sort(constructors, CommandProcessorUtil.createConstructorComparator()); 82 for (int i = 0; i < constructors.length; i++) 83 { 84 System.out.println("Constructor " + constructors[i]); 85 Element constructor = document.createElement("Constructor"); 86 constructor.setAttribute("name", constructors[i].getName()); 87 addParameters(constructor, document, constructors[i].getParameterTypes()); 88 root.appendChild(constructor); 89 } 90 } 91 return document; 92 } 93 94 protected void addParameters(Element node, Document document, Class [] parameters) 95 { 96 for (int j = 0; j < parameters.length; j++) 97 { 98 Element parameter = document.createElement("Parameter"); 99 parameter.setAttribute("type", parameters[j].getName()); 100 parameter.setAttribute("strinit", String.valueOf(CommandProcessorUtil.canCreateParameterValue(parameters[j].getName()))); 101 parameter.setAttribute("id", "" + j); 103 node.appendChild(parameter); 104 } 105 } 106 107 private Document createException(Document document, String classname, String message) 108 { 109 Element exceptionElement = document.createElement("Exception"); 110 document.appendChild(exceptionElement); 111 exceptionElement.setAttribute("classname", classname); 112 exceptionElement.setAttribute("errorMsg", message); 113 return document; 114 } 115 116 } 117 | Popular Tags |