001    /**
002     * Copyright (c) 2000-2013 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 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 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 = (ClassLoader)servletContext.getAttribute(
045                            PluginContextListener.PLUGIN_CLASS_LOADER);
046    
047                    String servletClass = servletConfig.getInitParameter("servlet-class");
048    
049                    _subContext = servletConfig.getInitParameter("sub-context");
050    
051                    if (_subContext == null) {
052                            _subContext = getServletName();
053                    }
054    
055                    servlet = (Servlet)InstanceFactory.newInstance(
056                            classLoader, servletClass);
057    
058                    if (!(servlet instanceof HttpServlet)) {
059                            throw new IllegalArgumentException(
060                                    "servlet-class is not an instance of " +
061                                            HttpServlet.class.getName());
062                    }
063    
064                    servlet.init(servletConfig);
065    
066                    PortalDelegatorServlet.addDelegate(_subContext, (HttpServlet)servlet);
067            }
068    
069            private String _subContext;
070    
071    }