KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: CallClassMethod.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
25 package org.ofbiz.minilang.method.callops;
26
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 class 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 CallClassMethod extends MethodOperation {
43     
44     public static final String JavaDoc module = CallClassMethod.class.getName();
45
46     String JavaDoc className;
47     String JavaDoc methodName;
48     ContextAccessor retFieldAcsr;
49     ContextAccessor retMapAcsr;
50
51     /** A list of MethodObject objects to use as the method call parameters */
52     List parameters;
53
54     public CallClassMethod(Element element, SimpleMethod simpleMethod) {
55         super(element, simpleMethod);
56         className = element.getAttribute("class-name");
57         methodName = element.getAttribute("method-name");
58         retFieldAcsr = new ContextAccessor(element.getAttribute("ret-field-name"));
59         retMapAcsr = new ContextAccessor(element.getAttribute("ret-map-name"));
60         
61         List parameterElements = UtilXml.childElementList(element);
62         if (parameterElements.size() > 0) {
63             parameters = new ArrayList(parameterElements.size());
64             
65             Iterator parameterIter = parameterElements.iterator();
66             while (parameterIter.hasNext()) {
67                 Element parameterElement = (Element) parameterIter.next();
68                 MethodObject methodObject = null;
69                 if ("string".equals(parameterElement.getNodeName())) {
70                     methodObject = new StringObject(parameterElement, simpleMethod);
71                 } else if ("field".equals(parameterElement.getNodeName())) {
72                     methodObject = new FieldObject(parameterElement, simpleMethod);
73                 } else {
74                     //whoops, invalid tag here, print warning
75
Debug.logWarning("Found an unsupported tag under the call-object-method tag: " + parameterElement.getNodeName() + "; ignoring", module);
76                 }
77                 if (methodObject != null) {
78                     parameters.add(methodObject);
79                 }
80             }
81         }
82     }
83
84     public boolean exec(MethodContext methodContext) {
85         String JavaDoc className = methodContext.expandString(this.className);
86         String JavaDoc methodName = methodContext.expandString(this.methodName);
87         
88         Class JavaDoc methodClass = null;
89         try {
90             methodClass = ObjectType.loadClass(className, methodContext.getLoader());
91         } catch (ClassNotFoundException JavaDoc e) {
92             Debug.logError(e, "Class to create not found with name " + className + " in create-object operation", module);
93
94             String JavaDoc errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [Class to create not found with name " + className + ": " + e.toString() + "]";
95             methodContext.setErrorReturn(errMsg, simpleMethod);
96             return false;
97         }
98         
99         return CallObjectMethod.callMethod(simpleMethod, methodContext, parameters, methodClass, null, methodName, retFieldAcsr, retMapAcsr);
100     }
101
102     public String JavaDoc rawString() {
103         // TODO: something more than the empty tag
104
return "<call-class-method/>";
105     }
106     public String JavaDoc expandedString(MethodContext methodContext) {
107         // TODO: something more than a stub/dummy
108
return this.rawString();
109     }
110 }
111
Popular Tags