KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: CreateObject.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 package org.ofbiz.minilang.method.callops;
25
26 import java.lang.reflect.*;
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  * Creates a Java object 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 CreateObject extends MethodOperation {
43     
44     public static final String JavaDoc module = CreateObject.class.getName();
45
46     String JavaDoc className;
47     ContextAccessor fieldAcsr;
48     ContextAccessor mapAcsr;
49
50     /** A list of MethodObject objects to use as the method call parameters */
51     List parameters;
52
53     public CreateObject(Element element, SimpleMethod simpleMethod) {
54         super(element, simpleMethod);
55         className = element.getAttribute("class-name");
56         fieldAcsr = new ContextAccessor(element.getAttribute("field-name"));
57         mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
58         
59         List parameterElements = UtilXml.childElementList(element);
60         if (parameterElements.size() > 0) {
61             parameters = new ArrayList(parameterElements.size());
62             
63             Iterator parameterIter = parameterElements.iterator();
64             while (parameterIter.hasNext()) {
65                 Element parameterElement = (Element) parameterIter.next();
66                 MethodObject methodObject = null;
67                 if ("string".equals(parameterElement.getNodeName())) {
68                     methodObject = new StringObject(parameterElement, simpleMethod);
69                 } else if ("field".equals(parameterElement.getNodeName())) {
70                     methodObject = new FieldObject(parameterElement, simpleMethod);
71                 } else {
72                     //whoops, invalid tag here, print warning
73
Debug.logWarning("Found an unsupported tag under the call-object-method tag: " + parameterElement.getNodeName() + "; ignoring", module);
74                 }
75                 if (methodObject != null) {
76                     parameters.add(methodObject);
77                 }
78             }
79         }
80     }
81
82     public boolean exec(MethodContext methodContext) {
83         String JavaDoc className = methodContext.expandString(this.className);
84
85         Class JavaDoc methodClass = null;
86         try {
87             methodClass = ObjectType.loadClass(className, methodContext.getLoader());
88         } catch (ClassNotFoundException JavaDoc e) {
89             Debug.logError(e, "Class to create not found with name " + className + " in create-object operation", module);
90
91             String JavaDoc errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [Class to create not found with name " + className + ": " + e.toString() + "]";
92             methodContext.setErrorReturn(errMsg, simpleMethod);
93             return false;
94         }
95         
96         Object JavaDoc[] args = null;
97         Class JavaDoc[] parameterTypes = null;
98         if (parameters != null) {
99             args = new Object JavaDoc[parameters.size()];
100             parameterTypes = new Class JavaDoc[parameters.size()];
101
102             Iterator parameterIter = parameters.iterator();
103             int i = 0;
104             while (parameterIter.hasNext()) {
105                 MethodObject methodObjectDef = (MethodObject) parameterIter.next();
106                 args[i] = methodObjectDef.getObject(methodContext);
107
108                 Class JavaDoc typeClass = methodObjectDef.getTypeClass(methodContext.getLoader());
109                 if (typeClass == null) {
110                     String JavaDoc errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [Parameter type not found with name " + methodObjectDef.getTypeName() + "]";
111                     Debug.logError(errMsg, module);
112                     methodContext.setErrorReturn(errMsg, simpleMethod);
113                     return false;
114                 }
115
116                 parameterTypes[i] = typeClass;
117                 i++;
118             }
119         }
120         
121         try {
122             Constructor constructor = methodClass.getConstructor(parameterTypes);
123             try {
124                 Object JavaDoc newObject = constructor.newInstance(args);
125                 
126                 //if fieldAcsr is empty, ignore return value
127
if (!fieldAcsr.isEmpty()) {
128                     if (!mapAcsr.isEmpty()) {
129                         Map retMap = (Map) mapAcsr.get(methodContext);
130
131                         if (retMap == null) {
132                             retMap = new HashMap();
133                             mapAcsr.put(methodContext, retMap);
134                         }
135                         fieldAcsr.put(retMap, newObject, methodContext);
136                     } else {
137                         // no map name, use the env
138
fieldAcsr.put(methodContext, newObject);
139                     }
140                 }
141             } catch (InstantiationException JavaDoc e) {
142                 Debug.logError(e, "Could not instantiate object in create-object operation", module);
143
144                 String JavaDoc errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [Could not instantiate object: " + e.toString() + "]";
145                 methodContext.setErrorReturn(errMsg, simpleMethod);
146                 return false;
147             } catch (IllegalAccessException JavaDoc e) {
148                 Debug.logError(e, "Illegal access constructing object in create-object operation", module);
149
150                 String JavaDoc errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [Illegal access constructing object: " + e.toString() + "]";
151                 methodContext.setErrorReturn(errMsg, simpleMethod);
152                 return false;
153             } catch (IllegalArgumentException JavaDoc e) {
154                 Debug.logError(e, "Illegal argument calling method in create-object operation", module);
155
156                 String JavaDoc errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [Illegal argument calling constructor: " + e.toString() + "]";
157                 methodContext.setErrorReturn(errMsg, simpleMethod);
158                 return false;
159             } catch (InvocationTargetException e) {
160                 Debug.logError(e.getTargetException(), "Constructor in create-object operation threw an exception", module);
161
162                 String JavaDoc errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [Constructor in create-object threw an exception: " + e.getTargetException() + "]";
163                 methodContext.setErrorReturn(errMsg, simpleMethod);
164                 return false;
165             }
166         } catch (NoSuchMethodException JavaDoc e) {
167             Debug.logError(e, "Could not find constructor to execute in simple-method create-object operation", module);
168             
169             String JavaDoc errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [Could not find constructor to execute: " + e.toString() + "]";
170             methodContext.setErrorReturn(errMsg, simpleMethod);
171             return false;
172         } catch (SecurityException JavaDoc e) {
173             Debug.logError(e, "Security exception finding constructor to execute in simple-method create-object operation", module);
174             
175             String JavaDoc errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [Security exception finding constructor to execute: " + e.toString() + "]";
176             methodContext.setErrorReturn(errMsg, simpleMethod);
177             return false;
178         }
179         
180         return true;
181     }
182
183     public String JavaDoc rawString() {
184         // TODO: something more than the empty tag
185
return "<create-object/>";
186     }
187     public String JavaDoc expandedString(MethodContext methodContext) {
188         // TODO: something more than a stub/dummy
189
return this.rawString();
190     }
191 }
192
Popular Tags