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.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            public void portalDestroy() {
053                    if (!_calledPortalDestroy) {
054                            PortalLifecycleUtil.removeDestroy(this);
055    
056                            doPortalDestroy();
057    
058                            _calledPortalDestroy = true;
059                    }
060            }
061    
062            public void portalInit() {
063                    Thread currentThread = Thread.currentThread();
064    
065                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
066    
067                    ClassLoader portalClassLoader = PortalClassLoaderUtil.getClassLoader();
068    
069                    try {
070                            currentThread.setContextClassLoader(portalClassLoader);
071    
072                            String servletClass = _servletConfig.getInitParameter(
073                                    "servlet-class");
074    
075                            _servlet = (HttpServlet)InstanceFactory.newInstance(
076                                    portalClassLoader, servletClass);
077    
078                            _servlet.init(_servletConfig);
079                    }
080                    catch (Exception e) {
081                            _log.error(e, e);
082                    }
083                    finally {
084                            currentThread.setContextClassLoader(contextClassLoader);
085                    }
086            }
087    
088            @Override
089            public void service(
090                            HttpServletRequest request, HttpServletResponse response)
091                    throws IOException, ServletException {
092    
093                    Thread currentThread = Thread.currentThread();
094    
095                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
096    
097                    try {
098                            currentThread.setContextClassLoader(
099                                    PortalClassLoaderUtil.getClassLoader());
100    
101                            _servlet.service(request, response);
102                    }
103                    finally {
104                            currentThread.setContextClassLoader(contextClassLoader);
105                    }
106            }
107    
108            protected void doPortalDestroy() {
109                    Thread currentThread = Thread.currentThread();
110    
111                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
112    
113                    try {
114                            currentThread.setContextClassLoader(
115                                    PortalClassLoaderUtil.getClassLoader());
116    
117                            _servlet.destroy();
118                    }
119                    finally {
120                            currentThread.setContextClassLoader(contextClassLoader);
121                    }
122            }
123    
124            private static Log _log = LogFactoryUtil.getLog(
125                    PortalClassLoaderServlet.class);
126    
127            private boolean _calledPortalDestroy;
128            private HttpServlet _servlet;
129            private ServletConfig _servletConfig;
130    
131    }