01: package org.jicengine.element.impl;
02:
03: import org.jicengine.element.VariableElement;
04: import org.jicengine.operation.Context;
05: import org.jicengine.operation.DuplicateNameException;
06: import org.jicengine.operation.Factory;
07: import org.jicengine.operation.LocalContext;
08: import org.jicengine.operation.Operation;
09: 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 factoryName;
21: private String[] parameterNames;
22: private Class[] parameterTypes;
23: private VariableElement factoryElement;
24:
25: private String contextVariableName;
26:
27: public RegisterFactoryOperation(String factoryName,
28: String[] parameterNames, Class[] parameterTypes,
29: String contextVariableName) {
30: this .factoryName = factoryName;
31: this .parameterNames = parameterNames;
32: this .parameterTypes = parameterTypes;
33: this .contextVariableName = contextVariableName;
34: }
35:
36: public void setFactoryElement(VariableElement factoryElement) {
37: this .factoryElement = factoryElement;
38: }
39:
40: public VariableElement getFactoryElement() {
41: return this .factoryElement;
42: }
43:
44: public boolean needsParameters() {
45: return false;
46: }
47:
48: public boolean needsParameter(String name) {
49: return false;
50: }
51:
52: public Object execute(Context context) throws OperationException {
53: // we got the action context of the current element as a parameter
54:
55: // we want the element context of the parent element.
56: // (why?)
57: Context elementContext = ((LocalContext) context).getParent();
58:
59: // the factory-element is executed in a replica of the element context
60: // (because the original element context may get modified before the
61: // factory is used)
62: Context executionContext = elementContext.replicate();
63:
64: // the factory is stored into the element context of the parent
65: Context parentElementContext = ((LocalContext) elementContext)
66: .getParent();
67:
68: Factory factory = new ElementExecutionFactory(this .factoryName,
69: this .parameterNames, this .parameterTypes,
70: getFactoryElement(), executionContext);
71:
72: // store the factory to the root-context:
73: try {
74: parentElementContext.addObject(this .contextVariableName,
75: factory);
76: } catch (DuplicateNameException e) {
77: throw new OperationException(
78: "Duplicate factory definition: '"
79: + this .factoryName + "' ('"
80: + this .contextVariableName + "')");
81: }
82:
83: return null;
84: }
85: }
|