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.portal.servlet.filters.threaddump;
016    
017    import com.liferay.portal.kernel.servlet.TryFinallyFilter;
018    import com.liferay.portal.servlet.filters.BasePortalFilter;
019    import com.liferay.portal.util.PropsValues;
020    
021    import java.util.concurrent.Executors;
022    import java.util.concurrent.ScheduledExecutorService;
023    import java.util.concurrent.ScheduledFuture;
024    import java.util.concurrent.TimeUnit;
025    
026    import javax.servlet.http.HttpServletRequest;
027    import javax.servlet.http.HttpServletResponse;
028    
029    /**
030     * @author Shuyang Zhou
031     * @author Brian Wing Shun Chan
032     */
033    public class ThreadDumpFilter
034            extends BasePortalFilter implements TryFinallyFilter {
035    
036            @Override
037            public void doFilterFinally(
038                    HttpServletRequest request, HttpServletResponse response,
039                    Object object) {
040    
041                    ScheduledFuture<?> scheduledFuture = (ScheduledFuture<?>)object;
042    
043                    scheduledFuture.cancel(true);
044            }
045    
046            @Override
047            public Object doFilterTry(
048                    HttpServletRequest request, HttpServletResponse response) {
049    
050                    ScheduledFuture<?> scheduledFuture = _scheduledExecutorService.schedule(
051                            _threadDumper, PropsValues.THREAD_DUMP_SPEED_THRESHOLD,
052                            TimeUnit.SECONDS);
053    
054                    return scheduledFuture;
055            }
056    
057            private static final int _MAX_THREAD_DUMPERS = 5;
058    
059            private static ScheduledExecutorService _scheduledExecutorService =
060                    Executors.newScheduledThreadPool(_MAX_THREAD_DUMPERS);
061            private static ThreadDumper _threadDumper = new ThreadDumper();
062    
063    }