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