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