Coverage Report - org.apache.tapestry.enhance.InjectAssetWorker
 
Classes in this File Line Coverage Branch Coverage Complexity
InjectAssetWorker
0%
0/27
0%
0/10
2.667
 
 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.ApplicationRuntimeException;
 21  
 import org.apache.hivemind.ErrorLog;
 22  
 import org.apache.hivemind.Location;
 23  
 import org.apache.hivemind.service.MethodSignature;
 24  
 import org.apache.hivemind.util.Defense;
 25  
 import org.apache.tapestry.IAsset;
 26  
 import org.apache.tapestry.spec.IAssetSpecification;
 27  
 import org.apache.tapestry.spec.IComponentSpecification;
 28  
 
 29  
 /**
 30  
  * Injects assets as component properties.
 31  
  * 
 32  
  * @author Howard M. Lewis Ship
 33  
  * @since 4.0
 34  
  */
 35  0
 public class InjectAssetWorker implements EnhancementWorker
 36  
 {
 37  
 
 38  
     private ErrorLog _errorLog;
 39  
 
 40  
     public void performEnhancement(EnhancementOperation op,
 41  
             IComponentSpecification spec)
 42  
     {
 43  0
         Iterator i = spec.getAssetNames().iterator();
 44  0
         while(i.hasNext())
 45  
         {
 46  0
             String name = (String) i.next();
 47  
 
 48  0
             IAssetSpecification as = spec.getAsset(name);
 49  
 
 50  0
             String propertyName = as.getPropertyName();
 51  
 
 52  0
             if (propertyName != null)
 53  
             {
 54  
                 try
 55  
                 {
 56  0
                     injectAsset(op, name, propertyName, as.getLocation());
 57  
                 }
 58  0
                 catch (Exception ex)
 59  
                 {
 60  0
                     _errorLog.error(EnhanceMessages.errorAddingProperty(
 61  
                             propertyName, op.getBaseClass(), ex), as
 62  
                             .getLocation(), ex);
 63  0
                 }
 64  
             }
 65  0
         }
 66  0
     }
 67  
 
 68  
     public void injectAsset(EnhancementOperation op, String assetName,
 69  
             String propertyName, Location location)
 70  
     {
 71  0
         Defense.notNull(op, "op");
 72  0
         Defense.notNull(assetName, "assetName");
 73  0
         Defense.notNull(propertyName, "propertyName");
 74  
 
 75  0
         Class propertyType = EnhanceUtils.extractPropertyType(op, propertyName,
 76  
                 null);
 77  
 
 78  0
         op.claimReadonlyProperty(propertyName);
 79  
 
 80  0
         if (!propertyType.isAssignableFrom(IAsset.class))
 81  0
             throw new ApplicationRuntimeException(EnhanceMessages
 82  
                     .incompatiblePropertyType(propertyName, propertyType,
 83  
                             IAsset.class));
 84  
 
 85  0
         String methodName = op.getAccessorMethodName(propertyName);
 86  
 
 87  0
         MethodSignature sig = new MethodSignature(propertyType, methodName,
 88  
                 null, null);
 89  
 
 90  0
         String code = "return getAsset(\"" + assetName + "\");";
 91  
 
 92  0
         op.addMethod(Modifier.PUBLIC, sig, code, location);
 93  0
     }
 94  
 
 95  
     public void setErrorLog(ErrorLog errorLog)
 96  
     {
 97  0
         _errorLog = errorLog;
 98  0
     }
 99  
 }