001    // Copyright 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.enhance;
016    
017    import org.apache.hivemind.Location;
018    import org.apache.hivemind.Resource;
019    import org.apache.hivemind.service.MethodSignature;
020    import org.apache.hivemind.util.Defense;
021    import org.apache.tapestry.IAsset;
022    import org.apache.tapestry.IScript;
023    import org.apache.tapestry.asset.AssetSource;
024    import org.apache.tapestry.engine.IScriptSource;
025    import org.apache.tapestry.spec.InjectSpecification;
026    
027    import java.lang.reflect.Modifier;
028    
029    /**
030     * Injects {@link org.apache.tapestry.IScript} instances directly into pages or components.
031     *
032     * @author Howard M. Lewis Ship
033     * @since 4.0
034     */
035    public class InjectScriptWorker implements InjectEnhancementWorker
036    {
037        private IScriptSource _source;
038    
039        private AssetSource _assetSource;
040    
041        public void performEnhancement(EnhancementOperation op, InjectSpecification spec)
042        {
043            String propertyName = spec.getProperty();
044            String scriptName = spec.getObject();
045            Location location = spec.getLocation();
046    
047            injectScript(op, propertyName, scriptName, location);
048        }
049    
050        /**
051         * Injects a script reference.
052         *
053         * @param op
054         *            the enhancement operation
055         * @param propertyName
056         *            the name of the property to inject
057         * @param scriptName
058         *            the name of the script (relative to the location)
059         * @param location
060         *            the location of the specification; primarily used as the base location for finding
061         *            the script.
062         */
063    
064        public void injectScript(EnhancementOperation op, String propertyName,
065                                 String scriptName, Location location)
066        {
067            Defense.notNull(op, "op");
068            Defense.notNull(propertyName, "propertyName");
069            Defense.notNull(scriptName, "scriptName");
070            Defense.notNull(location, "location");
071    
072            op.claimReadonlyProperty(propertyName);
073    
074            Class propertyType = EnhanceUtils.verifyPropertyType(op, propertyName, IScript.class);
075    
076            // PropertyType will likely be either java.lang.Object or IScript
077    
078            String methodName = op.getAccessorMethodName(propertyName);
079    
080            Resource resource = location.getResource().getRelativeResource(scriptName);
081    
082            // if can't find resource
083    
084            if (resource.getResourceURL() == null)
085            {
086                IAsset scriptAsset = _assetSource.findAsset(location.getResource(), op.getSpecification(),
087                                                            scriptName, null, location);
088    
089                if (scriptAsset != null)
090                {
091                    resource = scriptAsset.getResourceLocation();
092                }
093            }
094    
095            DeferredScript script = new DeferredScriptImpl(resource, _source, location);
096    
097            String fieldName = op.addInjectedField("_$script", DeferredScript.class, script);
098    
099            MethodSignature sig = new MethodSignature(propertyType, methodName, null, null);
100    
101            op.addMethod(Modifier.PUBLIC, sig, "return " + fieldName + ".getScript();", location);
102        }
103    
104        public void setSource(IScriptSource source)
105        {
106            _source = source;
107        }
108    
109        public void setAssetSource(AssetSource assetSource)
110        {
111            _assetSource = assetSource;
112        }
113    }