This project has retired. For details please refer to its
Attic page.
TilesContextVariableResolverFactory xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.tiles.mvel;
23
24 import java.beans.PropertyDescriptor;
25 import java.lang.reflect.InvocationTargetException;
26 import java.lang.reflect.Method;
27 import java.util.HashMap;
28
29 import org.apache.tiles.TilesApplicationContext;
30 import org.apache.tiles.context.TilesRequestContext;
31 import org.apache.tiles.context.TilesRequestContextHolder;
32 import org.apache.tiles.reflect.CannotAccessMethodException;
33 import org.apache.tiles.util.CombinedBeanInfo;
34 import org.mvel2.integration.VariableResolver;
35 import org.mvel2.integration.impl.BaseVariableResolverFactory;
36
37 /***
38 * Resolves {@link org.apache.tiles.context.TilesRequestContext} and
39 * {@link org.apache.tiles.TilesApplicationContext} properties as variables.
40 *
41 * @version $Rev: 817009 $ $Date: 2009-09-20 13:26:26 +0200 (dom, 20 set 2009) $
42 * @since 2.2.0
43 */
44 public class TilesContextVariableResolverFactory extends
45 BaseVariableResolverFactory {
46
47 /***
48 * The Tiles request holder.
49 */
50 private TilesRequestContextHolder requestHolder;
51
52 /***
53 * Beaninfo about {@link org.apache.tiles.context.TilesRequestContext} and
54 * {@link org.apache.tiles.TilesApplicationContext}.
55 */
56 private CombinedBeanInfo requestBeanInfo = new CombinedBeanInfo(
57 TilesRequestContext.class, TilesApplicationContext.class);
58
59 /***
60 * Constructor.
61 *
62 * @param requestHolder The Tiles request holder.
63 * @since 2.2.0
64 */
65 public TilesContextVariableResolverFactory(TilesRequestContextHolder requestHolder) {
66 this.requestHolder = requestHolder;
67 variableResolvers = new HashMap<String, VariableResolver>();
68 for (PropertyDescriptor descriptor : requestBeanInfo
69 .getMappedDescriptors(TilesRequestContext.class).values()) {
70 String descriptorName = descriptor.getName();
71 variableResolvers.put(descriptorName, new RequestVariableResolver(descriptorName));
72 }
73 for (PropertyDescriptor descriptor : requestBeanInfo
74 .getMappedDescriptors(TilesApplicationContext.class).values()) {
75 String descriptorName = descriptor.getName();
76 variableResolvers.put(descriptorName, new ApplicationVariableResolver(descriptorName));
77 }
78 }
79
80 /*** {@inheritDoc} */
81 public VariableResolver createVariable(String name, Object value) {
82 if (nextFactory != null) {
83 return nextFactory.createVariable(name, value);
84 }
85 throw new UnsupportedOperationException("This variable resolver factory is read only");
86 }
87
88 /*** {@inheritDoc} */
89 public VariableResolver createVariable(String name, Object value,
90 Class<?> type) {
91 if (nextFactory != null) {
92 return nextFactory.createVariable(name, value, type);
93 }
94 throw new UnsupportedOperationException("This variable resolver factory is read only");
95 }
96
97 /*** {@inheritDoc} */
98 public boolean isResolveable(String name) {
99 if (variableResolvers.containsKey(name)) {
100 return true;
101 }
102 return isNextResolveable(name);
103 }
104
105 /*** {@inheritDoc} */
106 public boolean isTarget(String name) {
107 return variableResolvers.containsKey(name);
108 }
109
110 /***
111 * Resolves a {@link org.apache.tiles.context.TilesRequestContext} property as a variable.
112 *
113 * @version $Rev: 817009 $ $Date: 2009-09-20 13:26:26 +0200 (dom, 20 set 2009) $
114 * @since 2.2.0
115 */
116 private class RequestVariableResolver implements VariableResolver {
117
118 /***
119 * The name of the property.
120 */
121 private String name;
122
123 /***
124 * The property descriptor.
125 */
126 private PropertyDescriptor descriptor;
127
128 /***
129 * Constructor.
130 *
131 * @param name The name of the property.
132 * @since 2.2.0
133 */
134 public RequestVariableResolver(String name) {
135 this.name = name;
136 descriptor = requestBeanInfo.getMappedDescriptors(
137 TilesRequestContext.class).get(name);
138 }
139
140 /*** {@inheritDoc} */
141 public int getFlags() {
142 return 0;
143 }
144
145 /*** {@inheritDoc} */
146 public String getName() {
147 return name;
148 }
149
150 /*** {@inheritDoc} */
151 @SuppressWarnings("unchecked")
152 public Class getType() {
153 return descriptor.getPropertyType();
154 }
155
156 /*** {@inheritDoc} */
157 public Object getValue() {
158 Method method = descriptor.getReadMethod();
159 try {
160 return method.invoke(requestHolder.getTilesRequestContext());
161 } catch (IllegalArgumentException e) {
162 throw new CannotAccessMethodException(
163 "Arguments are wrong for property '"
164 + descriptor.getName() + "'", e);
165 } catch (IllegalAccessException e) {
166 throw new CannotAccessMethodException(
167 "Cannot access getter method for property '"
168 + descriptor.getName() + "'", e);
169 } catch (InvocationTargetException e) {
170 throw new CannotAccessMethodException(
171 "The getter method for property '"
172 + descriptor.getName() + "' threw an exception",
173 e);
174 }
175 }
176
177 /*** {@inheritDoc} */
178 @SuppressWarnings("unchecked")
179 public void setStaticType(Class type) {
180
181 }
182
183 /*** {@inheritDoc} */
184 public void setValue(Object value) {
185 throw new UnsupportedOperationException("This resolver is read-only");
186 }
187 }
188
189 /***
190 * Resolves a {@link org.apache.tiles.TilesApplicationContext} property as a
191 * variable.
192 *
193 * @version $Rev: 817009 $ $Date: 2009-09-20 13:26:26 +0200 (dom, 20 set 2009) $
194 * @since 2.2.0
195 */
196 private class ApplicationVariableResolver implements VariableResolver {
197
198 /***
199 * The name of the property.
200 *
201 * @since 2.2.0
202 */
203 private String name;
204
205 /***
206 * The property descriptor.
207 *
208 * @since 2.2.0
209 */
210 private PropertyDescriptor descriptor;
211
212 /***
213 * Constructor.
214 *
215 * @param name The name of the property.
216 * @since 2.2.0
217 */
218 public ApplicationVariableResolver(String name) {
219 this.name = name;
220 descriptor = requestBeanInfo.getMappedDescriptors(
221 TilesApplicationContext.class).get(name);
222 }
223
224 /*** {@inheritDoc} */
225 public int getFlags() {
226 return 0;
227 }
228
229 /*** {@inheritDoc} */
230 public String getName() {
231 return name;
232 }
233
234 /*** {@inheritDoc} */
235 @SuppressWarnings("unchecked")
236 public Class getType() {
237 return descriptor.getPropertyType();
238 }
239
240 /*** {@inheritDoc} */
241 public Object getValue() {
242 Method method = descriptor.getReadMethod();
243 try {
244 return method.invoke(requestHolder.getTilesRequestContext()
245 .getApplicationContext());
246 } catch (IllegalArgumentException e) {
247 throw new CannotAccessMethodException(
248 "Arguments are wrong for property '"
249 + descriptor.getName() + "'", e);
250 } catch (IllegalAccessException e) {
251 throw new CannotAccessMethodException(
252 "Cannot access getter method for property '"
253 + descriptor.getName() + "'", e);
254 } catch (InvocationTargetException e) {
255 throw new CannotAccessMethodException(
256 "The getter method for property '"
257 + descriptor.getName() + "' threw an exception",
258 e);
259 }
260 }
261
262 /*** {@inheritDoc} */
263 @SuppressWarnings("unchecked")
264 public void setStaticType(Class type) {
265
266 }
267
268 /*** {@inheritDoc} */
269 public void setValue(Object value) {
270 throw new UnsupportedOperationException("This resolver is read-only");
271 }
272 }
273 }