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