|
|||||||||||||||||||
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% | 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 |
import java.util.Iterator;
|
|
19 |
import java.util.List;
|
|
20 |
|
|
21 |
import org.apache.hivemind.ErrorLog;
|
|
22 |
import org.apache.hivemind.service.BodyBuilder;
|
|
23 |
import org.apache.hivemind.service.ClassFabUtils;
|
|
24 |
import org.apache.hivemind.service.MethodSignature;
|
|
25 |
import org.apache.tapestry.engine.state.ApplicationStateManager;
|
|
26 |
import org.apache.tapestry.event.PageDetachListener;
|
|
27 |
import org.apache.tapestry.spec.IComponentSpecification;
|
|
28 |
import org.apache.tapestry.spec.InjectStateSpecification;
|
|
29 |
|
|
30 |
/**
|
|
31 |
* Worker for injecting application state objects as properties of the component. These properties
|
|
32 |
* are read/write and must be "live" (changes are propogated back into the
|
|
33 |
* {@link org.apache.tapestry.engine.state.ApplicationStateManager}). They should also cache in a
|
|
34 |
* local variable for efficiency, and clear out that variable at the end of the request.
|
|
35 |
*
|
|
36 |
* @author Howard M. Lewis Ship
|
|
37 |
* @since 3.1
|
|
38 |
*/
|
|
39 |
public class InjectStateWorker implements EnhancementWorker |
|
40 |
{ |
|
41 |
private ErrorLog _errorLog;
|
|
42 |
|
|
43 |
private ApplicationStateManager _applicationStateManager;
|
|
44 |
|
|
45 | 534 |
public void performEnhancement(EnhancementOperation op, IComponentSpecification spec) |
46 |
{ |
|
47 | 534 |
List injects = spec.getInjectStateSpecifications(); |
48 |
|
|
49 | 534 |
if (injects.isEmpty())
|
50 | 532 |
return;
|
51 |
|
|
52 |
// TODO: The EnhancementOperation should have a way of assigning non-conflicting
|
|
53 |
// attribute names. What if someone injects a property named "applicationStateManager"
|
|
54 |
// as well?
|
|
55 |
|
|
56 | 2 |
op.addField( |
57 |
"_$applicationStateManager",
|
|
58 |
ApplicationStateManager.class,
|
|
59 |
_applicationStateManager); |
|
60 |
|
|
61 | 2 |
Iterator i = injects.iterator(); |
62 | 2 |
while (i.hasNext())
|
63 |
{ |
|
64 | 2 |
InjectStateSpecification iss = (InjectStateSpecification) i.next(); |
65 |
|
|
66 | 2 |
try
|
67 |
{ |
|
68 | 2 |
injectState(op, iss.getProperty(), iss.getObjectName()); |
69 |
} |
|
70 |
catch (Exception ex)
|
|
71 |
{ |
|
72 | 1 |
_errorLog.error(EnhanceMessages.errorAddingProperty(iss.getProperty(), op |
73 |
.getBaseClass(), ex), iss.getLocation(), ex); |
|
74 |
} |
|
75 |
} |
|
76 |
} |
|
77 |
|
|
78 | 2 |
private void injectState(EnhancementOperation op, String propertyName, String objectName) |
79 |
{ |
|
80 | 2 |
Class propertyType = |
81 |
EnhanceUtils.extractPropertyType(op, propertyName, null);
|
|
82 | 2 |
String fieldName = "_$" + propertyName;
|
83 |
|
|
84 | 2 |
op.claimProperty(propertyName); |
85 |
|
|
86 | 1 |
op.addField(fieldName, propertyType); |
87 |
|
|
88 | 1 |
BodyBuilder builder = new BodyBuilder();
|
89 |
|
|
90 |
// Accessor
|
|
91 |
|
|
92 | 1 |
builder.begin(); |
93 | 1 |
builder.addln("if ({0} == null)", fieldName);
|
94 | 1 |
builder.addln( |
95 |
" {0} = ({1}) _$applicationStateManager.get(\"{2}\");",
|
|
96 |
fieldName, |
|
97 |
ClassFabUtils.getJavaClassName(propertyType), |
|
98 |
objectName); |
|
99 | 1 |
builder.addln("return {0};", fieldName);
|
100 | 1 |
builder.end(); |
101 |
|
|
102 | 1 |
String methodName = op.getAccessorMethodName(propertyName); |
103 |
|
|
104 | 1 |
MethodSignature sig = new MethodSignature(propertyType, methodName, null, null); |
105 |
|
|
106 | 1 |
op.addMethod(Modifier.PUBLIC, sig, builder.toString()); |
107 |
|
|
108 |
// Mutator
|
|
109 |
|
|
110 | 1 |
builder.clear(); |
111 | 1 |
builder.begin(); |
112 | 1 |
builder.addln("_$applicationStateManager.store(\"{0}\", $1);", objectName);
|
113 | 1 |
builder.addln("{0} = $1;", fieldName);
|
114 | 1 |
builder.end(); |
115 |
|
|
116 | 1 |
sig = new MethodSignature(void.class, EnhanceUtils.createMutatorMethodName(propertyName), |
117 |
new Class[]
|
|
118 |
{ propertyType }, null);
|
|
119 |
|
|
120 | 1 |
op.addMethod(Modifier.PUBLIC, sig, builder.toString()); |
121 |
|
|
122 |
// Extend pageDetached() to clean out the cached field value.
|
|
123 |
|
|
124 | 1 |
op.extendMethodImplementation( |
125 |
PageDetachListener.class,
|
|
126 |
EnhanceUtils.PAGE_DETACHED_SIGNATURE, |
|
127 |
fieldName + " = null;");
|
|
128 |
} |
|
129 |
|
|
130 | 54 |
public void setApplicationStateManager(ApplicationStateManager applicationStateManager) |
131 |
{ |
|
132 | 54 |
_applicationStateManager = applicationStateManager; |
133 |
} |
|
134 |
|
|
135 | 53 |
public void setErrorLog(ErrorLog errorLog) |
136 |
{ |
|
137 | 53 |
_errorLog = errorLog; |
138 |
} |
|
139 |
} |
|