KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > enhance > InjectMessagesWorker


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.enhance;
16
17 import java.lang.reflect.Modifier JavaDoc;
18
19 import org.apache.hivemind.ErrorLog;
20 import org.apache.hivemind.Messages;
21 import org.apache.hivemind.service.BodyBuilder;
22 import org.apache.hivemind.service.MethodSignature;
23 import org.apache.hivemind.util.Defense;
24 import org.apache.tapestry.services.ComponentMessagesSource;
25 import org.apache.tapestry.spec.IComponentSpecification;
26
27 /**
28  * Injects the read-only {@link org.apache.tapestry.IComponent#getMessages() messages}property into
29  * all components.
30  *
31  * @author Howard M. Lewis Ship
32  * @since 4.0
33  */

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