001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet;
016    
017    import com.liferay.portal.kernel.messaging.sender.SingleDestinationMessageSender;
018    import com.liferay.portal.monitoring.RequestStatus;
019    import com.liferay.portal.monitoring.statistics.DataSampleThreadLocal;
020    import com.liferay.portal.monitoring.statistics.portlet.PortletRequestDataSample;
021    import com.liferay.portal.monitoring.statistics.portlet.PortletRequestType;
022    import com.liferay.portal.util.PropsValues;
023    
024    import java.io.IOException;
025    
026    import javax.portlet.ActionRequest;
027    import javax.portlet.ActionResponse;
028    import javax.portlet.EventRequest;
029    import javax.portlet.EventResponse;
030    import javax.portlet.Portlet;
031    import javax.portlet.PortletConfig;
032    import javax.portlet.PortletContext;
033    import javax.portlet.PortletException;
034    import javax.portlet.RenderRequest;
035    import javax.portlet.RenderResponse;
036    import javax.portlet.ResourceRequest;
037    import javax.portlet.ResourceResponse;
038    
039    /**
040     * @author Michael C. Han
041     * @author Karthik Sudarshan
042     */
043    public class MonitoringPortlet implements InvokerPortlet {
044    
045            public static boolean isMonitoringPortletActionRequest() {
046                    return _monitoringPortletActionRequest;
047            }
048    
049            public static boolean isMonitoringPortletEventRequest() {
050                    return _monitoringPortletEventRequest;
051            }
052    
053            public static boolean isMonitoringPortletRenderRequest() {
054                    return _monitoringPortletRenderRequest;
055            }
056    
057            public static boolean isMonitoringPortletResourceRequest() {
058                    return _monitoringPortletResourceRequest;
059            }
060    
061            public static void setMonitoringPortletActionRequest(
062                    boolean monitoringPortletActionRequest) {
063    
064                    _monitoringPortletActionRequest = monitoringPortletActionRequest;
065            }
066    
067            public static void setMonitoringPortletEventRequest(
068                    boolean monitoringPortletEventRequest) {
069    
070                    _monitoringPortletEventRequest = monitoringPortletEventRequest;
071            }
072    
073            public static void setMonitoringPortletRenderRequest(
074                    boolean monitoringPortletRenderRequest) {
075    
076                    _monitoringPortletRenderRequest = monitoringPortletRenderRequest;
077            }
078    
079            public static void setMonitoringPortletResourceRequest(
080                    boolean monitoringPortletResourceRequest) {
081    
082                    _monitoringPortletResourceRequest = monitoringPortletResourceRequest;
083            }
084    
085            public MonitoringPortlet() {
086            }
087    
088            public MonitoringPortlet(
089                    InvokerPortlet invokerPortlet,
090                    SingleDestinationMessageSender singleDestinationMessageSender) {
091    
092                    _invokerPortlet = invokerPortlet;
093                    _singleDestinationMessageSender = singleDestinationMessageSender;
094            }
095    
096            public InvokerPortlet create(
097                            com.liferay.portal.model.Portlet portletModel, Portlet portlet,
098                            PortletConfig portletConfig, PortletContext portletContext,
099                            boolean checkAuthToken, boolean facesPortlet, boolean strutsPortlet,
100                            boolean strutsBridgePortlet)
101                    throws PortletException {
102    
103                    InvokerPortlet invokerPortlet = _invokerPortlet.create(
104                            portletModel, portlet, portletContext);
105    
106                    MonitoringPortlet monitoringPortlet = new MonitoringPortlet(
107                            invokerPortlet, _singleDestinationMessageSender);
108    
109                    monitoringPortlet.prepare(
110                            portletModel, portlet, portletConfig, portletContext,
111                            checkAuthToken, facesPortlet, strutsPortlet, strutsBridgePortlet);
112    
113                    return monitoringPortlet;
114            }
115    
116            public InvokerPortlet create(
117                            com.liferay.portal.model.Portlet portletModel, Portlet portlet,
118                            PortletContext portletContext)
119                    throws PortletException {
120    
121                    InvokerPortlet invokerPortlet = _invokerPortlet.create(
122                            portletModel, portlet, portletContext);
123    
124                    MonitoringPortlet monitoringPortlet = new MonitoringPortlet(
125                            invokerPortlet, _singleDestinationMessageSender);
126    
127                    monitoringPortlet.prepare(portletModel, portlet, portletContext);
128    
129                    return monitoringPortlet;
130            }
131    
132            public void destroy() {
133                    _invokerPortlet.destroy();
134            }
135    
136            public Integer getExpCache() {
137                    return _invokerPortlet.getExpCache();
138            }
139    
140            public Portlet getPortlet() {
141                    return _invokerPortlet.getPortlet();
142            }
143    
144            public ClassLoader getPortletClassLoader() {
145                    return _invokerPortlet.getPortletClassLoader();
146            }
147    
148            public PortletConfig getPortletConfig() {
149                    return _invokerPortlet.getPortletConfig();
150            }
151    
152            public PortletContext getPortletContext() {
153                    return _invokerPortlet.getPortletContext();
154            }
155    
156            public Portlet getPortletInstance() {
157                    return _invokerPortlet.getPortletInstance();
158            }
159    
160            public void init(PortletConfig portletConfig) throws PortletException {
161                    _invokerPortlet.init(portletConfig);
162            }
163    
164            public boolean isCheckAuthToken() {
165                    return _invokerPortlet.isCheckAuthToken();
166            }
167    
168            public boolean isDestroyable() {
169                    return _invokerPortlet.isDestroyable();
170            }
171    
172            public boolean isFacesPortlet() {
173                    return _invokerPortlet.isFacesPortlet();
174            }
175    
176            public boolean isStrutsBridgePortlet() {
177                    return _invokerPortlet.isStrutsBridgePortlet();
178            }
179    
180            public boolean isStrutsPortlet() {
181                    return _invokerPortlet.isStrutsPortlet();
182            }
183    
184            public void prepare(
185                            com.liferay.portal.model.Portlet portletModel, Portlet portlet,
186                            PortletConfig portletConfig, PortletContext portletContext,
187                            boolean checkAuthToken, boolean facesPortlet, boolean strutsPortlet,
188                            boolean strutsBridgePortlet)
189                    throws PortletException {
190    
191                    _invokerPortlet.prepare(
192                            portletModel, portlet, portletConfig, portletContext,
193                            checkAuthToken, facesPortlet, strutsPortlet, strutsBridgePortlet);
194            }
195    
196            public void prepare(
197                            com.liferay.portal.model.Portlet portletModel, Portlet portlet,
198                            PortletContext portletContext)
199                    throws PortletException {
200    
201                    _invokerPortlet.prepare(portletModel, portlet, portletContext);
202            }
203    
204            public void processAction(
205                            ActionRequest actionRequest, ActionResponse actionResponse)
206                    throws IOException, PortletException {
207    
208                    PortletRequestDataSample portletRequestDataSample = null;
209    
210                    try {
211                            if (_monitoringPortletActionRequest) {
212                                    portletRequestDataSample = new PortletRequestDataSample(
213                                            PortletRequestType.ACTION, actionRequest, actionResponse);
214    
215                                    portletRequestDataSample.prepare();
216                            }
217    
218                            _invokerPortlet.processAction(actionRequest, actionResponse);
219    
220                            if (_monitoringPortletActionRequest) {
221                                    portletRequestDataSample.capture(RequestStatus.SUCCESS);
222                            }
223                    }
224                    catch (Exception e) {
225                            _processException(
226                                    _monitoringPortletActionRequest, portletRequestDataSample, e);
227                    }
228                    finally {
229                            if (portletRequestDataSample != null) {
230                                    _singleDestinationMessageSender.send(portletRequestDataSample);
231    
232                                    DataSampleThreadLocal.addDataSample(portletRequestDataSample);
233                            }
234                    }
235            }
236    
237            public void processEvent(
238                            EventRequest eventRequest, EventResponse eventResponse)
239                    throws IOException, PortletException {
240    
241                    PortletRequestDataSample portletRequestDataSample = null;
242    
243                    try {
244                            if (_monitoringPortletEventRequest) {
245                                    portletRequestDataSample = new PortletRequestDataSample(
246                                            PortletRequestType.EVENT, eventRequest, eventResponse);
247    
248                                    portletRequestDataSample.prepare();
249                            }
250    
251                            _invokerPortlet.processEvent(eventRequest, eventResponse);
252    
253                            if (_monitoringPortletEventRequest) {
254                                    portletRequestDataSample.capture(RequestStatus.SUCCESS);
255                            }
256                    }
257                    catch (Exception e) {
258                            _processException(
259                                    _monitoringPortletEventRequest, portletRequestDataSample, e);
260                    }
261                    finally {
262                            if (portletRequestDataSample != null) {
263                                    _singleDestinationMessageSender.send(portletRequestDataSample);
264    
265                                    DataSampleThreadLocal.addDataSample(portletRequestDataSample);
266                            }
267                    }
268            }
269    
270            public void render(
271                            RenderRequest renderRequest, RenderResponse renderResponse)
272                    throws IOException, PortletException {
273    
274                    PortletRequestDataSample portletRequestDataSample = null;
275    
276                    try {
277                            if (_monitoringPortletRenderRequest) {
278                                    portletRequestDataSample = new PortletRequestDataSample(
279                                            PortletRequestType.RENDER, renderRequest, renderResponse);
280    
281                                    portletRequestDataSample.prepare();
282                            }
283    
284                            _invokerPortlet.render(renderRequest, renderResponse);
285    
286                            if (_monitoringPortletRenderRequest) {
287                                    portletRequestDataSample.capture(RequestStatus.SUCCESS);
288                            }
289                    }
290                    catch (Exception e) {
291                            _processException(
292                                    _monitoringPortletRenderRequest, portletRequestDataSample, e);
293                    }
294                    finally {
295                            if (portletRequestDataSample != null) {
296                                    _singleDestinationMessageSender.send(portletRequestDataSample);
297    
298                                    DataSampleThreadLocal.addDataSample(portletRequestDataSample);
299                            }
300                    }
301            }
302    
303            public void serveResource(
304                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
305                    throws IOException, PortletException {
306    
307                    PortletRequestDataSample portletRequestDataSample = null;
308    
309                    try {
310                            if (_monitoringPortletResourceRequest) {
311                                    portletRequestDataSample = new PortletRequestDataSample(
312                                            PortletRequestType.RESOURCE, resourceRequest,
313                                            resourceResponse);
314    
315                                    portletRequestDataSample.prepare();
316                            }
317    
318                            _invokerPortlet.serveResource(resourceRequest, resourceResponse);
319    
320                            if (_monitoringPortletResourceRequest) {
321                                    portletRequestDataSample.capture(RequestStatus.SUCCESS);
322                            }
323                    }
324                    catch (Exception e) {
325                            _processException(
326                                    _monitoringPortletResourceRequest, portletRequestDataSample, e);
327                    }
328                    finally {
329                            if (portletRequestDataSample != null) {
330                                    _singleDestinationMessageSender.send(portletRequestDataSample);
331    
332                                    DataSampleThreadLocal.addDataSample(portletRequestDataSample);
333                            }
334                    }
335            }
336    
337            public void setInvokerPortlet(InvokerPortlet invokerPortlet) {
338                    _invokerPortlet = invokerPortlet;
339            }
340    
341            public void setPortletFilters() throws PortletException {
342                    _invokerPortlet.setPortletFilters();
343            }
344    
345            public void setSingleDestinationMessageSender(
346                    SingleDestinationMessageSender singleDestinationMessageSender) {
347    
348                    _singleDestinationMessageSender = singleDestinationMessageSender;
349            }
350    
351            private void _processException(
352                            boolean monitoringPortletRequest,
353                            PortletRequestDataSample portletRequestDataSample, Exception e)
354                    throws IOException, PortletException {
355    
356                    if (monitoringPortletRequest) {
357                            portletRequestDataSample.capture(RequestStatus.ERROR);
358                    }
359    
360                    if (e instanceof IOException) {
361                            throw (IOException)e;
362                    }
363                    else if (e instanceof PortletException) {
364                            throw (PortletException)e;
365                    }
366                    else {
367                            throw new PortletException("Unable to process portlet", e);
368                    }
369            }
370    
371            private static boolean _monitoringPortletActionRequest =
372                    PropsValues.MONITORING_PORTLET_ACTION_REQUEST;
373            private static boolean _monitoringPortletEventRequest =
374                    PropsValues.MONITORING_PORTLET_EVENT_REQUEST;
375            private static boolean _monitoringPortletRenderRequest =
376                    PropsValues.MONITORING_PORTLET_RENDER_REQUEST;
377            private static boolean _monitoringPortletResourceRequest =
378                    PropsValues.MONITORING_PORTLET_RESOURCE_REQUEST;
379    
380            private InvokerPortlet _invokerPortlet;
381            private SingleDestinationMessageSender _singleDestinationMessageSender;
382    
383    }