1 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.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 34 public class InjectMessagesWorker implements EnhancementWorker 35 { 36 final String 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 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 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 |