|
|||||||||||||||||||
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
InjectBeanWorker.java | 100% | 100% | 100% | 100% |
|
1 | // Copyright 2005 The Apache Software Foundation | |
2 | // | |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | // you may not use this file except in compliance with the License. | |
5 | // You may obtain a copy of the License at | |
6 | // | |
7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
8 | // | |
9 | // Unless required by applicable law or agreed to in writing, software | |
10 | // distributed under the License is distributed on an "AS IS" BASIS, | |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | // See the License for the specific language governing permissions and | |
13 | // limitations under the License. | |
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.service.ClassFabUtils; | |
22 | import org.apache.hivemind.service.MethodSignature; | |
23 | import org.apache.hivemind.util.Defense; | |
24 | import org.apache.tapestry.spec.IBeanSpecification; | |
25 | import org.apache.tapestry.spec.IComponentSpecification; | |
26 | ||
27 | /** | |
28 | * Injects a property that will dynamically access a managed bean. | |
29 | * | |
30 | * @author Howard M. Lewis Ship | |
31 | * @since 4.0 | |
32 | */ | |
33 | public class InjectBeanWorker implements EnhancementWorker | |
34 | { | |
35 | private ErrorLog _errorLog; | |
36 | ||
37 | 421 | public void performEnhancement(EnhancementOperation op, IComponentSpecification spec) |
38 | { | |
39 | 421 | Iterator i = spec.getBeanNames().iterator(); |
40 | ||
41 | 421 | while (i.hasNext()) |
42 | { | |
43 | 30 | String name = (String) i.next(); |
44 | ||
45 | 30 | IBeanSpecification bs = spec.getBeanSpecification(name); |
46 | ||
47 | 30 | String propertyName = bs.getPropertyName(); |
48 | 30 | if (propertyName != null) |
49 | { | |
50 | 6 | try |
51 | { | |
52 | 6 | injectBean(op, name, propertyName); |
53 | } | |
54 | catch (Exception ex) | |
55 | { | |
56 | 1 | _errorLog.error(EnhanceMessages.errorAddingProperty(propertyName, op |
57 | .getBaseClass(), ex), bs.getLocation(), ex); | |
58 | } | |
59 | } | |
60 | } | |
61 | } | |
62 | ||
63 | 6 | public void injectBean(EnhancementOperation op, String beanName, String propertyName) |
64 | { | |
65 | 6 | Defense.notNull(op, "op"); |
66 | 6 | Defense.notNull(beanName, "beanName"); |
67 | 6 | Defense.notNull(propertyName, "propertyName"); |
68 | ||
69 | 6 | op.claimProperty(propertyName); |
70 | ||
71 | 5 | Class propertyType = EnhanceUtils.extractPropertyType(op, propertyName, null); |
72 | ||
73 | 5 | String methodName = op.getAccessorMethodName(propertyName); |
74 | ||
75 | 5 | MethodSignature sig = new MethodSignature(propertyType, methodName, null, null); |
76 | ||
77 | // i.e. | |
78 | // return (foo.bar.Baz) getBeans().getBean("baz"); | |
79 | ||
80 | 5 | op.addMethod(Modifier.PUBLIC, sig, "return (" |
81 | + ClassFabUtils.getJavaClassName(propertyType) + ") getBeans().getBean(\"" | |
82 | + beanName + "\");"); | |
83 | } | |
84 | ||
85 | 42 | public void setErrorLog(ErrorLog errorLog) |
86 | { | |
87 | 42 | _errorLog = errorLog; |
88 | } | |
89 | } |
|