Coverage Report - org.apache.tapestry.enhance.InjectBeanWorker
 
Classes in this File Line Coverage Branch Coverage Complexity
InjectBeanWorker
0%
0/24
0%
0/4
2
 
 1  
 // Copyright 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.enhance;
 16  
 
 17  
 import java.lang.reflect.Modifier;
 18  
 import java.util.Iterator;
 19  
 
 20  
 import org.apache.hivemind.ErrorLog;
 21  
 import org.apache.hivemind.Location;
 22  
 import org.apache.hivemind.service.ClassFabUtils;
 23  
 import org.apache.hivemind.service.MethodSignature;
 24  
 import org.apache.hivemind.util.Defense;
 25  
 import org.apache.tapestry.spec.IBeanSpecification;
 26  
 import org.apache.tapestry.spec.IComponentSpecification;
 27  
 
 28  
 /**
 29  
  * Injects a property that will dynamically access a managed bean.
 30  
  * 
 31  
  * @author Howard M. Lewis Ship
 32  
  * @since 4.0
 33  
  */
 34  0
 public class InjectBeanWorker implements EnhancementWorker
 35  
 {
 36  
 
 37  
     private ErrorLog _errorLog;
 38  
 
 39  
     public void performEnhancement(EnhancementOperation op,
 40  
             IComponentSpecification spec)
 41  
     {
 42  0
         Iterator i = spec.getBeanNames().iterator();
 43  
 
 44  0
         while(i.hasNext())
 45  
         {
 46  0
             String name = (String) i.next();
 47  
 
 48  0
             IBeanSpecification bs = spec.getBeanSpecification(name);
 49  
 
 50  0
             String propertyName = bs.getPropertyName();
 51  0
             if (propertyName != null)
 52  
             {
 53  
                 try
 54  
                 {
 55  0
                     injectBean(op, name, propertyName, bs.getLocation());
 56  
                 }
 57  0
                 catch (Exception ex)
 58  
                 {
 59  0
                     _errorLog.error(EnhanceMessages.errorAddingProperty(
 60  
                             propertyName, op.getBaseClass(), ex), bs
 61  
                             .getLocation(), ex);
 62  0
                 }
 63  
             }
 64  0
         }
 65  0
     }
 66  
 
 67  
     public void injectBean(EnhancementOperation op, String beanName,
 68  
             String propertyName, Location location)
 69  
     {
 70  0
         Defense.notNull(op, "op");
 71  0
         Defense.notNull(beanName, "beanName");
 72  0
         Defense.notNull(propertyName, "propertyName");
 73  
 
 74  0
         op.claimReadonlyProperty(propertyName);
 75  
 
 76  0
         Class propertyType = EnhanceUtils.extractPropertyType(op, propertyName,
 77  
                 null);
 78  
 
 79  0
         String methodName = op.getAccessorMethodName(propertyName);
 80  
 
 81  0
         MethodSignature sig = new MethodSignature(propertyType, methodName,
 82  
                 null, null);
 83  
 
 84  
         // i.e.
 85  
         // return (foo.bar.Baz) getBeans().getBean("baz");
 86  
 
 87  0
         op.addMethod(Modifier.PUBLIC, sig, "return ("
 88  
                 + ClassFabUtils.getJavaClassName(propertyType)
 89  
                 + ") getBeans().getBean(\"" + beanName + "\");", location);
 90  0
     }
 91  
 
 92  
     public void setErrorLog(ErrorLog errorLog)
 93  
     {
 94  0
         _errorLog = errorLog;
 95  0
     }
 96  
 }