1 package org.apache.velocity.tools.view;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.velocity.util.introspection.AbstractChainableUberspector;
23 import org.apache.velocity.util.introspection.Info;
24 import org.apache.velocity.util.introspection.Introspector;
25 import org.apache.velocity.util.introspection.VelMethod;
26 import org.apache.velocity.util.introspection.VelPropertyGet;
27 import org.apache.velocity.util.introspection.VelPropertySet;
28 import org.apache.velocity.runtime.parser.node.AbstractExecutor;
29 import org.apache.velocity.runtime.parser.node.SetExecutor;
30 import org.apache.velocity.runtime.log.Log;
31
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpSession;
34 import javax.servlet.ServletContext;
35 import java.lang.reflect.InvocationTargetException;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 public class WebappUberspector extends AbstractChainableUberspector
57 {
58
59
60
61
62
63
64
65
66
67 public VelPropertyGet getPropertyGet(Object obj, String identifier, Info i)
68 throws Exception
69 {
70 VelPropertyGet ret = super.getPropertyGet(obj,identifier,i);
71 if(ret == null)
72 {
73 Class claz = obj.getClass();
74 if(obj instanceof HttpServletRequest
75 || obj instanceof HttpSession
76 || obj instanceof ServletContext)
77 {
78 AbstractExecutor executor = new GetAttributeExecutor(log, introspector, claz, identifier);
79 ret = executor.isAlive() ? new VelGetterImpl(executor) : null;
80 }
81 }
82 return ret;
83 }
84
85
86
87
88 @Override
89 public void init()
90 {
91 try
92 {
93 super.init();
94 }
95 catch (RuntimeException re)
96 {
97 throw re;
98 }
99 catch (Exception e)
100 {
101 throw new RuntimeException(e);
102 }
103
104
105 introspector = new Introspector(log);
106 }
107
108
109
110
111
112
113
114
115
116
117 public VelPropertySet getPropertySet(Object obj, String identifier,
118 Object arg, Info i)
119 throws Exception
120 {
121 VelPropertySet ret = super.getPropertySet(obj,identifier,arg,i);
122 if(ret == null) {
123 Class claz = obj.getClass();
124 if(obj instanceof HttpServletRequest
125 || obj instanceof HttpSession
126 || obj instanceof ServletContext)
127 {
128 SetExecutor executor = new SetAttributeExecutor(log, introspector, claz, arg, identifier);
129 ret = executor.isAlive() ? new VelSetterImpl(executor) : null;
130 }
131 }
132 return ret;
133 }
134
135
136
137
138
139 public class GetAttributeExecutor extends AbstractExecutor
140 {
141 private final Introspector introspector;
142
143
144 private Object [] params;
145
146
147
148
149
150
151
152 public GetAttributeExecutor(final Log log, final Introspector introspector,
153 final Class clazz, final String property)
154 {
155 this.log = log;
156 this.introspector = introspector;
157 this.params = new Object[] { property };
158
159 discover(clazz);
160 }
161
162 protected void discover(final Class clazz)
163 {
164 try
165 {
166 setMethod(introspector.getMethod(clazz, "getAttribute", params));
167 }
168
169
170
171 catch( RuntimeException e )
172 {
173 throw e;
174 }
175 catch(Exception e)
176 {
177 log.error("While looking for getAttribute('" + params[0] + "') method:", e);
178 }
179 }
180
181
182
183
184 public Object execute(final Object o)
185 throws IllegalAccessException, InvocationTargetException
186 {
187 return isAlive() ? getMethod().invoke(o, params) : null;
188 }
189 }
190
191
192
193
194 public class SetAttributeExecutor extends SetExecutor
195 {
196 private final Introspector introspector;
197 private final String property;
198
199
200
201
202
203
204
205
206 public SetAttributeExecutor(final Log log, final Introspector introspector,
207 final Class clazz, final Object arg, final String property)
208 {
209 this.log = log;
210 this.introspector = introspector;
211 this.property = property;
212
213 discover(clazz, arg);
214 }
215
216
217
218
219
220 protected void discover(final Class clazz, final Object arg)
221 {
222 Object [] params = new Object[] { property, arg };
223
224 try
225 {
226 setMethod(introspector.getMethod(clazz, "setAttribute", params));
227 }
228
229
230
231 catch( RuntimeException e )
232 {
233 throw e;
234 }
235 catch(Exception e)
236 {
237 log.error("While looking for put('" + params[0] + "') method:", e);
238 }
239 }
240
241
242
243
244 public Object execute(final Object o, final Object value)
245 throws IllegalAccessException, InvocationTargetException
246 {
247 Object [] params;
248
249 if (isAlive())
250 {
251 params = new Object [] { property, value };
252 return getMethod().invoke(o, params);
253 }
254
255 return null;
256 }
257 }
258 }