001    /**
002     * Copyright (c) 2000-2010 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.kernel.servlet;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
020    import com.liferay.portal.kernel.util.PortalInitable;
021    import com.liferay.portal.kernel.util.PortalInitableUtil;
022    import com.liferay.portal.kernel.util.StringUtil;
023    
024    import java.io.IOException;
025    
026    import javax.servlet.Filter;
027    import javax.servlet.FilterChain;
028    import javax.servlet.FilterConfig;
029    import javax.servlet.ServletException;
030    import javax.servlet.ServletRequest;
031    import javax.servlet.ServletResponse;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class PortalClassLoaderFilter implements Filter, PortalInitable {
037    
038            public void destroy() {
039                    Thread currentThread = Thread.currentThread();
040    
041                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
042    
043                    try {
044                            currentThread.setContextClassLoader(
045                                    PortalClassLoaderUtil.getClassLoader());
046    
047                            _filter.destroy();
048                    }
049                    finally {
050                            currentThread.setContextClassLoader(contextClassLoader);
051                    }
052            }
053    
054            public void doFilter(
055                            ServletRequest servletRequest, ServletResponse servletResponse,
056                            FilterChain filterChain)
057                    throws IOException, ServletException {
058    
059                    Thread currentThread = Thread.currentThread();
060    
061                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
062    
063                    try {
064                            currentThread.setContextClassLoader(
065                                    PortalClassLoaderUtil.getClassLoader());
066    
067                            _filter.doFilter(servletRequest, servletResponse, filterChain);
068                    }
069                    finally {
070                            currentThread.setContextClassLoader(contextClassLoader);
071                    }
072            }
073    
074            public void init(FilterConfig filterConfig) {
075                    _filterConfig = filterConfig;
076    
077                    PortalInitableUtil.init(this);
078            }
079    
080            public void portalInit() {
081                    try {
082                            ClassLoader classLoader = PortalClassLoaderUtil.getClassLoader();
083    
084                            String filterClass = _filterConfig.getInitParameter("filter-class");
085    
086                            if (filterClass.startsWith("com.liferay.filters.")) {
087                                    filterClass = StringUtil.replace(
088                                            filterClass, "com.liferay.filters.",
089                                            "com.liferay.portal.servlet.filters.");
090                            }
091    
092                            _filter = (Filter)classLoader.loadClass(filterClass).newInstance();
093    
094                            _filter.init(_filterConfig);
095                    }
096                    catch (Exception e) {
097                            _log.error(e, e);
098                    }
099            }
100    
101            private static Log _log = LogFactoryUtil.getLog(
102                    PortalClassLoaderFilter.class);
103    
104            private Filter _filter;
105            private FilterConfig _filterConfig;
106    
107    }