1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
package org.apache.tapestry.enhance; |
15 | |
|
16 | |
import org.apache.hivemind.ErrorLog; |
17 | |
import org.apache.hivemind.Location; |
18 | |
import org.apache.hivemind.service.MethodSignature; |
19 | |
import org.apache.tapestry.IComponent; |
20 | |
import org.apache.tapestry.event.PageDetachListener; |
21 | |
import org.apache.tapestry.spec.IComponentSpecification; |
22 | |
|
23 | |
import java.lang.reflect.Modifier; |
24 | |
import java.util.Iterator; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | 0 | public class ClientIdPropertyWorker implements EnhancementWorker |
31 | |
{ |
32 | |
private static final String PROPERTY_NAME = "clientId"; |
33 | |
|
34 | |
private ErrorLog _errorLog; |
35 | |
|
36 | |
public void performEnhancement(EnhancementOperation op, IComponentSpecification spec) |
37 | |
{ |
38 | 0 | Location location = spec.getLocation(); |
39 | |
|
40 | 0 | Iterator i = op.findUnclaimedAbstractProperties().iterator(); |
41 | |
|
42 | 0 | while (i.hasNext()) |
43 | |
{ |
44 | 0 | String name = (String) i.next(); |
45 | |
|
46 | 0 | if (name.equals(PROPERTY_NAME)) { |
47 | |
|
48 | |
try |
49 | |
{ |
50 | 0 | createProperty(op, name, location); |
51 | |
} |
52 | 0 | catch (Exception ex) |
53 | |
{ |
54 | 0 | _errorLog.error( |
55 | |
EnhanceMessages.errorAddingProperty(name, op.getBaseClass(), ex), |
56 | |
location, |
57 | |
ex); |
58 | 0 | } |
59 | |
|
60 | 0 | break; |
61 | |
} |
62 | 0 | } |
63 | 0 | } |
64 | |
|
65 | |
private void createProperty(EnhancementOperation op, String name, Location location) |
66 | |
{ |
67 | |
|
68 | |
|
69 | 0 | Class propertyType = op.getPropertyType(name); |
70 | |
|
71 | 0 | String fieldName = "_$" + name; |
72 | 0 | String defaultFieldName = fieldName + "$defaultValue"; |
73 | |
|
74 | 0 | op.addField(fieldName, propertyType); |
75 | 0 | op.addField(defaultFieldName, propertyType); |
76 | |
|
77 | 0 | String methodName = op.getAccessorMethodName(name); |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | 0 | StringBuffer str = new StringBuffer(); |
83 | |
|
84 | 0 | str.append(" if(").append(fieldName).append(" == null){") |
85 | |
.append(" if (getPage() == null) { return null; }") |
86 | |
.append(" String tempId = getSpecifiedId();") |
87 | |
.append(" if (tempId == null) { return null; }") |
88 | |
.append(" return getPage().getRequestCycle().peekUniqueId(org.apache.tapestry.TapestryUtils#convertTapestryIdToNMToken(tempId));") |
89 | |
.append("} else { "); |
90 | |
|
91 | |
|
92 | |
|
93 | 0 | str.append("return ").append(fieldName).append(";"); |
94 | 0 | str.append("}"); |
95 | |
|
96 | 0 | op.addMethod( |
97 | |
Modifier.PUBLIC, |
98 | |
new MethodSignature(propertyType, methodName, null, null), |
99 | |
str.toString(), |
100 | |
location); |
101 | |
|
102 | 0 | EnhanceUtils.createSimpleMutator(op, fieldName, name, propertyType, location); |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | 0 | op.extendMethodImplementation( |
108 | 0 | IComponent.class, |
109 | |
EnhanceUtils.FINISH_LOAD_SIGNATURE, |
110 | |
defaultFieldName + " = " + fieldName + ";"); |
111 | |
|
112 | |
|
113 | |
|
114 | 0 | op.extendMethodImplementation( |
115 | |
PageDetachListener.class, |
116 | |
EnhanceUtils.PAGE_DETACHED_SIGNATURE, |
117 | |
fieldName + " = " + defaultFieldName + ";"); |
118 | |
|
119 | |
|
120 | |
|
121 | 0 | op.claimProperty(name); |
122 | 0 | } |
123 | |
|
124 | |
public void setErrorLog(ErrorLog errorLog) |
125 | |
{ |
126 | 0 | _errorLog = errorLog; |
127 | 0 | } |
128 | |
} |