View Javadoc

1   /*   Copyright (C) 2004 Finalist IT Group
2    *
3    *   This file is part of JAG - the Java J2EE Application Generator
4    *
5    *   JAG is free software; you can redistribute it and/or modify
6    *   it under the terms of the GNU General Public License as published by
7    *   the Free Software Foundation; either version 2 of the License, or
8    *   (at your option) any later version.
9    *   JAG is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   *   GNU General Public License for more details.
13   *   You should have received a copy of the GNU General Public License
14   *   along with JAG; if not, write to the Free Software
15   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   */
17  
18  package com.finalist.util.genelv.struts;
19  
20  import com.finalist.util.genelv.exceptions.BusinessLogicException;
21  import com.finalist.util.genelv.exceptions.WrappedEJBException;
22  import org.apache.log4j.Category;
23  import org.apache.struts.action.Action;
24  import org.apache.struts.action.ActionForm;
25  import org.apache.struts.action.ActionForward;
26  import org.apache.struts.action.ActionMapping;
27  
28  import javax.servlet.ServletException;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  import java.io.IOException;
32  import java.rmi.RemoteException;
33  
34  /***
35   * Implementation of <strong>GenericAction</strong>
36   *
37   * @author Gerard van de Weerd
38   * @version $1.0
39   */
40  public abstract class GenericAction extends Action {
41     static Category log = Category.getInstance(GenericAction.class);
42  
43  
44     /***
45      * Method perform
46      *
47      *
48      * @param mapping
49      * @param form
50      * @param request
51      * @param response
52      *
53      * @return
54      *
55      * @throws IOException
56      * @throws ServletException
57      *
58      */
59     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
60        throws IOException, ServletException {
61        try {
62           // Check if the ActionForm raised a Remote or WrappedEjbException
63           GenericForm childForm = (GenericForm) form;
64           if (childForm != null) {
65              Throwable formRaisedException = childForm.getRaisedException();
66              if (formRaisedException != null) {
67                 // If it is not null; throw it!
68                 log.error("exception occurred in ActionForm: " + childForm.getClass().getName());
69                 throw formRaisedException;
70              }
71           }
72           // Call the child class' doPerform method
73           ActionForward af = this.doPerform(mapping, form, request, response);
74           if (af == null)
75              return (mapping.findForward("success"));
76           else
77              return af;
78        }
79        catch (RemoteException ex) {
80           log.error("Remote Exception occured: ", ex);
81           Throwable saveEx = ex;
82           Throwable innerEx = ex.detail;
83           // get the inner exception iso the 'normal' remote exception
84           if (innerEx instanceof WrappedEJBException) {
85              saveEx = innerEx;
86           }
87           request.getSession().setAttribute(ExceptionConstants.EXCEPTION, saveEx);
88           return mapping.findForward(ExceptionConstants.EXCEPTION_PAGE);
89        }
90        catch (BusinessLogicException ex) {
91           log.error("BusinessLogicException occured: ", ex);
92           request.getSession().setAttribute(ExceptionConstants.EXCEPTION, ex);
93           return mapping.findForward(ExceptionConstants.EXCEPTION_PAGE);
94        }
95           // Catch all exceptions!
96        catch (Exception ex) {
97           log.error("A checked exception occured: ", ex);
98           request.getSession().setAttribute(ExceptionConstants.EXCEPTION, ex);
99           return mapping.findForward(ExceptionConstants.EXCEPTION_PAGE);
100       }
101       catch (Throwable t) {
102          log.error("An unchecked exception occured: ", t);
103          request.getSession().setAttribute(ExceptionConstants.EXCEPTION, t);
104          return mapping.findForward(ExceptionConstants.EXCEPTION_PAGE);
105       }
106    }
107 
108 
109    /*** The actual perform function: MUST be implemented by subclasses. */
110    abstract public ActionForward doPerform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws RemoteException, WrappedEJBException, Exception;
111 
112 }