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.struts;
016    
017    import com.liferay.portal.kernel.struts.BaseStrutsPortletAction;
018    import com.liferay.portal.util.ClassLoaderUtil;
019    
020    import javax.portlet.ActionRequest;
021    import javax.portlet.ActionResponse;
022    import javax.portlet.PortletConfig;
023    import javax.portlet.RenderRequest;
024    import javax.portlet.RenderResponse;
025    import javax.portlet.ResourceRequest;
026    import javax.portlet.ResourceResponse;
027    
028    import org.apache.struts.action.ActionForm;
029    import org.apache.struts.action.ActionForward;
030    import org.apache.struts.action.ActionMapping;
031    
032    /**
033     * @author Mika Koivisto
034     */
035    public class StrutsPortletActionAdapter extends BaseStrutsPortletAction {
036    
037            public StrutsPortletActionAdapter(
038                    PortletAction portletAction, ActionMapping actionMapping,
039                    ActionForm actionForm) {
040    
041                    _portletAction = portletAction;
042                    _actionMapping = actionMapping;
043                    _actionForm = actionForm;
044            }
045    
046            @Override
047            public void processAction(
048                            PortletConfig portletConfig, ActionRequest actionRequest,
049                            ActionResponse actionResponse)
050                    throws Exception {
051    
052                    Thread currentThread = Thread.currentThread();
053    
054                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
055    
056                    currentThread.setContextClassLoader(
057                            ClassLoaderUtil.getPortalClassLoader());
058    
059                    try {
060                            _portletAction.processAction(
061                                    _actionMapping, _actionForm, portletConfig, actionRequest,
062                                    actionResponse);
063                    }
064                    finally {
065                            currentThread.setContextClassLoader(contextClassLoader);
066                    }
067            }
068    
069            @Override
070            public String render(
071                            PortletConfig portletConfig, RenderRequest renderRequest,
072                            RenderResponse renderResponse)
073                    throws Exception {
074    
075                    Thread currentThread = Thread.currentThread();
076    
077                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
078    
079                    currentThread.setContextClassLoader(
080                            ClassLoaderUtil.getPortalClassLoader());
081    
082                    try {
083                            ActionForward actionForward = _portletAction.render(
084                                    _actionMapping, _actionForm, portletConfig, renderRequest,
085                                    renderResponse);
086    
087                            if (actionForward != null) {
088                                    return actionForward.getPath();
089                            }
090    
091                            return null;
092                    }
093                    finally {
094                            currentThread.setContextClassLoader(contextClassLoader);
095                    }
096            }
097    
098            @Override
099            public void serveResource(
100                            PortletConfig portletConfig, ResourceRequest resourceRequest,
101                            ResourceResponse resourceResponse)
102                    throws Exception {
103    
104                    Thread currentThread = Thread.currentThread();
105    
106                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
107    
108                    currentThread.setContextClassLoader(
109                            ClassLoaderUtil.getPortalClassLoader());
110    
111                    try {
112                            _portletAction.serveResource(
113                                    _actionMapping, _actionForm, portletConfig, resourceRequest,
114                                    resourceResponse);
115                    }
116                    finally {
117                            currentThread.setContextClassLoader(contextClassLoader);
118                    }
119            }
120    
121            private ActionForm _actionForm;
122            private ActionMapping _actionMapping;
123            private PortletAction _portletAction;
124    
125    }