KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > method > callops > CallObjectMethod


1 /*
2  * $Id: CallObjectMethod.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

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 /**
36  * Calls a Java object method using the given fields as parameters
37  *
38  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
39  * @version $Rev: 5462 $
40  * @since 2.0
41  */

42 public class CallObjectMethod extends MethodOperation {
43     
44     public static final String JavaDoc module = CallClassMethod.class.getName();
45
46     ContextAccessor objFieldAcsr;
47     ContextAccessor objMapAcsr;
48     String JavaDoc methodName;
49     ContextAccessor retFieldAcsr;
50     ContextAccessor retMapAcsr;
51
52     /** A list of MethodObject objects to use as the method call parameters */
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                     //whoops, invalid tag here, print warning
77
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 JavaDoc methodName = methodContext.expandString(this.methodName);
88
89         Object JavaDoc 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 JavaDoc 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             // no map name, try the env
102
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 JavaDoc 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 JavaDoc 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 JavaDoc methodClass, Object JavaDoc methodObject, String JavaDoc methodName, ContextAccessor retFieldAcsr, ContextAccessor retMapAcsr) {
118         Object JavaDoc[] args = null;
119         Class JavaDoc[] parameterTypes = null;
120
121         if (parameters != null) {
122             args = new Object JavaDoc[parameters.size()];
123             parameterTypes = new Class JavaDoc[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 JavaDoc typeClass = methodObjectDef.getTypeClass(methodContext.getLoader());
132                 if (typeClass == null) {
133                     String JavaDoc 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 JavaDoc retValue = method.invoke(methodObject, args);
148                 
149                 //if retFieldAcsr is empty, ignore return value
150
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                         // no map name, use the env
161
retFieldAcsr.put(methodContext, retValue);
162                     }
163                 }
164             } catch (IllegalAccessException JavaDoc e) {
165                 Debug.logError(e, "Could not access method in call method operation", module);
166                 String JavaDoc 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 JavaDoc e) {
170                 Debug.logError(e, "Illegal argument calling method in call method operation", module);
171                 String JavaDoc 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 JavaDoc 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 JavaDoc e) {
181             Debug.logError(e, "Could not find method to execute in simple-method call method operation", module);
182             String JavaDoc 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 JavaDoc e) {
186             Debug.logError(e, "Security exception finding method to execute in simple-method call method operation", module);
187             String JavaDoc 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 JavaDoc rawString() {
196         // TODO: something more than the empty tag
197
return "<call-object-method/>";
198     }
199     public String JavaDoc expandedString(MethodContext methodContext) {
200         // TODO: something more than a stub/dummy
201
return this.rawString();
202     }
203 }
204
Popular Tags