001: /*
002: * $Id: CallClassMethod.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:
025: package org.ofbiz.minilang.method.callops;
026:
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 class 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 CallClassMethod extends MethodOperation {
043:
044: public static final String module = CallClassMethod.class.getName();
045:
046: String className;
047: String methodName;
048: ContextAccessor retFieldAcsr;
049: ContextAccessor retMapAcsr;
050:
051: /** A list of MethodObject objects to use as the method call parameters */
052: List parameters;
053:
054: public CallClassMethod(Element element, SimpleMethod simpleMethod) {
055: super (element, simpleMethod);
056: className = element.getAttribute("class-name");
057: methodName = element.getAttribute("method-name");
058: retFieldAcsr = new ContextAccessor(element
059: .getAttribute("ret-field-name"));
060: retMapAcsr = new ContextAccessor(element
061: .getAttribute("ret-map-name"));
062:
063: List parameterElements = UtilXml
064: .childElementList(element, null);
065: if (parameterElements.size() > 0) {
066: parameters = new ArrayList(parameterElements.size());
067:
068: Iterator parameterIter = parameterElements.iterator();
069: while (parameterIter.hasNext()) {
070: Element parameterElement = (Element) parameterIter
071: .next();
072: MethodObject methodObject = null;
073: if ("string".equals(parameterElement.getNodeName())) {
074: methodObject = new StringObject(parameterElement,
075: simpleMethod);
076: } else if ("field".equals(parameterElement
077: .getNodeName())) {
078: methodObject = new FieldObject(parameterElement,
079: simpleMethod);
080: } else {
081: //whoops, invalid tag here, print warning
082: Debug.logWarning(
083: "Found an unsupported tag under the call-object-method tag: "
084: + parameterElement.getNodeName()
085: + "; ignoring", module);
086: }
087: if (methodObject != null) {
088: parameters.add(methodObject);
089: }
090: }
091: }
092: }
093:
094: public boolean exec(MethodContext methodContext) {
095: String className = methodContext.expandString(this .className);
096: String methodName = methodContext.expandString(this .methodName);
097:
098: Class methodClass = null;
099: try {
100: methodClass = ObjectType.loadClass(className, methodContext
101: .getLoader());
102: } catch (ClassNotFoundException e) {
103: Debug
104: .logError(e,
105: "Class to create not found with name "
106: + className
107: + " in create-object operation",
108: module);
109:
110: String errMsg = "ERROR: Could not complete the "
111: + simpleMethod.getShortDescription()
112: + " process [Class to create not found with name "
113: + className + ": " + e.toString() + "]";
114: methodContext.setErrorReturn(errMsg, simpleMethod);
115: return false;
116: }
117:
118: return CallObjectMethod.callMethod(simpleMethod, methodContext,
119: parameters, methodClass, null, methodName,
120: retFieldAcsr, retMapAcsr);
121: }
122: }
|