KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jicengine > element > impl > ElementExecutionFactory


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 /**
9  * <p>
10  * A <code>Factory</code> that operates by executing an element in a stored
11  * context.
12  * </p>
13  *
14  *
15  * @author timo laitinen
16  */

17 public class ElementExecutionFactory implements Factory {
18
19     private String JavaDoc name;
20     private VariableElement result;
21     private Context elementContext;
22     private String JavaDoc[] parameterNames;
23     private Class JavaDoc[] parameterTypes;
24
25     public ElementExecutionFactory(String JavaDoc name, String JavaDoc[] parameterNames, Class JavaDoc[] 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 JavaDoc invoke(Object JavaDoc[] 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 JavaDoc expected = "";
45                 String JavaDoc 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 JavaDoc 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 JavaDoc toString()
69   {
70     return "factory '" + this.name + "'";
71   }
72 }
73
Popular Tags