Coverage Report - org.apache.tapestry.enhance.InjectStateWorker
 
Classes in this File Line Coverage Branch Coverage Complexity
InjectStateWorker
0%
0/31
0%
0/4
1
 
 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 org.apache.hivemind.Location;
 18  
 import org.apache.hivemind.service.BodyBuilder;
 19  
 import org.apache.hivemind.service.ClassFabUtils;
 20  
 import org.apache.hivemind.service.MethodSignature;
 21  
 import org.apache.hivemind.util.Defense;
 22  
 import org.apache.tapestry.engine.state.ApplicationStateManager;
 23  
 import org.apache.tapestry.event.PageDetachListener;
 24  
 import org.apache.tapestry.spec.InjectSpecification;
 25  
 
 26  
 import java.lang.reflect.Modifier;
 27  
 
 28  
 /**
 29  
  * Worker for injecting application state objects as properties of the
 30  
  * component. These properties are read/write and must be "live" (changes are
 31  
  * propogated back into the
 32  
  * {@link org.apache.tapestry.engine.state.ApplicationStateManager}).
 33  
  *
 34  
  * They should also cache in a local variable for efficiency, and clear out that
 35  
  * variable at the end of the request.
 36  
  * 
 37  
  * @author Howard M. Lewis Ship
 38  
  * @since 4.0
 39  
  */
 40  0
 public class InjectStateWorker implements InjectEnhancementWorker
 41  
 {
 42  
 
 43  
     private ApplicationStateManager _applicationStateManager;
 44  
 
 45  
     public void performEnhancement(EnhancementOperation op,
 46  
                                    InjectSpecification spec)
 47  
     {
 48  0
         injectState(op, spec.getObject(), spec.getProperty(), spec.getLocation());
 49  0
     }
 50  
 
 51  
     void injectState(EnhancementOperation op, String objectName,
 52  
             String propertyName, Location location)
 53  
     {
 54  0
         Defense.notNull(op, "op");
 55  0
         Defense.notNull(objectName, "objectName");
 56  0
         Defense.notNull(propertyName, "propertyName");
 57  
 
 58  0
         Class propertyType = EnhanceUtils.extractPropertyType(op, propertyName, null);
 59  0
         String fieldName = "_$" + propertyName;
 60  
 
 61  
         // State properties are read/write
 62  
 
 63  0
         op.claimProperty(propertyName);
 64  
 
 65  0
         op.addField(fieldName, propertyType);
 66  
 
 67  0
         String managerField = op.addInjectedField("_$applicationStateManager", ApplicationStateManager.class, _applicationStateManager);
 68  
 
 69  0
         BodyBuilder builder = new BodyBuilder();
 70  
 
 71  
         // Accessor
 72  
 
 73  0
         builder.begin();
 74  0
         builder.addln("if ({0} == null)", fieldName);
 75  0
         builder.addln("  {0} = ({1}) {2}.get(\"{3}\");", new Object[] {
 76  
                 fieldName, ClassFabUtils.getJavaClassName(propertyType),
 77  
                 managerField, objectName });
 78  0
         builder.addln("return {0};", fieldName);
 79  0
         builder.end();
 80  
 
 81  0
         String methodName = op.getAccessorMethodName(propertyName);
 82  
 
 83  0
         MethodSignature sig = new MethodSignature(propertyType, methodName,
 84  
                 null, null);
 85  
 
 86  0
         op.addMethod(Modifier.PUBLIC, sig, builder.toString(), location);
 87  
 
 88  
         // Mutator
 89  
 
 90  0
         builder.clear();
 91  0
         builder.begin();
 92  0
         builder.addln("{0}.store(\"{1}\", $1);", managerField, objectName);
 93  0
         builder.addln("{0} = $1;", fieldName);
 94  0
         builder.end();
 95  
 
 96  0
         sig = new MethodSignature(void.class, EnhanceUtils
 97  
                 .createMutatorMethodName(propertyName),
 98  
                 new Class[] { propertyType }, null);
 99  
 
 100  0
         op.addMethod(Modifier.PUBLIC, sig, builder.toString(), location);
 101  
 
 102  
         // Extend pageDetached() to clean out the cached field value.
 103  
 
 104  0
         op.extendMethodImplementation(PageDetachListener.class,
 105  
                 EnhanceUtils.PAGE_DETACHED_SIGNATURE, fieldName + " = null;");
 106  0
     }
 107  
 
 108  
     public void setApplicationStateManager(ApplicationStateManager applicationStateManager)
 109  
     {
 110  0
         _applicationStateManager = applicationStateManager;
 111  0
     }
 112  
 }