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