View Javadoc

1   /*
2    * $Id: ActionSession.java 121 2004-11-01 13:05:23Z 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 java.util.HashMap;
26  import java.util.Iterator;
27  import java.io.Serializable;
28  import javax.servlet.http.HttpSessionBindingListener;
29  import javax.servlet.http.HttpSessionBindingEvent;
30  
31  /**
32   * This object serves as transport object for all instances of statefull actions
33   * for an user, instead of saving them individually in the user's session.
34   *
35   * @author  Jose M. Palomar
36   * @version $Revision: 121 $
37   *
38   * @todo Actions may not be serializable!!! So i must make instances map
39   * transient. If server crashes sessions are lost. Must think another
40   * method for saving or maybe simply remove STATEFULL actions from the
41   * framework.
42   */
43  public final class ActionSession implements HttpSessionBindingListener, Serializable {
44  
45      // Constants
46  
47      // Fields
48      /**
49       * Map os action instaces.
50       */
51      private transient HashMap instaces = new HashMap();
52  
53      // Constructors
54      /**
55       * Constructs a new <code>ActionSession</code> object.
56       */
57      public ActionSession() {
58      }
59  
60      // Methods
61      /**
62       * Saves an action instance on session.
63       *
64       * @param name String action class name.
65       * @param action Action action instance.
66       */
67      public void setInstance(String name, Action action) {
68          instaces.put(name, action);
69      }
70  
71      /**
72       * Obtains an action instance from session.
73       *
74       * @param name String action class name.
75       * @return Action action instance or <code>null</code> if not found.
76       */
77      public Action getInstance(String name) {
78          return (Action) instaces.get(name);
79      }
80  
81      // HttpSessionBindingListener Interface Methods
82  
83      /**
84       * Notifies the object that it is being bound to a session and identifies the
85       * session. This method do nothing.
86       *
87       * @param httpSessionBindingEvent HttpSessionBindingEvent the event that
88       * identifies the session.
89       * @see javax.servlet.http.HttpSessionBindingListener#valueBound(HttpSessionBindingEvent)
90       */
91      public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {
92      }
93  
94      /**
95       * Notifies the object that it is being unbound from a session and identifies the
96       * session. When <code>ActionSession</code> is unbound it call <code>destroy</code>
97       * method for all action instances in session to allow them to release any resource.
98       *
99       * @param httpSessionBindingEvent HttpSessionBindingEvent the event that
100      * identifies the session.
101      * @see javax.servlet.http.HttpSessionBindingListener#valueUnbound(HttpSessionBindingEvent)
102      */
103     public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {
104             Iterator actions = instaces.values().iterator();
105             while (actions.hasNext()) {
106                 Action action = (Action) actions.next();
107                 action.destroy();
108             }
109     }
110 
111 }