1 15 package org.apache.tapestry.enhance; 16 17 import java.util.Iterator ; 18 19 import org.apache.hivemind.ErrorLog; 20 import org.apache.hivemind.service.ClassFabUtils; 21 import org.apache.hivemind.util.Defense; 22 import org.apache.tapestry.IComponent; 23 import org.apache.tapestry.spec.IComponentSpecification; 24 import org.apache.tapestry.spec.IContainedComponent; 25 26 33 public class InjectComponentWorker implements EnhancementWorker 34 { 35 private ErrorLog _errorLog; 36 37 public void performEnhancement(EnhancementOperation op, IComponentSpecification spec) 38 { 39 Iterator i = spec.getComponentIds().iterator(); 40 41 while (i.hasNext()) 42 { 43 String id = (String ) i.next(); 44 45 IContainedComponent cc = spec.getComponent(id); 46 47 String propertyName = cc.getPropertyName(); 48 49 if (propertyName != null) 50 { 51 try 52 { 53 injectComponent(op, id, propertyName); 54 } 55 catch (Exception ex) 56 { 57 _errorLog.error(EnhanceMessages.errorAddingProperty(propertyName, op 58 .getBaseClass(), ex), cc.getLocation(), ex); 59 } 60 } 61 } 62 } 63 64 public void injectComponent(EnhancementOperation op, String componentId, String propertyName) 65 { 66 Defense.notNull(op, "op"); 67 Defense.notNull(componentId, "componentId"); 68 Defense.notNull(propertyName, "propertyName"); 69 70 Class propertyType = EnhanceUtils.extractPropertyType(op, propertyName, null); 71 72 op.claimProperty(propertyName); 73 74 String fieldName = "_$" + propertyName; 75 76 op.addField(fieldName, propertyType); 77 78 EnhanceUtils.createSimpleAccessor(op, fieldName, propertyName, propertyType); 79 80 82 String code = fieldName + " = (" + ClassFabUtils.getJavaClassName(propertyType) 83 + ") getComponent(\"" + componentId + "\");"; 84 85 op.extendMethodImplementation(IComponent.class, EnhanceUtils.FINISH_LOAD_SIGNATURE, code); 86 } 87 88 public void setErrorLog(ErrorLog errorLog) 89 { 90 _errorLog = errorLog; 91 } 92 } | Popular Tags |