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