1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.enhance; |
16 |
| |
17 |
| import java.util.Iterator; |
18 |
| |
19 |
| import org.apache.hivemind.ErrorLog; |
20 |
| import org.apache.hivemind.Location; |
21 |
| import org.apache.hivemind.service.ClassFabUtils; |
22 |
| import org.apache.hivemind.util.Defense; |
23 |
| import org.apache.tapestry.IComponent; |
24 |
| import org.apache.tapestry.spec.IComponentSpecification; |
25 |
| import org.apache.tapestry.spec.IContainedComponent; |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| public class InjectComponentWorker implements EnhancementWorker |
35 |
| { |
36 |
| private ErrorLog _errorLog; |
37 |
| |
38 |
421
| public void performEnhancement(EnhancementOperation op, IComponentSpecification spec)
|
39 |
| { |
40 |
421
| Iterator i = spec.getComponentIds().iterator();
|
41 |
| |
42 |
421
| while (i.hasNext())
|
43 |
| { |
44 |
259
| String id = (String) i.next();
|
45 |
| |
46 |
259
| IContainedComponent cc = spec.getComponent(id);
|
47 |
| |
48 |
259
| String propertyName = cc.getPropertyName();
|
49 |
| |
50 |
259
| if (propertyName != null)
|
51 |
| { |
52 |
2
| try
|
53 |
| { |
54 |
2
| injectComponent(op, id, propertyName, cc.getLocation());
|
55 |
| } |
56 |
| catch (Exception ex) |
57 |
| { |
58 |
1
| _errorLog.error(EnhanceMessages.errorAddingProperty(propertyName, op
|
59 |
| .getBaseClass(), ex), cc.getLocation(), ex); |
60 |
| } |
61 |
| } |
62 |
| } |
63 |
| } |
64 |
| |
65 |
2
| public void injectComponent(EnhancementOperation op, String componentId, String propertyName, Location location)
|
66 |
| { |
67 |
2
| Defense.notNull(op, "op");
|
68 |
2
| Defense.notNull(componentId, "componentId");
|
69 |
2
| Defense.notNull(propertyName, "propertyName");
|
70 |
| |
71 |
2
| Class propertyType = EnhanceUtils.extractPropertyType(op, propertyName, null);
|
72 |
| |
73 |
2
| op.claimReadonlyProperty(propertyName);
|
74 |
| |
75 |
1
| String fieldName = "_$" + propertyName;
|
76 |
| |
77 |
1
| op.addField(fieldName, propertyType);
|
78 |
| |
79 |
1
| EnhanceUtils.createSimpleAccessor(op, fieldName, propertyName, propertyType, location);
|
80 |
| |
81 |
| |
82 |
| |
83 |
1
| String code = fieldName + " = (" + ClassFabUtils.getJavaClassName(propertyType)
|
84 |
| + ") getComponent(\"" + componentId + "\");"; |
85 |
| |
86 |
1
| op.extendMethodImplementation(IComponent.class, EnhanceUtils.FINISH_LOAD_SIGNATURE, code);
|
87 |
| } |
88 |
| |
89 |
42
| public void setErrorLog(ErrorLog errorLog)
|
90 |
| { |
91 |
42
| _errorLog = errorLog;
|
92 |
| } |
93 |
| } |