1 15 package org.apache.tapestry.enhance; 16 17 import java.lang.reflect.Modifier ; 18 19 import org.apache.hivemind.Location; 20 import org.apache.hivemind.Resource; 21 import org.apache.hivemind.service.MethodSignature; 22 import org.apache.hivemind.util.Defense; 23 import org.apache.tapestry.IScript; 24 import org.apache.tapestry.engine.IScriptSource; 25 import org.apache.tapestry.spec.InjectSpecification; 26 27 33 public class InjectScriptWorker implements InjectEnhancementWorker 34 { 35 private IScriptSource _source; 36 37 public void performEnhancement(EnhancementOperation op, InjectSpecification spec) 38 { 39 String propertyName = spec.getProperty(); 40 String scriptName = spec.getObject(); 41 Location location = spec.getLocation(); 42 43 injectScript(op, propertyName, scriptName, location); 44 } 45 46 59 60 public void injectScript(EnhancementOperation op, String propertyName, String scriptName, 61 Location location) 62 { 63 Defense.notNull(op, "op"); 64 Defense.notNull(propertyName, "propertyName"); 65 Defense.notNull(scriptName, "scriptName"); 66 Defense.notNull(location, "location"); 67 68 op.claimProperty(propertyName); 69 70 Class propertyType = EnhanceUtils.verifyPropertyType(op, propertyName, IScript.class); 71 72 74 String methodName = op.getAccessorMethodName(propertyName); 75 76 Resource resource = location.getResource().getRelativeResource(scriptName); 77 78 DeferredScript script = new DeferredScriptImpl(resource, _source, location); 79 80 String fieldName = op.addInjectedField("_$script", DeferredScript.class, script); 81 82 MethodSignature sig = new MethodSignature(propertyType, methodName, null, null); 83 84 op.addMethod(Modifier.PUBLIC, sig, "return " + fieldName + ".getScript();"); 85 } 86 87 public void setSource(IScriptSource source) 88 { 89 _source = source; 90 } 91 } 92 | Popular Tags |