View Javadoc

1   /*
2    * $Id: PortletSessionMap.java 651946 2008-04-27 13:41:38Z apetrelli $
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  
22  package org.apache.struts2.portlet;
23  
24  import java.util.AbstractMap;
25  import java.util.Enumeration;
26  import java.util.HashSet;
27  import java.util.Map;
28  import java.util.Set;
29  
30  import javax.portlet.PortletRequest;
31  import javax.portlet.PortletSession;
32  
33  import com.opensymphony.xwork2.util.logging.Logger;
34  import com.opensymphony.xwork2.util.logging.LoggerFactory;
35  
36  /***
37   * A simple implementation of the {@link java.util.Map} interface to handle a collection of portlet session
38   * attributes. The {@link #entrySet()} method enumerates over all session attributes and creates a Set of entries.
39   * Note, this will occur lazily - only when the entry set is asked for.
40   *
41   */
42  public class PortletSessionMap extends AbstractMap {
43  
44      private static final Logger LOG = LoggerFactory.getLogger(PortletSessionMap.class);
45  
46      private PortletSession session = null;
47      private Set<Object> entries = null;
48  
49      /***
50       * Creates a new session map given a portlet request.
51       *
52       * @param request the portlet request object.
53       */
54      public PortletSessionMap(PortletRequest request) {
55          this.session = request.getPortletSession();
56      }
57  
58      /***
59       * @see java.util.Map#entrySet()
60       */
61      public Set entrySet() {
62          synchronized (session) {
63              if (entries == null) {
64                  entries = new HashSet<Object>();
65  
66                  Enumeration enumeration = session.getAttributeNames();
67  
68                  while (enumeration.hasMoreElements()) {
69                      final String key = enumeration.nextElement().toString();
70                      final Object value = session.getAttribute(key);
71                      entries.add(new Map.Entry() {
72                          public boolean equals(Object obj) {
73                              Map.Entry entry = (Map.Entry) obj;
74  
75                              return ((key == null) ? (entry.getKey() == null)
76                                      : key.equals(entry.getKey()))
77                                      && ((value == null) ? (entry.getValue() == null)
78                                              : value.equals(entry.getValue()));
79                          }
80  
81                          public int hashCode() {
82                              return ((key == null) ? 0 : key.hashCode())
83                                      ^ ((value == null) ? 0 : value.hashCode());
84                          }
85  
86                          public Object getKey() {
87                              return key;
88                          }
89  
90                          public Object getValue() {
91                              return value;
92                          }
93  
94                          public Object setValue(Object obj) {
95                              session.setAttribute(key, obj);
96  
97                              return value;
98                          }
99                      });
100                 }
101             }
102         }
103 
104         return entries;
105     }
106 
107     /***
108      * Returns the session attribute associated with the given key or
109      * <tt>null</tt> if it doesn't exist.
110      *
111      * @param key the name of the session attribute.
112      * @return the session attribute or <tt>null</tt> if it doesn't exist.
113      */
114     public Object get(Object key) {
115         synchronized (session) {
116             return session.getAttribute(key.toString());
117         }
118     }
119 
120     /***
121      * Saves an attribute in the session.
122      *
123      * @param key the name of the session attribute.
124      * @param value the value to set.
125      * @return the object that was just set.
126      */
127     public Object put(Object key, Object value) {
128         synchronized (session) {
129             entries = null;
130             session.setAttribute(key.toString(), value);
131 
132             return get(key);
133         }
134     }
135 
136     /***
137      * @see java.util.Map#clear()
138      */
139     public void clear() {
140         synchronized (session) {
141             entries = null;
142             session.invalidate();
143         }
144     }
145 
146     /***
147      * Removes the specified session attribute.
148      *
149      * @param key the name of the attribute to remove.
150      * @return the value that was removed or <tt>null</tt> if the value was
151      * not found (and hence, not removed).
152      */
153     public Object remove(Object key) {
154         synchronized (session) {
155             entries = null;
156 
157             Object value = get(key);
158             session.removeAttribute(key.toString());
159 
160             return value;
161         }
162     }
163 }