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