001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.minilang.method.callops;
019:
020: import java.util.*;
021:
022: import org.w3c.dom.*;
023: import org.ofbiz.base.util.*;
024:
025: import org.ofbiz.minilang.*;
026: import org.ofbiz.minilang.method.*;
027:
028: /**
029: * Calls a Java class method using the given fields as parameters
030: */
031: public class CallClassMethod extends MethodOperation {
032:
033: public static final String module = CallClassMethod.class.getName();
034:
035: String className;
036: String methodName;
037: ContextAccessor retFieldAcsr;
038: ContextAccessor retMapAcsr;
039:
040: /** A list of MethodObject objects to use as the method call parameters */
041: List parameters;
042:
043: public CallClassMethod(Element element, SimpleMethod simpleMethod) {
044: super (element, simpleMethod);
045: className = element.getAttribute("class-name");
046: methodName = element.getAttribute("method-name");
047: retFieldAcsr = new ContextAccessor(element
048: .getAttribute("ret-field-name"));
049: retMapAcsr = new ContextAccessor(element
050: .getAttribute("ret-map-name"));
051:
052: List parameterElements = UtilXml.childElementList(element);
053: if (parameterElements.size() > 0) {
054: parameters = new ArrayList(parameterElements.size());
055:
056: Iterator parameterIter = parameterElements.iterator();
057: while (parameterIter.hasNext()) {
058: Element parameterElement = (Element) parameterIter
059: .next();
060: MethodObject methodObject = null;
061: if ("string".equals(parameterElement.getNodeName())) {
062: methodObject = new StringObject(parameterElement,
063: simpleMethod);
064: } else if ("field".equals(parameterElement
065: .getNodeName())) {
066: methodObject = new FieldObject(parameterElement,
067: simpleMethod);
068: } else {
069: //whoops, invalid tag here, print warning
070: Debug.logWarning(
071: "Found an unsupported tag under the call-object-method tag: "
072: + parameterElement.getNodeName()
073: + "; ignoring", module);
074: }
075: if (methodObject != null) {
076: parameters.add(methodObject);
077: }
078: }
079: }
080: }
081:
082: public boolean exec(MethodContext methodContext) {
083: String className = methodContext.expandString(this .className);
084: String methodName = methodContext.expandString(this .methodName);
085:
086: Class methodClass = null;
087: try {
088: methodClass = ObjectType.loadClass(className, methodContext
089: .getLoader());
090: } catch (ClassNotFoundException e) {
091: Debug
092: .logError(e,
093: "Class to create not found with name "
094: + className
095: + " in create-object operation",
096: module);
097:
098: String errMsg = "ERROR: Could not complete the "
099: + simpleMethod.getShortDescription()
100: + " process [Class to create not found with name "
101: + className + ": " + e.toString() + "]";
102: methodContext.setErrorReturn(errMsg, simpleMethod);
103: return false;
104: }
105:
106: return CallObjectMethod.callMethod(simpleMethod, methodContext,
107: parameters, methodClass, null, methodName,
108: retFieldAcsr, retMapAcsr);
109: }
110:
111: public String rawString() {
112: // TODO: something more than the empty tag
113: return "<call-class-method/>";
114: }
115:
116: public String expandedString(MethodContext methodContext) {
117: // TODO: something more than a stub/dummy
118: return this.rawString();
119: }
120: }
|