KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > portlet > RenderService


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// 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.portlet;
16
17 import java.io.IOException JavaDoc;
18
19 import javax.portlet.PortletRequest;
20
21 import org.apache.tapestry.IRequestCycle;
22 import org.apache.tapestry.engine.IEngineService;
23 import org.apache.tapestry.engine.ILink;
24 import org.apache.tapestry.services.ServiceConstants;
25
26 /**
27  * Responsible for rendering out a page; a Portlet render URL is built during action processing that
28  * stores the active page; this is the page that will be rendered. The render service is (typically)
29  * the only service that operates during a portlet RenderRequest. All other services will be an
30  * ActionRequest that (via {@link org.apache.tapestry.portlet.PortletResponseRenderer}, writes
31  * query parameters to activate this service during the render request.
32  * <p>
33  * Problematic is is anything related to the portlet mode or window state. As per the Portlet spec,
34  * when the user clicks the "help" or "edit" buttons (or the minimize, maximize, etc.), this causes
35  * a new RenderRequest, but explicitly keeps the render parameters set by the most recent
36  * ActionRequest. But what Tapestry needs is to detect that the mode or state has changed and select
37  * a different page to render the response. So we store the mode and state in effect when the
38  * ActionRequest executed as two more query parameters, and detect changes to mode and state that
39  * way. If there is a change, then we ignore the page query parameter and use the
40  * {@link PortletPageResolver} to figure out the correct page to display instead.
41  *
42  * @author Howard M. Lewis Ship
43  * @since 4.0
44  * @see org.apache.tapestry.services.impl.ResponseRendererImpl
45  */

46 public class RenderService implements IEngineService
47 {
48     private PortletRequest _request;
49
50     private PortletRenderer _portletRenderer;
51
52     private PortletPageResolver _pageResolver;
53
54     public ILink getLink(IRequestCycle cycle, Object JavaDoc parameter)
55     {
56         throw new UnsupportedOperationException JavaDoc(PortletMessages.unsupportedMethod("getLink"));
57     }
58
59     public void service(IRequestCycle cycle) throws IOException JavaDoc
60     {
61         String JavaDoc pageName = getPageNameToRender(cycle);
62
63         _portletRenderer.renderPage(cycle, pageName);
64     }
65
66     private String JavaDoc getPageNameToRender(IRequestCycle cycle)
67     {
68         if (isStateChange(cycle))
69             return _pageResolver.getPageNameForRequest(cycle);
70
71         return cycle.getParameter(ServiceConstants.PAGE);
72     }
73
74     /**
75      * Returns true if the portlet mode or the window state has changed since. The values stored
76      * previously (during an action request) are compared to the current values.
77      */

78
79     boolean isStateChange(IRequestCycle cycle)
80     {
81         String JavaDoc expectedPortletMode = cycle.getParameter(PortletConstants.PORTLET_MODE);
82         String JavaDoc expectedWindowState = cycle.getParameter(PortletConstants.WINDOW_STATE);
83
84         return !(_request.getPortletMode().toString().equals(expectedPortletMode) && _request
85                 .getWindowState().toString().equals(expectedWindowState));
86
87     }
88
89     public String JavaDoc getName()
90     {
91         return PortletConstants.RENDER_SERVICE;
92     }
93
94     public void setPortletRenderer(PortletRenderer portletRenderer)
95     {
96         _portletRenderer = portletRenderer;
97     }
98
99     public void setRequest(PortletRequest request)
100     {
101         _request = request;
102     }
103
104     public void setPageResolver(PortletPageResolver pageResolver)
105     {
106         _pageResolver = pageResolver;
107     }
108 }
Popular Tags