001    /**
002     * Copyright (c) 2000-2010 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            public void init(ServletConfig servletConfig) {
034                    String servletClass = servletConfig.getInitParameter("servlet-class");
035    
036                    _subContext = servletConfig.getInitParameter("sub-context");
037    
038                    if (_subContext == null) {
039                            _subContext = getServletName();
040                    }
041    
042                    try {
043                            Thread currentThread = Thread.currentThread();
044    
045                            ClassLoader contextClassLoader =
046                                    currentThread.getContextClassLoader();
047    
048                            HttpServlet servlet = (HttpServlet)contextClassLoader.loadClass(
049                                    servletClass).newInstance();
050    
051                            servlet.init(servletConfig);
052    
053                            PortalDelegatorServlet.addDelegate(_subContext, servlet);
054                    }
055                    catch (Exception e) {
056                            _log.error(e, e);
057                    }
058            }
059    
060            public void destroy() {
061                    PortalDelegatorServlet.removeDelegate(_subContext);
062            }
063    
064            private static Log _log = LogFactoryUtil.getLog(
065                    PortalDelegateServlet.class);
066    
067            private String _subContext;
068    
069    }