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