1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.enhance; |
16 |
| |
17 |
| import java.lang.reflect.Modifier; |
18 |
| |
19 |
| import org.apache.hivemind.Location; |
20 |
| import org.apache.hivemind.service.BodyBuilder; |
21 |
| import org.apache.hivemind.service.ClassFabUtils; |
22 |
| import org.apache.hivemind.service.MethodSignature; |
23 |
| import org.apache.hivemind.util.Defense; |
24 |
| import org.apache.tapestry.engine.state.ApplicationStateManager; |
25 |
| import org.apache.tapestry.event.PageDetachListener; |
26 |
| import org.apache.tapestry.spec.InjectSpecification; |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| public class InjectStateWorker implements InjectEnhancementWorker |
38 |
| { |
39 |
| private ApplicationStateManager _applicationStateManager; |
40 |
| |
41 |
2
| public void performEnhancement(EnhancementOperation op, InjectSpecification spec)
|
42 |
| { |
43 |
2
| injectState(op, spec.getObject(), spec.getProperty(), spec.getLocation());
|
44 |
| } |
45 |
| |
46 |
2
| void injectState(EnhancementOperation op, String objectName, String propertyName,
|
47 |
| Location location) |
48 |
| { |
49 |
2
| Defense.notNull(op, "op");
|
50 |
2
| Defense.notNull(objectName, "objectName");
|
51 |
2
| Defense.notNull(propertyName, "propertyName");
|
52 |
| |
53 |
2
| Class propertyType = EnhanceUtils.extractPropertyType(op, propertyName, null);
|
54 |
2
| String fieldName = "_$" + propertyName;
|
55 |
| |
56 |
| |
57 |
| |
58 |
2
| op.claimProperty(propertyName);
|
59 |
| |
60 |
2
| op.addField(fieldName, propertyType);
|
61 |
| |
62 |
2
| String managerField = op.addInjectedField(
|
63 |
| "_$applicationStateManager", |
64 |
| ApplicationStateManager.class, |
65 |
| _applicationStateManager); |
66 |
| |
67 |
2
| BodyBuilder builder = new BodyBuilder();
|
68 |
| |
69 |
| |
70 |
| |
71 |
2
| builder.begin();
|
72 |
2
| builder.addln("if ({0} == null)", fieldName);
|
73 |
2
| builder.addln(" {0} = ({1}) {2}.get(\"{3}\");", new Object[]
|
74 |
| { fieldName, ClassFabUtils.getJavaClassName(propertyType), managerField, objectName }); |
75 |
2
| builder.addln("return {0};", fieldName);
|
76 |
2
| builder.end();
|
77 |
| |
78 |
2
| String methodName = op.getAccessorMethodName(propertyName);
|
79 |
| |
80 |
2
| MethodSignature sig = new MethodSignature(propertyType, methodName, null, null);
|
81 |
| |
82 |
2
| op.addMethod(Modifier.PUBLIC, sig, builder.toString(), location);
|
83 |
| |
84 |
| |
85 |
| |
86 |
2
| builder.clear();
|
87 |
2
| builder.begin();
|
88 |
2
| builder.addln("{0}.store(\"{1}\", $1);", managerField, objectName);
|
89 |
2
| builder.addln("{0} = $1;", fieldName);
|
90 |
2
| builder.end();
|
91 |
| |
92 |
2
| sig = new MethodSignature(void.class, EnhanceUtils.createMutatorMethodName(propertyName),
|
93 |
| new Class[] |
94 |
| { propertyType }, null); |
95 |
| |
96 |
2
| op.addMethod(Modifier.PUBLIC, sig, builder.toString(), location);
|
97 |
| |
98 |
| |
99 |
| |
100 |
2
| op.extendMethodImplementation(
|
101 |
| PageDetachListener.class, |
102 |
| EnhanceUtils.PAGE_DETACHED_SIGNATURE, |
103 |
| fieldName + " = null;"); |
104 |
| } |
105 |
| |
106 |
2
| public void setApplicationStateManager(ApplicationStateManager applicationStateManager)
|
107 |
| { |
108 |
2
| _applicationStateManager = applicationStateManager;
|
109 |
| } |
110 |
| } |