1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.engine; |
16 |
| |
17 |
| import java.io.IOException; |
18 |
| import java.util.ArrayList; |
19 |
| import java.util.List; |
20 |
| import java.util.Locale; |
21 |
| |
22 |
| import javax.servlet.RequestDispatcher; |
23 |
| import javax.servlet.ServletContext; |
24 |
| import javax.servlet.ServletException; |
25 |
| |
26 |
| |
27 |
| import org.apache.commons.logging.Log; |
28 |
| import org.apache.commons.logging.LogFactory; |
29 |
| import org.apache.hivemind.ApplicationRuntimeException; |
30 |
| import org.apache.hivemind.ClassResolver; |
31 |
| import org.apache.hivemind.util.Defense; |
32 |
| import org.apache.hivemind.util.ToStringBuilder; |
33 |
| import org.apache.tapestry.ApplicationServlet; |
34 |
| import org.apache.tapestry.Constants; |
35 |
| import org.apache.tapestry.IEngine; |
36 |
| import org.apache.tapestry.IPage; |
37 |
| import org.apache.tapestry.IRequestCycle; |
38 |
| import org.apache.tapestry.PageRedirectException; |
39 |
| import org.apache.tapestry.RedirectException; |
40 |
| import org.apache.tapestry.StaleLinkException; |
41 |
| import org.apache.tapestry.StaleSessionException; |
42 |
| import org.apache.tapestry.TapestryConstants; |
43 |
| import org.apache.tapestry.listener.ListenerMap; |
44 |
| import org.apache.tapestry.request.RequestContext; |
45 |
| import org.apache.tapestry.services.DataSqueezer; |
46 |
| import org.apache.tapestry.services.Infrastructure; |
47 |
| import org.apache.tapestry.spec.IApplicationSpecification; |
48 |
| import org.apache.tapestry.web.WebRequest; |
49 |
| import org.apache.tapestry.web.WebResponse; |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
| |
80 |
| |
81 |
| |
82 |
| |
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
| public abstract class AbstractEngine implements IEngine |
98 |
| { |
99 |
| private static final Log LOG = LogFactory.getLog(AbstractEngine.class); |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
| private Infrastructure _infrastructure; |
107 |
| |
108 |
| private ListenerMap _listeners; |
109 |
| |
110 |
| |
111 |
| |
112 |
| |
113 |
| |
114 |
| private Locale _locale; |
115 |
| |
116 |
| |
117 |
| |
118 |
| |
119 |
| |
120 |
| |
121 |
| public static final String VISIT_CLASS_PROPERTY_NAME = "org.apache.tapestry.visit-class"; |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
| |
127 |
24
| protected void activateExceptionPage(IRequestCycle cycle, Throwable cause)
|
128 |
| { |
129 |
24
| _infrastructure.getExceptionPresenter().presentException(cycle, cause);
|
130 |
| } |
131 |
| |
132 |
| |
133 |
| |
134 |
| |
135 |
| |
136 |
| |
137 |
| |
138 |
0
| public void reportException(String reportTitle, Throwable ex)
|
139 |
| { |
140 |
0
| _infrastructure.getRequestExceptionReporter().reportRequestException(reportTitle, ex);
|
141 |
| } |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
| |
147 |
| |
148 |
0
| protected void cleanupAfterRequest(IRequestCycle cycle)
|
149 |
| { |
150 |
| |
151 |
| } |
152 |
| |
153 |
| |
154 |
| |
155 |
| |
156 |
| |
157 |
| |
158 |
332
| public Locale getLocale()
|
159 |
| { |
160 |
332
| return _locale;
|
161 |
| } |
162 |
| |
163 |
| |
164 |
| |
165 |
| |
166 |
| |
167 |
| |
168 |
| |
169 |
| |
170 |
85
| public IEngineService getService(String name)
|
171 |
| { |
172 |
85
| return _infrastructure.getServiceMap().getService(name);
|
173 |
| } |
174 |
| |
175 |
| |
176 |
| |
177 |
6
| public IApplicationSpecification getSpecification()
|
178 |
| { |
179 |
6
| return _infrastructure.getApplicationSpecification();
|
180 |
| } |
181 |
| |
182 |
| |
183 |
| |
184 |
0
| public ISpecificationSource getSpecificationSource()
|
185 |
| { |
186 |
0
| return _infrastructure.getSpecificationSource();
|
187 |
| } |
188 |
| |
189 |
| |
190 |
| |
191 |
| |
192 |
| |
193 |
| |
194 |
0
| protected void redirect(String pageName, IRequestCycle cycle,
|
195 |
| ApplicationRuntimeException exception) throws IOException |
196 |
| { |
197 |
0
| IPage page = cycle.getPage(pageName);
|
198 |
| |
199 |
0
| cycle.activate(page);
|
200 |
| |
201 |
0
| renderResponse(cycle);
|
202 |
| } |
203 |
| |
204 |
| |
205 |
| |
206 |
| |
207 |
| |
208 |
| |
209 |
2
| public void renderResponse(IRequestCycle cycle) throws IOException
|
210 |
| { |
211 |
2
| _infrastructure.getResponseRenderer().renderResponse(cycle);
|
212 |
| } |
213 |
| |
214 |
| |
215 |
| |
216 |
| |
217 |
| |
218 |
132
| public void service(WebRequest request, WebResponse response) throws IOException
|
219 |
| { |
220 |
132
| IRequestCycle cycle = null;
|
221 |
132
| IMonitor monitor = null;
|
222 |
132
| IEngineService service = null;
|
223 |
| |
224 |
132
| if (_infrastructure == null)
|
225 |
49
| _infrastructure = (Infrastructure) request.getAttribute(Constants.INFRASTRUCTURE_KEY);
|
226 |
| |
227 |
132
| try
|
228 |
| { |
229 |
132
| try
|
230 |
| { |
231 |
132
| cycle = _infrastructure.getRequestCycleFactory().newRequestCycle(this);
|
232 |
| |
233 |
132
| monitor = cycle.getMonitor();
|
234 |
132
| service = cycle.getService();
|
235 |
| |
236 |
132
| monitor.serviceBegin(service.getName(), _infrastructure.getRequest()
|
237 |
| .getRequestURI()); |
238 |
| |
239 |
| |
240 |
| |
241 |
132
| service.service(cycle);
|
242 |
| |
243 |
102
| return;
|
244 |
| } |
245 |
| catch (PageRedirectException ex) |
246 |
| { |
247 |
3
| handlePageRedirectException(cycle, ex);
|
248 |
| } |
249 |
| catch (RedirectException ex) |
250 |
| { |
251 |
0
| handleRedirectException(cycle, ex);
|
252 |
| } |
253 |
| catch (StaleLinkException ex) |
254 |
| { |
255 |
1
| handleStaleLinkException(cycle, ex);
|
256 |
| } |
257 |
| catch (StaleSessionException ex) |
258 |
| { |
259 |
3
| handleStaleSessionException(cycle, ex);
|
260 |
| } |
261 |
| } |
262 |
| catch (Exception ex) |
263 |
| { |
264 |
24
| monitor.serviceException(ex);
|
265 |
| |
266 |
| |
267 |
| |
268 |
| |
269 |
| |
270 |
24
| if (LOG.isDebugEnabled())
|
271 |
0
| LOG.debug("Uncaught exception", ex);
|
272 |
| |
273 |
24
| activateExceptionPage(cycle, ex);
|
274 |
| } |
275 |
| finally |
276 |
| { |
277 |
132
| if (service != null)
|
278 |
132
| monitor.serviceEnd(service.getName());
|
279 |
| |
280 |
132
| try
|
281 |
| { |
282 |
132
| cycle.cleanup();
|
283 |
132
| _infrastructure.getApplicationStateManager().flush();
|
284 |
| } |
285 |
| catch (Exception ex) |
286 |
| { |
287 |
0
| reportException(EngineMessages.exceptionDuringCleanup(ex), ex);
|
288 |
| } |
289 |
| } |
290 |
| } |
291 |
| |
292 |
| |
293 |
| |
294 |
| |
295 |
| |
296 |
| |
297 |
| |
298 |
| |
299 |
| |
300 |
| |
301 |
| |
302 |
3
| protected void handlePageRedirectException(IRequestCycle cycle, PageRedirectException exception)
|
303 |
| throws IOException |
304 |
| { |
305 |
3
| List pageNames = new ArrayList();
|
306 |
| |
307 |
3
| String pageName = exception.getTargetPageName();
|
308 |
| |
309 |
3
| while (true)
|
310 |
| { |
311 |
5
| if (pageNames.contains(pageName))
|
312 |
| { |
313 |
1
| pageNames.add(pageName);
|
314 |
| |
315 |
1
| throw new ApplicationRuntimeException(EngineMessages.validateCycle(pageNames));
|
316 |
| } |
317 |
| |
318 |
| |
319 |
| |
320 |
4
| pageNames.add(pageName);
|
321 |
| |
322 |
4
| try
|
323 |
| { |
324 |
| |
325 |
| |
326 |
4
| cycle.activate(pageName);
|
327 |
| |
328 |
2
| break;
|
329 |
| } |
330 |
| catch (PageRedirectException secondRedirectException) |
331 |
| { |
332 |
2
| pageName = secondRedirectException.getTargetPageName();
|
333 |
| } |
334 |
| } |
335 |
| |
336 |
2
| renderResponse(cycle);
|
337 |
| } |
338 |
| |
339 |
| |
340 |
| |
341 |
| |
342 |
| |
343 |
| |
344 |
| |
345 |
| |
346 |
| |
347 |
| |
348 |
| |
349 |
| |
350 |
| |
351 |
| |
352 |
| |
353 |
| |
354 |
| |
355 |
| |
356 |
| |
357 |
| |
358 |
| |
359 |
| |
360 |
1
| protected void handleStaleLinkException(IRequestCycle cycle, StaleLinkException exception)
|
361 |
| throws IOException |
362 |
| { |
363 |
1
| _infrastructure.getStaleLinkExceptionPresenter()
|
364 |
| .presentStaleLinkException(cycle, exception); |
365 |
| } |
366 |
| |
367 |
| |
368 |
| |
369 |
| |
370 |
| |
371 |
| |
372 |
| |
373 |
| |
374 |
| |
375 |
| |
376 |
| |
377 |
| |
378 |
| |
379 |
| |
380 |
3
| protected void handleStaleSessionException(IRequestCycle cycle, StaleSessionException exception)
|
381 |
| { |
382 |
3
| _infrastructure.getStaleSessionExceptionPresenter().presentStaleSessionException(
|
383 |
| cycle, |
384 |
| exception); |
385 |
| } |
386 |
| |
387 |
| |
388 |
| |
389 |
| |
390 |
| |
391 |
63
| public void setLocale(Locale value)
|
392 |
| { |
393 |
63
| Defense.notNull(value, "locale");
|
394 |
| |
395 |
63
| _locale = value;
|
396 |
| |
397 |
| |
398 |
| |
399 |
63
| if (_infrastructure != null)
|
400 |
12
| _infrastructure.setLocale(value);
|
401 |
| } |
402 |
| |
403 |
| |
404 |
| |
405 |
| |
406 |
| |
407 |
118
| public ClassResolver getClassResolver()
|
408 |
| { |
409 |
118
| return _infrastructure.getClassResolver();
|
410 |
| } |
411 |
| |
412 |
| |
413 |
| |
414 |
| |
415 |
| |
416 |
| |
417 |
| |
418 |
| |
419 |
0
| public String toString()
|
420 |
| { |
421 |
0
| ToStringBuilder builder = new ToStringBuilder(this);
|
422 |
| |
423 |
0
| builder.append("locale", _locale);
|
424 |
| |
425 |
0
| return builder.toString();
|
426 |
| } |
427 |
| |
428 |
| |
429 |
| |
430 |
| |
431 |
| |
432 |
| |
433 |
| |
434 |
| |
435 |
| |
436 |
| |
437 |
2
| public Object getVisit()
|
438 |
| { |
439 |
2
| return _infrastructure.getApplicationStateManager().get("visit");
|
440 |
| } |
441 |
| |
442 |
0
| public void setVisit(Object visit)
|
443 |
| { |
444 |
0
| _infrastructure.getApplicationStateManager().store("visit", visit);
|
445 |
| } |
446 |
| |
447 |
| |
448 |
| |
449 |
| |
450 |
| |
451 |
| |
452 |
| |
453 |
1
| public Object getVisit(IRequestCycle cycle)
|
454 |
| { |
455 |
1
| return getVisit();
|
456 |
| } |
457 |
| |
458 |
0
| public boolean getHasVisit()
|
459 |
| { |
460 |
0
| return _infrastructure.getApplicationStateManager().exists("visit");
|
461 |
| } |
462 |
| |
463 |
| |
464 |
| |
465 |
| |
466 |
| |
467 |
| |
468 |
| |
469 |
| |
470 |
| |
471 |
| |
472 |
| |
473 |
| |
474 |
1
| public Object getGlobal()
|
475 |
| { |
476 |
1
| return _infrastructure.getApplicationStateManager().get("global");
|
477 |
| } |
478 |
| |
479 |
0
| public IScriptSource getScriptSource()
|
480 |
| { |
481 |
0
| return _infrastructure.getScriptSource();
|
482 |
| } |
483 |
| |
484 |
| |
485 |
| |
486 |
| |
487 |
| |
488 |
| |
489 |
| |
490 |
0
| public ListenerMap getListeners()
|
491 |
| { |
492 |
0
| if (_listeners == null)
|
493 |
0
| _listeners = _infrastructure.getListenerMapSource().getListenerMapForObject(this);
|
494 |
| |
495 |
0
| return _listeners;
|
496 |
| } |
497 |
| |
498 |
| |
499 |
| |
500 |
| |
501 |
| |
502 |
| |
503 |
| |
504 |
| |
505 |
| |
506 |
| |
507 |
0
| protected void handleRedirectException(IRequestCycle cycle, RedirectException redirectException)
|
508 |
| { |
509 |
0
| String location = redirectException.getRedirectLocation();
|
510 |
| |
511 |
0
| if (LOG.isDebugEnabled())
|
512 |
0
| LOG.debug("Redirecting to: " + location);
|
513 |
| |
514 |
0
| _infrastructure.getRequest().forward(location);
|
515 |
| } |
516 |
| |
517 |
| |
518 |
| |
519 |
| |
520 |
| |
521 |
0
| public DataSqueezer getDataSqueezer()
|
522 |
| { |
523 |
0
| return _infrastructure.getDataSqueezer();
|
524 |
| } |
525 |
| |
526 |
| |
527 |
| |
528 |
0
| public IPropertySource getPropertySource()
|
529 |
| { |
530 |
0
| return _infrastructure.getApplicationPropertySource();
|
531 |
| } |
532 |
| |
533 |
| |
534 |
24
| public Infrastructure getInfrastructure()
|
535 |
| { |
536 |
24
| return _infrastructure;
|
537 |
| } |
538 |
| |
539 |
358
| public String getOutputEncoding()
|
540 |
| { |
541 |
358
| return _infrastructure.getOutputEncoding();
|
542 |
| } |
543 |
| } |