KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > dispatcher > SessionMap


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.dispatcher;
6
7 import javax.servlet.http.HttpServletRequest JavaDoc;
8 import javax.servlet.http.HttpSession JavaDoc;
9 import java.io.Serializable JavaDoc;
10 import java.util.*;
11
12
13 /**
14  * A simple implementation of the {@link java.util.Map} interface to handle a collection of HTTP session
15  * attributes. The {@link #entrySet()} method enumerates over all session attributes and creates a Set of entries.
16  * Note, this will occur lazily - only when the entry set is asked for.
17  *
18  * @author <a HREF="mailto:rickard@middleware-company.com">Rickard Öberg</a>
19  * @author Bill Lynch (docs)
20  */

21 public class SessionMap extends AbstractMap implements Serializable JavaDoc {
22     //~ Instance fields ////////////////////////////////////////////////////////
23

24     protected HttpSession JavaDoc session;
25     protected Set entries;
26     protected HttpServletRequest JavaDoc request;
27
28     //~ Constructors ///////////////////////////////////////////////////////////
29

30     /**
31      * Creates a new session map given a http servlet request. Note, ths enumeration of request
32      * attributes will occur when the map entries are asked for.
33      *
34      * @param request the http servlet request object.
35      */

36     public SessionMap(HttpServletRequest JavaDoc request) {
37         // note, holding on to this request and relying on lazy session initalization will not work
38
// if you are running your action invocation in a background task, such as using the
39
// "exec-and-wait" interceptor
40
this.request = request;
41         this.session = request.getSession(false);
42     }
43
44     //~ Methods ////////////////////////////////////////////////////////////////
45

46     /**
47      * Removes all attributes from the session as well as clears entries in this map.
48      */

49     public void clear() {
50         if (session == null) {
51             return;
52         }
53
54         synchronized (session) {
55             entries = null;
56         }
57     }
58
59     /**
60      * Returns a Set of attributes from the http session.
61      *
62      * @return a Set of attributes from the http session.
63      */

64     public Set entrySet() {
65         if (session == null) {
66             return Collections.EMPTY_SET;
67         }
68
69         synchronized (session) {
70             if (entries == null) {
71                 entries = new HashSet();
72
73                 Enumeration enumeration = session.getAttributeNames();
74
75                 while (enumeration.hasMoreElements()) {
76                     final String JavaDoc key = enumeration.nextElement().toString();
77                     final Object JavaDoc value = session.getAttribute(key);
78                     entries.add(new Map.Entry() {
79                         public boolean equals(Object JavaDoc obj) {
80                             Map.Entry entry = (Map.Entry) obj;
81
82                             return ((key == null) ? (entry.getKey() == null) : key.equals(entry.getKey())) && ((value == null) ? (entry.getValue() == null) : value.equals(entry.getValue()));
83                         }
84
85                         public int hashCode() {
86                             return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode());
87                         }
88
89                         public Object JavaDoc getKey() {
90                             return key;
91                         }
92
93                         public Object JavaDoc getValue() {
94                             return value;
95                         }
96
97                         public Object JavaDoc setValue(Object JavaDoc obj) {
98                             session.setAttribute(key.toString(), obj);
99
100                             return value;
101                         }
102                     });
103                 }
104             }
105         }
106
107         return entries;
108     }
109
110     /**
111      * Returns the session attribute associated with the given key or <tt>null</tt> if it doesn't exist.
112      *
113      * @param key the name of the session attribute.
114      * @return the session attribute or <tt>null</tt> if it doesn't exist.
115      */

116     public Object JavaDoc get(Object JavaDoc key) {
117         if (session == null) {
118             return null;
119         }
120
121         synchronized (session) {
122             return session.getAttribute(key.toString());
123         }
124     }
125
126     /**
127      * Saves an attribute in the session.
128      *
129      * @param key the name of the session attribute.
130      * @param value the value to set.
131      * @return the object that was just set.
132      */

133     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
134         synchronized (this) {
135             if (session == null) {
136                 session = request.getSession(true);
137             }
138         }
139
140         synchronized (session) {
141             entries = null;
142             session.setAttribute(key.toString(), value);
143
144             return get(key);
145         }
146     }
147
148     /**
149      * Removes the specified session attribute.
150      *
151      * @param key the name of the attribute to remove.
152      * @return the value that was removed or <tt>null</tt> if the value was not found (and hence, not removed).
153      */

154     public Object JavaDoc remove(Object JavaDoc key) {
155         if (session == null) {
156             return null;
157         }
158
159         synchronized (session) {
160             entries = null;
161
162             Object JavaDoc value = get(key);
163             session.removeAttribute(key.toString());
164
165             return value;
166         }
167     }
168 }
169
Popular Tags