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.util.axis;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.servlet.BaseFilter;
020    import com.liferay.portal.kernel.util.ReflectionUtil;
021    
022    import java.lang.reflect.Field;
023    
024    import javax.servlet.FilterChain;
025    import javax.servlet.http.HttpServletRequest;
026    import javax.servlet.http.HttpServletResponse;
027    
028    import org.apache.axis.utils.cache.MethodCache;
029    
030    /**
031     * @author Shuyang Zhou
032     * @author Brian Wing Shun Chan
033     */
034    public class AxisCleanUpFilter extends BaseFilter {
035    
036            @Override
037            protected Log getLog() {
038                    return _log;
039            }
040    
041            @Override
042            protected void processFilter(
043                            HttpServletRequest request, HttpServletResponse response,
044                            FilterChain filterChain)
045                    throws Exception {
046    
047                    try {
048                            processFilter(
049                                    AxisCleanUpFilter.class, request, response, filterChain);
050                    }
051                    finally {
052                            try {
053                                    ThreadLocal<?> cacheThreadLocal =
054                                            (ThreadLocal<?>)_cacheField.get(null);
055    
056                                    if (cacheThreadLocal != null) {
057                                            cacheThreadLocal.remove();
058                                    }
059                            }
060                            catch (Exception e) {
061                                    _log.error(e, e);
062                            }
063                    }
064            }
065    
066            private static Log _log = LogFactoryUtil.getLog(AxisCleanUpFilter.class);
067    
068            private static Field _cacheField;
069    
070            static {
071                    try {
072                            _cacheField = ReflectionUtil.getDeclaredField(
073                                    MethodCache.class, "cache");
074                    }
075                    catch (Exception e) {
076                            _log.error(e, e);
077                    }
078            }
079    
080    }