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 java.util.Iterator;
018    
019    import org.apache.hivemind.ApplicationRuntimeException;
020    import org.apache.hivemind.ErrorLog;
021    import org.apache.hivemind.Location;
022    import org.apache.hivemind.util.Defense;
023    import org.apache.tapestry.IAsset;
024    import org.apache.tapestry.IComponent;
025    import org.apache.tapestry.spec.IAssetSpecification;
026    import org.apache.tapestry.spec.IComponentSpecification;
027    
028    /**
029     * Injects assets as component properties.
030     * 
031     * @author Howard M. Lewis Ship
032     * @since 4.0
033     */
034    public class InjectAssetWorker implements EnhancementWorker
035    {
036        private ErrorLog _errorLog;
037    
038        public void performEnhancement(EnhancementOperation op, IComponentSpecification spec)
039        {
040            Iterator i = spec.getAssetNames().iterator();
041            while (i.hasNext())
042            {
043                String name = (String) i.next();
044    
045                IAssetSpecification as = spec.getAsset(name);
046    
047                String propertyName = as.getPropertyName();
048    
049                if (propertyName != null)
050                {
051                    try
052                    {
053                        injectAsset(op, name, propertyName, as.getLocation());
054                    }
055                    catch (Exception ex)
056                    {
057                        _errorLog.error(EnhanceMessages.errorAddingProperty(propertyName, op
058                                .getBaseClass(), ex), as.getLocation(), ex);
059                    }
060                }
061            }
062        }
063    
064        public void injectAsset(EnhancementOperation op, String assetName, String propertyName, Location location)
065        {
066            Defense.notNull(op, "op");
067            Defense.notNull(assetName, "assetName");
068            Defense.notNull(propertyName, "propertyName");
069    
070            Class propertyType = EnhanceUtils.extractPropertyType(op, propertyName, null);
071    
072            op.claimReadonlyProperty(propertyName);
073    
074            if (!propertyType.isAssignableFrom(IAsset.class))
075                throw new ApplicationRuntimeException(EnhanceMessages.incompatiblePropertyType(
076                        propertyName,
077                        propertyType,
078                        IAsset.class));
079    
080            String fieldName = "_$" + propertyName;
081    
082            op.addField(fieldName, propertyType);
083    
084            EnhanceUtils.createSimpleAccessor(op, fieldName, propertyName, propertyType, location);
085    
086            // i.e. _$fred = getAsset("barney");
087    
088            String code = fieldName + " = getAsset(\"" + assetName + "\");";
089    
090            op.extendMethodImplementation(IComponent.class, EnhanceUtils.FINISH_LOAD_SIGNATURE, code);
091        }
092    
093        public void setErrorLog(ErrorLog errorLog)
094        {
095            _errorLog = errorLog;
096        }
097    }