1 15 package org.apache.tapestry.enhance; 16 17 import java.lang.reflect.Modifier ; 18 import java.util.Iterator ; 19 20 import org.apache.hivemind.ErrorLog; 21 import org.apache.hivemind.service.ClassFabUtils; 22 import org.apache.hivemind.service.MethodSignature; 23 import org.apache.hivemind.util.Defense; 24 import org.apache.tapestry.spec.IBeanSpecification; 25 import org.apache.tapestry.spec.IComponentSpecification; 26 27 33 public class InjectBeanWorker implements EnhancementWorker 34 { 35 private ErrorLog _errorLog; 36 37 public void performEnhancement(EnhancementOperation op, IComponentSpecification spec) 38 { 39 Iterator i = spec.getBeanNames().iterator(); 40 41 while (i.hasNext()) 42 { 43 String name = (String ) i.next(); 44 45 IBeanSpecification bs = spec.getBeanSpecification(name); 46 47 String propertyName = bs.getPropertyName(); 48 if (propertyName != null) 49 { 50 try 51 { 52 injectBean(op, name, propertyName); 53 } 54 catch (Exception ex) 55 { 56 _errorLog.error(EnhanceMessages.errorAddingProperty(propertyName, op 57 .getBaseClass(), ex), bs.getLocation(), ex); 58 } 59 } 60 } 61 } 62 63 public void injectBean(EnhancementOperation op, String beanName, String propertyName) 64 { 65 Defense.notNull(op, "op"); 66 Defense.notNull(beanName, "beanName"); 67 Defense.notNull(propertyName, "propertyName"); 68 69 op.claimProperty(propertyName); 70 71 Class propertyType = EnhanceUtils.extractPropertyType(op, propertyName, null); 72 73 String methodName = op.getAccessorMethodName(propertyName); 74 75 MethodSignature sig = new MethodSignature(propertyType, methodName, null, null); 76 77 80 op.addMethod(Modifier.PUBLIC, sig, "return (" 81 + ClassFabUtils.getJavaClassName(propertyType) + ") getBeans().getBean(\"" 82 + beanName + "\");"); 83 } 84 85 public void setErrorLog(ErrorLog errorLog) 86 { 87 _errorLog = errorLog; 88 } 89 } | Popular Tags |