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