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            @Override
043            public void destroy() {
044                    portalDestroy();
045            }
046    
047            @Override
048            public void doFilter(
049                            ServletRequest servletRequest, ServletResponse servletResponse,
050                            FilterChain filterChain)
051                    throws IOException, ServletException {
052    
053                    Thread currentThread = Thread.currentThread();
054    
055                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
056    
057                    try {
058                            currentThread.setContextClassLoader(
059                                    PortalClassLoaderUtil.getClassLoader());
060    
061                            FilterChain contextClassLoaderFilterChain =
062                                    (FilterChain)ProxyUtil.newProxyInstance(
063                                            contextClassLoader, new Class[] {FilterChain.class},
064                                            new ClassLoaderBeanHandler(
065                                                    filterChain, contextClassLoader));
066    
067                            InvokerFilterChain invokerFilterChain = new InvokerFilterChain(
068                                    contextClassLoaderFilterChain);
069    
070                            invokerFilterChain.addFilter(_filter);
071    
072                            invokerFilterChain.doFilter(servletRequest, servletResponse);
073                    }
074                    finally {
075                            currentThread.setContextClassLoader(contextClassLoader);
076                    }
077            }
078    
079            @Override
080            public void init(FilterConfig filterConfig) {
081                    _filterConfig = filterConfig;
082    
083                    registerPortalLifecycle();
084            }
085    
086            @Override
087            public boolean isFilterEnabled() {
088                    if (_liferayFilter != null) {
089                            return _liferayFilter.isFilterEnabled();
090                    }
091    
092                    return true;
093            }
094    
095            @Override
096            public boolean isFilterEnabled(
097                    HttpServletRequest request, HttpServletResponse response) {
098    
099                    if (_liferayFilter != null) {
100                            return _liferayFilter.isFilterEnabled(request, response);
101                    }
102    
103                    return true;
104            }
105    
106            @Override
107            public void setFilterEnabled(boolean filterEnabled) {
108                    if (_liferayFilter != null) {
109                            _liferayFilter.setFilterEnabled(filterEnabled);
110                    }
111            }
112    
113            @Override
114            protected void doPortalDestroy() {
115                    Thread currentThread = Thread.currentThread();
116    
117                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
118    
119                    try {
120                            currentThread.setContextClassLoader(
121                                    PortalClassLoaderUtil.getClassLoader());
122    
123                            _filter.destroy();
124                    }
125                    finally {
126                            currentThread.setContextClassLoader(contextClassLoader);
127                    }
128            }
129    
130            @Override
131            protected void doPortalInit() throws Exception {
132                    ClassLoader classLoader = PortalClassLoaderUtil.getClassLoader();
133    
134                    String filterClass = _filterConfig.getInitParameter("filter-class");
135    
136                    if (filterClass.startsWith("com.liferay.filters.")) {
137                            filterClass = StringUtil.replace(
138                                    filterClass, "com.liferay.filters.",
139                                    "com.liferay.portal.servlet.filters.");
140                    }
141    
142                    _filter = (Filter)InstanceFactory.newInstance(classLoader, filterClass);
143    
144                    _filter.init(_filterConfig);
145    
146                    if (_filter instanceof LiferayFilter) {
147                            _liferayFilter = (LiferayFilter)_filter;
148                    }
149            }
150    
151            private Filter _filter;
152            private FilterConfig _filterConfig;
153            private LiferayFilter _liferayFilter;
154    
155    }