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