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.lang.reflect.*;
021: import java.util.*;
022:
023: import org.w3c.dom.*;
024: import org.ofbiz.base.util.*;
025:
026: import org.ofbiz.minilang.*;
027: import org.ofbiz.minilang.method.*;
028:
029: /**
030: * Creates a Java object using the given fields as parameters
031: */
032: public class CreateObject extends MethodOperation {
033:
034: public static final String module = CreateObject.class.getName();
035:
036: String className;
037: ContextAccessor fieldAcsr;
038: ContextAccessor mapAcsr;
039:
040: /** A list of MethodObject objects to use as the method call parameters */
041: List parameters;
042:
043: public CreateObject(Element element, SimpleMethod simpleMethod) {
044: super (element, simpleMethod);
045: className = element.getAttribute("class-name");
046: fieldAcsr = new ContextAccessor(element
047: .getAttribute("field-name"));
048: mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
049:
050: List parameterElements = UtilXml.childElementList(element);
051: if (parameterElements.size() > 0) {
052: parameters = new ArrayList(parameterElements.size());
053:
054: Iterator parameterIter = parameterElements.iterator();
055: while (parameterIter.hasNext()) {
056: Element parameterElement = (Element) parameterIter
057: .next();
058: MethodObject methodObject = null;
059: if ("string".equals(parameterElement.getNodeName())) {
060: methodObject = new StringObject(parameterElement,
061: simpleMethod);
062: } else if ("field".equals(parameterElement
063: .getNodeName())) {
064: methodObject = new FieldObject(parameterElement,
065: simpleMethod);
066: } else {
067: //whoops, invalid tag here, print warning
068: Debug.logWarning(
069: "Found an unsupported tag under the call-object-method tag: "
070: + parameterElement.getNodeName()
071: + "; ignoring", module);
072: }
073: if (methodObject != null) {
074: parameters.add(methodObject);
075: }
076: }
077: }
078: }
079:
080: public boolean exec(MethodContext methodContext) {
081: String className = methodContext.expandString(this .className);
082:
083: Class methodClass = null;
084: try {
085: methodClass = ObjectType.loadClass(className, methodContext
086: .getLoader());
087: } catch (ClassNotFoundException e) {
088: Debug
089: .logError(e,
090: "Class to create not found with name "
091: + className
092: + " in create-object operation",
093: module);
094:
095: String errMsg = "ERROR: Could not complete the "
096: + simpleMethod.getShortDescription()
097: + " process [Class to create not found with name "
098: + className + ": " + e.toString() + "]";
099: methodContext.setErrorReturn(errMsg, simpleMethod);
100: return false;
101: }
102:
103: Object[] args = null;
104: Class[] parameterTypes = null;
105: if (parameters != null) {
106: args = new Object[parameters.size()];
107: parameterTypes = new Class[parameters.size()];
108:
109: Iterator parameterIter = parameters.iterator();
110: int i = 0;
111: while (parameterIter.hasNext()) {
112: MethodObject methodObjectDef = (MethodObject) parameterIter
113: .next();
114: args[i] = methodObjectDef.getObject(methodContext);
115:
116: Class typeClass = methodObjectDef
117: .getTypeClass(methodContext.getLoader());
118: if (typeClass == null) {
119: String errMsg = "ERROR: Could not complete the "
120: + simpleMethod.getShortDescription()
121: + " process [Parameter type not found with name "
122: + methodObjectDef.getTypeName() + "]";
123: Debug.logError(errMsg, module);
124: methodContext.setErrorReturn(errMsg, simpleMethod);
125: return false;
126: }
127:
128: parameterTypes[i] = typeClass;
129: i++;
130: }
131: }
132:
133: try {
134: Constructor constructor = methodClass
135: .getConstructor(parameterTypes);
136: try {
137: Object newObject = constructor.newInstance(args);
138:
139: //if fieldAcsr is empty, ignore return value
140: if (!fieldAcsr.isEmpty()) {
141: if (!mapAcsr.isEmpty()) {
142: Map retMap = (Map) mapAcsr.get(methodContext);
143:
144: if (retMap == null) {
145: retMap = new HashMap();
146: mapAcsr.put(methodContext, retMap);
147: }
148: fieldAcsr.put(retMap, newObject, methodContext);
149: } else {
150: // no map name, use the env
151: fieldAcsr.put(methodContext, newObject);
152: }
153: }
154: } catch (InstantiationException e) {
155: Debug
156: .logError(
157: e,
158: "Could not instantiate object in create-object operation",
159: module);
160:
161: String errMsg = "ERROR: Could not complete the "
162: + simpleMethod.getShortDescription()
163: + " process [Could not instantiate object: "
164: + e.toString() + "]";
165: methodContext.setErrorReturn(errMsg, simpleMethod);
166: return false;
167: } catch (IllegalAccessException e) {
168: Debug
169: .logError(
170: e,
171: "Illegal access constructing object in create-object operation",
172: module);
173:
174: String errMsg = "ERROR: Could not complete the "
175: + simpleMethod.getShortDescription()
176: + " process [Illegal access constructing object: "
177: + e.toString() + "]";
178: methodContext.setErrorReturn(errMsg, simpleMethod);
179: return false;
180: } catch (IllegalArgumentException e) {
181: Debug
182: .logError(
183: e,
184: "Illegal argument calling method in create-object operation",
185: module);
186:
187: String errMsg = "ERROR: Could not complete the "
188: + simpleMethod.getShortDescription()
189: + " process [Illegal argument calling constructor: "
190: + e.toString() + "]";
191: methodContext.setErrorReturn(errMsg, simpleMethod);
192: return false;
193: } catch (InvocationTargetException e) {
194: Debug
195: .logError(
196: e.getTargetException(),
197: "Constructor in create-object operation threw an exception",
198: module);
199:
200: String errMsg = "ERROR: Could not complete the "
201: + simpleMethod.getShortDescription()
202: + " process [Constructor in create-object threw an exception: "
203: + e.getTargetException() + "]";
204: methodContext.setErrorReturn(errMsg, simpleMethod);
205: return false;
206: }
207: } catch (NoSuchMethodException e) {
208: Debug
209: .logError(
210: e,
211: "Could not find constructor to execute in simple-method create-object operation",
212: module);
213:
214: String errMsg = "ERROR: Could not complete the "
215: + simpleMethod.getShortDescription()
216: + " process [Could not find constructor to execute: "
217: + e.toString() + "]";
218: methodContext.setErrorReturn(errMsg, simpleMethod);
219: return false;
220: } catch (SecurityException e) {
221: Debug
222: .logError(
223: e,
224: "Security exception finding constructor to execute in simple-method create-object operation",
225: module);
226:
227: String errMsg = "ERROR: Could not complete the "
228: + simpleMethod.getShortDescription()
229: + " process [Security exception finding constructor to execute: "
230: + e.toString() + "]";
231: methodContext.setErrorReturn(errMsg, simpleMethod);
232: return false;
233: }
234:
235: return true;
236: }
237:
238: public String rawString() {
239: // TODO: something more than the empty tag
240: return "<create-object/>";
241: }
242:
243: public String expandedString(MethodContext methodContext) {
244: // TODO: something more than a stub/dummy
245: return this.rawString();
246: }
247: }
|