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