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.portal.kernel.servlet;
016    
017    import com.liferay.portal.kernel.bean.ClassLoaderBeanHandler;
018    import com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain;
019    import com.liferay.portal.kernel.util.BasePortalLifecycle;
020    import com.liferay.portal.kernel.util.InstanceFactory;
021    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
022    import com.liferay.portal.kernel.util.ProxyUtil;
023    import com.liferay.portal.kernel.util.StringUtil;
024    
025    import java.io.IOException;
026    
027    import javax.servlet.Filter;
028    import javax.servlet.FilterChain;
029    import javax.servlet.FilterConfig;
030    import javax.servlet.ServletException;
031    import javax.servlet.ServletRequest;
032    import javax.servlet.ServletResponse;
033    import javax.servlet.http.HttpServletRequest;
034    import javax.servlet.http.HttpServletResponse;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     */
039    public class PortalClassLoaderFilter
040            extends BasePortalLifecycle implements LiferayFilter {
041    
042            public void destroy() {
043                    portalDestroy();
044            }
045    
046            public void doFilter(
047                            ServletRequest servletRequest, ServletResponse servletResponse,
048                            FilterChain filterChain)
049                    throws IOException, ServletException {
050    
051                    Thread currentThread = Thread.currentThread();
052    
053                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
054    
055                    try {
056                            currentThread.setContextClassLoader(
057                                    PortalClassLoaderUtil.getClassLoader());
058    
059                            FilterChain contextClassLoaderFilterChain =
060                                    (FilterChain)ProxyUtil.newProxyInstance(
061                                            contextClassLoader, new Class[] {FilterChain.class},
062                                            new ClassLoaderBeanHandler(
063                                                    filterChain, contextClassLoader));
064    
065                            InvokerFilterChain invokerFilterChain = new InvokerFilterChain(
066                                    contextClassLoaderFilterChain);
067    
068                            invokerFilterChain.addFilter(_filter);
069    
070                            invokerFilterChain.doFilter(servletRequest, servletResponse);
071                    }
072                    finally {
073                            currentThread.setContextClassLoader(contextClassLoader);
074                    }
075            }
076    
077            public void init(FilterConfig filterConfig) {
078                    _filterConfig = filterConfig;
079    
080                    registerPortalLifecycle();
081            }
082    
083            public boolean isFilterEnabled() {
084                    if (_liferayFilter != null) {
085                            return _liferayFilter.isFilterEnabled();
086                    }
087    
088                    return true;
089            }
090    
091            public boolean isFilterEnabled(
092                    HttpServletRequest request, HttpServletResponse response) {
093    
094                    if (_liferayFilter != null) {
095                            return _liferayFilter.isFilterEnabled(request, response);
096                    }
097    
098                    return true;
099            }
100    
101            public void setFilterEnabled(boolean filterEnabled) {
102                    if (_liferayFilter != null) {
103                            _liferayFilter.setFilterEnabled(filterEnabled);
104                    }
105            }
106    
107            @Override
108            protected void doPortalDestroy() {
109                    Thread currentThread = Thread.currentThread();
110    
111                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
112    
113                    try {
114                            currentThread.setContextClassLoader(
115                                    PortalClassLoaderUtil.getClassLoader());
116    
117                            _filter.destroy();
118                    }
119                    finally {
120                            currentThread.setContextClassLoader(contextClassLoader);
121                    }
122            }
123    
124            @Override
125            protected void doPortalInit() throws Exception {
126                    ClassLoader classLoader = PortalClassLoaderUtil.getClassLoader();
127    
128                    String filterClass = _filterConfig.getInitParameter("filter-class");
129    
130                    if (filterClass.startsWith("com.liferay.filters.")) {
131                            filterClass = StringUtil.replace(
132                                    filterClass, "com.liferay.filters.",
133                                    "com.liferay.portal.servlet.filters.");
134                    }
135    
136                    _filter = (Filter)InstanceFactory.newInstance(classLoader, filterClass);
137    
138                    _filter.init(_filterConfig);
139    
140                    if (_filter instanceof LiferayFilter) {
141                            _liferayFilter = (LiferayFilter)_filter;
142                    }
143            }
144    
145            private Filter _filter;
146            private FilterConfig _filterConfig;
147            private LiferayFilter _liferayFilter;
148    
149    }