001: /*
002: * $Id: CallObjectMethod.java,v 1.1 2003/08/17 06:06:13 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.minilang.method.callops;
025:
026: import java.lang.reflect.*;
027: import java.util.*;
028:
029: import org.w3c.dom.*;
030: import org.ofbiz.base.util.*;
031:
032: import org.ofbiz.minilang.*;
033: import org.ofbiz.minilang.method.*;
034:
035: /**
036: * Calls a Java object method using the given fields as parameters
037: *
038: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
039: * @version $Revision: 1.1 $
040: * @since 2.0
041: */
042: public class CallObjectMethod extends MethodOperation {
043:
044: public static final String module = CallClassMethod.class.getName();
045:
046: ContextAccessor objFieldAcsr;
047: ContextAccessor objMapAcsr;
048: String methodName;
049: ContextAccessor retFieldAcsr;
050: ContextAccessor retMapAcsr;
051:
052: /** A list of MethodObject objects to use as the method call parameters */
053: List parameters;
054:
055: public CallObjectMethod(Element element, SimpleMethod simpleMethod) {
056: super (element, simpleMethod);
057: objFieldAcsr = new ContextAccessor(element
058: .getAttribute("obj-field-name"));
059: objMapAcsr = new ContextAccessor(element
060: .getAttribute("obj-map-name"));
061: methodName = element.getAttribute("method-name");
062: retFieldAcsr = new ContextAccessor(element
063: .getAttribute("ret-field-name"));
064: retMapAcsr = new ContextAccessor(element
065: .getAttribute("ret-map-name"));
066:
067: List parameterElements = UtilXml
068: .childElementList(element, null);
069: if (parameterElements.size() > 0) {
070: parameters = new ArrayList(parameterElements.size());
071:
072: Iterator parameterIter = parameterElements.iterator();
073: while (parameterIter.hasNext()) {
074: Element parameterElement = (Element) parameterIter
075: .next();
076: MethodObject methodObject = null;
077: if ("string".equals(parameterElement.getNodeName())) {
078: methodObject = new StringObject(parameterElement,
079: simpleMethod);
080: } else if ("field".equals(parameterElement
081: .getNodeName())) {
082: methodObject = new FieldObject(parameterElement,
083: simpleMethod);
084: } else {
085: //whoops, invalid tag here, print warning
086: Debug.logWarning(
087: "Found an unsupported tag under the call-object-method tag: "
088: + parameterElement.getNodeName()
089: + "; ignoring", module);
090: }
091: if (methodObject != null) {
092: parameters.add(methodObject);
093: }
094: }
095: }
096: }
097:
098: public boolean exec(MethodContext methodContext) {
099: String methodName = methodContext.expandString(this .methodName);
100:
101: Object methodObject = null;
102: if (!objMapAcsr.isEmpty()) {
103: Map fromMap = (Map) objMapAcsr.get(methodContext);
104: if (fromMap == null) {
105: Debug
106: .logWarning(
107: "Map not found with name "
108: + objMapAcsr
109: + ", which should contain the object to execute a method on; not executing method, rerturning error.",
110: module);
111:
112: String errMsg = "ERROR: Could not complete the "
113: + simpleMethod.getShortDescription()
114: + " process [Map not found with name "
115: + objMapAcsr
116: + ", which should contain the object to execute a method on]";
117: methodContext.setErrorReturn(errMsg, simpleMethod);
118: return false;
119: }
120: methodObject = objFieldAcsr.get(fromMap, methodContext);
121: } else {
122: // no map name, try the env
123: methodObject = objFieldAcsr.get(methodContext);
124: }
125:
126: if (methodObject == null) {
127: if (Debug.infoOn())
128: Debug
129: .logInfo(
130: "Object not found to execute method on with name "
131: + objFieldAcsr
132: + " in Map with name "
133: + objMapAcsr
134: + ", not executing method, rerturning error.",
135: module);
136:
137: String errMsg = "ERROR: Could not complete the "
138: + simpleMethod.getShortDescription()
139: + " process [Object not found to execute method on with name "
140: + objFieldAcsr + " in Map with name " + objMapAcsr
141: + "]";
142: methodContext.setErrorReturn(errMsg, simpleMethod);
143: return false;
144: }
145:
146: Class methodClass = methodObject.getClass();
147: return CallObjectMethod.callMethod(simpleMethod, methodContext,
148: parameters, methodClass, methodObject, methodName,
149: retFieldAcsr, retMapAcsr);
150: }
151:
152: public static boolean callMethod(SimpleMethod simpleMethod,
153: MethodContext methodContext, List parameters,
154: Class methodClass, Object methodObject, String methodName,
155: ContextAccessor retFieldAcsr, ContextAccessor retMapAcsr) {
156: Object[] args = null;
157: Class[] parameterTypes = null;
158:
159: if (parameters != null) {
160: args = new Object[parameters.size()];
161: parameterTypes = new Class[parameters.size()];
162:
163: Iterator parameterIter = parameters.iterator();
164: int i = 0;
165: while (parameterIter.hasNext()) {
166: MethodObject methodObjectDef = (MethodObject) parameterIter
167: .next();
168: args[i] = methodObjectDef.getObject(methodContext);
169:
170: Class typeClass = methodObjectDef
171: .getTypeClass(methodContext.getLoader());
172: if (typeClass == null) {
173: String errMsg = "ERROR: Could not complete the "
174: + simpleMethod.getShortDescription()
175: + " process [Parameter type not found with name "
176: + methodObjectDef.getTypeName() + "]";
177: Debug.logError(errMsg, module);
178: methodContext.setErrorReturn(errMsg, simpleMethod);
179: return false;
180: }
181:
182: parameterTypes[i] = typeClass;
183: i++;
184: }
185: }
186:
187: try {
188: Method method = methodClass.getMethod(methodName,
189: parameterTypes);
190: try {
191: Object retValue = method.invoke(methodObject, args);
192:
193: //if retFieldAcsr is empty, ignore return value
194: if (!retFieldAcsr.isEmpty()) {
195: if (!retMapAcsr.isEmpty()) {
196: Map retMap = (Map) retMapAcsr
197: .get(methodContext);
198:
199: if (retMap == null) {
200: retMap = new HashMap();
201: retMapAcsr.put(methodContext, retMap);
202: }
203: retFieldAcsr.put(retMap, retValue,
204: methodContext);
205: } else {
206: // no map name, use the env
207: retFieldAcsr.put(methodContext, retValue);
208: }
209: }
210: } catch (IllegalAccessException e) {
211: Debug
212: .logError(
213: e,
214: "Could not access method in call method operation",
215: module);
216: String errMsg = "ERROR: Could not complete the "
217: + simpleMethod.getShortDescription()
218: + " process [Could not access method to execute named "
219: + methodName + ": " + e.toString() + "]";
220: methodContext.setErrorReturn(errMsg, simpleMethod);
221: return false;
222: } catch (IllegalArgumentException e) {
223: Debug
224: .logError(
225: e,
226: "Illegal argument calling method in call method operation",
227: module);
228: String errMsg = "ERROR: Could not complete the "
229: + simpleMethod.getShortDescription()
230: + " process [Illegal argument calling method to execute named "
231: + methodName + ": " + e.toString() + "]";
232: methodContext.setErrorReturn(errMsg, simpleMethod);
233: return false;
234: } catch (InvocationTargetException e) {
235: Debug
236: .logError(
237: e.getTargetException(),
238: "Method in call method operation threw an exception",
239: module);
240: String errMsg = "ERROR: Could not complete the "
241: + simpleMethod.getShortDescription()
242: + " process [Method to execute named "
243: + methodName + " threw an exception: "
244: + e.getTargetException() + "]";
245: methodContext.setErrorReturn(errMsg, simpleMethod);
246: return false;
247: }
248: } catch (NoSuchMethodException e) {
249: Debug
250: .logError(
251: e,
252: "Could not find method to execute in simple-method call method operation",
253: module);
254: String errMsg = "ERROR: Could not complete the "
255: + simpleMethod.getShortDescription()
256: + " process [Could not find method to execute named "
257: + methodName + ": " + e.toString() + "]";
258: methodContext.setErrorReturn(errMsg, simpleMethod);
259: return false;
260: } catch (SecurityException e) {
261: Debug
262: .logError(
263: e,
264: "Security exception finding method to execute in simple-method call method operation",
265: module);
266: String errMsg = "ERROR: Could not complete the "
267: + simpleMethod.getShortDescription()
268: + " process [Security exception finding method to execute named "
269: + methodName + ": " + e.toString() + "]";
270: methodContext.setErrorReturn(errMsg, simpleMethod);
271: return false;
272: }
273:
274: return true;
275: }
276: }
|