1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
package org.apache.tapestry.enhance; |
15 |
|
|
16 |
|
import org.apache.commons.logging.Log; |
17 |
|
import org.apache.hivemind.Location; |
18 |
|
import org.apache.hivemind.internal.Module; |
19 |
|
import org.apache.tapestry.spec.IComponentSpecification; |
20 |
|
|
21 |
|
import java.util.Iterator; |
22 |
|
import java.util.List; |
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
public class AutowireWorker implements EnhancementWorker |
31 |
|
{ |
32 |
|
private final Log _log; |
33 |
|
|
34 |
|
private final Module _module; |
35 |
|
|
36 |
|
public AutowireWorker( Module module, Log log) |
37 |
4 |
{ |
38 |
4 |
_module = module; |
39 |
4 |
_log = log; |
40 |
4 |
} |
41 |
|
|
42 |
|
public void performEnhancement( EnhancementOperation op, IComponentSpecification spec ) |
43 |
|
{ |
44 |
4 |
final List propertyNames = op.findUnclaimedAbstractProperties(); |
45 |
|
|
46 |
4 |
for( Iterator i = propertyNames.iterator(); i.hasNext(); ) { |
47 |
|
|
48 |
4 |
String propertyName = ( String ) i.next(); |
49 |
|
|
50 |
4 |
Class propertyType = op.getPropertyType( propertyName ); |
51 |
4 |
if( propertyType == null ) |
52 |
0 |
propertyType = Object.class; |
53 |
|
|
54 |
4 |
if (!op.canClaimAsReadOnlyProperty(propertyName)) |
55 |
0 |
continue; |
56 |
|
|
57 |
4 |
if( _module.containsService( propertyType )) { |
58 |
|
|
59 |
2 |
final Object serviceProxy = _module.getService( propertyType ); |
60 |
2 |
final Location location = spec.getLocation(); |
61 |
|
|
62 |
2 |
_log.debug( EnhanceMessages.autowiring( propertyName, spec, serviceProxy ) ); |
63 |
|
|
64 |
2 |
final String fieldName = op.addInjectedField( "_$" + propertyName, propertyType, serviceProxy ); |
65 |
|
|
66 |
2 |
EnhanceUtils.createSimpleAccessor( op, fieldName, propertyName, propertyType, location ); |
67 |
2 |
op.claimReadonlyProperty( propertyName ); |
68 |
|
} |
69 |
|
|
70 |
4 |
} |
71 |
4 |
} |
72 |
|
} |