View Javadoc

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