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