KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > chain > web > portlet > PortletSessionScopeMap


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.chain.web.portlet;
17
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27 import javax.portlet.PortletSession;
28
29
30 /**
31  * <p>Private implementation of <code>Map</code> for portlet session
32  * attributes.</p>
33  *
34  * @author Craig R. McClanahan
35  * @version $Revision: 1.5 $ $Date: 2004/11/30 05:52:23 $
36  */

37
38 final class PortletSessionScopeMap implements Map JavaDoc {
39
40
41     public PortletSessionScopeMap(PortletSession session) {
42         this.session = session;
43     }
44
45
46     private PortletSession session = null;
47
48
49     public void clear() {
50         Iterator JavaDoc keys = keySet().iterator();
51         while (keys.hasNext()) {
52             session.removeAttribute((String JavaDoc) keys.next());
53         }
54     }
55
56
57     public boolean containsKey(Object JavaDoc key) {
58         return (session.getAttribute(key(key)) != null);
59     }
60
61
62     public boolean containsValue(Object JavaDoc value) {
63         if (value == null) {
64             return (false);
65         }
66         Enumeration JavaDoc keys =
67         session.getAttributeNames(PortletSession.PORTLET_SCOPE);
68         while (keys.hasMoreElements()) {
69             Object JavaDoc next = session.getAttribute((String JavaDoc) keys.nextElement());
70             if (next == value) {
71                 return (true);
72             }
73         }
74         return (false);
75     }
76
77
78     public Set JavaDoc entrySet() {
79         Set JavaDoc set = new HashSet JavaDoc();
80         Enumeration JavaDoc keys =
81         session.getAttributeNames(PortletSession.PORTLET_SCOPE);
82         while (keys.hasMoreElements()) {
83             set.add(session.getAttribute((String JavaDoc) keys.nextElement()));
84         }
85         return (set);
86     }
87
88
89     public boolean equals(Object JavaDoc o) {
90         return (session.equals(o));
91     }
92
93
94     public Object JavaDoc get(Object JavaDoc key) {
95         return (session.getAttribute(key(key)));
96     }
97
98
99     public int hashCode() {
100         return (session.hashCode());
101     }
102
103
104     public boolean isEmpty() {
105         return (size() < 1);
106     }
107
108
109     public Set JavaDoc keySet() {
110         Set JavaDoc set = new HashSet JavaDoc();
111         Enumeration JavaDoc keys =
112         session.getAttributeNames(PortletSession.PORTLET_SCOPE);
113         while (keys.hasMoreElements()) {
114             set.add(keys.nextElement());
115         }
116         return (set);
117     }
118
119
120     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
121         if (value == null) {
122             return (remove(key));
123         }
124         String JavaDoc skey = key(key);
125         Object JavaDoc previous = session.getAttribute(skey);
126         session.setAttribute(skey, value);
127         return (previous);
128     }
129
130
131     public void putAll(Map JavaDoc map) {
132         Iterator JavaDoc keys = map.keySet().iterator();
133         while (keys.hasNext()) {
134             String JavaDoc key = (String JavaDoc) keys.next();
135             session.setAttribute(key, map.get(key));
136         }
137     }
138
139
140     public Object JavaDoc remove(Object JavaDoc key) {
141         String JavaDoc skey = key(key);
142         Object JavaDoc previous = session.getAttribute(skey);
143         session.removeAttribute(skey);
144         return (previous);
145     }
146
147
148     public int size() {
149         int n = 0;
150         Enumeration JavaDoc keys =
151         session.getAttributeNames(PortletSession.PORTLET_SCOPE);
152         while (keys.hasMoreElements()) {
153             keys.nextElement();
154             n++;
155         }
156         return (n);
157     }
158
159
160     public Collection JavaDoc values() {
161         List JavaDoc list = new ArrayList JavaDoc();
162         Enumeration JavaDoc keys =
163         session.getAttributeNames(PortletSession.PORTLET_SCOPE);
164         while (keys.hasMoreElements()) {
165             list.add(session.getAttribute((String JavaDoc) keys.nextElement()));
166         }
167         return (list);
168     }
169
170
171     private String JavaDoc key(Object JavaDoc key) {
172         if (key == null) {
173             throw new IllegalArgumentException JavaDoc();
174         } else if (key instanceof String JavaDoc) {
175             return ((String JavaDoc) key);
176         } else {
177             return (key.toString());
178         }
179     }
180
181
182 }
183
Popular Tags