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 | |
|
19 | |
import org.apache.hivemind.ErrorLog; |
20 | |
import org.apache.hivemind.Location; |
21 | |
import org.apache.hivemind.Messages; |
22 | |
import org.apache.hivemind.service.BodyBuilder; |
23 | |
import org.apache.hivemind.service.MethodSignature; |
24 | |
import org.apache.hivemind.util.Defense; |
25 | |
import org.apache.tapestry.services.ComponentMessagesSource; |
26 | |
import org.apache.tapestry.spec.IComponentSpecification; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | 0 | public class InjectMessagesWorker implements EnhancementWorker |
36 | |
{ |
37 | 0 | final String _messagesProperty = "messages"; |
38 | |
|
39 | 0 | final MethodSignature _methodSignature = new MethodSignature(Messages.class, "getMessages", |
40 | |
null, null); |
41 | |
|
42 | |
private ErrorLog _errorLog; |
43 | |
|
44 | |
private ComponentMessagesSource _componentMessagesSource; |
45 | |
|
46 | |
public void performEnhancement(EnhancementOperation op, IComponentSpecification spec) |
47 | |
{ |
48 | 0 | Location location = spec.getLocation(); |
49 | |
|
50 | |
try |
51 | |
{ |
52 | 0 | injectMessages(op, location); |
53 | |
} |
54 | 0 | catch (Exception ex) |
55 | |
{ |
56 | 0 | _errorLog.error(EnhanceMessages.errorAddingProperty(_messagesProperty, op |
57 | |
.getBaseClass(), ex), location, ex); |
58 | 0 | } |
59 | 0 | } |
60 | |
|
61 | |
public void injectMessages(EnhancementOperation op, Location location) |
62 | |
{ |
63 | 0 | Defense.notNull(op, "op"); |
64 | |
|
65 | 0 | op.claimReadonlyProperty(_messagesProperty); |
66 | |
|
67 | 0 | String sourceField = op.addInjectedField( |
68 | |
"_$componentMessagesSource", |
69 | |
ComponentMessagesSource.class, |
70 | |
_componentMessagesSource); |
71 | |
|
72 | 0 | op.addField("_$messages", Messages.class); |
73 | |
|
74 | 0 | BodyBuilder builder = new BodyBuilder(); |
75 | 0 | builder.begin(); |
76 | 0 | builder.addln("if (_$messages == null)"); |
77 | 0 | builder.addln(" _$messages = {0}.getMessages(this);", sourceField); |
78 | 0 | builder.addln("return _$messages;"); |
79 | 0 | builder.end(); |
80 | |
|
81 | 0 | op.addMethod(Modifier.PUBLIC, _methodSignature, builder.toString(), location); |
82 | 0 | } |
83 | |
|
84 | |
public void setComponentMessagesSource(ComponentMessagesSource componentMessagesSource) |
85 | |
{ |
86 | 0 | _componentMessagesSource = componentMessagesSource; |
87 | 0 | } |
88 | |
|
89 | |
public void setErrorLog(ErrorLog errorLog) |
90 | |
{ |
91 | 0 | _errorLog = errorLog; |
92 | 0 | } |
93 | |
} |