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