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.ErrorLog;
021import org.apache.hivemind.Location;
022import org.apache.hivemind.service.ClassFabUtils;
023import org.apache.hivemind.service.MethodSignature;
024import org.apache.hivemind.util.Defense;
025import org.apache.tapestry.spec.IBeanSpecification;
026import org.apache.tapestry.spec.IComponentSpecification;
027
028/**
029 * Injects a property that will dynamically access a managed bean.
030 * 
031 * @author Howard M. Lewis Ship
032 * @since 4.0
033 */
034public class InjectBeanWorker implements EnhancementWorker
035{
036    private ErrorLog _errorLog;
037
038    public void performEnhancement(EnhancementOperation op, IComponentSpecification spec)
039    {
040        Iterator i = spec.getBeanNames().iterator();
041
042        while (i.hasNext())
043        {
044            String name = (String) i.next();
045
046            IBeanSpecification bs = spec.getBeanSpecification(name);
047
048            String propertyName = bs.getPropertyName();
049            if (propertyName != null)
050            {
051                try
052                {
053                    injectBean(op, name, propertyName, bs.getLocation());
054                }
055                catch (Exception ex)
056                {
057                    _errorLog.error(EnhanceMessages.errorAddingProperty(propertyName, op
058                            .getBaseClass(), ex), bs.getLocation(), ex);
059                }
060            }
061        }
062    }
063
064    public void injectBean(EnhancementOperation op, String beanName, String propertyName,
065            Location location)
066    {
067        Defense.notNull(op, "op");
068        Defense.notNull(beanName, "beanName");
069        Defense.notNull(propertyName, "propertyName");
070
071        op.claimReadonlyProperty(propertyName);
072
073        Class propertyType = EnhanceUtils.extractPropertyType(op, propertyName, null);
074
075        String methodName = op.getAccessorMethodName(propertyName);
076
077        MethodSignature sig = new MethodSignature(propertyType, methodName, null, null);
078
079        // i.e.
080        // return (foo.bar.Baz) getBeans().getBean("baz");
081
082        op.addMethod(Modifier.PUBLIC, sig, "return ("
083                + ClassFabUtils.getJavaClassName(propertyType) + ") getBeans().getBean(\""
084                + beanName + "\");", location);
085    }
086
087    public void setErrorLog(ErrorLog errorLog)
088    {
089        _errorLog = errorLog;
090    }
091}