View Javadoc

1   /*
2    * $Id: PortletSessionMap.java 421778 2006-07-14 01:09:55Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.portlet;
19  
20  import java.util.AbstractMap;
21  import java.util.Enumeration;
22  import java.util.HashSet;
23  import java.util.Map;
24  import java.util.Set;
25  
26  import javax.portlet.PortletRequest;
27  import javax.portlet.PortletSession;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  /***
33   * A simple implementation of the {@link java.util.Map} interface to handle a collection of portlet session
34   * attributes. The {@link #entrySet()} method enumerates over all session attributes and creates a Set of entries.
35   * Note, this will occur lazily - only when the entry set is asked for.
36   * 
37   */
38  public class PortletSessionMap extends AbstractMap {
39      
40      private static final Log LOG = LogFactory.getLog(PortletSessionMap.class);
41  
42      private PortletSession session = null;
43      private Set<Object> entries = null;
44  
45      /***
46       * Creates a new session map given a portlet request. 
47       *
48       * @param request the portlet request object.
49       */
50      public PortletSessionMap(PortletRequest request) {
51          this.session = request.getPortletSession();
52          if(LOG.isDebugEnabled()) {
53              LOG.debug("Dumping session info: ");
54              Enumeration enumeration = session.getAttributeNames();
55              while(enumeration.hasMoreElements()) {
56                  String key = (String)enumeration.nextElement();
57                  Object val = session.getAttribute(key);
58                  LOG.debug(key + " = " + val);
59              }
60          }
61      }
62  
63      /***
64       * @see java.util.Map#entrySet()
65       */
66      public Set entrySet() {
67          synchronized (session) {
68              if (entries == null) {
69                  entries = new HashSet<Object>();
70  
71                  Enumeration enumeration = session.getAttributeNames();
72  
73                  while (enumeration.hasMoreElements()) {
74                      final String key = enumeration.nextElement().toString();
75                      final Object value = session.getAttribute(key);
76                      entries.add(new Map.Entry() {
77                          public boolean equals(Object obj) {
78                              Map.Entry entry = (Map.Entry) obj;
79  
80                              return ((key == null) ? (entry.getKey() == null)
81                                      : key.equals(entry.getKey()))
82                                      && ((value == null) ? (entry.getValue() == null)
83                                              : value.equals(entry.getValue()));
84                          }
85  
86                          public int hashCode() {
87                              return ((key == null) ? 0 : key.hashCode())
88                                      ^ ((value == null) ? 0 : value.hashCode());
89                          }
90  
91                          public Object getKey() {
92                              return key;
93                          }
94  
95                          public Object getValue() {
96                              return value;
97                          }
98  
99                          public Object setValue(Object obj) {
100                             session.setAttribute(key, obj);
101 
102                             return value;
103                         }
104                     });
105                 }
106             }
107         }
108 
109         return entries;
110     }
111 
112     /***
113      * Returns the session attribute associated with the given key or
114      * <tt>null</tt> if it doesn't exist.
115      * 
116      * @param key the name of the session attribute.
117      * @return the session attribute or <tt>null</tt> if it doesn't exist.
118      */
119     public Object get(Object key) {
120         synchronized (session) {
121             return session.getAttribute(key.toString());
122         }
123     }
124 
125     /***
126      * Saves an attribute in the session.
127      * 
128      * @param key the name of the session attribute.
129      * @param value the value to set.
130      * @return the object that was just set.
131      */
132     public Object put(Object key, Object value) {
133         synchronized (session) {
134             entries = null;
135             session.setAttribute(key.toString(), value);
136 
137             return get(key);
138         }
139     }
140 
141     /***
142      * @see java.util.Map#clear()
143      */
144     public void clear() {
145         synchronized (session) {
146             entries = null;
147             session.invalidate();
148         }
149     }
150 
151     /***
152      * Removes the specified session attribute.
153      * 
154      * @param key the name of the attribute to remove.
155      * @return the value that was removed or <tt>null</tt> if the value was
156      * not found (and hence, not removed).
157      */
158     public Object remove(Object key) {
159         synchronized (session) {
160             entries = null;
161 
162             Object value = get(key);
163             session.removeAttribute(key.toString());
164 
165             return value;
166         }
167     }
168 }