View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.chain.web.portlet;
18  
19  
20  import java.util.ArrayList;
21  import java.util.Collection;
22  import java.util.Enumeration;
23  import java.util.HashSet;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Set;
28  import javax.portlet.PortletSession;
29  import javax.portlet.PortletRequest;
30  import org.apache.commons.chain.web.MapEntry;
31  
32  
33  /**
34   * <p>Private implementation of <code>Map</code> for portlet session
35   * attributes.</p>
36   *
37   * @author Craig R. McClanahan
38   * @version $Revision: 661007 $ $Date: 2008-05-28 17:28:49 +0100 (Wed, 28 May 2008) $
39   */
40  
41  final class PortletSessionScopeMap implements Map {
42  
43  
44      public PortletSessionScopeMap(PortletRequest request) {
45          this.request = request;
46          sessionExists();
47      }
48  
49  
50      private PortletSession session = null;
51      private PortletRequest request = null;
52  
53  
54      public void clear() {
55          if (sessionExists()) {
56              Iterator keys = keySet().iterator();
57              while (keys.hasNext()) {
58                  session.removeAttribute((String) keys.next());
59              }
60          }
61      }
62  
63  
64      public boolean containsKey(Object key) {
65          if (sessionExists()) {
66              return (session.getAttribute(key(key)) != null);
67          } else {
68              return false;
69          }
70      }
71  
72  
73      public boolean containsValue(Object value) {
74          if (value == null || !sessionExists()) {
75              return (false);
76          }
77          Enumeration keys =
78          session.getAttributeNames(PortletSession.PORTLET_SCOPE);
79          while (keys.hasMoreElements()) {
80              Object next = session.getAttribute((String) keys.nextElement());
81              if (value.equals(next)) {
82                  return (true);
83              }
84          }
85          return (false);
86      }
87  
88  
89      public Set entrySet() {
90          Set set = new HashSet();
91          if (sessionExists()) {
92              Enumeration keys =
93              session.getAttributeNames(PortletSession.PORTLET_SCOPE);
94              String key;
95              while (keys.hasMoreElements()) {
96                  key = (String) keys.nextElement();
97                  set.add(new MapEntry(key, session.getAttribute(key), true));
98              }
99          }
100         return (set);
101     }
102 
103 
104     public boolean equals(Object o) {
105         if (sessionExists()) {
106             return (session.equals(o));
107         } else {
108             return false;
109         }
110     }
111 
112 
113     public Object get(Object key) {
114         if (sessionExists()) {
115             return (session.getAttribute(key(key)));
116         } else {
117             return null;
118         }
119     }
120 
121 
122     public int hashCode() {
123         if (sessionExists()) {
124             return (session.hashCode());
125         } else {
126             return 0;
127         }
128     }
129 
130 
131     public boolean isEmpty() {
132         if (sessionExists() &&
133             session.getAttributeNames().hasMoreElements()) {
134             return false;
135         } else {
136             return true;
137         }
138     }
139 
140 
141     public Set keySet() {
142         Set set = new HashSet();
143         if (sessionExists()) {
144             Enumeration keys =
145             session.getAttributeNames(PortletSession.PORTLET_SCOPE);
146             while (keys.hasMoreElements()) {
147                 set.add(keys.nextElement());
148             }
149         }
150         return (set);
151     }
152 
153 
154     public Object put(Object key, Object value) {
155         if (value == null) {
156             return (remove(key));
157         }
158 
159         // Ensure the Session is created, if it
160         // doesn't exist
161         if (session == null) {
162             session = request.getPortletSession();
163             request = null;
164         }
165 
166         String skey = key(key);
167         Object previous = session.getAttribute(skey);
168         session.setAttribute(skey, value);
169         return (previous);
170     }
171 
172 
173     public void putAll(Map map) {
174         Iterator entries = map.entrySet().iterator();
175         while (entries.hasNext()) {
176             Map.Entry entry = (Map.Entry)entries.next();
177             put(entry.getKey(), entry.getValue());
178         }
179     }
180 
181 
182     public Object remove(Object key) {
183         if (sessionExists()) {
184             String skey = key(key);
185             Object previous = session.getAttribute(skey);
186             session.removeAttribute(skey);
187             return (previous);
188         } else {
189             return (null);
190         }
191     }
192 
193 
194     public int size() {
195         int n = 0;
196         if (sessionExists()) {
197             Enumeration keys =
198             session.getAttributeNames(PortletSession.PORTLET_SCOPE);
199             while (keys.hasMoreElements()) {
200                 keys.nextElement();
201                 n++;
202             }
203         }
204         return (n);
205     }
206 
207 
208     public Collection values() {
209         List list = new ArrayList();
210         if (sessionExists()) {
211             Enumeration keys =
212             session.getAttributeNames(PortletSession.PORTLET_SCOPE);
213             while (keys.hasMoreElements()) {
214                 list.add(session.getAttribute((String) keys.nextElement()));
215             }
216         }
217         return (list);
218     }
219 
220 
221     private String key(Object key) {
222         if (key == null) {
223             throw new IllegalArgumentException();
224         } else if (key instanceof String) {
225             return ((String) key);
226         } else {
227             return (key.toString());
228         }
229     }
230 
231     private boolean sessionExists() {
232         if (session == null) {
233             session = request.getPortletSession(false);
234             if (session != null) {
235                 request = null;
236             }
237         }
238         if (session != null) {
239             return true;
240         } else {
241             return false;
242         }
243     }
244 
245 }