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