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.ApplicationRuntimeException; |
20 |
| import org.apache.hivemind.ErrorLog; |
21 |
| import org.apache.hivemind.Location; |
22 |
| import org.apache.hivemind.util.Defense; |
23 |
| import org.apache.tapestry.IAsset; |
24 |
| import org.apache.tapestry.IComponent; |
25 |
| import org.apache.tapestry.spec.IAssetSpecification; |
26 |
| import org.apache.tapestry.spec.IComponentSpecification; |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| public class InjectAssetWorker implements EnhancementWorker |
35 |
| { |
36 |
| private ErrorLog _errorLog; |
37 |
| |
38 |
421
| public void performEnhancement(EnhancementOperation op, IComponentSpecification spec)
|
39 |
| { |
40 |
421
| Iterator i = spec.getAssetNames().iterator();
|
41 |
421
| while (i.hasNext())
|
42 |
| { |
43 |
39
| String name = (String) i.next();
|
44 |
| |
45 |
39
| IAssetSpecification as = spec.getAsset(name);
|
46 |
| |
47 |
39
| String propertyName = as.getPropertyName();
|
48 |
| |
49 |
39
| if (propertyName != null)
|
50 |
| { |
51 |
2
| try
|
52 |
| { |
53 |
2
| injectAsset(op, name, propertyName, as.getLocation());
|
54 |
| } |
55 |
| catch (Exception ex) |
56 |
| { |
57 |
1
| _errorLog.error(EnhanceMessages.errorAddingProperty(propertyName, op
|
58 |
| .getBaseClass(), ex), as.getLocation(), ex); |
59 |
| } |
60 |
| } |
61 |
| } |
62 |
| } |
63 |
| |
64 |
3
| public void injectAsset(EnhancementOperation op, String assetName, String propertyName, Location location)
|
65 |
| { |
66 |
3
| Defense.notNull(op, "op");
|
67 |
3
| Defense.notNull(assetName, "assetName");
|
68 |
3
| Defense.notNull(propertyName, "propertyName");
|
69 |
| |
70 |
3
| Class propertyType = EnhanceUtils.extractPropertyType(op, propertyName, null);
|
71 |
| |
72 |
3
| op.claimReadonlyProperty(propertyName);
|
73 |
| |
74 |
2
| if (!propertyType.isAssignableFrom(IAsset.class))
|
75 |
1
| throw new ApplicationRuntimeException(EnhanceMessages.incompatiblePropertyType(
|
76 |
| propertyName, |
77 |
| propertyType, |
78 |
| IAsset.class)); |
79 |
| |
80 |
1
| String fieldName = "_$" + propertyName;
|
81 |
| |
82 |
1
| op.addField(fieldName, propertyType);
|
83 |
| |
84 |
1
| EnhanceUtils.createSimpleAccessor(op, fieldName, propertyName, propertyType, location);
|
85 |
| |
86 |
| |
87 |
| |
88 |
1
| String code = fieldName + " = getAsset(\"" + assetName + "\");";
|
89 |
| |
90 |
1
| op.extendMethodImplementation(IComponent.class, EnhanceUtils.FINISH_LOAD_SIGNATURE, code);
|
91 |
| } |
92 |
| |
93 |
42
| public void setErrorLog(ErrorLog errorLog)
|
94 |
| { |
95 |
42
| _errorLog = errorLog;
|
96 |
| } |
97 |
| } |