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