1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.portlet;
23
24 import java.util.AbstractMap;
25 import java.util.Enumeration;
26 import java.util.HashSet;
27 import java.util.Map;
28 import java.util.Set;
29
30 import javax.portlet.PortletRequest;
31 import javax.portlet.PortletSession;
32
33 import com.opensymphony.xwork2.util.logging.Logger;
34 import com.opensymphony.xwork2.util.logging.LoggerFactory;
35
36 /***
37 * A simple implementation of the {@link java.util.Map} interface to handle a collection of portlet session
38 * attributes. The {@link #entrySet()} method enumerates over all session attributes and creates a Set of entries.
39 * Note, this will occur lazily - only when the entry set is asked for.
40 *
41 */
42 public class PortletSessionMap extends AbstractMap {
43
44 private static final Logger LOG = LoggerFactory.getLogger(PortletSessionMap.class);
45
46 private PortletSession session = null;
47 private Set<Object> entries = null;
48
49 /***
50 * Creates a new session map given a portlet request.
51 *
52 * @param request the portlet request object.
53 */
54 public PortletSessionMap(PortletRequest request) {
55 this.session = request.getPortletSession();
56 }
57
58 /***
59 * @see java.util.Map#entrySet()
60 */
61 public Set entrySet() {
62 synchronized (session) {
63 if (entries == null) {
64 entries = new HashSet<Object>();
65
66 Enumeration enumeration = session.getAttributeNames();
67
68 while (enumeration.hasMoreElements()) {
69 final String key = enumeration.nextElement().toString();
70 final Object value = session.getAttribute(key);
71 entries.add(new Map.Entry() {
72 public boolean equals(Object obj) {
73 Map.Entry entry = (Map.Entry) obj;
74
75 return ((key == null) ? (entry.getKey() == null)
76 : key.equals(entry.getKey()))
77 && ((value == null) ? (entry.getValue() == null)
78 : value.equals(entry.getValue()));
79 }
80
81 public int hashCode() {
82 return ((key == null) ? 0 : key.hashCode())
83 ^ ((value == null) ? 0 : value.hashCode());
84 }
85
86 public Object getKey() {
87 return key;
88 }
89
90 public Object getValue() {
91 return value;
92 }
93
94 public Object setValue(Object obj) {
95 session.setAttribute(key, obj);
96
97 return value;
98 }
99 });
100 }
101 }
102 }
103
104 return entries;
105 }
106
107 /***
108 * Returns the session attribute associated with the given key or
109 * <tt>null</tt> if it doesn't exist.
110 *
111 * @param key the name of the session attribute.
112 * @return the session attribute or <tt>null</tt> if it doesn't exist.
113 */
114 public Object get(Object key) {
115 synchronized (session) {
116 return session.getAttribute(key.toString());
117 }
118 }
119
120 /***
121 * Saves an attribute in the session.
122 *
123 * @param key the name of the session attribute.
124 * @param value the value to set.
125 * @return the object that was just set.
126 */
127 public Object put(Object key, Object value) {
128 synchronized (session) {
129 entries = null;
130 session.setAttribute(key.toString(), value);
131
132 return get(key);
133 }
134 }
135
136 /***
137 * @see java.util.Map#clear()
138 */
139 public void clear() {
140 synchronized (session) {
141 entries = null;
142 session.invalidate();
143 }
144 }
145
146 /***
147 * Removes the specified session attribute.
148 *
149 * @param key the name of the attribute to remove.
150 * @return the value that was removed or <tt>null</tt> if the value was
151 * not found (and hence, not removed).
152 */
153 public Object remove(Object key) {
154 synchronized (session) {
155 entries = null;
156
157 Object value = get(key);
158 session.removeAttribute(key.toString());
159
160 return value;
161 }
162 }
163 }