1 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 42 public class CreateObject extends MethodOperation { 43 44 public static final String module = CreateObject.class.getName(); 45 46 String className; 47 ContextAccessor fieldAcsr; 48 ContextAccessor mapAcsr; 49 50 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 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 className = methodContext.expandString(this.className); 84 85 Class methodClass = null; 86 try { 87 methodClass = ObjectType.loadClass(className, methodContext.getLoader()); 88 } catch (ClassNotFoundException e) { 89 Debug.logError(e, "Class to create not found with name " + className + " in create-object operation", module); 90 91 String 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 [] args = null; 97 Class [] parameterTypes = null; 98 if (parameters != null) { 99 args = new Object [parameters.size()]; 100 parameterTypes = new Class [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 typeClass = methodObjectDef.getTypeClass(methodContext.getLoader()); 109 if (typeClass == null) { 110 String 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 newObject = constructor.newInstance(args); 125 126 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 fieldAcsr.put(methodContext, newObject); 139 } 140 } 141 } catch (InstantiationException e) { 142 Debug.logError(e, "Could not instantiate object in create-object operation", module); 143 144 String 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 e) { 148 Debug.logError(e, "Illegal access constructing object in create-object operation", module); 149 150 String 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 e) { 154 Debug.logError(e, "Illegal argument calling method in create-object operation", module); 155 156 String 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 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 e) { 167 Debug.logError(e, "Could not find constructor to execute in simple-method create-object operation", module); 168 169 String 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 e) { 173 Debug.logError(e, "Security exception finding constructor to execute in simple-method create-object operation", module); 174 175 String 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 rawString() { 184 return "<create-object/>"; 186 } 187 public String expandedString(MethodContext methodContext) { 188 return this.rawString(); 190 } 191 } 192 | Popular Tags |