001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.portlet;
006:
007: import java.io.Serializable;
008: import java.util.AbstractMap;
009: import java.util.Enumeration;
010: import java.util.HashSet;
011: import java.util.Map;
012: import java.util.Set;
013:
014: import javax.portlet.PortletRequest;
015: import javax.portlet.PortletSession;
016:
017: import org.apache.commons.logging.Log;
018: import org.apache.commons.logging.LogFactory;
019:
020: /**
021: * A simple implementation of the {@link java.util.Map} interface to handle a collection of portlet session
022: * attributes. The {@link #entrySet()} method enumerates over all session attributes and creates a Set of entries.
023: * Note, this will occur lazily - only when the entry set is asked for.
024: *
025: * @author Nils-Helge Garli
026: */
027: public class PortletSessionMap extends AbstractMap implements
028: Serializable {
029:
030: private static final long serialVersionUID = 1627179482311266132L;
031:
032: private static final Log LOG = LogFactory
033: .getLog(PortletSessionMap.class);
034:
035: private PortletSession session = null;
036:
037: private Set entries = null;
038:
039: /**
040: * Creates a new session map given a portlet request.
041: *
042: * @param request the portlet request object.
043: */
044: public PortletSessionMap(PortletRequest request) {
045: this .session = request.getPortletSession();
046: if (LOG.isDebugEnabled()) {
047: LOG.debug("Dumping session info: ");
048: Enumeration enumeration = session.getAttributeNames();
049: while (enumeration.hasMoreElements()) {
050: String key = (String) enumeration.nextElement();
051: Object val = session.getAttribute(key);
052: LOG.debug(key + " = " + val);
053: }
054: }
055: }
056:
057: /**
058: * @see java.util.Map#entrySet()
059: */
060: public Set entrySet() {
061: synchronized (session) {
062: if (entries == null) {
063: entries = new HashSet();
064:
065: Enumeration enumeration = session.getAttributeNames();
066:
067: while (enumeration.hasMoreElements()) {
068: final String key = enumeration.nextElement()
069: .toString();
070: final Object value = session.getAttribute(key);
071: entries.add(new Map.Entry() {
072: public boolean equals(Object obj) {
073: Map.Entry entry = (Map.Entry) obj;
074:
075: return ((key == null) ? (entry.getKey() == null)
076: : key.equals(entry.getKey()))
077: && ((value == null) ? (entry
078: .getValue() == null)
079: : value.equals(entry
080: .getValue()));
081: }
082:
083: public int hashCode() {
084: return ((key == null) ? 0 : key.hashCode())
085: ^ ((value == null) ? 0 : value
086: .hashCode());
087: }
088:
089: public Object getKey() {
090: return key;
091: }
092:
093: public Object getValue() {
094: return value;
095: }
096:
097: public Object setValue(Object obj) {
098: session.setAttribute(key, obj);
099:
100: return value;
101: }
102: });
103: }
104: }
105: }
106:
107: return entries;
108: }
109:
110: /**
111: * Returns the session attribute associated with the given key or
112: * <tt>null</tt> if it doesn't exist.
113: *
114: * @param key the name of the session attribute.
115: * @return the session attribute or <tt>null</tt> if it doesn't exist.
116: */
117: public Object get(Object key) {
118: synchronized (session) {
119: return session.getAttribute(key.toString());
120: }
121: }
122:
123: /**
124: * Saves an attribute in the session.
125: *
126: * @param key the name of the session attribute.
127: * @param value the value to set.
128: * @return the object that was just set.
129: */
130: public Object put(Object key, Object value) {
131: synchronized (session) {
132: entries = null;
133: session.setAttribute(key.toString(), value);
134:
135: return get(key);
136: }
137: }
138:
139: /**
140: * @see java.util.Map#clear()
141: */
142: public void clear() {
143: synchronized (session) {
144: entries = null;
145: session.invalidate();
146: }
147: }
148:
149: /**
150: * Removes the specified session attribute.
151: *
152: * @param key the name of the attribute to remove.
153: * @return the value that was removed or <tt>null</tt> if the value was
154: * not found (and hence, not removed).
155: */
156: public Object remove(Object key) {
157: synchronized (session) {
158: entries = null;
159:
160: Object value = get(key);
161: session.removeAttribute(key.toString());
162:
163: return value;
164: }
165: }
166: }
|