1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.pluto.container.driver;
18
19 import java.io.IOException;
20 import java.util.Timer;
21 import java.util.TimerTask;
22
23 import javax.portlet.ActionRequest;
24 import javax.portlet.ActionResponse;
25 import javax.portlet.EventPortlet;
26 import javax.portlet.EventRequest;
27 import javax.portlet.EventResponse;
28 import javax.portlet.Portlet;
29 import javax.portlet.PortletConfig;
30 import javax.portlet.PortletException;
31 import javax.portlet.PortletRequest;
32 import javax.portlet.PortletResponse;
33 import javax.portlet.RenderRequest;
34 import javax.portlet.RenderResponse;
35 import javax.portlet.ResourceRequest;
36 import javax.portlet.ResourceResponse;
37 import javax.portlet.ResourceServingPortlet;
38 import javax.portlet.UnavailableException;
39 import javax.servlet.ServletConfig;
40 import javax.servlet.ServletContext;
41 import javax.servlet.ServletException;
42 import javax.servlet.http.HttpServlet;
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
45
46 import org.apache.pluto.container.FilterManager;
47 import org.apache.pluto.container.PortletContainerException;
48 import org.apache.pluto.container.PortletInvokerService;
49 import org.apache.pluto.container.PortletRequestContext;
50 import org.apache.pluto.container.PortletResponseContext;
51 import org.apache.pluto.container.PortletWindow;
52 import org.apache.pluto.container.om.portlet.PortletDefinition;
53
54
55
56
57
58
59
60
61 public class PortletServlet extends HttpServlet
62 {
63 private static final long serialVersionUID = -5096339022539360365L;
64
65 static class NullPortlet implements EventPortlet, ResourceServingPortlet, Portlet
66 {
67 public void processEvent(EventRequest arg0, EventResponse arg1)
68 throws PortletException, IOException
69 {
70 }
71
72 public void serveResource(ResourceRequest arg0, ResourceResponse arg1)
73 throws PortletException, IOException
74 {
75 }
76
77 public void destroy()
78 {
79 }
80
81 public void init(PortletConfig arg0) throws PortletException
82 {
83 }
84
85 public void processAction(ActionRequest arg0, ActionResponse arg1)
86 throws PortletException, IOException
87 {
88 }
89
90 public void render(RenderRequest arg0, RenderResponse arg1)
91 throws PortletException, IOException
92 {
93 }
94 }
95
96
97
98
99
100 private String portletName;
101
102
103
104
105 private Portlet portlet;
106
107
108
109
110 private DriverPortletContext portletContext;
111
112
113
114
115 private DriverPortletConfig portletConfig;
116
117
118
119
120
121 private EventPortlet eventPortlet;
122
123
124 private ResourceServingPortlet resourceServingPortlet;
125
126 private PortletContextService contextService;
127
128 private boolean started = false;
129 Timer startTimer;
130
131
132
133 public String getServletInfo()
134 {
135 return "Pluto PortletServlet [" + portletName + "]";
136 }
137
138
139
140
141
142
143
144 public void init(ServletConfig config) throws ServletException
145 {
146
147
148 super.init(config);
149
150
151 portletName = getInitParameter("portlet-name");
152
153 started = false;
154
155 startTimer = new Timer(true);
156 final ServletContext servletContext = getServletContext();
157 final ClassLoader paClassLoader = Thread.currentThread().getContextClassLoader();
158 startTimer.schedule(new TimerTask()
159 {
160 public void run()
161 {
162 synchronized(servletContext)
163 {
164 if (startTimer != null)
165 {
166 if (attemptRegistration(servletContext, paClassLoader ))
167 {
168 startTimer.cancel();
169 startTimer = null;
170 }
171 }
172 }
173 }
174 }, 1, 1);
175 }
176
177 protected boolean attemptRegistration(ServletContext context, ClassLoader paClassLoader)
178 {
179 if (PlutoServices.getServices() != null)
180 {
181 contextService = PlutoServices.getServices().getPortletContextService();
182 try
183 {
184 ServletConfig sConfig = getServletConfig();
185 if (sConfig == null)
186 {
187 String msg = "Problem obtaining servlet configuration(getServletConfig() returns null).";
188 context.log(msg);
189 return true;
190 }
191
192 String applicationName = contextService.register(sConfig);
193 started = true;
194 portletContext = contextService.getPortletContext(applicationName);
195 portletConfig = contextService.getPortletConfig(applicationName, portletName);
196
197 }
198 catch (PortletContainerException ex)
199 {
200 context.log(ex.getMessage(),ex);
201 return true;
202 }
203
204 PortletDefinition portletDD = portletConfig.getPortletDefinition();
205
206
207 try
208 {
209 Class<?> clazz = paClassLoader.loadClass((portletDD.getPortletClass()));
210 portlet = (Portlet) clazz.newInstance();
211 portlet.init(portletConfig);
212 initializeEventPortlet();
213 initializeResourceServingPortlet();
214 return true;
215 }
216 catch (Exception ex)
217 {
218 context.log(ex.getMessage(),ex);
219
220 portlet = null;
221 portletConfig = null;
222 return true;
223 }
224 }
225 return false;
226 }
227
228 public void destroy()
229 {
230 synchronized(getServletContext())
231 {
232 if ( startTimer != null )
233 {
234 startTimer.cancel();
235 startTimer = null;
236 }
237 else if ( started && portletContext != null)
238 {
239 started = false;
240 contextService.unregister(portletContext);
241 if (portlet != null)
242 {
243 try
244 {
245 portlet.destroy();
246 }
247 catch (Exception e)
248 {
249
250 }
251 portlet = null;
252 }
253 }
254 super.destroy();
255 }
256 }
257
258 protected void doGet(HttpServletRequest request,
259 HttpServletResponse response) throws ServletException, IOException
260 {
261 dispatch(request, response);
262 }
263
264 protected void doPost(HttpServletRequest request,
265 HttpServletResponse response) throws ServletException, IOException
266 {
267 dispatch(request, response);
268 }
269
270 protected void doPut(HttpServletRequest request,
271 HttpServletResponse response) throws ServletException, IOException
272 {
273 dispatch(request, response);
274 }
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295 private void dispatch(HttpServletRequest request,
296 HttpServletResponse response) throws ServletException, IOException
297 {
298 if (portlet == null)
299 {
300 throw new javax.servlet.UnavailableException("Portlet "+portletName+" unavailable");
301 }
302
303
304 Integer methodId = (Integer) request.getAttribute(PortletInvokerService.METHOD_ID);
305
306 final PortletRequest portletRequest = (PortletRequest)request.getAttribute(PortletInvokerService.PORTLET_REQUEST);
307
308 final PortletResponse portletResponse = (PortletResponse)request.getAttribute(PortletInvokerService.PORTLET_RESPONSE);
309
310 final PortletRequestContext requestContext = (PortletRequestContext)portletRequest.getAttribute(PortletInvokerService.REQUEST_CONTEXT);
311 final PortletResponseContext responseContext = (PortletResponseContext)portletRequest.getAttribute(PortletInvokerService.RESPONSE_CONTEXT);
312
313 final FilterManager filterManager = (FilterManager)request.getAttribute(PortletInvokerService.FILTER_MANAGER);
314
315 request.removeAttribute(PortletInvokerService.METHOD_ID);
316 request.removeAttribute(PortletInvokerService.PORTLET_REQUEST);
317 request.removeAttribute(PortletInvokerService.PORTLET_RESPONSE);
318 request.removeAttribute(PortletInvokerService.FILTER_MANAGER);
319
320 requestContext.init(portletConfig, getServletContext(), request, response);
321 responseContext.init(request, response);
322
323 PortletWindow window = requestContext.getPortletWindow();
324
325 PortletInvocationEvent event = new PortletInvocationEvent(portletRequest, window, methodId.intValue());
326
327 notify(event, true, null);
328
329
330
331
332 try
333 {
334
335
336 if (methodId == PortletInvokerService.METHOD_RENDER)
337 {
338 RenderRequest renderRequest = (RenderRequest) portletRequest;
339 RenderResponse renderResponse = (RenderResponse) portletResponse;
340 filterManager.processFilter(renderRequest, renderResponse,
341 portlet, portletContext);
342 }
343
344
345
346 else if (methodId == PortletInvokerService.METHOD_RESOURCE)
347 {
348 ResourceRequest resourceRequest = (ResourceRequest) portletRequest;
349 ResourceResponse resourceResponse = (ResourceResponse) portletResponse;
350 filterManager.processFilter(resourceRequest, resourceResponse,
351 resourceServingPortlet, portletContext);
352 }
353
354
355 else if (methodId == PortletInvokerService.METHOD_ACTION)
356 {
357 ActionRequest actionRequest = (ActionRequest) portletRequest;
358 ActionResponse actionResponse = (ActionResponse) portletResponse;
359 filterManager.processFilter(actionRequest, actionResponse,
360 portlet, portletContext);
361 }
362
363
364 else if (methodId == PortletInvokerService.METHOD_EVENT)
365 {
366 EventRequest eventRequest = (EventRequest) portletRequest;
367 EventResponse eventResponse = (EventResponse) portletResponse;
368 filterManager.processFilter(eventRequest, eventResponse,
369 eventPortlet, portletContext);
370 }
371
372 else if (methodId == PortletInvokerService.METHOD_ADMIN)
373 {
374 PortalAdministrationService pas = PlutoServices.getServices().getPortalAdministrationService();
375
376 for (AdministrativeRequestListener l : pas.getAdministrativeRequestListeners())
377 {
378 l.administer(portletRequest, portletResponse);
379 }
380 }
381
382
383 else if (methodId == PortletInvokerService.METHOD_LOAD)
384 {
385
386 }
387
388 notify(event, false, null);
389
390 }
391 catch (UnavailableException ex)
392 {
393
394
395
396
397
398
399
400
401 try
402 {
403 portlet.destroy();
404 }
405 catch (Throwable th)
406 {
407
408 this.getServletContext().log("Error during portlet destroy.", th);
409 }
410
411 portlet = null;
412
413
414 throw new javax.servlet.UnavailableException(ex.getMessage());
415
416 }
417 catch (PortletException ex)
418 {
419 notify(event, false, ex);
420 throw new ServletException(ex);
421
422 }
423 }
424
425 protected void notify(PortletInvocationEvent event, boolean pre, Throwable e)
426 {
427 PortalAdministrationService pas = PlutoServices.getServices().getPortalAdministrationService();
428
429 for (PortletInvocationListener listener : pas.getPortletInvocationListeners())
430 {
431 if (pre)
432 {
433 listener.onBegin(event);
434 }
435 else if (e == null)
436 {
437 listener.onEnd(event);
438 }
439 else
440 {
441 listener.onError(event, e);
442 }
443 }
444 }
445
446 private void initializeEventPortlet()
447 {
448 if (portlet instanceof EventPortlet)
449 {
450 eventPortlet = (EventPortlet) portlet;
451 }
452 else
453 {
454 eventPortlet = new NullPortlet();
455 }
456 }
457
458 private void initializeResourceServingPortlet()
459 {
460 if (portlet instanceof ResourceServingPortlet)
461 {
462 resourceServingPortlet = (ResourceServingPortlet) portlet;
463 }
464 else
465 {
466 resourceServingPortlet = new NullPortlet();
467 }
468 }
469 }