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 | 5 | private CombinedBeanInfo requestBeanInfo = new CombinedBeanInfo( |
50 | |
Request.class, ApplicationContext.class); |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
public TilesContextVariableResolverFactory(TilesRequestContextHolder requestHolder) { |
59 | 5 | super(requestHolder); |
60 | 5 | } |
61 | |
|
62 | |
|
63 | |
public boolean isTarget(String name) { |
64 | 4 | 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 | 2 | VariableResolver resolver = null; |
74 | 2 | PropertyDescriptor descriptor = requestBeanInfo.getMappedDescriptors(Request.class).get(name); |
75 | 2 | if (descriptor != null) { |
76 | 1 | resolver = new RequestVariableResolver(name, descriptor); |
77 | |
} else { |
78 | 1 | descriptor = requestBeanInfo.getMappedDescriptors(ApplicationContext.class).get(name); |
79 | 1 | if (descriptor != null) { |
80 | 1 | resolver = new ApplicationVariableResolver(name, descriptor); |
81 | |
} |
82 | |
} |
83 | 2 | 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 | 1 | public RequestVariableResolver(String name, PropertyDescriptor descriptor) { |
107 | 1 | super(name); |
108 | 1 | this.descriptor = descriptor; |
109 | 1 | } |
110 | |
|
111 | |
|
112 | |
@SuppressWarnings("rawtypes") |
113 | |
public Class getType() { |
114 | 1 | return descriptor.getPropertyType(); |
115 | |
} |
116 | |
|
117 | |
|
118 | |
public Object getValue() { |
119 | 1 | Method method = descriptor.getReadMethod(); |
120 | |
try { |
121 | 1 | return method.invoke(requestHolder.getTilesRequestContext()); |
122 | 0 | } catch (IllegalArgumentException e) { |
123 | 0 | throw new CannotAccessMethodException( |
124 | |
"Arguments are wrong for property '" |
125 | |
+ descriptor.getName() + "'", e); |
126 | 0 | } catch (IllegalAccessException e) { |
127 | 0 | throw new CannotAccessMethodException( |
128 | |
"Cannot access getter method for property '" |
129 | |
+ descriptor.getName() + "'", e); |
130 | 0 | } catch (InvocationTargetException e) { |
131 | 0 | 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 | 1 | public ApplicationVariableResolver(String name, PropertyDescriptor descriptor) { |
163 | 1 | super(name); |
164 | 1 | this.descriptor = descriptor; |
165 | 1 | } |
166 | |
|
167 | |
|
168 | |
@SuppressWarnings("rawtypes") |
169 | |
public Class getType() { |
170 | 1 | return descriptor.getPropertyType(); |
171 | |
} |
172 | |
|
173 | |
|
174 | |
public Object getValue() { |
175 | 1 | Method method = descriptor.getReadMethod(); |
176 | |
try { |
177 | 1 | return method.invoke(requestHolder.getTilesRequestContext() |
178 | |
.getApplicationContext()); |
179 | 0 | } catch (IllegalArgumentException e) { |
180 | 0 | throw new CannotAccessMethodException( |
181 | |
"Arguments are wrong for property '" |
182 | |
+ descriptor.getName() + "'", e); |
183 | 0 | } catch (IllegalAccessException e) { |
184 | 0 | throw new CannotAccessMethodException( |
185 | |
"Cannot access getter method for property '" |
186 | |
+ descriptor.getName() + "'", e); |
187 | 0 | } catch (InvocationTargetException e) { |
188 | 0 | throw new CannotAccessMethodException( |
189 | |
"The getter method for property '" |
190 | |
+ descriptor.getName() + "' threw an exception", |
191 | |
e); |
192 | |
} |
193 | |
} |
194 | |
} |
195 | |
} |