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