View Javadoc

1   /*
2    * $Id: ActionWrapper.java 269 2005-08-10 17:49:22Z josem $
3    *
4    * Tarsis
5    * Copyright (C) 2002 Talika Open Source Group
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   *
21   */
22  
23  package org.talika.tarsis.command.action;
24  
25  import org.talika.tarsis.Globals;
26  import org.talika.tarsis.command.Request;
27  import org.talika.tarsis.command.Response;
28  import org.talika.tarsis.log.Logger;
29  
30  /**
31   * This is a wrapper for statefull actions. Instead of having an specialized
32   * implementation of <code>Action</code> for statefull actions we have this
33   * class that wraps a normal action and do all the statefull tricky work.
34   *
35   * @author  Jose M. Palomar
36   * @version $Revision: 269 $
37   */
38  public final class ActionWrapper extends AbstractAction {
39  
40      // Constants
41  
42      // Fields
43      /**
44       * Class of action that we wrap.
45       */
46      private Class actionClass;
47  
48      /**
49       * Tarsis logger.
50       */
51      private Logger logger;
52  
53      // Constructors
54      /**
55       * Constructs a new <code>ActionWrapper</code>.
56       *
57       * @param actionClass Class class of action that we wrap.
58       */
59      public ActionWrapper(Class actionClass) {
60          this.actionClass = actionClass;
61      }
62  
63      /**
64       * Called by Tarsis MVC Framework to validate request everytime command is
65       * invoked.<br>
66       * <br>
67       * This method is called only if command is validable.<br>
68       * Wrapper first call <code>findActionSession</code> on current request to
69       * obtain action instance from session. Then call <code>validate</code> method over
70       * instance.
71       *
72       * @param request Request object representing client's request.
73       * @throws ActionException if an exception has occurred that interferes with the
74       * action's normal operation.
75       * @see org.talika.tarsis.command.action.Action#validate(Request)
76       */
77      public void validate(Request request) throws ActionException {
78          findActionSession(request).validate(request);
79      }
80  
81      /**
82       * Called by Tarsis MVC Framework to handle request everytime command is
83       * invoked.<br>
84       * Wrapper first call <code>findActionSession</code> on current request to
85       * obtain action instance from session. Then call <code>execute</code> method over
86       * instance.
87       *
88       * @param request Request object representing client's request.
89       * @param response Response object representing client's reponse.
90       * @return String the name of view to be forwarded or <code>null</code> for
91       * default view.
92       * @throws ActionException if an exception has occurred that interferes with the
93       * action's normal operation.
94       * @see org.talika.tarsis.command.action.Action#execute(Request, Response)
95       */
96      public String execute(Request request, Response response) throws ActionException {
97          return findActionSession(request).execute(request, response);
98      }
99  
100     /**
101      * Obtains action instance from current session. If there is no instance for
102      * this action in session it creates and initializates a new one. Then stores
103      * the new instance in current session for further use.
104      *
105      * @param request Request client request.
106      * @return Action action instance.
107      * @throws ActionException if an exception has occurred while trying to obtain
108      * action instance.
109      */
110     private Action findActionSession(Request request) throws ActionException {
111 
112         ActionSession session = (ActionSession) request.getSession().getAttribute(Globals.ACTION_SESSION_ATTR);
113         if (session == null) {
114             if (getContext().getLogger().isDebugEnabled()) {
115                 getContext().getLogger().logDebug("Creating session " + request.getSession().getId());
116             }
117             session = new ActionSession();
118             request.getSession().setAttribute(Globals.ACTION_SESSION_ATTR, session);
119         }
120 
121         Action action = (Action) session.getInstance(actionClass.getName());
122         if (action == null) {
123 
124             if (getContext().getLogger().isDebugEnabled()) {
125                 getContext().getLogger().logDebug("Adding action " + actionClass.getName() +
126                                                   " to session " + request.getSession().getId());
127             }
128 
129             try {
130                 action = (Action) actionClass.newInstance();
131             }
132             catch (InstantiationException ie) {
133                 throw new ActionException("Error creating a new instance of action " + actionClass.getName());
134             }
135             catch (IllegalAccessException iae) {
136                 throw new ActionException("Error creating a new instance of action " + actionClass.getName());
137             }
138 
139             action.init(getContext());
140             session.setInstance(actionClass.getName(), action);
141 
142         }
143 
144         return action;
145 
146     }
147 
148 }