001    /**
002     * Copyright (c) 2000-2012 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.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 void destroy() {
097                    _invokerPortlet.destroy();
098            }
099    
100            public Integer getExpCache() {
101                    return _invokerPortlet.getExpCache();
102            }
103    
104            public Portlet getPortlet() {
105                    return _invokerPortlet.getPortlet();
106            }
107    
108            public ClassLoader getPortletClassLoader() {
109                    return _invokerPortlet.getPortletClassLoader();
110            }
111    
112            public PortletConfig getPortletConfig() {
113                    return _invokerPortlet.getPortletConfig();
114            }
115    
116            public PortletContext getPortletContext() {
117                    return _invokerPortlet.getPortletContext();
118            }
119    
120            public Portlet getPortletInstance() {
121                    return _invokerPortlet.getPortletInstance();
122            }
123    
124            public void init(PortletConfig portletConfig) throws PortletException {
125                    PortletConfigImpl portletConfigImpl = (PortletConfigImpl)portletConfig;
126    
127                    _invokerPortlet.init(portletConfigImpl);
128    
129                    com.liferay.portal.model.Portlet portletModel =
130                            portletConfigImpl.getPortlet();
131    
132                    _actionTimeout = portletModel.getActionTimeout();
133                    _renderTimeout = portletModel.getRenderTimeout();
134            }
135    
136            public boolean isCheckAuthToken() {
137                    return _invokerPortlet.isCheckAuthToken();
138            }
139    
140            public boolean isFacesPortlet() {
141                    return _invokerPortlet.isFacesPortlet();
142            }
143    
144            public boolean isStrutsBridgePortlet() {
145                    return _invokerPortlet.isStrutsBridgePortlet();
146            }
147    
148            public boolean isStrutsPortlet() {
149                    return _invokerPortlet.isStrutsPortlet();
150            }
151    
152            public void processAction(
153                            ActionRequest actionRequest, ActionResponse actionResponse)
154                    throws IOException, PortletException {
155    
156                    PortletRequestDataSample portletRequestDataSample = null;
157    
158                    try {
159                            if (_monitoringPortletActionRequest) {
160                                    portletRequestDataSample = new PortletRequestDataSample(
161                                            PortletRequestType.ACTION, actionRequest, actionResponse);
162    
163                                    portletRequestDataSample.setTimeout(_actionTimeout);
164    
165                                    portletRequestDataSample.prepare();
166                            }
167    
168                            _invokerPortlet.processAction(actionRequest, actionResponse);
169    
170                            if (_monitoringPortletActionRequest) {
171                                    portletRequestDataSample.capture(RequestStatus.SUCCESS);
172                            }
173                    }
174                    catch (Exception e) {
175                            _processException(
176                                    _monitoringPortletActionRequest, portletRequestDataSample, e);
177                    }
178                    finally {
179                            if (portletRequestDataSample != null) {
180                                    _singleDestinationMessageSender.send(portletRequestDataSample);
181    
182                                    DataSampleThreadLocal.addDataSample(portletRequestDataSample);
183                            }
184                    }
185            }
186    
187            public void processEvent(
188                            EventRequest eventRequest, EventResponse eventResponse)
189                    throws IOException, PortletException {
190    
191                    PortletRequestDataSample portletRequestDataSample = null;
192    
193                    try {
194                            if (_monitoringPortletEventRequest) {
195                                    portletRequestDataSample = new PortletRequestDataSample(
196                                            PortletRequestType.EVENT, eventRequest, eventResponse);
197    
198                                    portletRequestDataSample.prepare();
199                            }
200    
201                            _invokerPortlet.processEvent(eventRequest, eventResponse);
202    
203                            if (_monitoringPortletEventRequest) {
204                                    portletRequestDataSample.capture(RequestStatus.SUCCESS);
205                            }
206                    }
207                    catch (Exception e) {
208                            _processException(
209                                    _monitoringPortletEventRequest, portletRequestDataSample, e);
210                    }
211                    finally {
212                            if (portletRequestDataSample != null) {
213                                    _singleDestinationMessageSender.send(portletRequestDataSample);
214    
215                                    DataSampleThreadLocal.addDataSample(portletRequestDataSample);
216                            }
217                    }
218            }
219    
220            public void render(
221                            RenderRequest renderRequest, RenderResponse renderResponse)
222                    throws IOException, PortletException {
223    
224                    PortletRequestDataSample portletRequestDataSample = null;
225    
226                    try {
227                            if (_monitoringPortletRenderRequest) {
228                                    portletRequestDataSample = new PortletRequestDataSample(
229                                            PortletRequestType.RENDER, renderRequest, renderResponse);
230    
231                                    portletRequestDataSample.setTimeout(_renderTimeout);
232    
233                                    portletRequestDataSample.prepare();
234                            }
235    
236                            _invokerPortlet.render(renderRequest, renderResponse);
237    
238                            if (_monitoringPortletRenderRequest) {
239                                    portletRequestDataSample.capture(RequestStatus.SUCCESS);
240                            }
241                    }
242                    catch (Exception e) {
243                            _processException(
244                                    _monitoringPortletRenderRequest, portletRequestDataSample, e);
245                    }
246                    finally {
247                            if (portletRequestDataSample != null) {
248                                    _singleDestinationMessageSender.send(portletRequestDataSample);
249    
250                                    DataSampleThreadLocal.addDataSample(portletRequestDataSample);
251                            }
252                    }
253            }
254    
255            public void serveResource(
256                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
257                    throws IOException, PortletException {
258    
259                    PortletRequestDataSample portletRequestDataSample = null;
260    
261                    try {
262                            if (_monitoringPortletResourceRequest) {
263                                    portletRequestDataSample = new PortletRequestDataSample(
264                                            PortletRequestType.RESOURCE, resourceRequest,
265                                            resourceResponse);
266    
267                                    portletRequestDataSample.prepare();
268                            }
269    
270                            _invokerPortlet.serveResource(resourceRequest, resourceResponse);
271    
272                            if (_monitoringPortletResourceRequest) {
273                                    portletRequestDataSample.capture(RequestStatus.SUCCESS);
274                            }
275                    }
276                    catch (Exception e) {
277                            _processException(
278                                    _monitoringPortletResourceRequest, portletRequestDataSample, e);
279                    }
280                    finally {
281                            if (portletRequestDataSample != null) {
282                                    _singleDestinationMessageSender.send(portletRequestDataSample);
283    
284                                    DataSampleThreadLocal.addDataSample(portletRequestDataSample);
285                            }
286                    }
287            }
288    
289            public void setInvokerPortlet(InvokerPortlet invokerPortlet) {
290                    _invokerPortlet = invokerPortlet;
291            }
292    
293            public void setPortletFilters() throws PortletException {
294                    _invokerPortlet.setPortletFilters();
295            }
296    
297            public void setSingleDestinationMessageSender(
298                    SingleDestinationMessageSender singleDestinationMessageSender) {
299    
300                    _singleDestinationMessageSender = singleDestinationMessageSender;
301            }
302    
303            private void _processException(
304                            boolean monitoringPortletRequest,
305                            PortletRequestDataSample portletRequestDataSample, Exception e)
306                    throws IOException, PortletException {
307    
308                    if (monitoringPortletRequest) {
309                            portletRequestDataSample.capture(RequestStatus.ERROR);
310                    }
311    
312                    if (e instanceof IOException) {
313                            throw (IOException)e;
314                    }
315                    else if (e instanceof PortletException) {
316                            throw (PortletException)e;
317                    }
318                    else {
319                            throw new PortletException("Unable to process portlet", e);
320                    }
321            }
322    
323            private static boolean _monitoringPortletActionRequest =
324                    PropsValues.MONITORING_PORTLET_ACTION_REQUEST;
325            private static boolean _monitoringPortletEventRequest =
326                    PropsValues.MONITORING_PORTLET_EVENT_REQUEST;
327            private static boolean _monitoringPortletRenderRequest =
328                    PropsValues.MONITORING_PORTLET_RENDER_REQUEST;
329            private static boolean _monitoringPortletResourceRequest =
330                    PropsValues.MONITORING_PORTLET_RESOURCE_REQUEST;
331    
332            private long _actionTimeout;
333            private InvokerPortlet _invokerPortlet;
334            private long _renderTimeout;
335            private SingleDestinationMessageSender _singleDestinationMessageSender;
336    
337    }