1 16 17 18 package org.apache.xmlrpc; 19 20 import java.lang.reflect.InvocationTargetException ; 21 import java.lang.reflect.Method ; 22 import java.util.Vector ; 23 24 32 public class Invoker implements XmlRpcHandler 33 { 34 private Object invokeTarget; 35 private Class targetClass; 36 37 public Invoker(Object target) 38 { 39 invokeTarget = target; 40 targetClass = (invokeTarget instanceof Class ) ? (Class ) invokeTarget : 41 invokeTarget.getClass(); 42 if (XmlRpc.debug) 43 { 44 System.out.println("Target object is " + targetClass); 45 } 46 } 47 48 51 public Object execute(String methodName, Vector params) throws Exception 52 { 53 Class [] argClasses = null; 55 Object [] argValues = null; 56 if (params != null) 57 { 58 argClasses = new Class [params.size()]; 59 argValues = new Object [params.size()]; 60 for (int i = 0; i < params.size(); i++) 61 { 62 argValues[i] = params.elementAt(i); 63 if (argValues[i] instanceof Integer ) 64 { 65 argClasses[i] = Integer.TYPE; 66 } 67 else if (argValues[i] instanceof Double ) 68 { 69 argClasses[i] = Double.TYPE; 70 } 71 else if (argValues[i] instanceof Boolean ) 72 { 73 argClasses[i] = Boolean.TYPE; 74 } 75 else 76 { 77 argClasses[i] = argValues[i].getClass(); 78 } 79 } 80 } 81 82 Method method = null; 84 85 int dot = methodName.lastIndexOf('.'); 88 if (dot > -1 && dot + 1 < methodName.length()) 89 { 90 methodName = methodName.substring(dot + 1); 91 } 92 93 if (XmlRpc.debug) 94 { 95 System.out.println("Searching for method: " + methodName + 96 " in class " + targetClass.getName()); 97 for (int i = 0; i < argClasses.length; i++) 98 { 99 System.out.println("Parameter " + i + ": " + argValues[i] 100 + " (" + argClasses[i] + ')'); 101 } 102 } 103 104 try 105 { 106 method = targetClass.getMethod(methodName, argClasses); 107 } 108 catch(NoSuchMethodException nsm_e) 110 { 111 throw nsm_e; 112 } 113 catch(SecurityException s_e) 114 { 115 throw s_e; 116 } 117 118 if (method.getDeclaringClass() == Object .class) 121 { 122 throw new XmlRpcException(0, "Invoker can't call methods " 123 + "defined in java.lang.Object"); 124 } 125 126 Object returnValue = null; 128 try 129 { 130 returnValue = method.invoke(invokeTarget, argValues); 131 } 132 catch(IllegalAccessException iacc_e) 133 { 134 throw iacc_e; 135 } 136 catch(IllegalArgumentException iarg_e) 137 { 138 throw iarg_e; 139 } 140 catch(InvocationTargetException it_e) 141 { 142 if (XmlRpc.debug) 143 { 144 it_e.getTargetException().printStackTrace(); 145 } 146 Throwable t = it_e.getTargetException(); 148 if (t instanceof XmlRpcException) 149 { 150 throw (XmlRpcException) t; 151 } 152 throw new Exception (t.toString()); 154 } 155 if (returnValue == null && method.getReturnType() == Void.TYPE) 156 { 157 throw new IllegalArgumentException 159 ("void return types for handler methods not supported"); 160 } 161 return returnValue; 162 } 163 } 164 | Popular Tags |