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.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            public void doFilterFinally(
037                    HttpServletRequest request, HttpServletResponse response,
038                    Object object) {
039    
040                    ScheduledFuture<?> scheduledFuture = (ScheduledFuture<?>)object;
041    
042                    scheduledFuture.cancel(true);
043            }
044    
045            public Object doFilterTry(
046                    HttpServletRequest request, HttpServletResponse response) {
047    
048                    ScheduledFuture<?> scheduledFuture = _scheduledExecutorService.schedule(
049                            _threadDumper, PropsValues.THREAD_DUMP_SPEED_THRESHOLD,
050                            TimeUnit.SECONDS);
051    
052                    return scheduledFuture;
053            }
054    
055            private static final int _MAX_THREAD_DUMPERS = 5;
056    
057            private static ScheduledExecutorService _scheduledExecutorService =
058                    Executors.newScheduledThreadPool(_MAX_THREAD_DUMPERS);
059            private static ThreadDumper _threadDumper = new ThreadDumper();
060    
061    }