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