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
28 import org.apache.tiles.context.TilesRequestContextHolder;
29 import org.apache.tiles.request.ApplicationContext;
30 import org.apache.tiles.request.Request;
31 import org.apache.tiles.request.reflect.CannotAccessMethodException;
32 import org.apache.tiles.util.CombinedBeanInfo;
33 import org.mvel2.integration.VariableResolver;
34
35
36
37
38
39
40
41
42 public class TilesContextVariableResolverFactory extends
43 ReadOnlyVariableResolverFactory {
44
45
46
47
48
49 private CombinedBeanInfo requestBeanInfo = new CombinedBeanInfo(
50 Request.class, ApplicationContext.class);
51
52
53
54
55
56
57
58 public TilesContextVariableResolverFactory(TilesRequestContextHolder requestHolder) {
59 super(requestHolder);
60 }
61
62
63 public boolean isTarget(String name) {
64 return requestBeanInfo.getMappedDescriptors(Request.class).containsKey(
65 name)
66 || requestBeanInfo.getMappedDescriptors(
67 ApplicationContext.class).containsKey(name);
68 }
69
70
71 @Override
72 public VariableResolver createVariableResolver(String name) {
73 VariableResolver resolver = null;
74 PropertyDescriptor descriptor = requestBeanInfo.getMappedDescriptors(Request.class).get(name);
75 if (descriptor != null) {
76 resolver = new RequestVariableResolver(name, descriptor);
77 } else {
78 descriptor = requestBeanInfo.getMappedDescriptors(ApplicationContext.class).get(name);
79 if (descriptor != null) {
80 resolver = new ApplicationVariableResolver(name, descriptor);
81 }
82 }
83 return resolver;
84 }
85
86
87
88
89
90
91
92 private class RequestVariableResolver extends ReadOnlyVariableResolver {
93
94
95
96
97 private PropertyDescriptor descriptor;
98
99
100
101
102
103
104
105
106 public RequestVariableResolver(String name, PropertyDescriptor descriptor) {
107 super(name);
108 this.descriptor = descriptor;
109 }
110
111
112 @SuppressWarnings("rawtypes")
113 public Class getType() {
114 return descriptor.getPropertyType();
115 }
116
117
118 public Object getValue() {
119 Method method = descriptor.getReadMethod();
120 try {
121 return method.invoke(requestHolder.getTilesRequestContext());
122 } catch (IllegalArgumentException e) {
123 throw new CannotAccessMethodException(
124 "Arguments are wrong for property '"
125 + descriptor.getName() + "'", e);
126 } catch (IllegalAccessException e) {
127 throw new CannotAccessMethodException(
128 "Cannot access getter method for property '"
129 + descriptor.getName() + "'", e);
130 } catch (InvocationTargetException e) {
131 throw new CannotAccessMethodException(
132 "The getter method for property '"
133 + descriptor.getName() + "' threw an exception",
134 e);
135 }
136 }
137 }
138
139
140
141
142
143
144
145
146 private class ApplicationVariableResolver extends ReadOnlyVariableResolver {
147
148
149
150
151
152
153 private PropertyDescriptor descriptor;
154
155
156
157
158
159
160
161
162 public ApplicationVariableResolver(String name, PropertyDescriptor descriptor) {
163 super(name);
164 this.descriptor = descriptor;
165 }
166
167
168 @SuppressWarnings("rawtypes")
169 public Class getType() {
170 return descriptor.getPropertyType();
171 }
172
173
174 public Object getValue() {
175 Method method = descriptor.getReadMethod();
176 try {
177 return method.invoke(requestHolder.getTilesRequestContext()
178 .getApplicationContext());
179 } catch (IllegalArgumentException e) {
180 throw new CannotAccessMethodException(
181 "Arguments are wrong for property '"
182 + descriptor.getName() + "'", e);
183 } catch (IllegalAccessException e) {
184 throw new CannotAccessMethodException(
185 "Cannot access getter method for property '"
186 + descriptor.getName() + "'", e);
187 } catch (InvocationTargetException e) {
188 throw new CannotAccessMethodException(
189 "The getter method for property '"
190 + descriptor.getName() + "' threw an exception",
191 e);
192 }
193 }
194 }
195 }