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, request, response, filterChain);
051                    }
052                    finally {
053                            try {
054                                    ThreadLocal<?> cacheThreadLocal =
055                                            (ThreadLocal<?>)_CACHE_FIELD.get(null);
056    
057                                    if (cacheThreadLocal != null) {
058                                            cacheThreadLocal.remove();
059                                    }
060                            }
061                            catch (Exception e) {
062                                    _log.error(e, e);
063                            }
064                    }
065            }
066    
067            private static final Field _CACHE_FIELD;
068    
069            private static final Log _log = LogFactoryUtil.getLog(
070                    AxisCleanUpFilter.class);
071    
072            static {
073                    try {
074                            _CACHE_FIELD = ReflectionUtil.getDeclaredField(
075                                    MethodCache.class, "cache");
076                    }
077                    catch (Exception e) {
078                            throw new LoggedExceptionInInitializerError(e);
079                    }
080            }
081    
082    }