1 15 package org.apache.tapestry.enhance; 16 17 import java.lang.reflect.Modifier ; 18 19 import org.apache.hivemind.service.BodyBuilder; 20 import org.apache.hivemind.service.ClassFabUtils; 21 import org.apache.hivemind.service.MethodSignature; 22 import org.apache.hivemind.util.Defense; 23 import org.apache.tapestry.engine.state.ApplicationStateManager; 24 import org.apache.tapestry.event.PageDetachListener; 25 import org.apache.tapestry.spec.InjectSpecification; 26 27 36 public class InjectStateWorker implements InjectEnhancementWorker 37 { 38 private ApplicationStateManager _applicationStateManager; 39 40 public void performEnhancement(EnhancementOperation op, InjectSpecification spec) 41 { 42 injectState(op, spec.getObject(), spec.getProperty()); 43 } 44 45 public void injectState(EnhancementOperation op, String objectName, String propertyName) 46 { 47 Defense.notNull(op, "op"); 48 Defense.notNull(objectName, "objectName"); 49 Defense.notNull(propertyName, "propertyName"); 50 51 Class propertyType = EnhanceUtils.extractPropertyType(op, propertyName, null); 52 String fieldName = "_$" + propertyName; 53 54 op.claimProperty(propertyName); 55 56 op.addField(fieldName, propertyType); 57 58 String managerField = op.addInjectedField( 59 "_$applicationStateManager", 60 ApplicationStateManager.class, 61 _applicationStateManager); 62 63 BodyBuilder builder = new BodyBuilder(); 64 65 67 builder.begin(); 68 builder.addln("if ({0} == null)", fieldName); 69 builder.addln(" {0} = ({1}) {2}.get(\"{3}\");", new Object [] 70 { fieldName, ClassFabUtils.getJavaClassName(propertyType), managerField, objectName }); 71 builder.addln("return {0};", fieldName); 72 builder.end(); 73 74 String methodName = op.getAccessorMethodName(propertyName); 75 76 MethodSignature sig = new MethodSignature(propertyType, methodName, null, null); 77 78 op.addMethod(Modifier.PUBLIC, sig, builder.toString()); 79 80 82 builder.clear(); 83 builder.begin(); 84 builder.addln("{0}.store(\"{1}\", $1);", managerField, objectName); 85 builder.addln("{0} = $1;", fieldName); 86 builder.end(); 87 88 sig = new MethodSignature(void.class, EnhanceUtils.createMutatorMethodName(propertyName), 89 new Class [] 90 { propertyType }, null); 91 92 op.addMethod(Modifier.PUBLIC, sig, builder.toString()); 93 94 96 op.extendMethodImplementation( 97 PageDetachListener.class, 98 EnhanceUtils.PAGE_DETACHED_SIGNATURE, 99 fieldName + " = null;"); 100 } 101 102 public void setApplicationStateManager(ApplicationStateManager applicationStateManager) 103 { 104 _applicationStateManager = applicationStateManager; 105 } 106 } | Popular Tags |