1 package org.jfox.ioc.management.http; 2 3 import java.lang.reflect.Method ; 4 import java.util.List ; 5 import java.util.Map ; 6 import javax.servlet.http.HttpServletRequest ; 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 18 19 public class InvokeServlet extends SuperServlet { 20 21 22 public String getTemplate() { 23 return "resultview.vm"; 24 } 25 26 public void buildContext(HttpServletRequest request, Context ctx) throws Exception { 27 String operHashCode = request.getParameter("operHashCode"); 28 String _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 method = operInfo.getMethod(); 35 Class [] paramTypes = method.getParameterTypes(); 36 String [] _values = new String [paramTypes.length]; 37 for(int i = 0; i < paramTypes.length; i++) { 38 String _value = request.getParameter(operInfo.getName() + "_" + i); 39 if(_value == null) { 40 _value = ""; 41 } 42 _values[i] = _value.trim(); 43 } 44 45 Object result = null; 46 47 Object [] 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 ) ) { 60 ctx.put("isList", true); 61 } 62 else if(result instanceof Map ) { 63 ctx.put("isMap", true); 64 } 65 } 66 else { 67 } 68 69 } 70 71 private Object [] createObjects(String [] values, Class [] signatures) throws Exception { 72 Object [] objects = new Object [values.length]; 73 for(int i = 0; i < objects.length; i++) { 74 Object obj = Classes.newObject(signatures[i], values[i]); 75 objects[i] = obj; 76 } 77 return objects; 78 } 79 80 public static void main(String [] args) { 81 82 } 83 } 84 85 | Popular Tags |