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