1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.engine; |
16 |
|
|
17 |
|
import org.apache.commons.fileupload.RequestContext; |
18 |
|
import org.apache.commons.logging.Log; |
19 |
|
import org.apache.commons.logging.LogFactory; |
20 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
21 |
|
import org.apache.hivemind.ErrorLog; |
22 |
|
import org.apache.hivemind.impl.ErrorLogImpl; |
23 |
|
import org.apache.hivemind.util.Defense; |
24 |
|
import org.apache.hivemind.util.ToStringBuilder; |
25 |
|
import org.apache.tapestry.*; |
26 |
|
import org.apache.tapestry.record.PageRecorderImpl; |
27 |
|
import org.apache.tapestry.record.PropertyPersistenceStrategySource; |
28 |
|
import org.apache.tapestry.services.AbsoluteURLBuilder; |
29 |
|
import org.apache.tapestry.services.Infrastructure; |
30 |
|
import org.apache.tapestry.services.ResponseBuilder; |
31 |
|
import org.apache.tapestry.util.IdAllocator; |
32 |
|
import org.apache.tapestry.util.QueryParameterMap; |
33 |
|
|
34 |
|
import java.util.HashMap; |
35 |
|
import java.util.Iterator; |
36 |
|
import java.util.Map; |
37 |
|
import java.util.Stack; |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
public class RequestCycle implements IRequestCycle |
47 |
|
{ |
48 |
2 |
private static final Log LOG = LogFactory.getLog(RequestCycle.class); |
49 |
|
|
50 |
|
protected ResponseBuilder _responseBuilder; |
51 |
|
|
52 |
|
private IPage _page; |
53 |
|
|
54 |
|
private IEngine _engine; |
55 |
|
|
56 |
|
private String _serviceName; |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
private PropertyPersistenceStrategySource _strategySource; |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
private IPageSource _pageSource; |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
private Infrastructure _infrastructure; |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
private QueryParameterMap _parameters; |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
private AbsoluteURLBuilder _absoluteURLBuilder; |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
private Map _loadedPages; |
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
private Map _pageRecorders; |
96 |
|
|
97 |
4 |
private boolean _rewinding = false; |
98 |
|
|
99 |
4 |
private Map _attributes = new HashMap(); |
100 |
|
|
101 |
|
private int _targetActionId; |
102 |
|
|
103 |
|
private IComponent _targetComponent; |
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
private Object[] _listenerParameters; |
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
private ErrorLog _log; |
112 |
|
|
113 |
|
|
114 |
|
|
115 |
4 |
private IdAllocator _idAllocator = new IdAllocator(); |
116 |
|
|
117 |
4 |
private Stack _renderStack = new Stack(); |
118 |
|
|
119 |
4 |
private boolean _focusDisabled = false; |
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
public RequestCycle(IEngine engine, QueryParameterMap parameters, String serviceName, |
136 |
|
RequestCycleEnvironment environment) |
137 |
2 |
{ |
138 |
|
|
139 |
|
|
140 |
2 |
_engine = engine; |
141 |
2 |
_parameters = parameters; |
142 |
2 |
_serviceName = serviceName; |
143 |
|
|
144 |
|
|
145 |
|
|
146 |
2 |
_infrastructure = environment.getInfrastructure(); |
147 |
2 |
_pageSource = _infrastructure.getPageSource(); |
148 |
2 |
_strategySource = environment.getStrategySource(); |
149 |
2 |
_absoluteURLBuilder = environment.getAbsoluteURLBuilder(); |
150 |
2 |
_log = new ErrorLogImpl(environment.getErrorHandler(), LOG); |
151 |
2 |
} |
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
public RequestCycle() |
159 |
2 |
{ |
160 |
2 |
} |
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
public void cleanup() |
168 |
|
{ |
169 |
0 |
if (_loadedPages == null) |
170 |
0 |
return; |
171 |
|
|
172 |
0 |
Iterator i = _loadedPages.values().iterator(); |
173 |
|
|
174 |
0 |
while (i.hasNext()) |
175 |
|
{ |
176 |
0 |
IPage page = (IPage) i.next(); |
177 |
|
|
178 |
0 |
_pageSource.releasePage(page); |
179 |
0 |
} |
180 |
|
|
181 |
0 |
_loadedPages = null; |
182 |
0 |
_pageRecorders = null; |
183 |
0 |
_renderStack.clear(); |
184 |
0 |
} |
185 |
|
|
186 |
|
public IEngineService getService() |
187 |
|
{ |
188 |
1 |
return _infrastructure.getServiceMap().getService(_serviceName); |
189 |
|
} |
190 |
|
|
191 |
|
public String encodeURL(String URL) |
192 |
|
{ |
193 |
0 |
return _infrastructure.getResponse().encodeURL(URL); |
194 |
|
} |
195 |
|
|
196 |
|
public IEngine getEngine() |
197 |
|
{ |
198 |
2 |
return _engine; |
199 |
|
} |
200 |
|
|
201 |
|
public Object getAttribute(String name) |
202 |
|
{ |
203 |
4 |
return _attributes.get(name); |
204 |
|
} |
205 |
|
|
206 |
|
public IPage getPage() |
207 |
|
{ |
208 |
0 |
return _page; |
209 |
|
} |
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
|
215 |
|
public IPage getPage(String name) |
216 |
|
{ |
217 |
0 |
Defense.notNull(name, "name"); |
218 |
|
|
219 |
0 |
IPage result = null; |
220 |
|
|
221 |
0 |
if (_loadedPages != null) |
222 |
0 |
result = (IPage) _loadedPages.get(name); |
223 |
|
|
224 |
0 |
if (result == null) |
225 |
|
{ |
226 |
0 |
result = loadPage(name); |
227 |
|
|
228 |
0 |
if (_loadedPages == null) |
229 |
0 |
_loadedPages = new HashMap(); |
230 |
|
|
231 |
0 |
_loadedPages.put(name, result); |
232 |
|
} |
233 |
|
|
234 |
0 |
return result; |
235 |
|
} |
236 |
|
|
237 |
|
private IPage loadPage(String name) |
238 |
|
{ |
239 |
0 |
IPage result = _pageSource.getPage(this, name); |
240 |
|
|
241 |
|
|
242 |
|
|
243 |
|
|
244 |
0 |
IPageRecorder recorder = getPageRecorder(name); |
245 |
|
|
246 |
|
|
247 |
|
|
248 |
|
|
249 |
|
|
250 |
0 |
recorder.rollback(result); |
251 |
|
|
252 |
|
|
253 |
|
|
254 |
|
|
255 |
0 |
result.setChangeObserver(recorder); |
256 |
|
|
257 |
|
|
258 |
|
|
259 |
0 |
result.firePageAttached(); |
260 |
|
|
261 |
0 |
return result; |
262 |
|
} |
263 |
|
|
264 |
|
|
265 |
|
|
266 |
|
|
267 |
|
|
268 |
|
|
269 |
|
protected IPageRecorder getPageRecorder(String name) |
270 |
|
{ |
271 |
0 |
if (_pageRecorders == null) |
272 |
0 |
_pageRecorders = new HashMap(); |
273 |
|
|
274 |
0 |
IPageRecorder result = (IPageRecorder) _pageRecorders.get(name); |
275 |
|
|
276 |
0 |
if (result == null) |
277 |
|
{ |
278 |
0 |
result = new PageRecorderImpl(name, _strategySource, _log); |
279 |
0 |
_pageRecorders.put(name, result); |
280 |
|
} |
281 |
|
|
282 |
0 |
return result; |
283 |
|
} |
284 |
|
|
285 |
|
public void setResponseBuilder(ResponseBuilder builder) |
286 |
|
{ |
287 |
|
|
288 |
|
|
289 |
|
|
290 |
|
|
291 |
0 |
_responseBuilder = builder; |
292 |
0 |
} |
293 |
|
|
294 |
|
public ResponseBuilder getResponseBuilder() |
295 |
|
{ |
296 |
0 |
return _responseBuilder; |
297 |
|
} |
298 |
|
|
299 |
|
|
300 |
|
|
301 |
|
|
302 |
|
public boolean renderStackEmpty() |
303 |
|
{ |
304 |
0 |
return _renderStack.isEmpty(); |
305 |
|
} |
306 |
|
|
307 |
|
|
308 |
|
|
309 |
|
|
310 |
|
public IRender renderStackPeek() |
311 |
|
{ |
312 |
0 |
if (_renderStack.size() < 1) |
313 |
0 |
return null; |
314 |
|
|
315 |
0 |
return (IRender)_renderStack.peek(); |
316 |
|
} |
317 |
|
|
318 |
|
|
319 |
|
|
320 |
|
|
321 |
|
public IRender renderStackPop() |
322 |
|
{ |
323 |
0 |
if (_renderStack.size() == 0) |
324 |
0 |
return null; |
325 |
|
|
326 |
0 |
return (IRender)_renderStack.pop(); |
327 |
|
} |
328 |
|
|
329 |
|
|
330 |
|
|
331 |
|
|
332 |
|
public IRender renderStackPush(IRender render) |
333 |
|
{ |
334 |
0 |
if (_renderStack.size() > 0 && _renderStack.peek() == render) |
335 |
0 |
return render; |
336 |
|
|
337 |
0 |
return (IRender)_renderStack.push(render); |
338 |
|
} |
339 |
|
|
340 |
|
|
341 |
|
|
342 |
|
|
343 |
|
public int renderStackSearch(IRender render) |
344 |
|
{ |
345 |
0 |
return _renderStack.search(render); |
346 |
|
} |
347 |
|
|
348 |
|
|
349 |
|
|
350 |
|
|
351 |
|
public Iterator renderStackIterator() |
352 |
|
{ |
353 |
0 |
return _renderStack.iterator(); |
354 |
|
} |
355 |
|
|
356 |
|
public boolean isRewinding() |
357 |
|
{ |
358 |
0 |
return _rewinding; |
359 |
|
} |
360 |
|
|
361 |
|
public boolean isRewound(IComponent component) |
362 |
|
{ |
363 |
|
|
364 |
|
|
365 |
0 |
if (!_rewinding) |
366 |
0 |
return false; |
367 |
|
|
368 |
|
|
369 |
|
|
370 |
0 |
if (component == _targetComponent) |
371 |
0 |
return true; |
372 |
|
|
373 |
|
|
374 |
|
|
375 |
0 |
throw new StaleLinkException(component, Integer.toHexString(_targetActionId), _targetComponent.getExtendedId()); |
376 |
|
} |
377 |
|
|
378 |
|
public void removeAttribute(String name) |
379 |
|
{ |
380 |
0 |
if (LOG.isDebugEnabled()) |
381 |
0 |
LOG.debug("Removing attribute " + name); |
382 |
|
|
383 |
0 |
_attributes.remove(name); |
384 |
0 |
} |
385 |
|
|
386 |
|
|
387 |
|
|
388 |
|
|
389 |
|
|
390 |
|
|
391 |
|
public void renderPage(ResponseBuilder builder) |
392 |
|
{ |
393 |
0 |
_rewinding = false; |
394 |
|
|
395 |
|
try |
396 |
|
{ |
397 |
0 |
_page.renderPage(builder, this); |
398 |
|
|
399 |
|
} |
400 |
0 |
catch (ApplicationRuntimeException ex) |
401 |
|
{ |
402 |
|
|
403 |
|
|
404 |
0 |
throw ex; |
405 |
|
} |
406 |
0 |
catch (Throwable ex) |
407 |
|
{ |
408 |
|
|
409 |
|
|
410 |
|
|
411 |
0 |
throw new ApplicationRuntimeException(ex.getMessage(), _page, null, ex); |
412 |
|
} |
413 |
|
finally |
414 |
|
{ |
415 |
0 |
reset(); |
416 |
0 |
} |
417 |
|
|
418 |
0 |
} |
419 |
|
|
420 |
|
|
421 |
|
|
422 |
|
|
423 |
|
|
424 |
|
private void reset() |
425 |
|
{ |
426 |
0 |
_attributes.clear(); |
427 |
0 |
_idAllocator.clear(); |
428 |
0 |
} |
429 |
|
|
430 |
|
|
431 |
|
|
432 |
|
|
433 |
|
|
434 |
|
|
435 |
|
|
436 |
|
|
437 |
|
|
438 |
|
|
439 |
|
|
440 |
|
|
441 |
|
|
442 |
|
public void rewindForm(IForm form) |
443 |
|
{ |
444 |
0 |
IPage page = form.getPage(); |
445 |
0 |
_rewinding = true; |
446 |
|
|
447 |
0 |
_targetComponent = form; |
448 |
|
|
449 |
|
try |
450 |
|
{ |
451 |
0 |
page.beginPageRender(); |
452 |
|
|
453 |
0 |
form.rewind(NullWriter.getSharedInstance(), this); |
454 |
|
|
455 |
|
|
456 |
|
|
457 |
|
|
458 |
0 |
throw new StaleLinkException(Tapestry.format("RequestCycle.form-rewind-failure", form.getExtendedId()), form); |
459 |
|
} |
460 |
0 |
catch (RenderRewoundException ex) |
461 |
|
{ |
462 |
|
|
463 |
|
} |
464 |
0 |
catch (ApplicationRuntimeException ex) |
465 |
|
{ |
466 |
|
|
467 |
0 |
throw ex; |
468 |
|
} |
469 |
0 |
catch (Throwable ex) |
470 |
|
{ |
471 |
|
|
472 |
|
|
473 |
|
|
474 |
0 |
throw new ApplicationRuntimeException(ex.getMessage(), page, null, ex); |
475 |
|
} |
476 |
|
finally |
477 |
|
{ |
478 |
0 |
page.endPageRender(); |
479 |
|
|
480 |
0 |
reset(); |
481 |
0 |
_rewinding = false; |
482 |
0 |
} |
483 |
0 |
} |
484 |
|
|
485 |
|
|
486 |
|
|
487 |
|
|
488 |
|
public void disableFocus() |
489 |
|
{ |
490 |
0 |
_focusDisabled = true; |
491 |
0 |
} |
492 |
|
|
493 |
|
|
494 |
|
|
495 |
|
|
496 |
|
public boolean isFocusDisabled() |
497 |
|
{ |
498 |
0 |
return _focusDisabled; |
499 |
|
} |
500 |
|
|
501 |
|
public void setAttribute(String name, Object value) |
502 |
|
{ |
503 |
2 |
if (LOG.isDebugEnabled()) |
504 |
0 |
LOG.debug("Set attribute " + name + " to " + value); |
505 |
|
|
506 |
2 |
_attributes.put(name, value); |
507 |
2 |
} |
508 |
|
|
509 |
|
|
510 |
|
|
511 |
|
|
512 |
|
|
513 |
|
|
514 |
|
public void commitPageChanges() |
515 |
|
{ |
516 |
0 |
if (LOG.isDebugEnabled()) |
517 |
0 |
LOG.debug("Committing page changes"); |
518 |
|
|
519 |
0 |
if (_pageRecorders == null || _pageRecorders.isEmpty()) |
520 |
0 |
return; |
521 |
|
|
522 |
0 |
Iterator i = _pageRecorders.values().iterator(); |
523 |
|
|
524 |
0 |
while (i.hasNext()) |
525 |
|
{ |
526 |
0 |
IPageRecorder recorder = (IPageRecorder) i.next(); |
527 |
|
|
528 |
0 |
recorder.commit(); |
529 |
0 |
} |
530 |
0 |
} |
531 |
|
|
532 |
|
|
533 |
|
|
534 |
|
|
535 |
|
|
536 |
|
|
537 |
|
|
538 |
|
public void discardPage(String name) |
539 |
|
{ |
540 |
0 |
forgetPage(name); |
541 |
0 |
} |
542 |
|
|
543 |
|
|
544 |
|
public Object[] getListenerParameters() |
545 |
|
{ |
546 |
0 |
return _listenerParameters; |
547 |
|
} |
548 |
|
|
549 |
|
|
550 |
|
public void setListenerParameters(Object[] parameters) |
551 |
|
{ |
552 |
0 |
_listenerParameters = parameters; |
553 |
0 |
} |
554 |
|
|
555 |
|
|
556 |
|
|
557 |
|
public void activate(String name) |
558 |
|
{ |
559 |
0 |
IPage page = getPage(name); |
560 |
|
|
561 |
0 |
activate(page); |
562 |
0 |
} |
563 |
|
|
564 |
|
|
565 |
|
|
566 |
|
public void activate(IPage page) |
567 |
|
{ |
568 |
0 |
Defense.notNull(page, "page"); |
569 |
|
|
570 |
0 |
if (LOG.isDebugEnabled()) |
571 |
0 |
LOG.debug("Activating page " + page); |
572 |
|
|
573 |
0 |
Tapestry.clearMethodInvocations(); |
574 |
|
|
575 |
0 |
page.validate(this); |
576 |
|
|
577 |
0 |
Tapestry.checkMethodInvocation(Tapestry.ABSTRACTPAGE_VALIDATE_METHOD_ID, "validate()", page); |
578 |
|
|
579 |
0 |
_page = page; |
580 |
0 |
} |
581 |
|
|
582 |
|
|
583 |
|
public String getParameter(String name) |
584 |
|
{ |
585 |
0 |
return _parameters.getParameterValue(name); |
586 |
|
} |
587 |
|
|
588 |
|
|
589 |
|
public String[] getParameters(String name) |
590 |
|
{ |
591 |
0 |
return _parameters.getParameterValues(name); |
592 |
|
} |
593 |
|
|
594 |
|
|
595 |
|
|
596 |
|
|
597 |
|
public String toString() |
598 |
|
{ |
599 |
0 |
ToStringBuilder b = new ToStringBuilder(this); |
600 |
|
|
601 |
0 |
b.append("rewinding", _rewinding); |
602 |
|
|
603 |
0 |
b.append("serviceName", _serviceName); |
604 |
|
|
605 |
0 |
b.append("serviceParameters", _listenerParameters); |
606 |
|
|
607 |
0 |
if (_loadedPages != null) |
608 |
0 |
b.append("loadedPages", _loadedPages.keySet()); |
609 |
|
|
610 |
0 |
b.append("attributes", _attributes); |
611 |
0 |
b.append("targetActionId", _targetActionId); |
612 |
0 |
b.append("targetComponent", _targetComponent); |
613 |
|
|
614 |
0 |
return b.toString(); |
615 |
|
} |
616 |
|
|
617 |
|
|
618 |
|
|
619 |
|
public String getAbsoluteURL(String partialURL) |
620 |
|
{ |
621 |
0 |
String contextPath = _infrastructure.getRequest().getContextPath(); |
622 |
|
|
623 |
0 |
return _absoluteURLBuilder.constructURL(contextPath + partialURL); |
624 |
|
} |
625 |
|
|
626 |
|
|
627 |
|
|
628 |
|
public void forgetPage(String pageName) |
629 |
|
{ |
630 |
1 |
Defense.notNull(pageName, "pageName"); |
631 |
|
|
632 |
1 |
_strategySource.discardAllStoredChanged(pageName); |
633 |
1 |
} |
634 |
|
|
635 |
|
|
636 |
|
|
637 |
|
public Infrastructure getInfrastructure() |
638 |
|
{ |
639 |
1 |
return _infrastructure; |
640 |
|
} |
641 |
|
|
642 |
|
|
643 |
|
|
644 |
|
public String getUniqueId(String baseId) |
645 |
|
{ |
646 |
0 |
return _idAllocator.allocateId(baseId); |
647 |
|
} |
648 |
|
|
649 |
|
|
650 |
|
|
651 |
|
public String peekUniqueId(String baseId) |
652 |
|
{ |
653 |
0 |
return _idAllocator.peekNextId(baseId); |
654 |
|
} |
655 |
|
|
656 |
|
|
657 |
|
public void sendRedirect(String URL) |
658 |
|
{ |
659 |
1 |
throw new RedirectException(URL); |
660 |
|
} |
661 |
|
|
662 |
|
} |