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.servlet;
016    
017    import com.liferay.portal.kernel.servlet.PluginContextListener;
018    import com.liferay.portal.util.ClassLoaderUtil;
019    
020    import java.io.IOException;
021    
022    import javax.servlet.RequestDispatcher;
023    import javax.servlet.ServletContext;
024    import javax.servlet.ServletException;
025    import javax.servlet.ServletRequest;
026    import javax.servlet.ServletResponse;
027    
028    /**
029     * @author Raymond Augé
030     */
031    public class ClassLoaderRequestDispatcherWrapper implements RequestDispatcher {
032    
033            public ClassLoaderRequestDispatcherWrapper(
034                    ServletContext servletContext, RequestDispatcher requestDispatcher) {
035    
036                    _servletContext = servletContext;
037                    _requestDispatcher = requestDispatcher;
038            }
039    
040            public void forward(
041                            ServletRequest servletRequest, ServletResponse servletResponse)
042                    throws IOException, ServletException {
043    
044                    doDispatch(servletRequest, servletResponse, false);
045            }
046    
047            public void include(
048                            ServletRequest servletRequest, ServletResponse servletResponse)
049                    throws IOException, ServletException {
050    
051                    doDispatch(servletRequest, servletResponse, true);
052            }
053    
054            protected void doDispatch(
055                            ServletRequest servletRequest, ServletResponse servletResponse,
056                            boolean include)
057                    throws IOException, ServletException {
058    
059                    ClassLoader contextClassLoader =
060                            ClassLoaderUtil.getContextClassLoader();
061    
062                    ClassLoader pluginClassLoader =
063                            (ClassLoader)_servletContext.getAttribute(
064                                    PluginContextListener.PLUGIN_CLASS_LOADER);
065    
066                    try {
067                            if (pluginClassLoader == null) {
068                                    ClassLoaderUtil.setContextClassLoader(
069                                            ClassLoaderUtil.getPortalClassLoader());
070                            }
071                            else {
072                                    ClassLoaderUtil.setContextClassLoader(pluginClassLoader);
073                            }
074    
075                            if (include) {
076                                    _requestDispatcher.include(servletRequest, servletResponse);
077                            }
078                            else {
079                                    _requestDispatcher.forward(servletRequest, servletResponse);
080                            }
081                    }
082                    finally {
083                            ClassLoaderUtil.setContextClassLoader(contextClassLoader);
084                    }
085            }
086    
087            private RequestDispatcher _requestDispatcher;
088            private ServletContext _servletContext;
089    
090    }