001    /**
002     * Copyright (c) 2000-2013 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.kernel.monitoring.RequestStatus;
019    import com.liferay.portal.kernel.monitoring.statistics.DataSampleThreadLocal;
020    import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
021    import com.liferay.portal.monitoring.statistics.portlet.PortletRequestDataSample;
022    import com.liferay.portal.monitoring.statistics.portlet.PortletRequestType;
023    import com.liferay.portal.util.PropsValues;
024    
025    import java.io.IOException;
026    
027    import javax.portlet.ActionRequest;
028    import javax.portlet.ActionResponse;
029    import javax.portlet.EventRequest;
030    import javax.portlet.EventResponse;
031    import javax.portlet.Portlet;
032    import javax.portlet.PortletConfig;
033    import javax.portlet.PortletContext;
034    import javax.portlet.PortletException;
035    import javax.portlet.RenderRequest;
036    import javax.portlet.RenderResponse;
037    import javax.portlet.ResourceRequest;
038    import javax.portlet.ResourceResponse;
039    
040    /**
041     * @author Michael C. Han
042     * @author Karthik Sudarshan
043     * @author Raymond Aug??
044     */
045    public class MonitoringPortlet implements InvokerPortlet {
046    
047            public static boolean isMonitoringPortletActionRequest() {
048                    return _monitoringPortletActionRequest;
049            }
050    
051            public static boolean isMonitoringPortletEventRequest() {
052                    return _monitoringPortletEventRequest;
053            }
054    
055            public static boolean isMonitoringPortletRenderRequest() {
056                    return _monitoringPortletRenderRequest;
057            }
058    
059            public static boolean isMonitoringPortletResourceRequest() {
060                    return _monitoringPortletResourceRequest;
061            }
062    
063            public static void setMonitoringPortletActionRequest(
064                    boolean monitoringPortletActionRequest) {
065    
066                    _monitoringPortletActionRequest = monitoringPortletActionRequest;
067            }
068    
069            public static void setMonitoringPortletEventRequest(
070                    boolean monitoringPortletEventRequest) {
071    
072                    _monitoringPortletEventRequest = monitoringPortletEventRequest;
073            }
074    
075            public static void setMonitoringPortletRenderRequest(
076                    boolean monitoringPortletRenderRequest) {
077    
078                    _monitoringPortletRenderRequest = monitoringPortletRenderRequest;
079            }
080    
081            public static void setMonitoringPortletResourceRequest(
082                    boolean monitoringPortletResourceRequest) {
083    
084                    _monitoringPortletResourceRequest = monitoringPortletResourceRequest;
085            }
086    
087            public MonitoringPortlet() {
088            }
089    
090            public MonitoringPortlet(
091                    InvokerPortlet invokerPortlet,
092                    SingleDestinationMessageSender singleDestinationMessageSender) {
093    
094                    _invokerPortlet = invokerPortlet;
095                    _singleDestinationMessageSender = singleDestinationMessageSender;
096            }
097    
098            @Override
099            public void destroy() {
100                    _invokerPortlet.destroy();
101            }
102    
103            @Override
104            public Integer getExpCache() {
105                    return _invokerPortlet.getExpCache();
106            }
107    
108            @Override
109            public Portlet getPortlet() {
110                    return _invokerPortlet.getPortlet();
111            }
112    
113            @Override
114            public ClassLoader getPortletClassLoader() {
115                    return _invokerPortlet.getPortletClassLoader();
116            }
117    
118            @Override
119            public PortletConfig getPortletConfig() {
120                    return _invokerPortlet.getPortletConfig();
121            }
122    
123            @Override
124            public PortletContext getPortletContext() {
125                    return _invokerPortlet.getPortletContext();
126            }
127    
128            @Override
129            public Portlet getPortletInstance() {
130                    return _invokerPortlet.getPortletInstance();
131            }
132    
133            @Override
134            public void init(PortletConfig portletConfig) throws PortletException {
135                    LiferayPortletConfig liferayPortletConfig =
136                            (LiferayPortletConfig)portletConfig;
137    
138                    _invokerPortlet.init(liferayPortletConfig);
139    
140                    com.liferay.portal.model.Portlet portletModel =
141                            liferayPortletConfig.getPortlet();
142    
143                    _actionTimeout = portletModel.getActionTimeout();
144                    _renderTimeout = portletModel.getRenderTimeout();
145            }
146    
147            @Override
148            public boolean isCheckAuthToken() {
149                    return _invokerPortlet.isCheckAuthToken();
150            }
151    
152            @Override
153            public boolean isFacesPortlet() {
154                    return _invokerPortlet.isFacesPortlet();
155            }
156    
157            @Override
158            public boolean isStrutsBridgePortlet() {
159                    return _invokerPortlet.isStrutsBridgePortlet();
160            }
161    
162            @Override
163            public boolean isStrutsPortlet() {
164                    return _invokerPortlet.isStrutsPortlet();
165            }
166    
167            @Override
168            public void processAction(
169                            ActionRequest actionRequest, ActionResponse actionResponse)
170                    throws IOException, PortletException {
171    
172                    PortletRequestDataSample portletRequestDataSample = null;
173    
174                    try {
175                            if (_monitoringPortletActionRequest) {
176                                    portletRequestDataSample = new PortletRequestDataSample(
177                                            PortletRequestType.ACTION, actionRequest, actionResponse);
178    
179                                    portletRequestDataSample.setTimeout(_actionTimeout);
180    
181                                    portletRequestDataSample.prepare();
182                            }
183    
184                            _invokerPortlet.processAction(actionRequest, actionResponse);
185    
186                            if (_monitoringPortletActionRequest) {
187                                    portletRequestDataSample.capture(RequestStatus.SUCCESS);
188                            }
189                    }
190                    catch (Exception e) {
191                            _processException(
192                                    _monitoringPortletActionRequest, portletRequestDataSample, e);
193                    }
194                    finally {
195                            if (portletRequestDataSample != null) {
196                                    _singleDestinationMessageSender.send(portletRequestDataSample);
197    
198                                    DataSampleThreadLocal.addDataSample(portletRequestDataSample);
199                            }
200                    }
201            }
202    
203            @Override
204            public void processEvent(
205                            EventRequest eventRequest, EventResponse eventResponse)
206                    throws IOException, PortletException {
207    
208                    PortletRequestDataSample portletRequestDataSample = null;
209    
210                    try {
211                            if (_monitoringPortletEventRequest) {
212                                    portletRequestDataSample = new PortletRequestDataSample(
213                                            PortletRequestType.EVENT, eventRequest, eventResponse);
214    
215                                    portletRequestDataSample.prepare();
216                            }
217    
218                            _invokerPortlet.processEvent(eventRequest, eventResponse);
219    
220                            if (_monitoringPortletEventRequest) {
221                                    portletRequestDataSample.capture(RequestStatus.SUCCESS);
222                            }
223                    }
224                    catch (Exception e) {
225                            _processException(
226                                    _monitoringPortletEventRequest, portletRequestDataSample, e);
227                    }
228                    finally {
229                            if (portletRequestDataSample != null) {
230                                    _singleDestinationMessageSender.send(portletRequestDataSample);
231    
232                                    DataSampleThreadLocal.addDataSample(portletRequestDataSample);
233                            }
234                    }
235            }
236    
237            @Override
238            public void render(
239                            RenderRequest renderRequest, RenderResponse renderResponse)
240                    throws IOException, PortletException {
241    
242                    PortletRequestDataSample portletRequestDataSample = null;
243    
244                    try {
245                            if (_monitoringPortletRenderRequest) {
246                                    portletRequestDataSample = new PortletRequestDataSample(
247                                            PortletRequestType.RENDER, renderRequest, renderResponse);
248    
249                                    portletRequestDataSample.setTimeout(_renderTimeout);
250    
251                                    portletRequestDataSample.prepare();
252                            }
253    
254                            _invokerPortlet.render(renderRequest, renderResponse);
255    
256                            if (_monitoringPortletRenderRequest) {
257                                    portletRequestDataSample.capture(RequestStatus.SUCCESS);
258                            }
259                    }
260                    catch (Exception e) {
261                            _processException(
262                                    _monitoringPortletRenderRequest, portletRequestDataSample, e);
263                    }
264                    finally {
265                            if (portletRequestDataSample != null) {
266                                    _singleDestinationMessageSender.send(portletRequestDataSample);
267    
268                                    DataSampleThreadLocal.addDataSample(portletRequestDataSample);
269                            }
270                    }
271            }
272    
273            @Override
274            public void serveResource(
275                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
276                    throws IOException, PortletException {
277    
278                    PortletRequestDataSample portletRequestDataSample = null;
279    
280                    try {
281                            if (_monitoringPortletResourceRequest) {
282                                    portletRequestDataSample = new PortletRequestDataSample(
283                                            PortletRequestType.RESOURCE, resourceRequest,
284                                            resourceResponse);
285    
286                                    portletRequestDataSample.prepare();
287                            }
288    
289                            _invokerPortlet.serveResource(resourceRequest, resourceResponse);
290    
291                            if (_monitoringPortletResourceRequest) {
292                                    portletRequestDataSample.capture(RequestStatus.SUCCESS);
293                            }
294                    }
295                    catch (Exception e) {
296                            _processException(
297                                    _monitoringPortletResourceRequest, portletRequestDataSample, e);
298                    }
299                    finally {
300                            if (portletRequestDataSample != null) {
301                                    _singleDestinationMessageSender.send(portletRequestDataSample);
302    
303                                    DataSampleThreadLocal.addDataSample(portletRequestDataSample);
304                            }
305                    }
306            }
307    
308            public void setInvokerPortlet(InvokerPortlet invokerPortlet) {
309                    _invokerPortlet = invokerPortlet;
310            }
311    
312            @Override
313            public void setPortletFilters() throws PortletException {
314                    _invokerPortlet.setPortletFilters();
315            }
316    
317            public void setSingleDestinationMessageSender(
318                    SingleDestinationMessageSender singleDestinationMessageSender) {
319    
320                    _singleDestinationMessageSender = singleDestinationMessageSender;
321            }
322    
323            private void _processException(
324                            boolean monitoringPortletRequest,
325                            PortletRequestDataSample portletRequestDataSample, Exception e)
326                    throws IOException, PortletException {
327    
328                    if (monitoringPortletRequest) {
329                            portletRequestDataSample.capture(RequestStatus.ERROR);
330                    }
331    
332                    if (e instanceof IOException) {
333                            throw (IOException)e;
334                    }
335                    else if (e instanceof PortletException) {
336                            throw (PortletException)e;
337                    }
338                    else {
339                            throw new PortletException("Unable to process portlet", e);
340                    }
341            }
342    
343            private static boolean _monitoringPortletActionRequest =
344                    PropsValues.MONITORING_PORTLET_ACTION_REQUEST;
345            private static boolean _monitoringPortletEventRequest =
346                    PropsValues.MONITORING_PORTLET_EVENT_REQUEST;
347            private static boolean _monitoringPortletRenderRequest =
348                    PropsValues.MONITORING_PORTLET_RENDER_REQUEST;
349            private static boolean _monitoringPortletResourceRequest =
350                    PropsValues.MONITORING_PORTLET_RESOURCE_REQUEST;
351    
352            private long _actionTimeout;
353            private InvokerPortlet _invokerPortlet;
354            private long _renderTimeout;
355            private SingleDestinationMessageSender _singleDestinationMessageSender;
356    
357    }