KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > enhance > InjectStateWorker


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 JavaDoc;
18
19 import org.apache.hivemind.service.BodyBuilder;
20 import org.apache.hivemind.service.ClassFabUtils;
21 import org.apache.hivemind.service.MethodSignature;
22 import org.apache.hivemind.util.Defense;
23 import org.apache.tapestry.engine.state.ApplicationStateManager;
24 import org.apache.tapestry.event.PageDetachListener;
25 import org.apache.tapestry.spec.InjectSpecification;
26
27 /**
28  * Worker for injecting application state objects as properties of the component. These properties
29  * are read/write and must be "live" (changes are propogated back into the
30  * {@link org.apache.tapestry.engine.state.ApplicationStateManager}). They should also cache in a
31  * local variable for efficiency, and clear out that variable at the end of the request.
32  *
33  * @author Howard M. Lewis Ship
34  * @since 4.0
35  */

36 public class InjectStateWorker implements InjectEnhancementWorker
37 {
38     private ApplicationStateManager _applicationStateManager;
39
40     public void performEnhancement(EnhancementOperation op, InjectSpecification spec)
41     {
42         injectState(op, spec.getObject(), spec.getProperty());
43     }
44
45     public void injectState(EnhancementOperation op, String JavaDoc objectName, String JavaDoc propertyName)
46     {
47         Defense.notNull(op, "op");
48         Defense.notNull(objectName, "objectName");
49         Defense.notNull(propertyName, "propertyName");
50
51         Class JavaDoc propertyType = EnhanceUtils.extractPropertyType(op, propertyName, null);
52         String JavaDoc fieldName = "_$" + propertyName;
53
54         op.claimProperty(propertyName);
55
56         op.addField(fieldName, propertyType);
57
58         String JavaDoc managerField = op.addInjectedField(
59                 "_$applicationStateManager",
60                 ApplicationStateManager.class,
61                 _applicationStateManager);
62
63         BodyBuilder builder = new BodyBuilder();
64
65         // Accessor
66

67         builder.begin();
68         builder.addln("if ({0} == null)", fieldName);
69         builder.addln(" {0} = ({1}) {2}.get(\"{3}\");", new Object JavaDoc[]
70         { fieldName, ClassFabUtils.getJavaClassName(propertyType), managerField, objectName });
71         builder.addln("return {0};", fieldName);
72         builder.end();
73
74         String JavaDoc methodName = op.getAccessorMethodName(propertyName);
75
76         MethodSignature sig = new MethodSignature(propertyType, methodName, null, null);
77
78         op.addMethod(Modifier.PUBLIC, sig, builder.toString());
79
80         // Mutator
81

82         builder.clear();
83         builder.begin();
84         builder.addln("{0}.store(\"{1}\", $1);", managerField, objectName);
85         builder.addln("{0} = $1;", fieldName);
86         builder.end();
87
88         sig = new MethodSignature(void.class, EnhanceUtils.createMutatorMethodName(propertyName),
89                 new Class JavaDoc[]
90                 { propertyType }, null);
91
92         op.addMethod(Modifier.PUBLIC, sig, builder.toString());
93
94         // Extend pageDetached() to clean out the cached field value.
95

96         op.extendMethodImplementation(
97                 PageDetachListener.class,
98                 EnhanceUtils.PAGE_DETACHED_SIGNATURE,
99                 fieldName + " = null;");
100     }
101
102     public void setApplicationStateManager(ApplicationStateManager applicationStateManager)
103     {
104         _applicationStateManager = applicationStateManager;
105     }
106 }
Popular Tags