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.servlet;
016    
017    import com.liferay.portal.kernel.security.access.control.AccessControlThreadLocal;
018    import com.liferay.portal.kernel.util.ClassLoaderUtil;
019    
020    import java.io.IOException;
021    
022    import javax.servlet.ServletConfig;
023    import javax.servlet.ServletContext;
024    import javax.servlet.ServletException;
025    import javax.servlet.http.HttpServletRequest;
026    import javax.servlet.http.HttpServletResponse;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class AxisServlet extends com.liferay.util.axis.AxisServlet {
032    
033            @Override
034            public void init(ServletConfig servletConfig) throws ServletException {
035                    ServletContext servletContext = servletConfig.getServletContext();
036    
037                    _pluginClassLoader = servletContext.getClassLoader();
038    
039                    if (_pluginClassLoader == ClassLoaderUtil.getPortalClassLoader()) {
040                            super.init(servletConfig);
041                    }
042                    else {
043                            ClassLoader contextClassLoader =
044                                    ClassLoaderUtil.getContextClassLoader();
045    
046                            try {
047                                    ClassLoaderUtil.setContextClassLoader(_pluginClassLoader);
048    
049                                    super.init(servletConfig);
050                            }
051                            finally {
052                                    ClassLoaderUtil.setContextClassLoader(contextClassLoader);
053                            }
054                    }
055            }
056    
057            @Override
058            public void service(
059                            HttpServletRequest request, HttpServletResponse response)
060                    throws IOException, ServletException {
061    
062                    boolean remoteAccess = AccessControlThreadLocal.isRemoteAccess();
063    
064                    try {
065                            AccessControlThreadLocal.setRemoteAccess(true);
066    
067                            if (_pluginClassLoader == ClassLoaderUtil.getPortalClassLoader()) {
068                                    super.service(request, response);
069                            }
070                            else {
071                                    ClassLoader contextClassLoader =
072                                            ClassLoaderUtil.getContextClassLoader();
073    
074                                    try {
075                                            ClassLoaderUtil.setContextClassLoader(_pluginClassLoader);
076    
077                                            super.service(request, response);
078                                    }
079                                    finally {
080                                            ClassLoaderUtil.setContextClassLoader(contextClassLoader);
081                                    }
082                            }
083                    }
084                    catch (IOException ioe) {
085                            throw ioe;
086                    }
087                    catch (ServletException se) {
088                            throw se;
089                    }
090                    catch (Exception e) {
091                            throw new ServletException(e);
092                    }
093                    finally {
094                            AccessControlThreadLocal.setRemoteAccess(remoteAccess);
095                    }
096            }
097    
098            private ClassLoader _pluginClassLoader;
099    
100    }