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