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