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.BaseStrutsAction;
018    import com.liferay.portal.util.ClassLoaderUtil;
019    
020    import javax.servlet.http.HttpServletRequest;
021    import javax.servlet.http.HttpServletResponse;
022    
023    import org.apache.struts.action.Action;
024    import org.apache.struts.action.ActionForm;
025    import org.apache.struts.action.ActionForward;
026    import org.apache.struts.action.ActionMapping;
027    
028    /**
029     * @author Mika Koivisto
030     */
031    public class StrutsActionAdapter extends BaseStrutsAction {
032    
033            public StrutsActionAdapter(
034                    Action action, ActionMapping actionMapping, ActionForm actionForm) {
035    
036                    _action = action;
037                    _actionMapping = actionMapping;
038                    _actionForm = actionForm;
039            }
040    
041            @Override
042            public String execute(
043                            HttpServletRequest request, HttpServletResponse response)
044                    throws Exception {
045    
046                    Thread currentThread = Thread.currentThread();
047    
048                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
049    
050                    currentThread.setContextClassLoader(
051                            ClassLoaderUtil.getPortalClassLoader());
052    
053                    try {
054                            ActionForward actionForward = _action.execute(
055                                    _actionMapping, _actionForm, request, response);
056    
057                            if (actionForward == null) {
058                                    return null;
059                            }
060    
061                            return actionForward.getPath();
062                    }
063                    finally {
064                            currentThread.setContextClassLoader(contextClassLoader);
065                    }
066            }
067    
068            private Action _action;
069            private ActionForm _actionForm;
070            private ActionMapping _actionMapping;
071    
072    }