This project has retired. For details please refer to its
Attic page.
PortletSessionScopeMap xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.tiles.portlet.context;
22
23
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Enumeration;
27 import java.util.HashSet;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Set;
32
33 import javax.portlet.PortletSession;
34
35 import org.apache.tiles.context.MapEntry;
36
37 /***
38 * <p>Private implementation of <code>Map</code> for portlet session
39 * attributes.</p>
40 *
41 * @version $Rev: 581978 $ $Date: 2007-10-04 19:59:41 +0200 (gio, 04 ott 2007) $
42 */
43
44 final class PortletSessionScopeMap implements Map<String, Object> {
45
46
47 /***
48 * Constructor.
49 *
50 * @param session The portlet session to use.
51 */
52 public PortletSessionScopeMap(PortletSession session) {
53 this.session = session;
54 }
55
56
57 /***
58 * The portlet session to use.
59 */
60 private PortletSession session = null;
61
62
63 /*** {@inheritDoc} */
64 public void clear() {
65 Iterator<String> keys = keySet().iterator();
66 while (keys.hasNext()) {
67 session.removeAttribute(keys.next());
68 }
69 }
70
71
72 /*** {@inheritDoc} */
73 public boolean containsKey(Object key) {
74 return (session.getAttribute(key(key)) != null);
75 }
76
77
78 /*** {@inheritDoc} */
79 @SuppressWarnings("unchecked")
80 public boolean containsValue(Object value) {
81 if (value == null) {
82 return (false);
83 }
84 Enumeration<String> keys =
85 session.getAttributeNames(PortletSession.PORTLET_SCOPE);
86 while (keys.hasMoreElements()) {
87 Object next = session.getAttribute(keys.nextElement());
88 if (next == value) {
89 return (true);
90 }
91 }
92 return (false);
93 }
94
95
96 /*** {@inheritDoc} */
97 @SuppressWarnings("unchecked")
98 public Set<Map.Entry<String, Object>> entrySet() {
99 Set<Map.Entry<String, Object>> set = new HashSet<Map.Entry<String, Object>>();
100 Enumeration<String> keys =
101 session.getAttributeNames(PortletSession.PORTLET_SCOPE);
102 String key;
103 while (keys.hasMoreElements()) {
104 key = keys.nextElement();
105 set.add(new MapEntry<String, Object>(key, session.getAttribute(key), true));
106 }
107 return (set);
108 }
109
110
111 /*** {@inheritDoc} */
112 @SuppressWarnings("unchecked")
113 public boolean equals(Object o) {
114 PortletSession otherSession = ((PortletSessionScopeMap) o).session;
115 boolean retValue = true;
116 synchronized (session) {
117 for (Enumeration<String> attribs = session.getAttributeNames(); attribs
118 .hasMoreElements()
119 && retValue;) {
120 String attributeName = attribs.nextElement();
121 retValue = session.getAttribute(attributeName).equals(
122 otherSession.getAttribute(attributeName));
123 }
124 }
125
126 return retValue;
127 }
128
129
130 /*** {@inheritDoc} */
131 public Object get(Object key) {
132 return (session.getAttribute(key(key)));
133 }
134
135
136 /*** {@inheritDoc} */
137 public int hashCode() {
138 return (session.hashCode());
139 }
140
141
142 /*** {@inheritDoc} */
143 public boolean isEmpty() {
144 return (size() < 1);
145 }
146
147
148 /*** {@inheritDoc} */
149 @SuppressWarnings("unchecked")
150 public Set<String> keySet() {
151 Set<String> set = new HashSet<String>();
152 Enumeration<String> keys =
153 session.getAttributeNames(PortletSession.PORTLET_SCOPE);
154 while (keys.hasMoreElements()) {
155 set.add(keys.nextElement());
156 }
157 return (set);
158 }
159
160
161 /*** {@inheritDoc} */
162 public Object put(String key, Object value) {
163 if (value == null) {
164 return (remove(key));
165 }
166 String skey = key(key);
167 Object previous = session.getAttribute(skey);
168 session.setAttribute(skey, value);
169 return (previous);
170 }
171
172
173 /*** {@inheritDoc} */
174 public void putAll(Map<? extends String, ? extends Object> map) {
175 Iterator<? extends String> keys = map.keySet().iterator();
176 while (keys.hasNext()) {
177 String key = keys.next();
178 session.setAttribute(key, map.get(key));
179 }
180 }
181
182
183 /*** {@inheritDoc} */
184 public Object remove(Object key) {
185 String skey = key(key);
186 Object previous = session.getAttribute(skey);
187 session.removeAttribute(skey);
188 return (previous);
189 }
190
191
192 /*** {@inheritDoc} */
193 @SuppressWarnings("unchecked")
194 public int size() {
195 int n = 0;
196 Enumeration<String> keys =
197 session.getAttributeNames(PortletSession.PORTLET_SCOPE);
198 while (keys.hasMoreElements()) {
199 keys.nextElement();
200 n++;
201 }
202 return (n);
203 }
204
205
206 /*** {@inheritDoc} */
207 @SuppressWarnings("unchecked")
208 public Collection<Object> values() {
209 List<Object> list = new ArrayList<Object>();
210 Enumeration<String> keys =
211 session.getAttributeNames(PortletSession.PORTLET_SCOPE);
212 while (keys.hasMoreElements()) {
213 list.add(session.getAttribute(keys.nextElement()));
214 }
215 return (list);
216 }
217
218
219 /***
220 * Returns the string representation of the key.
221 *
222 * @param key The key.
223 * @return The string representation of the key.
224 * @throws IllegalArgumentException If the key is <code>null</code>.
225 */
226 private String key(Object key) {
227 if (key == null) {
228 throw new IllegalArgumentException();
229 } else if (key instanceof String) {
230 return ((String) key);
231 } else {
232 return (key.toString());
233 }
234 }
235
236
237 }