001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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) {
045                    _servletConfig = servletConfig;
046    
047                    PortalLifecycleUtil.register(this);
048            }
049    
050            public void portalDestroy() {
051                    if (!_calledPortalDestroy) {
052                            PortalLifecycleUtil.removeDestroy(this);
053    
054                            doPortalDestroy();
055    
056                            _calledPortalDestroy = true;
057                    }
058            }
059    
060            public void portalInit() {
061                    Thread currentThread = Thread.currentThread();
062    
063                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
064    
065                    ClassLoader portalClassLoader = PortalClassLoaderUtil.getClassLoader();
066    
067                    try {
068                            currentThread.setContextClassLoader(portalClassLoader);
069    
070                            String servletClass = _servletConfig.getInitParameter(
071                                    "servlet-class");
072    
073                            _servlet = (HttpServlet)InstanceFactory.newInstance(
074                                    portalClassLoader, servletClass);
075    
076                            _servlet.init(_servletConfig);
077                    }
078                    catch (Exception e) {
079                            _log.error(e, e);
080                    }
081                    finally {
082                            currentThread.setContextClassLoader(contextClassLoader);
083                    }
084            }
085    
086            @Override
087            public void service(
088                            HttpServletRequest request, HttpServletResponse response)
089                    throws IOException, ServletException {
090    
091                    Thread currentThread = Thread.currentThread();
092    
093                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
094    
095                    try {
096                            currentThread.setContextClassLoader(
097                                    PortalClassLoaderUtil.getClassLoader());
098    
099                            _servlet.service(request, response);
100                    }
101                    finally {
102                            currentThread.setContextClassLoader(contextClassLoader);
103                    }
104            }
105    
106            protected void doPortalDestroy() {
107                    Thread currentThread = Thread.currentThread();
108    
109                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
110    
111                    try {
112                            currentThread.setContextClassLoader(
113                                    PortalClassLoaderUtil.getClassLoader());
114    
115                            _servlet.destroy();
116                    }
117                    finally {
118                            currentThread.setContextClassLoader(contextClassLoader);
119                    }
120            }
121    
122            private static Log _log = LogFactoryUtil.getLog(
123                    PortalClassLoaderServlet.class);
124    
125            private boolean _calledPortalDestroy;
126            private HttpServlet _servlet;
127            private ServletConfig _servletConfig;
128    
129    }