KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ioc > management > http > InvokeServlet


1 package org.jfox.ioc.management.http;
2
3 import java.lang.reflect.Method JavaDoc;
4 import java.util.List JavaDoc;
5 import java.util.Map JavaDoc;
6 import javax.servlet.http.HttpServletRequest JavaDoc;
7
8 import org.apache.velocity.context.Context;
9 import org.jfox.ioc.ComponentMeta;
10 import org.jfox.ioc.ComponentName;
11 import org.jfox.ioc.management.ManagementInfo;
12 import org.jfox.ioc.management.ManagementOperationInfo;
13 import org.jfox.ioc.util.Classes;
14
15 /**
16  * @author <a HREF="mailto:yy.young@gmail.com">Young Yang</a>
17  */

18
19 public class InvokeServlet extends SuperServlet {
20
21
22     public String JavaDoc getTemplate() {
23         return "resultview.vm";
24     }
25
26     public void buildContext(HttpServletRequest JavaDoc request, Context ctx) throws Exception JavaDoc {
27         String JavaDoc operHashCode = request.getParameter("operHashCode");
28         String JavaDoc _componentName = request.getParameter("componentname");
29         ComponentName componentName = ComponentName.parseString(_componentName,getRegistry().getClassLoaderRepository());
30
31         ComponentMeta meta = getRegistry().getComponentMeta(componentName);
32         ManagementInfo minfo = meta.getManagementInfo();
33         ManagementOperationInfo operInfo = minfo.getManagementOperationInfo(operHashCode);
34         Method JavaDoc method = operInfo.getMethod();
35         Class JavaDoc[] paramTypes = method.getParameterTypes();
36         String JavaDoc[] _values = new String JavaDoc[paramTypes.length];
37         for(int i = 0; i < paramTypes.length; i++) {
38             String JavaDoc _value = request.getParameter(operInfo.getName() + "_" + i);
39             if(_value == null) {
40                 _value = "";
41             }
42             _values[i] = _value.trim();
43         }
44
45         Object JavaDoc result = null;
46
47         Object JavaDoc[] values = createObjects(_values, paramTypes);
48         result = method.invoke(getRegistry().getComponentInstance(componentName), values);
49
50         ctx.put("title","Result View");
51         ctx.put("componentName",meta.getComponentName());
52         ctx.put("operInfo",operInfo);
53         ctx.put("result",result == null ? "" : result);
54
55         ctx.put("isList", false);
56         ctx.put("isMap", false);
57
58         if(result != null) {
59             if(result.getClass().isArray() || (result instanceof List JavaDoc) ) {
60                 ctx.put("isList", true);
61             }
62             else if(result instanceof Map JavaDoc) {
63                 ctx.put("isMap", true);
64             }
65         }
66         else {
67         }
68
69     }
70
71     private Object JavaDoc[] createObjects(String JavaDoc[] values, Class JavaDoc[] signatures) throws Exception JavaDoc {
72         Object JavaDoc[] objects = new Object JavaDoc[values.length];
73         for(int i = 0; i < objects.length; i++) {
74             Object JavaDoc obj = Classes.newObject(signatures[i], values[i]);
75             objects[i] = obj;
76         }
77         return objects;
78     }
79
80     public static void main(String JavaDoc[] args) {
81
82     }
83 }
84
85
Popular Tags