KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2005 Opensymphony. All Rights Reserved.
3  */

4 package com.opensymphony.webwork.portlet;
5
6 import javax.portlet.PortletRequest;
7 import javax.portlet.PortletSession;
8 import java.io.Serializable JavaDoc;
9 import java.util.*;
10
11
12 /**
13  * @author <a HREF="mailto:hu_pengfei@yahoo.com.cn">Henry Hu </a>
14  * @since 2005-7-18
15  */

16 public class SessionMap extends AbstractMap implements Serializable JavaDoc {
17     //~ Instance fields ////////////////////////////////////////////////////////
18

19     PortletSession session;
20     Set entries;
21
22     //~ Constructors ///////////////////////////////////////////////////////////
23

24     /**
25      * Creates a new session map given a http servlet request. Note, ths enumeration of request
26      * attributes will occur when the map entries are asked for.
27      *
28      * @param request the http servlet request object.
29      */

30     public SessionMap(PortletRequest request) {
31         this.session = request.getPortletSession(true);
32     }
33
34     //~ Methods ////////////////////////////////////////////////////////////////
35

36     /**
37      * Removes all attributes from the session as well as clears entries in this map.
38      */

39     public void clear() {
40         synchronized (session) {
41             entries = null;
42             session.invalidate();
43         }
44     }
45
46     /**
47      * Returns a Set of attributes from the http session.
48      *
49      * @return a Set of attributes from the http session.
50      */

51     public Set entrySet() {
52         synchronized (session) {
53             if (entries == null) {
54                 entries = new HashSet();
55
56                 Enumeration enumeration = session.getAttributeNames();
57
58                 while (enumeration.hasMoreElements()) {
59                     final String JavaDoc key = enumeration.nextElement().toString();
60                     final Object JavaDoc value = session.getAttribute(key);
61                     entries.add(new Map.Entry() {
62                         public boolean equals(Object JavaDoc obj) {
63                             Map.Entry entry = (Map.Entry) obj;
64
65                             return ((key == null) ? (entry.getKey() == null) : key.equals(entry.getKey())) && ((value == null) ? (entry.getValue() == null) : value.equals(entry.getValue()));
66                         }
67
68                         public int hashCode() {
69                             return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode());
70                         }
71
72                         public Object JavaDoc getKey() {
73                             return key;
74                         }
75
76                         public Object JavaDoc getValue() {
77                             return value;
78                         }
79
80                         public Object JavaDoc setValue(Object JavaDoc obj) {
81                             session.setAttribute(key.toString(), obj);
82
83                             return value;
84                         }
85                     });
86                 }
87             }
88         }
89
90         return entries;
91     }
92
93     /**
94      * Returns the session attribute associated with the given key or <tt>null</tt> if it doesn't exist.
95      *
96      * @param key the name of the session attribute.
97      * @return the session attribute or <tt>null</tt> if it doesn't exist.
98      */

99     public Object JavaDoc get(Object JavaDoc key) {
100         synchronized (session) {
101             return session.getAttribute(key.toString());
102         }
103     }
104
105     /**
106      * Saves an attribute in the session.
107      *
108      * @param key the name of the session attribute.
109      * @param value the value to set.
110      * @return the object that was just set.
111      */

112     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
113         synchronized (session) {
114             entries = null;
115             session.setAttribute(key.toString(), value);
116
117             return get(key);
118         }
119     }
120
121     /**
122      * Removes the specified session attribute.
123      *
124      * @param key the name of the attribute to remove.
125      * @return the value that was removed or <tt>null</tt> if the value was not found (and hence, not removed).
126      */

127     public Object JavaDoc remove(Object JavaDoc key) {
128         synchronized (session) {
129             entries = null;
130
131             Object JavaDoc value = get(key);
132             session.removeAttribute(key.toString());
133
134             return value;
135         }
136     }
137 }
138
Popular Tags