Coverage Report - org.apache.tapestry.enhance.InjectStateFlagWorker
 
Classes in this File Line Coverage Branch Coverage Complexity
InjectStateFlagWorker
0%
0/24
0%
0/6
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 org.apache.hivemind.ApplicationRuntimeException;
 18  
 import org.apache.hivemind.Location;
 19  
 import org.apache.hivemind.service.BodyBuilder;
 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.spec.InjectSpecification;
 24  
 
 25  
 import java.lang.reflect.Modifier;
 26  
 
 27  
 /**
 28  
  * Injects a boolean property that indicates if a particular application state object already
 29  
  * exists. This is useful in situations where you are trying to prevent the creation of the ASO (and
 30  
  * thus, prevent the creation of the HttpSession).
 31  
  * 
 32  
  * @author Howard M. Lewis Ship
 33  
  * @since 4.0
 34  
  */
 35  0
 public class InjectStateFlagWorker implements InjectEnhancementWorker
 36  
 {
 37  
     private ApplicationStateManager _applicationStateManager;
 38  
 
 39  
     public void performEnhancement(EnhancementOperation op, InjectSpecification spec)
 40  
     {
 41  0
         injectStateFlag(op, spec.getObject(), spec.getProperty(), spec.getLocation());
 42  0
     }
 43  
 
 44  
     void injectStateFlag(EnhancementOperation op, String objectName, String propertyName,
 45  
             Location location)
 46  
     {
 47  0
         Defense.notNull(op, "op");
 48  0
         Defense.notNull(objectName, "objectName");
 49  0
         Defense.notNull(propertyName, "propertyName");
 50  
 
 51  0
         Class propertyType = op.getPropertyType(propertyName);
 52  
 
 53  
         // null means no property at all; it's just in the XML
 54  
         // which is ok. Otherwise, make sure it is exactly boolean.
 55  
 
 56  0
         if (propertyType != null && propertyType != boolean.class)
 57  0
             throw new ApplicationRuntimeException(EnhanceMessages.mustBeBoolean(propertyName),
 58  
                     location, null);
 59  
 
 60  0
         op.claimReadonlyProperty(propertyName);
 61  
 
 62  0
         String managerField = op.addInjectedField(
 63  
                 "_$applicationStateManager",
 64  0
                 ApplicationStateManager.class,
 65  
                 _applicationStateManager);
 66  
 
 67  0
         BodyBuilder builder = new BodyBuilder();
 68  0
         builder.begin();
 69  0
         builder.add("return {0}.exists(", managerField);
 70  0
         builder.addQuoted(objectName);
 71  0
         builder.addln(");");
 72  0
         builder.end();
 73  
 
 74  0
         String methodName = op.getAccessorMethodName(propertyName);
 75  
 
 76  0
         MethodSignature sig = new MethodSignature(boolean.class, methodName, null, null);
 77  
 
 78  0
         op.addMethod(Modifier.PUBLIC, sig, builder.toString(), location);
 79  0
     }
 80  
 
 81  
     public void setApplicationStateManager(ApplicationStateManager applicationStateManager)
 82  
     {
 83  0
         _applicationStateManager = applicationStateManager;
 84  0
     }
 85  
 
 86  
 }