|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
InjectStateWorker.java | - | 100% | 100% | 100% |
|
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;
|
|
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.tapestry.engine.state.ApplicationStateManager;
|
|
23 |
import org.apache.tapestry.event.PageDetachListener;
|
|
24 |
import org.apache.tapestry.spec.InjectSpecification;
|
|
25 |
|
|
26 |
/**
|
|
27 |
* Worker for injecting application state objects as properties of the component. These properties
|
|
28 |
* are read/write and must be "live" (changes are propogated back into the
|
|
29 |
* {@link org.apache.tapestry.engine.state.ApplicationStateManager}). They should also cache in a
|
|
30 |
* local variable for efficiency, and clear out that variable at the end of the request.
|
|
31 |
*
|
|
32 |
* @author Howard M. Lewis Ship
|
|
33 |
* @since 4.0
|
|
34 |
*/
|
|
35 |
public class InjectStateWorker implements InjectEnhancementWorker |
|
36 |
{ |
|
37 |
private ApplicationStateManager _applicationStateManager;
|
|
38 |
|
|
39 | 1 |
public void performEnhancement(EnhancementOperation op, InjectSpecification spec) |
40 |
{ |
|
41 | 1 |
injectState(op, spec.getProperty(), spec.getObject()); |
42 |
} |
|
43 |
|
|
44 | 1 |
private void injectState(EnhancementOperation op, String propertyName, String objectName) |
45 |
{ |
|
46 | 1 |
Class propertyType = EnhanceUtils.extractPropertyType(op, propertyName, null);
|
47 | 1 |
String fieldName = "_$" + propertyName;
|
48 |
|
|
49 | 1 |
op.claimProperty(propertyName); |
50 |
|
|
51 | 1 |
op.addField(fieldName, propertyType); |
52 |
|
|
53 | 1 |
String managerField = op.addInjectedField( |
54 |
"_$applicationStateManager",
|
|
55 |
_applicationStateManager); |
|
56 |
|
|
57 | 1 |
BodyBuilder builder = new BodyBuilder();
|
58 |
|
|
59 |
// Accessor
|
|
60 |
|
|
61 | 1 |
builder.begin(); |
62 | 1 |
builder.addln("if ({0} == null)", fieldName);
|
63 | 1 |
builder.addln(" {0} = ({1}) {2}.get(\"{3}\");", new Object[] |
64 |
{ fieldName, ClassFabUtils.getJavaClassName(propertyType), managerField, objectName }); |
|
65 | 1 |
builder.addln("return {0};", fieldName);
|
66 | 1 |
builder.end(); |
67 |
|
|
68 | 1 |
String methodName = op.getAccessorMethodName(propertyName); |
69 |
|
|
70 | 1 |
MethodSignature sig = new MethodSignature(propertyType, methodName, null, null); |
71 |
|
|
72 | 1 |
op.addMethod(Modifier.PUBLIC, sig, builder.toString()); |
73 |
|
|
74 |
// Mutator
|
|
75 |
|
|
76 | 1 |
builder.clear(); |
77 | 1 |
builder.begin(); |
78 | 1 |
builder.addln("{0}.store(\"{1}\", $1);", managerField, objectName);
|
79 | 1 |
builder.addln("{0} = $1;", fieldName);
|
80 | 1 |
builder.end(); |
81 |
|
|
82 | 1 |
sig = new MethodSignature(void.class, EnhanceUtils.createMutatorMethodName(propertyName), |
83 |
new Class[]
|
|
84 |
{ propertyType }, null);
|
|
85 |
|
|
86 | 1 |
op.addMethod(Modifier.PUBLIC, sig, builder.toString()); |
87 |
|
|
88 |
// Extend pageDetached() to clean out the cached field value.
|
|
89 |
|
|
90 | 1 |
op.extendMethodImplementation( |
91 |
PageDetachListener.class,
|
|
92 |
EnhanceUtils.PAGE_DETACHED_SIGNATURE, |
|
93 |
fieldName + " = null;");
|
|
94 |
} |
|
95 |
|
|
96 | 1 |
public void setApplicationStateManager(ApplicationStateManager applicationStateManager) |
97 |
{ |
|
98 | 1 |
_applicationStateManager = applicationStateManager; |
99 |
} |
|
100 |
} |
|