View Javadoc

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;
20  import java.util.Collection;
21  import java.util.Enumeration;
22  import java.util.HashSet;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Set;
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 {
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 keys = keySet().iterator();
51          while (keys.hasNext()) {
52              session.removeAttribute((String) keys.next());
53          }
54      }
55  
56  
57      public boolean containsKey(Object key) {
58          return (session.getAttribute(key(key)) != null);
59      }
60  
61  
62      public boolean containsValue(Object value) {
63          if (value == null) {
64              return (false);
65          }
66          Enumeration keys =
67          session.getAttributeNames(PortletSession.PORTLET_SCOPE);
68          while (keys.hasMoreElements()) {
69              Object next = session.getAttribute((String) keys.nextElement());
70              if (next == value) {
71                  return (true);
72              }
73          }
74          return (false);
75      }
76  
77  
78      public Set entrySet() {
79          Set set = new HashSet();
80          Enumeration keys =
81          session.getAttributeNames(PortletSession.PORTLET_SCOPE);
82          while (keys.hasMoreElements()) {
83              set.add(session.getAttribute((String) keys.nextElement()));
84          }
85          return (set);
86      }
87  
88  
89      public boolean equals(Object o) {
90          return (session.equals(o));
91      }
92  
93  
94      public Object get(Object 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 keySet() {
110         Set set = new HashSet();
111         Enumeration 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 put(Object key, Object value) {
121         if (value == null) {
122             return (remove(key));
123         }
124         String skey = key(key);
125         Object previous = session.getAttribute(skey);
126         session.setAttribute(skey, value);
127         return (previous);
128     }
129 
130 
131     public void putAll(Map map) {
132         Iterator keys = map.keySet().iterator();
133         while (keys.hasNext()) {
134             String key = (String) keys.next();
135             session.setAttribute(key, map.get(key));
136         }
137     }
138 
139 
140     public Object remove(Object key) {
141         String skey = key(key);
142         Object previous = session.getAttribute(skey);
143         session.removeAttribute(skey);
144         return (previous);
145     }
146 
147 
148     public int size() {
149         int n = 0;
150         Enumeration 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 values() {
161         List list = new ArrayList();
162         Enumeration keys =
163         session.getAttributeNames(PortletSession.PORTLET_SCOPE);
164         while (keys.hasMoreElements()) {
165             list.add(session.getAttribute((String) keys.nextElement()));
166         }
167         return (list);
168     }
169 
170 
171     private String key(Object key) {
172         if (key == null) {
173             throw new IllegalArgumentException();
174         } else if (key instanceof String) {
175             return ((String) key);
176         } else {
177             return (key.toString());
178         }
179     }
180 
181 
182 }