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.setContextClassLoader(contextClassLoader);
071    
072                            invokerFilterChain.addFilter(_filter);
073    
074                            invokerFilterChain.doFilter(servletRequest, servletResponse);
075                    }
076                    finally {
077                            currentThread.setContextClassLoader(contextClassLoader);
078                    }
079            }
080    
081            @Override
082            public void init(FilterConfig filterConfig) {
083                    _filterConfig = filterConfig;
084    
085                    registerPortalLifecycle();
086            }
087    
088            @Override
089            public boolean isFilterEnabled() {
090                    if (_liferayFilter != null) {
091                            return _liferayFilter.isFilterEnabled();
092                    }
093    
094                    return true;
095            }
096    
097            @Override
098            public boolean isFilterEnabled(
099                    HttpServletRequest request, HttpServletResponse response) {
100    
101                    if (_liferayFilter != null) {
102                            return _liferayFilter.isFilterEnabled(request, response);
103                    }
104    
105                    return true;
106            }
107    
108            @Override
109            public void setFilterEnabled(boolean filterEnabled) {
110                    if (_liferayFilter != null) {
111                            _liferayFilter.setFilterEnabled(filterEnabled);
112                    }
113            }
114    
115            @Override
116            protected void doPortalDestroy() {
117                    Thread currentThread = Thread.currentThread();
118    
119                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
120    
121                    try {
122                            currentThread.setContextClassLoader(
123                                    PortalClassLoaderUtil.getClassLoader());
124    
125                            _filter.destroy();
126                    }
127                    finally {
128                            currentThread.setContextClassLoader(contextClassLoader);
129                    }
130            }
131    
132            @Override
133            protected void doPortalInit() throws Exception {
134                    ClassLoader classLoader = PortalClassLoaderUtil.getClassLoader();
135    
136                    String filterClass = _filterConfig.getInitParameter("filter-class");
137    
138                    if (filterClass.startsWith("com.liferay.filters.")) {
139                            filterClass = StringUtil.replace(
140                                    filterClass, "com.liferay.filters.",
141                                    "com.liferay.portal.servlet.filters.");
142                    }
143    
144                    _filter = (Filter)InstanceFactory.newInstance(classLoader, filterClass);
145    
146                    _filter.init(_filterConfig);
147    
148                    if (_filter instanceof LiferayFilter) {
149                            _liferayFilter = (LiferayFilter)_filter;
150                    }
151            }
152    
153            private Filter _filter;
154            private FilterConfig _filterConfig;
155            private LiferayFilter _liferayFilter;
156    
157    }