001    /**
002     * Copyright (c) 2000-2011 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.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.StringUtil;
022    
023    import java.io.IOException;
024    
025    import java.lang.reflect.Proxy;
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)Proxy.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            @Override
102            protected void doPortalDestroy() {
103                    Thread currentThread = Thread.currentThread();
104    
105                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
106    
107                    try {
108                            currentThread.setContextClassLoader(
109                                    PortalClassLoaderUtil.getClassLoader());
110    
111                            _filter.destroy();
112                    }
113                    finally {
114                            currentThread.setContextClassLoader(contextClassLoader);
115                    }
116            }
117    
118            @Override
119            protected void doPortalInit() throws Exception {
120                    ClassLoader classLoader = PortalClassLoaderUtil.getClassLoader();
121    
122                    String filterClass = _filterConfig.getInitParameter("filter-class");
123    
124                    if (filterClass.startsWith("com.liferay.filters.")) {
125                            filterClass = StringUtil.replace(
126                                    filterClass, "com.liferay.filters.",
127                                    "com.liferay.portal.servlet.filters.");
128                    }
129    
130                    _filter = (Filter)classLoader.loadClass(filterClass).newInstance();
131    
132                    _filter.init(_filterConfig);
133    
134                    if (_filter instanceof LiferayFilter) {
135                            _liferayFilter = (LiferayFilter)_filter;
136                    }
137            }
138    
139            private Filter _filter;
140            private FilterConfig _filterConfig;
141            private LiferayFilter _liferayFilter;
142    
143    }