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    
020    import javax.servlet.ServletConfig;
021    import javax.servlet.http.HttpServlet;
022    
023    /**
024     * <p>
025     * See http://issues.liferay.com/browse/LEP-2297.
026     * </p>
027     *
028     * @author Olaf Fricke
029     * @author Brian Wing Shun Chan
030     */
031    public class PortalDelegateServlet extends HttpServlet {
032    
033            @Override
034            public void destroy() {
035                    PortalDelegatorServlet.removeDelegate(_subContext);
036            }
037    
038            @Override
039            public void init(ServletConfig servletConfig) {
040                    String servletClass = servletConfig.getInitParameter("servlet-class");
041    
042                    _subContext = servletConfig.getInitParameter("sub-context");
043    
044                    if (_subContext == null) {
045                            _subContext = getServletName();
046                    }
047    
048                    try {
049                            Thread currentThread = Thread.currentThread();
050    
051                            ClassLoader contextClassLoader =
052                                    currentThread.getContextClassLoader();
053    
054                            HttpServlet servlet = (HttpServlet)contextClassLoader.loadClass(
055                                    servletClass).newInstance();
056    
057                            servlet.init(servletConfig);
058    
059                            PortalDelegatorServlet.addDelegate(_subContext, servlet);
060                    }
061                    catch (Exception e) {
062                            _log.error(e, e);
063                    }
064            }
065    
066            private static Log _log = LogFactoryUtil.getLog(
067                    PortalDelegateServlet.class);
068    
069            private String _subContext;
070    
071    }