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    
019    import org.apache.hivemind.Location;
020    import org.apache.hivemind.service.BodyBuilder;
021    import org.apache.hivemind.service.ClassFabUtils;
022    import org.apache.hivemind.service.MethodSignature;
023    import org.apache.hivemind.util.Defense;
024    import org.apache.tapestry.engine.state.ApplicationStateManager;
025    import org.apache.tapestry.event.PageDetachListener;
026    import org.apache.tapestry.spec.InjectSpecification;
027    
028    /**
029     * Worker for injecting application state objects as properties of the component. These properties
030     * are read/write and must be "live" (changes are propogated back into the
031     * {@link org.apache.tapestry.engine.state.ApplicationStateManager}). They should also cache in a
032     * local variable for efficiency, and clear out that variable at the end of the request.
033     * 
034     * @author Howard M. Lewis Ship
035     * @since 4.0
036     */
037    public class InjectStateWorker implements InjectEnhancementWorker
038    {
039        private ApplicationStateManager _applicationStateManager;
040    
041        public void performEnhancement(EnhancementOperation op, InjectSpecification spec)
042        {
043            injectState(op, spec.getObject(), spec.getProperty(), spec.getLocation());
044        }
045    
046        void injectState(EnhancementOperation op, String objectName, String propertyName,
047                Location location)
048        {
049            Defense.notNull(op, "op");
050            Defense.notNull(objectName, "objectName");
051            Defense.notNull(propertyName, "propertyName");
052    
053            Class propertyType = EnhanceUtils.extractPropertyType(op, propertyName, null);
054            String fieldName = "_$" + propertyName;
055    
056            // State properties are read/write
057    
058            op.claimProperty(propertyName);
059    
060            op.addField(fieldName, propertyType);
061    
062            String managerField = op.addInjectedField(
063                    "_$applicationStateManager",
064                    ApplicationStateManager.class,
065                    _applicationStateManager);
066    
067            BodyBuilder builder = new BodyBuilder();
068    
069            // Accessor
070    
071            builder.begin();
072            builder.addln("if ({0} == null)", fieldName);
073            builder.addln("  {0} = ({1}) {2}.get(\"{3}\");", new Object[]
074            { fieldName, ClassFabUtils.getJavaClassName(propertyType), managerField, objectName });
075            builder.addln("return {0};", fieldName);
076            builder.end();
077    
078            String methodName = op.getAccessorMethodName(propertyName);
079    
080            MethodSignature sig = new MethodSignature(propertyType, methodName, null, null);
081    
082            op.addMethod(Modifier.PUBLIC, sig, builder.toString(), location);
083    
084            // Mutator
085    
086            builder.clear();
087            builder.begin();
088            builder.addln("{0}.store(\"{1}\", $1);", managerField, objectName);
089            builder.addln("{0} = $1;", fieldName);
090            builder.end();
091    
092            sig = new MethodSignature(void.class, EnhanceUtils.createMutatorMethodName(propertyName),
093                    new Class[]
094                    { propertyType }, null);
095    
096            op.addMethod(Modifier.PUBLIC, sig, builder.toString(), location);
097    
098            // Extend pageDetached() to clean out the cached field value.
099    
100            op.extendMethodImplementation(
101                    PageDetachListener.class,
102                    EnhanceUtils.PAGE_DETACHED_SIGNATURE,
103                    fieldName + " = null;");
104        }
105    
106        public void setApplicationStateManager(ApplicationStateManager applicationStateManager)
107        {
108            _applicationStateManager = applicationStateManager;
109        }
110    }