KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > tools > adaptor > http > ConstructorsCommandProcessor


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8 package mx4j.tools.adaptor.http;
9
10 import java.io.IOException JavaDoc;
11 import java.lang.reflect.Constructor JavaDoc;
12 import java.util.Arrays JavaDoc;
13 import javax.management.JMException JavaDoc;
14 import javax.management.loading.DefaultLoaderRepository JavaDoc;
15
16 import org.w3c.dom.Document JavaDoc;
17 import org.w3c.dom.Element JavaDoc;
18
19 /**
20  * ConstructorsCommandProcessor, processes a request to query the available
21  * constructors for a classname
22  *
23  * @version $Revision: 1.3 $
24  */

25 public class ConstructorsCommandProcessor extends HttpCommandProcessorAdaptor
26 {
27    public ConstructorsCommandProcessor()
28    {
29    }
30
31    public Document JavaDoc executeRequest(HttpInputStream in) throws IOException JavaDoc, JMException JavaDoc
32    {
33       Document JavaDoc document = builder.newDocument();
34
35       String JavaDoc 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          // look class in default classloader
43
Class JavaDoc targetClass = null;
44          try
45          {
46             targetClass = DefaultLoaderRepository.loadClass(classname);
47          }
48          catch (ClassNotFoundException JavaDoc e)
49          {
50          }
51          try
52          {
53             if (targetClass == null)
54             {
55                targetClass = ClassLoader.getSystemClassLoader().loadClass(classname);
56             }
57          }
58          catch (ClassNotFoundException JavaDoc e)
59          {
60          }
61          try
62          {
63             if (targetClass == null)
64             {
65                targetClass = getClass().getClassLoader().loadClass(classname);
66             }
67          }
68          catch (ClassNotFoundException JavaDoc e)
69          {
70          }
71
72          if (targetClass == null)
73          {
74             return createException(document, classname, "class " + classname + " not found");
75          }
76
77          Element JavaDoc root = document.createElement("Class");
78          root.setAttribute("classname", classname);
79          document.appendChild(root);
80          Constructor JavaDoc[] 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 JavaDoc 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 JavaDoc node, Document JavaDoc document, Class JavaDoc[] parameters)
95    {
96       for (int j = 0; j < parameters.length; j++)
97       {
98          Element JavaDoc parameter = document.createElement("Parameter");
99          parameter.setAttribute("type", parameters[j].getName());
100          parameter.setAttribute("strinit", String.valueOf(CommandProcessorUtil.canCreateParameterValue(parameters[j].getName())));
101          // add id since order is relevant
102
parameter.setAttribute("id", "" + j);
103          node.appendChild(parameter);
104       }
105    }
106
107    private Document JavaDoc createException(Document JavaDoc document, String JavaDoc classname, String JavaDoc message)
108    {
109       Element JavaDoc 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