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