001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.servlet;
016    
017    import com.liferay.portal.kernel.util.BasePortalLifecycle;
018    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
019    import com.liferay.portal.kernel.util.StringUtil;
020    
021    import java.io.IOException;
022    
023    import javax.servlet.Filter;
024    import javax.servlet.FilterChain;
025    import javax.servlet.FilterConfig;
026    import javax.servlet.ServletException;
027    import javax.servlet.ServletRequest;
028    import javax.servlet.ServletResponse;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class PortalClassLoaderFilter
034            extends BasePortalLifecycle implements Filter{
035    
036            public void destroy() {
037                    portalDestroy();
038            }
039    
040            public void doFilter(
041                            ServletRequest servletRequest, ServletResponse servletResponse,
042                            FilterChain filterChain)
043                    throws IOException, ServletException {
044    
045                    Thread currentThread = Thread.currentThread();
046    
047                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
048    
049                    try {
050                            currentThread.setContextClassLoader(
051                                    PortalClassLoaderUtil.getClassLoader());
052    
053                            _filter.doFilter(servletRequest, servletResponse, filterChain);
054                    }
055                    finally {
056                            currentThread.setContextClassLoader(contextClassLoader);
057                    }
058            }
059    
060            public void init(FilterConfig filterConfig) {
061                    _filterConfig = filterConfig;
062    
063                    registerPortalLifecycle();
064            }
065    
066            protected void doPortalDestroy() {
067                    Thread currentThread = Thread.currentThread();
068    
069                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
070    
071                    try {
072                            currentThread.setContextClassLoader(
073                                    PortalClassLoaderUtil.getClassLoader());
074    
075                            _filter.destroy();
076                    }
077                    finally {
078                            currentThread.setContextClassLoader(contextClassLoader);
079                    }
080            }
081    
082            protected void doPortalInit() throws Exception {
083                    ClassLoader classLoader = PortalClassLoaderUtil.getClassLoader();
084    
085                    String filterClass = _filterConfig.getInitParameter("filter-class");
086    
087                    if (filterClass.startsWith("com.liferay.filters.")) {
088                            filterClass = StringUtil.replace(
089                                    filterClass, "com.liferay.filters.",
090                                    "com.liferay.portal.servlet.filters.");
091                    }
092    
093                    _filter = (Filter)classLoader.loadClass(filterClass).newInstance();
094    
095                    _filter.init(_filterConfig);
096            }
097    
098            private Filter _filter;
099            private FilterConfig _filterConfig;
100    
101    }