01: // Copyright 2006, 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.internal.services;
16:
17: import java.io.IOException;
18:
19: import org.apache.tapestry.ComponentEventHandler;
20: import org.apache.tapestry.TapestryConstants;
21: import org.apache.tapestry.internal.structure.Page;
22: import org.apache.tapestry.internal.util.Holder;
23: import org.apache.tapestry.runtime.Component;
24: import org.apache.tapestry.services.ActionResponseGenerator;
25: import org.apache.tapestry.services.ComponentEventResultProcessor;
26: import org.apache.tapestry.services.PageRenderRequestHandler;
27: import org.apache.tapestry.services.Response;
28:
29: /**
30: * Handles a PageLink as specified by a PageLinkPathSource by activating and then rendering the
31: * page.
32: */
33: public class PageRenderRequestHandlerImpl implements
34: PageRenderRequestHandler {
35: private final RequestPageCache _cache;
36:
37: private final ComponentEventResultProcessor _resultProcessor;
38:
39: private final PageResponseRenderer _pageResponseRenderer;
40:
41: private final Response _response;
42:
43: public PageRenderRequestHandlerImpl(RequestPageCache cache,
44: ComponentEventResultProcessor resultProcessor,
45: PageResponseRenderer pageResponseRenderer, Response response) {
46: _cache = cache;
47: _resultProcessor = resultProcessor;
48: _pageResponseRenderer = pageResponseRenderer;
49: _response = response;
50: }
51:
52: public ActionResponseGenerator handle(String logicalPageName,
53: String[] context) {
54: Page page = _cache.get(logicalPageName);
55:
56: final Holder<ActionResponseGenerator> holder = new Holder<ActionResponseGenerator>();
57:
58: ComponentEventHandler handler = new ComponentEventHandler() {
59: @SuppressWarnings("unchecked")
60: public boolean handleResult(Object result,
61: Component component, String methodDescription) {
62: ActionResponseGenerator generator = _resultProcessor
63: .processComponentEvent(result, component,
64: methodDescription);
65:
66: holder.put(generator);
67:
68: return true; // abort the event
69: }
70: };
71:
72: page.getRootElement().triggerEvent(
73: TapestryConstants.ACTIVATE_EVENT, context, handler);
74:
75: if (holder.hasValue())
76: return holder.get();
77:
78: try {
79: _pageResponseRenderer.renderPageResponse(page, _response);
80: } catch (IOException ex) {
81: throw new RuntimeException(ex);
82: }
83:
84: return null;
85: }
86: }
|