001    /**
002     * Copyright (c) 2000-2010 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.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
020    import com.liferay.portal.kernel.util.PortalLifecycle;
021    import com.liferay.portal.kernel.util.PortalLifecycleUtil;
022    
023    import java.io.IOException;
024    
025    import javax.servlet.ServletConfig;
026    import javax.servlet.ServletException;
027    import javax.servlet.http.HttpServlet;
028    import javax.servlet.http.HttpServletRequest;
029    import javax.servlet.http.HttpServletResponse;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class PortalClassLoaderServlet
035            extends HttpServlet implements PortalLifecycle {
036    
037            public void destroy() {
038                    portalDestroy();
039            }
040    
041            public void init(ServletConfig servletConfig) {
042                    _servletConfig = servletConfig;
043    
044                    PortalLifecycleUtil.register(this);
045            }
046    
047            public void portalDestroy() {
048                    if (!_calledPortalDestroy) {
049                            PortalLifecycleUtil.removeDestroy(this);
050    
051                            doPortalDestroy();
052    
053                            _calledPortalDestroy = true;
054                    }
055            }
056    
057            public void portalInit() {
058                    Thread currentThread = Thread.currentThread();
059    
060                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
061    
062                    ClassLoader portalClassLoader = PortalClassLoaderUtil.getClassLoader();
063    
064                    try {
065                            currentThread.setContextClassLoader(portalClassLoader);
066    
067                            String servletClass = _servletConfig.getInitParameter(
068                                    "servlet-class");
069    
070                            _servlet = (HttpServlet)portalClassLoader.loadClass(
071                                    servletClass).newInstance();
072    
073                            _servlet.init(_servletConfig);
074                    }
075                    catch (Exception e) {
076                            _log.error(e, e);
077                    }
078                    finally {
079                            currentThread.setContextClassLoader(contextClassLoader);
080                    }
081            }
082    
083            public void service(
084                            HttpServletRequest request, HttpServletResponse response)
085                    throws IOException, ServletException {
086    
087                    Thread currentThread = Thread.currentThread();
088    
089                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
090    
091                    try {
092                            currentThread.setContextClassLoader(
093                                    PortalClassLoaderUtil.getClassLoader());
094    
095                            _servlet.service(request, response);
096                    }
097                    finally {
098                            currentThread.setContextClassLoader(contextClassLoader);
099                    }
100            }
101    
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                            _servlet.destroy();
112                    }
113                    finally {
114                            currentThread.setContextClassLoader(contextClassLoader);
115                    }
116            }
117    
118            private static Log _log = LogFactoryUtil.getLog(
119                    PortalClassLoaderServlet.class);
120    
121            private boolean _calledPortalDestroy;
122            private HttpServlet _servlet;
123            private ServletConfig _servletConfig;
124    
125    }