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