001: /*
002: * $Id: PortletSessionMap.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.portlet;
022:
023: import java.util.AbstractMap;
024: import java.util.Enumeration;
025: import java.util.HashSet;
026: import java.util.Map;
027: import java.util.Set;
028:
029: import javax.portlet.PortletRequest;
030: import javax.portlet.PortletSession;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034:
035: /**
036: * A simple implementation of the {@link java.util.Map} interface to handle a collection of portlet session
037: * attributes. The {@link #entrySet()} method enumerates over all session attributes and creates a Set of entries.
038: * Note, this will occur lazily - only when the entry set is asked for.
039: *
040: */
041: public class PortletSessionMap extends AbstractMap {
042:
043: private static final Log LOG = LogFactory
044: .getLog(PortletSessionMap.class);
045:
046: private PortletSession session = null;
047: private Set<Object> entries = null;
048:
049: /**
050: * Creates a new session map given a portlet request.
051: *
052: * @param request the portlet request object.
053: */
054: public PortletSessionMap(PortletRequest request) {
055: this .session = request.getPortletSession();
056: if (LOG.isDebugEnabled()) {
057: LOG.debug("Dumping session info: ");
058: Enumeration enumeration = session.getAttributeNames();
059: while (enumeration.hasMoreElements()) {
060: String key = (String) enumeration.nextElement();
061: Object val = session.getAttribute(key);
062: LOG.debug(key + " = " + val);
063: }
064: }
065: }
066:
067: /**
068: * @see java.util.Map#entrySet()
069: */
070: public Set entrySet() {
071: synchronized (session) {
072: if (entries == null) {
073: entries = new HashSet<Object>();
074:
075: Enumeration enumeration = session.getAttributeNames();
076:
077: while (enumeration.hasMoreElements()) {
078: final String key = enumeration.nextElement()
079: .toString();
080: final Object value = session.getAttribute(key);
081: entries.add(new Map.Entry() {
082: public boolean equals(Object obj) {
083: Map.Entry entry = (Map.Entry) obj;
084:
085: return ((key == null) ? (entry.getKey() == null)
086: : key.equals(entry.getKey()))
087: && ((value == null) ? (entry
088: .getValue() == null)
089: : value.equals(entry
090: .getValue()));
091: }
092:
093: public int hashCode() {
094: return ((key == null) ? 0 : key.hashCode())
095: ^ ((value == null) ? 0 : value
096: .hashCode());
097: }
098:
099: public Object getKey() {
100: return key;
101: }
102:
103: public Object getValue() {
104: return value;
105: }
106:
107: public Object setValue(Object obj) {
108: session.setAttribute(key, obj);
109:
110: return value;
111: }
112: });
113: }
114: }
115: }
116:
117: return entries;
118: }
119:
120: /**
121: * Returns the session attribute associated with the given key or
122: * <tt>null</tt> if it doesn't exist.
123: *
124: * @param key the name of the session attribute.
125: * @return the session attribute or <tt>null</tt> if it doesn't exist.
126: */
127: public Object get(Object key) {
128: synchronized (session) {
129: return session.getAttribute(key.toString());
130: }
131: }
132:
133: /**
134: * Saves an attribute in the session.
135: *
136: * @param key the name of the session attribute.
137: * @param value the value to set.
138: * @return the object that was just set.
139: */
140: public Object put(Object key, Object value) {
141: synchronized (session) {
142: entries = null;
143: session.setAttribute(key.toString(), value);
144:
145: return get(key);
146: }
147: }
148:
149: /**
150: * @see java.util.Map#clear()
151: */
152: public void clear() {
153: synchronized (session) {
154: entries = null;
155: session.invalidate();
156: }
157: }
158:
159: /**
160: * Removes the specified session attribute.
161: *
162: * @param key the name of the attribute to remove.
163: * @return the value that was removed or <tt>null</tt> if the value was
164: * not found (and hence, not removed).
165: */
166: public Object remove(Object key) {
167: synchronized (session) {
168: entries = null;
169:
170: Object value = get(key);
171: session.removeAttribute(key.toString());
172:
173: return value;
174: }
175: }
176: }
|