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