1 24 package org.ofbiz.minilang.method.callops; 25 26 import java.lang.reflect.*; 27 import java.util.*; 28 29 import org.w3c.dom.*; 30 import org.ofbiz.base.util.*; 31 32 import org.ofbiz.minilang.*; 33 import org.ofbiz.minilang.method.*; 34 35 42 public class CallObjectMethod extends MethodOperation { 43 44 public static final String module = CallClassMethod.class.getName(); 45 46 ContextAccessor objFieldAcsr; 47 ContextAccessor objMapAcsr; 48 String methodName; 49 ContextAccessor retFieldAcsr; 50 ContextAccessor retMapAcsr; 51 52 53 List parameters; 54 55 public CallObjectMethod(Element element, SimpleMethod simpleMethod) { 56 super(element, simpleMethod); 57 objFieldAcsr = new ContextAccessor(element.getAttribute("obj-field-name")); 58 objMapAcsr = new ContextAccessor(element.getAttribute("obj-map-name")); 59 methodName = element.getAttribute("method-name"); 60 retFieldAcsr = new ContextAccessor(element.getAttribute("ret-field-name")); 61 retMapAcsr = new ContextAccessor(element.getAttribute("ret-map-name")); 62 63 List parameterElements = UtilXml.childElementList(element); 64 if (parameterElements.size() > 0) { 65 parameters = new ArrayList(parameterElements.size()); 66 67 Iterator parameterIter = parameterElements.iterator(); 68 while (parameterIter.hasNext()) { 69 Element parameterElement = (Element) parameterIter.next(); 70 MethodObject methodObject = null; 71 if ("string".equals(parameterElement.getNodeName())) { 72 methodObject = new StringObject(parameterElement, simpleMethod); 73 } else if ("field".equals(parameterElement.getNodeName())) { 74 methodObject = new FieldObject(parameterElement, simpleMethod); 75 } else { 76 Debug.logWarning("Found an unsupported tag under the call-object-method tag: " + parameterElement.getNodeName() + "; ignoring", module); 78 } 79 if (methodObject != null) { 80 parameters.add(methodObject); 81 } 82 } 83 } 84 } 85 86 public boolean exec(MethodContext methodContext) { 87 String methodName = methodContext.expandString(this.methodName); 88 89 Object methodObject = null; 90 if (!objMapAcsr.isEmpty()) { 91 Map fromMap = (Map) objMapAcsr.get(methodContext); 92 if (fromMap == null) { 93 Debug.logWarning("Map not found with name " + objMapAcsr + ", which should contain the object to execute a method on; not executing method, rerturning error.", module); 94 95 String errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [Map not found with name " + objMapAcsr + ", which should contain the object to execute a method on]"; 96 methodContext.setErrorReturn(errMsg, simpleMethod); 97 return false; 98 } 99 methodObject = objFieldAcsr.get(fromMap, methodContext); 100 } else { 101 methodObject = objFieldAcsr.get(methodContext); 103 } 104 105 if (methodObject == null) { 106 if (Debug.infoOn()) Debug.logInfo("Object not found to execute method on with name " + objFieldAcsr + " in Map with name " + objMapAcsr + ", not executing method, rerturning error.", module); 107 108 String errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [Object not found to execute method on with name " + objFieldAcsr + " in Map with name " + objMapAcsr + "]"; 109 methodContext.setErrorReturn(errMsg, simpleMethod); 110 return false; 111 } 112 113 Class methodClass = methodObject.getClass(); 114 return CallObjectMethod.callMethod(simpleMethod, methodContext, parameters, methodClass, methodObject, methodName, retFieldAcsr, retMapAcsr); 115 } 116 117 public static boolean callMethod(SimpleMethod simpleMethod, MethodContext methodContext, List parameters, Class methodClass, Object methodObject, String methodName, ContextAccessor retFieldAcsr, ContextAccessor retMapAcsr) { 118 Object [] args = null; 119 Class [] parameterTypes = null; 120 121 if (parameters != null) { 122 args = new Object [parameters.size()]; 123 parameterTypes = new Class [parameters.size()]; 124 125 Iterator parameterIter = parameters.iterator(); 126 int i = 0; 127 while (parameterIter.hasNext()) { 128 MethodObject methodObjectDef = (MethodObject) parameterIter.next(); 129 args[i] = methodObjectDef.getObject(methodContext); 130 131 Class typeClass = methodObjectDef.getTypeClass(methodContext.getLoader()); 132 if (typeClass == null) { 133 String errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [Parameter type not found with name " + methodObjectDef.getTypeName() + "]"; 134 Debug.logError(errMsg, module); 135 methodContext.setErrorReturn(errMsg, simpleMethod); 136 return false; 137 } 138 139 parameterTypes[i] = typeClass; 140 i++; 141 } 142 } 143 144 try { 145 Method method = methodClass.getMethod(methodName, parameterTypes); 146 try { 147 Object retValue = method.invoke(methodObject, args); 148 149 if (!retFieldAcsr.isEmpty()) { 151 if (!retMapAcsr.isEmpty()) { 152 Map retMap = (Map) retMapAcsr.get(methodContext); 153 154 if (retMap == null) { 155 retMap = new HashMap(); 156 retMapAcsr.put(methodContext, retMap); 157 } 158 retFieldAcsr.put(retMap, retValue, methodContext); 159 } else { 160 retFieldAcsr.put(methodContext, retValue); 162 } 163 } 164 } catch (IllegalAccessException e) { 165 Debug.logError(e, "Could not access method in call method operation", module); 166 String errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [Could not access method to execute named " + methodName + ": " + e.toString() + "]"; 167 methodContext.setErrorReturn(errMsg, simpleMethod); 168 return false; 169 } catch (IllegalArgumentException e) { 170 Debug.logError(e, "Illegal argument calling method in call method operation", module); 171 String errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [Illegal argument calling method to execute named " + methodName + ": " + e.toString() + "]"; 172 methodContext.setErrorReturn(errMsg, simpleMethod); 173 return false; 174 } catch (InvocationTargetException e) { 175 Debug.logError(e.getTargetException(), "Method in call method operation threw an exception", module); 176 String errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [Method to execute named " + methodName + " threw an exception: " + e.getTargetException() + "]"; 177 methodContext.setErrorReturn(errMsg, simpleMethod); 178 return false; 179 } 180 } catch (NoSuchMethodException e) { 181 Debug.logError(e, "Could not find method to execute in simple-method call method operation", module); 182 String errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [Could not find method to execute named " + methodName + ": " + e.toString() + "]"; 183 methodContext.setErrorReturn(errMsg, simpleMethod); 184 return false; 185 } catch (SecurityException e) { 186 Debug.logError(e, "Security exception finding method to execute in simple-method call method operation", module); 187 String errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [Security exception finding method to execute named " + methodName + ": " + e.toString() + "]"; 188 methodContext.setErrorReturn(errMsg, simpleMethod); 189 return false; 190 } 191 192 return true; 193 } 194 195 public String rawString() { 196 return "<call-object-method/>"; 198 } 199 public String expandedString(MethodContext methodContext) { 200 return this.rawString(); 202 } 203 } 204 | Popular Tags |