KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > pluto > invoker > impl > PortletInvokerImpl


1 /*
2  * Copyright 2003,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17
18  */

19
20 package org.apache.pluto.invoker.impl;
21
22 import java.io.IOException JavaDoc;
23
24 import javax.portlet.ActionRequest;
25 import javax.portlet.ActionResponse;
26 import javax.portlet.PortletException;
27 import javax.portlet.PortletRequest;
28 import javax.portlet.PortletResponse;
29 import javax.portlet.RenderRequest;
30 import javax.portlet.RenderResponse;
31 import javax.servlet.ServletContext JavaDoc;
32 import javax.servlet.ServletRequest JavaDoc;
33 import javax.servlet.ServletResponse JavaDoc;
34
35 import org.apache.pluto.PortletContainerServices;
36 import org.apache.pluto.core.CoreUtils;
37 import org.apache.pluto.core.InternalPortletRequest;
38 import org.apache.pluto.core.InternalPortletResponse;
39 import org.apache.pluto.invoker.PortletInvoker;
40 import org.apache.pluto.om.portlet.PortletDefinition;
41 import org.apache.pluto.om.servlet.ServletDefinition;
42 import org.apache.pluto.services.log.LogService;
43 import org.apache.pluto.services.log.Logger;
44
45 public class PortletInvokerImpl implements PortletInvoker
46 {
47     private javax.servlet.ServletConfig JavaDoc servletConfig;
48     private PortletDefinition portletDefinition;
49
50     /* This Logger can be saved due to the
51      * fact that a unique instance of PortletInvoker
52      * will be used for each request. We load it
53      * lazily since we only log exceptions at
54      * this point.
55      */

56     private Logger log = null;
57
58     public PortletInvokerImpl(PortletDefinition portletDefinition,
59                               javax.servlet.ServletConfig JavaDoc servletConfig)
60     {
61         this.portletDefinition = portletDefinition;
62         this.servletConfig = servletConfig;
63     }
64
65     // org.apache.pluto.invoker.PortletInvoker implementation -------------------------------------
66
public void action(ActionRequest request, ActionResponse response) throws PortletException,IOException JavaDoc
67     {
68         invoke(request,response,org.apache.pluto.Constants.METHOD_ACTION);
69     }
70
71     public void render(RenderRequest request, RenderResponse response) throws PortletException, IOException JavaDoc
72     {
73         invoke(request,response,org.apache.pluto.Constants.METHOD_RENDER);
74     }
75
76     public void load(PortletRequest request, RenderResponse response) throws PortletException
77     {
78         try
79         {
80             invoke(request,response,org.apache.pluto.Constants.METHOD_NOOP);
81         }
82         catch (IOException JavaDoc e)
83         {
84             getLog().error("PortletInvokerImpl.load() - Error while dispatching portlet.",e);
85             throw new PortletException(e);
86         }
87     }
88     // --------------------------------------------------------------------------------------------
89

90     // additional methods -------------------------------------------------------------------------
91
/*
92      * generic method to be used called by both, action and render
93      */

94     protected void invoke(PortletRequest portletRequest, PortletResponse portletResponse, Integer JavaDoc methodID)
95     throws PortletException,IOException JavaDoc
96     {
97         InternalPortletRequest internalPortletRequest = CoreUtils.getInternalRequest(portletRequest);
98
99         InternalPortletResponse internalPortletResponse = CoreUtils.getInternalResponse(portletResponse);
100
101         // gather all required data from request and response
102
ServletRequest JavaDoc servletRequest = ((javax.servlet.http.HttpServletRequestWrapper JavaDoc)internalPortletRequest).getRequest();
103
104         ServletResponse JavaDoc servletResponse = ((javax.servlet.http.HttpServletResponseWrapper JavaDoc)internalPortletResponse).getResponse();
105
106         ServletDefinition servletDefinition = portletDefinition.getServletDefinition();
107         ServletContext JavaDoc servletContext = servletConfig.getServletContext();
108
109         javax.servlet.RequestDispatcher JavaDoc dispatcher = servletDefinition.getRequestDispatcher(servletContext);
110
111         if (dispatcher != null)
112         {
113             try
114             {
115                 servletRequest.setAttribute(org.apache.pluto.Constants.METHOD_ID, methodID);
116
117                 servletRequest.setAttribute(org.apache.pluto.Constants.PORTLET_REQUEST, portletRequest);
118                 servletRequest.setAttribute(org.apache.pluto.Constants.PORTLET_RESPONSE, portletResponse);
119
120                 dispatcher.include(servletRequest, servletResponse);
121             }
122             catch (javax.servlet.UnavailableException JavaDoc e)
123             {
124                 getLog().error("PortletInvokerImpl.invoke() - Error while dispatching portlet.",e);
125                 if (e.isPermanent())
126                 {
127                     throw new javax.portlet.UnavailableException(e.getMessage());
128                 }
129                 else
130                 {
131                     throw new javax.portlet.UnavailableException(e.getMessage(), e.getUnavailableSeconds());
132                 }
133             }
134             catch (javax.servlet.ServletException JavaDoc e)
135             {
136                 if (e.getRootCause() != null)
137                 {
138                     getLog().error("PortletInvokerImpl.render() - Error while dispatching portlet.",
139                               e.getRootCause());
140                     if (e.getRootCause() instanceof PortletException)
141                     {
142                         throw (PortletException)e.getRootCause();
143                     }
144                     else
145                     {
146                         throw new PortletException(e.getRootCause());
147                     }
148                 }
149                 else
150                 {
151                     getLog().error("PortletInvokerImpl.invoke() - Error while dispatching portlet.",
152                               e);
153                     throw new PortletException(e);
154                 }
155             }
156             finally
157             {
158                 servletRequest.removeAttribute(org.apache.pluto.Constants.METHOD_ID);
159                 servletRequest.removeAttribute(org.apache.pluto.Constants.PORTLET_REQUEST);
160                 servletRequest.removeAttribute(org.apache.pluto.Constants.PORTLET_RESPONSE);
161             }
162         }
163         else
164         {
165             getLog().error("PortletInvokerImpl.action() - Unable to find RequestDispatcher.");
166             throw new PortletException("Unable to find dispatcher for context: "+servletContext.getServletContextName());
167         }
168     }
169     // --------------------------------------------------------------------------------------------
170

171     /** Provides lazy instantiation of the Logger.
172      * This is usefull since the log is currently only
173      * used when an error occurs. B/C of this, there is
174      * no reason to retrieve the log until needed.
175      * @return
176      */

177     private Logger getLog() {
178         if(log==null) {
179         // from here forward for this Container:
180
log = ((LogService)PortletContainerServices
181                 .get(LogService.class))
182                 .getLogger(getClass());
183         }
184         return log;
185     }
186 }
187
Popular Tags