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