KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jicengine.element.impl;
2
3 import org.jicengine.element.VariableElement;
4 import org.jicengine.operation.Context;
5 import org.jicengine.operation.DuplicateNameException;
6 import org.jicengine.operation.Factory;
7 import org.jicengine.operation.LocalContext;
8 import org.jicengine.operation.Operation;
9 import org.jicengine.operation.OperationException;
10
11 /**
12  * <p>
13  * Registers a Factory object i.e. stores it in the correct context with the
14  * prefix 'jic::'.
15  * </p>
16  *
17  * @author timo laitinen
18  */

19 public class RegisterFactoryOperation implements Operation {
20     private String JavaDoc factoryName;
21     private String JavaDoc[] parameterNames;
22     private Class JavaDoc[] parameterTypes;
23     private VariableElement factoryElement;
24
25   private String JavaDoc contextVariableName;
26   
27     public RegisterFactoryOperation(String JavaDoc factoryName, String JavaDoc[] parameterNames, Class JavaDoc[] parameterTypes, String JavaDoc contextVariableName)
28     {
29         this.factoryName = factoryName;
30         this.parameterNames = parameterNames;
31         this.parameterTypes = parameterTypes;
32     this.contextVariableName = contextVariableName;
33     }
34   
35     public void setFactoryElement(VariableElement factoryElement)
36     {
37         this.factoryElement = factoryElement;
38     }
39
40     public VariableElement getFactoryElement()
41     {
42         return this.factoryElement;
43     }
44
45     public boolean needsParameters()
46     {
47         return false;
48     }
49     public boolean needsParameter(String JavaDoc name)
50     {
51         return false;
52     }
53
54     public Object JavaDoc execute(Context context) throws OperationException
55     {
56         // we got the action context of the current element as a parameter
57

58     // we want the element context of the parent element.
59
// (why?)
60
Context elementContext = ((LocalContext)context).getParent();
61
62         // the factory-element is executed in a replica of the element context
63
// (because the original element context may get modified before the
64
// factory is used)
65
Context executionContext = elementContext.replicate();
66
67         // the factory is stored into the element context of the parent
68
Context parentElementContext = ((LocalContext)elementContext).getParent();
69
70
71         Factory factory = new ElementExecutionFactory(this.factoryName, this.parameterNames, this.parameterTypes, getFactoryElement(), executionContext);
72
73         // store the factory to the root-context:
74
try {
75       parentElementContext.addObject(this.contextVariableName, factory);
76     } catch (DuplicateNameException e){
77       throw new OperationException("Duplicate factory definition: '" + this.factoryName + "' ('" + this.contextVariableName + "')");
78     }
79
80         return null;
81     }
82 }
Popular Tags