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  import javax.portlet.PortletRequest;
29  import org.apache.commons.chain.web.MapEntry;
30  
31  
32  /***
33   * <p>Private implementation of <code>Map</code> for portlet session
34   * attributes.</p>
35   *
36   * @author Craig R. McClanahan
37   * @version $Revision: 412789 $ $Date: 2006-06-08 17:19:14 +0100 (Thu, 08 Jun 2006) $
38   */
39  
40  final class PortletSessionScopeMap implements Map {
41  
42  
43      public PortletSessionScopeMap(PortletRequest request) {
44          this.request = request;
45          sessionExists();
46      }
47  
48  
49      private PortletSession session = null;
50      private PortletRequest request = null;
51  
52  
53      public void clear() {
54          if (sessionExists()) {
55              Iterator keys = keySet().iterator();
56              while (keys.hasNext()) {
57                  session.removeAttribute((String) keys.next());
58              }
59          }
60      }
61  
62  
63      public boolean containsKey(Object key) {
64          if (sessionExists()) {
65              return (session.getAttribute(key(key)) != null);
66          } else {
67              return false;
68          }
69      }
70  
71  
72      public boolean containsValue(Object value) {
73          if (value == null || !sessionExists()) {
74              return (false);
75          }
76          Enumeration keys =
77          session.getAttributeNames(PortletSession.PORTLET_SCOPE);
78          while (keys.hasMoreElements()) {
79              Object next = session.getAttribute((String) keys.nextElement());
80              if (next == value) {
81                  return (true);
82              }
83          }
84          return (false);
85      }
86  
87  
88      public Set entrySet() {
89          Set set = new HashSet();
90          if (sessionExists()) {
91              Enumeration keys =
92              session.getAttributeNames(PortletSession.PORTLET_SCOPE);
93              String key;
94              while (keys.hasMoreElements()) {
95                  key = (String) keys.nextElement();
96                  set.add(new MapEntry(key, session.getAttribute(key), true));
97              }
98          }
99          return (set);
100     }
101 
102 
103     public boolean equals(Object o) {
104         if (sessionExists()) {
105             return (session.equals(o));
106         } else {
107             return false;
108         }
109     }
110 
111 
112     public Object get(Object key) {
113         if (sessionExists()) {
114             return (session.getAttribute(key(key)));
115         } else {
116             return null;
117         }
118     }
119 
120 
121     public int hashCode() {
122         if (sessionExists()) {
123             return (session.hashCode());
124         } else {
125             return 0;
126         }
127     }
128 
129 
130     public boolean isEmpty() {
131         if (sessionExists() &&
132             session.getAttributeNames().hasMoreElements()) {
133             return false;
134         } else {
135             return true;
136         }
137     }
138 
139 
140     public Set keySet() {
141         Set set = new HashSet();
142         if (sessionExists()) {
143             Enumeration keys =
144             session.getAttributeNames(PortletSession.PORTLET_SCOPE);
145             while (keys.hasMoreElements()) {
146                 set.add(keys.nextElement());
147             }
148         }
149         return (set);
150     }
151 
152 
153     public Object put(Object key, Object value) {
154         if (value == null) {
155             return (remove(key));
156         }
157 
158         // Ensure the Session is created, if it
159         // doesn't exist
160         if (session == null) {
161             session = request.getPortletSession();
162             request = null;
163         }
164 
165         String skey = key(key);
166         Object previous = session.getAttribute(skey);
167         session.setAttribute(skey, value);
168         return (previous);
169     }
170 
171 
172     public void putAll(Map map) {
173         Iterator keys = map.keySet().iterator();
174         while (keys.hasNext()) {
175             Object key = keys.next();
176             put(key, map.get(key));
177         }
178     }
179 
180 
181     public Object remove(Object key) {
182         if (sessionExists()) {
183             String skey = key(key);
184             Object previous = session.getAttribute(skey);
185             session.removeAttribute(skey);
186             return (previous);
187         } else {
188             return (null);
189         }
190     }
191 
192 
193     public int size() {
194         int n = 0;
195         if (sessionExists()) {
196             Enumeration keys =
197             session.getAttributeNames(PortletSession.PORTLET_SCOPE);
198             while (keys.hasMoreElements()) {
199                 keys.nextElement();
200                 n++;
201             }
202         }
203         return (n);
204     }
205 
206 
207     public Collection values() {
208         List list = new ArrayList();
209         if (sessionExists()) {
210             Enumeration keys =
211             session.getAttributeNames(PortletSession.PORTLET_SCOPE);
212             while (keys.hasMoreElements()) {
213                 list.add(session.getAttribute((String) keys.nextElement()));
214             }
215         }
216         return (list);
217     }
218 
219 
220     private String key(Object key) {
221         if (key == null) {
222             throw new IllegalArgumentException();
223         } else if (key instanceof String) {
224             return ((String) key);
225         } else {
226             return (key.toString());
227         }
228     }
229 
230     private boolean sessionExists() {
231         if (session == null) {
232             session = request.getPortletSession(false);
233             if (session != null) {
234                 request = null;
235             }
236         }
237         if (session != null) {
238             return true;
239         } else {
240             return false;
241         }
242     }
243 
244 }