1 package org.jicengine.element.impl; 2 3 import org.jicengine.operation.*; 4 import org.jicengine.element.VariableElement; 5 import org.jicengine.element.ElementException; 6 7 8 17 public class ElementExecutionFactory implements Factory { 18 19 private String name; 20 private VariableElement result; 21 private Context elementContext; 22 private String [] parameterNames; 23 private Class [] parameterTypes; 24 25 public ElementExecutionFactory(String name, String [] parameterNames, Class [] parameterTypes, VariableElement result, Context elementContext) 26 { 27 this.name = name; 28 this.parameterNames = parameterNames; 29 this.parameterTypes = parameterTypes; 30 this.result = result; 31 this.elementContext = elementContext; 32 } 33 34 public Object invoke(Object [] arguments) throws OperationException 35 { 36 if( arguments.length != this.parameterNames.length ){ 37 throw new OperationException("Factory '" + this.name + "' requires " + this.parameterNames.length + " arguments, got " + arguments.length); 38 } 39 40 Context executionContext = new LocalContext(this.name, this.elementContext); 41 42 for (int i = 0; i < arguments.length; i++) { 43 if( !org.jicengine.operation.ReflectionUtils.isAssignableFrom(parameterTypes[i], arguments[i].getClass()) ){ 44 String expected = ""; 45 String received = ""; 46 for (int j = 0; j < this.parameterTypes.length; j++) { 47 expected += this.parameterTypes[j].getName(); 48 received += arguments[j].getClass().getName(); 49 if( j+1 < this.parameterTypes.length ){ 50 expected += ", "; 51 received += ", "; 52 } 53 } 54 throw new OperationException(this.name + " expected (" + expected + "), got (" + received + ")"); 55 } 56 executionContext.addObject(parameterNames[i],arguments[i]); 57 } 58 59 Object resultObject = null; 60 try { 61 resultObject = this.result.getValue(executionContext, null); 62 } catch (ElementException ex) { 63 throw new OperationException(ex.getMessage(),ex); 64 } 65 return resultObject; 66 } 67 68 public String toString() 69 { 70 return "factory '" + this.name + "'"; 71 } 72 } 73 | Popular Tags |