KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > pluto > core > PortletServlet


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.core;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Enumeration JavaDoc;
24
25 import javax.portlet.ActionRequest;
26 import javax.portlet.ActionResponse;
27 import javax.portlet.Portlet;
28 import javax.portlet.PortletConfig;
29 import javax.portlet.PortletContext;
30 import javax.portlet.PortletException;
31 import javax.portlet.RenderRequest;
32 import javax.portlet.RenderResponse;
33 import javax.servlet.ServletConfig JavaDoc;
34 import javax.servlet.ServletContext JavaDoc;
35 import javax.servlet.ServletException JavaDoc;
36 import javax.servlet.ServletRequest JavaDoc;
37 import javax.servlet.ServletResponse JavaDoc;
38 import javax.servlet.http.HttpServlet JavaDoc;
39 import javax.servlet.http.HttpServletRequest JavaDoc;
40 import javax.servlet.http.HttpServletResponse JavaDoc;
41
42 import org.apache.pluto.factory.PortletObjectAccess;
43 import org.apache.pluto.om.ControllerObjectAccess;
44 import org.apache.pluto.om.portlet.PortletDefinition;
45 import org.apache.pluto.om.portlet.PortletDefinitionCtrl;
46 import org.apache.pluto.services.information.InformationProviderAccess;
47
48 public class PortletServlet extends HttpServlet JavaDoc
49 {
50
51     private boolean portletInitialized = false;
52     private Portlet portletClass = null;
53
54     private PortletContext portletContext;
55     private PortletConfig portletConfig;
56
57     // HttpServlet --------------------------------------------------------------------------------
58
public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc
59     {
60         super.init(config);
61         portletInitialized = false;
62
63         String JavaDoc classString = config.getInitParameter("portlet-class");
64         try
65         {
66             portletClass = (Portlet)Thread.currentThread().getContextClassLoader().loadClass(classString).newInstance();
67         }
68         catch (ClassNotFoundException JavaDoc e)
69         {
70             throw new ServletException JavaDoc(e);
71         }
72         catch (IllegalAccessException JavaDoc e)
73         {
74             throw new ServletException JavaDoc(e);
75         }
76         catch (InstantiationException JavaDoc e)
77         {
78             throw new ServletException JavaDoc(e);
79         }
80
81         // we can create the objects here and call init as we forbid the
82
// <load-on-startup>. This flag has to be false
83

84         // TBD - I should not use
85
String JavaDoc portletGUID = config.getInitParameter("portlet-guid");
86         PortletDefinition portletDefinition =
87         InformationProviderAccess.getStaticProvider().getPortletDefinition(org.apache.pluto.util.ObjectIDAccess.createObjectID(portletGUID));
88         if (portletDefinition==null)
89         {
90             throw new ServletException JavaDoc("portlet definition not found from GUID: " + portletGUID);
91         }
92         else
93         {
94             PortletDefinitionCtrl portletDefCtrl = (PortletDefinitionCtrl)ControllerObjectAccess.get(portletDefinition);
95             portletDefCtrl.setPortletClassLoader(Thread.currentThread().getContextClassLoader());
96         }
97
98         portletContext = PortletObjectAccess.getPortletContext(config.getServletContext(),
99                                                                portletDefinition.getPortletApplicationDefinition());
100         portletConfig = PortletObjectAccess.getPortletConfig(config,
101                                                              portletContext,
102                                                              portletDefinition);
103
104         try
105         {
106             portletClass.init(portletConfig);
107         }
108         catch (PortletException e)
109         {
110             throw new ServletException JavaDoc(e);
111         }
112
113         portletInitialized = true;
114
115     }
116
117     public void init() throws ServletException JavaDoc
118     {
119     }
120
121     public final ServletConfig JavaDoc getServletConfig ()
122     {
123         return super.getServletConfig();
124     }
125
126     public final String JavaDoc getInitParameter(String JavaDoc name)
127     {
128         return getServletConfig().getInitParameter(name);
129     }
130
131     public final Enumeration JavaDoc getInitParameterNames()
132     {
133         return getServletConfig().getInitParameterNames();
134     }
135
136     public ServletContext JavaDoc getServletContext()
137     {
138         return getServletConfig().getServletContext();
139     }
140
141     protected long getLastModified(HttpServletRequest JavaDoc req)
142     {
143         return -1;
144     }
145
146     public String JavaDoc getServletInfo()
147     {
148         return "";
149     }
150
151     public final void service(ServletRequest JavaDoc request, ServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc
152     {
153         super.service(request,response);
154     }
155
156     protected void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp) throws ServletException JavaDoc, IOException JavaDoc
157     {
158         dispatch(req,resp);
159     }
160
161     protected void doPost(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp) throws ServletException JavaDoc, IOException JavaDoc
162     {
163         dispatch(req,resp);
164     }
165
166     protected void doPut(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp) throws ServletException JavaDoc, IOException JavaDoc
167     {
168         dispatch(req,resp);
169     }
170
171     protected void doDelete(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp) throws ServletException JavaDoc, IOException JavaDoc
172     {
173         super.doDelete(req,resp);
174     }
175
176     protected void doOptions(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp) throws ServletException JavaDoc, IOException JavaDoc
177     {
178         super.doOptions(req,resp);
179     }
180
181     protected void doTrace(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp) throws ServletException JavaDoc, IOException JavaDoc
182     {
183         super.doTrace(req,resp);
184     }
185
186     public void destroy()
187     {
188         if (portletClass != null)
189         {
190             portletClass.destroy();
191         }
192         super.destroy();
193     }
194     // --------------------------------------------------------------------------------------------
195

196     // internal methods ---------------------------------------------------------------------------
197
private void dispatch(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc
198     {
199         if (!portletInitialized)
200         {
201             throw new ServletException JavaDoc("this portlet uses the <load-on-startup> flag. You have to turn it off");
202         }
203         try
204         {
205             // fill attribute, so that JSPs/servlets can access the config
206
request.setAttribute(org.apache.pluto.Constants.PORTLET_CONFIG, portletConfig);
207
208             Integer JavaDoc method_id = (Integer JavaDoc)request.getAttribute(org.apache.pluto.Constants.METHOD_ID);
209             if (method_id == org.apache.pluto.Constants.METHOD_RENDER)
210             {
211                 RenderRequest renderRequest = (RenderRequest)request.getAttribute(org.apache.pluto.Constants.PORTLET_REQUEST);
212                 RenderResponse renderResponse = (RenderResponse)request.getAttribute(org.apache.pluto.Constants.PORTLET_RESPONSE);
213
214                 // prepare container objects to run in this webModule
215
prepareRenderRequest(renderRequest, request);
216                 prepareRenderResponse(renderResponse, request, response);
217
218                 portletClass.render(renderRequest,renderResponse);
219             }
220             else if (method_id==org.apache.pluto.Constants.METHOD_ACTION)
221             {
222                 ActionRequest actionRequest = (ActionRequest)request.getAttribute(org.apache.pluto.Constants.PORTLET_REQUEST);
223                 ActionResponse actionResponse = (ActionResponse)request.getAttribute(org.apache.pluto.Constants.PORTLET_RESPONSE);
224
225                 // prepare container objects to run in this webModule
226
prepareActionRequest(actionRequest, request);
227                 prepareActionResponse(actionResponse, request, response);
228
229                 portletClass.processAction(actionRequest,actionResponse);
230             }
231             else if (method_id == org.apache.pluto.Constants.METHOD_NOOP)
232             {
233                 //nothing to do
234
}
235
236         }
237         catch (javax.portlet.UnavailableException e)
238         {
239             /*if (e.isPermanent()) {
240                 throw new javax.servlet.UnavailableException(e.getMessage());
241             } else {
242                 throw new javax.servlet.UnavailableException(e.getMessage(), e.getUnavailableSeconds());
243             }*/

244
245             // destroy isn't called by Tomcat, so we have to fix it
246
try
247             {
248                 portletClass.destroy();
249             }
250             catch (Throwable JavaDoc t)
251             {
252                 // don't care for Exception
253
}
254
255             // handle everything as permanently for now
256
throw new javax.servlet.UnavailableException JavaDoc(e.getMessage());
257         }
258         catch (PortletException e)
259         {
260             throw new ServletException JavaDoc(e);
261         }
262         finally
263         {
264             request.removeAttribute(org.apache.pluto.Constants.PORTLET_CONFIG);
265         }
266
267     }
268
269     private void prepareActionRequest(ActionRequest portletRequest,
270                                       HttpServletRequest JavaDoc servletRequest)
271     {
272         InternalPortletRequest internalPortletRequest =
273         CoreUtils.getInternalRequest(portletRequest);
274
275         internalPortletRequest.lateInit(servletRequest);
276     }
277
278     private void prepareRenderRequest(RenderRequest portletRequest,
279                                       HttpServletRequest JavaDoc servletRequest)
280     {
281         InternalPortletRequest internalPortletRequest =
282         CoreUtils.getInternalRequest(portletRequest);
283
284         internalPortletRequest.lateInit(servletRequest);
285     }
286
287     private void prepareRenderResponse(RenderResponse portletResponse,
288                                        HttpServletRequest JavaDoc servletRequest,
289                                        HttpServletResponse JavaDoc servletResponse)
290     {
291         InternalPortletResponse internalPortletResponse =
292         CoreUtils.getInternalResponse(portletResponse);
293
294         internalPortletResponse.lateInit(servletRequest, servletResponse);
295     }
296
297     private void prepareActionResponse(ActionResponse portletResponse,
298                                        HttpServletRequest JavaDoc servletRequest,
299                                        HttpServletResponse JavaDoc servletResponse)
300     {
301         InternalPortletResponse internalPortletResponse =
302         CoreUtils.getInternalResponse(portletResponse);
303
304         internalPortletResponse.lateInit(servletRequest, servletResponse);
305     }
306     // --------------------------------------------------------------------------------------------
307
}
308
Popular Tags