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