1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
46
47
48 /**
49 * Map os action instaces.
50 */
51 private transient HashMap instaces = new HashMap();
52
53
54 /**
55 * Constructs a new <code>ActionSession</code> object.
56 */
57 public ActionSession() {
58 }
59
60
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
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 }