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