001    /**
002     * Copyright (c) 2000-present 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.util.InstanceFactory;
018    
019    import javax.servlet.Servlet;
020    import javax.servlet.ServletContext;
021    import javax.servlet.http.HttpServlet;
022    
023    /**
024     * <p>
025     * See https://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 SecureServlet {
032    
033            @Override
034            protected void doPortalDestroy() {
035                    PortalDelegatorServlet.removeDelegate(_subContext);
036    
037                    servlet.destroy();
038            }
039    
040            @Override
041            protected void doPortalInit() throws Exception {
042                    ServletContext servletContext = servletConfig.getServletContext();
043    
044                    ClassLoader classLoader = servletContext.getClassLoader();
045    
046                    String servletClass = servletConfig.getInitParameter("servlet-class");
047    
048                    _subContext = servletConfig.getInitParameter("sub-context");
049    
050                    if (_subContext == null) {
051                            _subContext = getServletName();
052                    }
053    
054                    servlet = (Servlet)InstanceFactory.newInstance(
055                            classLoader, servletClass);
056    
057                    if (!(servlet instanceof HttpServlet)) {
058                            throw new IllegalArgumentException(
059                                    "servlet-class is not an instance of " +
060                                            HttpServlet.class.getName());
061                    }
062    
063                    servlet.init(servletConfig);
064    
065                    PortalDelegatorServlet.addDelegate(_subContext, (HttpServlet)servlet);
066            }
067    
068            private String _subContext;
069    
070    }