001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.chain.web.portlet;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.Enumeration;
021: import java.util.HashSet;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025: import java.util.Set;
026: import javax.portlet.PortletSession;
027: import javax.portlet.PortletRequest;
028: import org.apache.commons.chain.web.MapEntry;
029:
030: /**
031: * <p>Private implementation of <code>Map</code> for portlet session
032: * attributes.</p>
033: *
034: * @author Craig R. McClanahan
035: * @version $Revision: 412789 $ $Date: 2006-06-08 17:19:14 +0100 (Thu, 08 Jun 2006) $
036: */
037:
038: final class PortletSessionScopeMap implements Map {
039:
040: public PortletSessionScopeMap(PortletRequest request) {
041: this .request = request;
042: sessionExists();
043: }
044:
045: private PortletSession session = null;
046: private PortletRequest request = null;
047:
048: public void clear() {
049: if (sessionExists()) {
050: Iterator keys = keySet().iterator();
051: while (keys.hasNext()) {
052: session.removeAttribute((String) keys.next());
053: }
054: }
055: }
056:
057: public boolean containsKey(Object key) {
058: if (sessionExists()) {
059: return (session.getAttribute(key(key)) != null);
060: } else {
061: return false;
062: }
063: }
064:
065: public boolean containsValue(Object value) {
066: if (value == null || !sessionExists()) {
067: return (false);
068: }
069: Enumeration keys = session
070: .getAttributeNames(PortletSession.PORTLET_SCOPE);
071: while (keys.hasMoreElements()) {
072: Object next = session.getAttribute((String) keys
073: .nextElement());
074: if (next == value) {
075: return (true);
076: }
077: }
078: return (false);
079: }
080:
081: public Set entrySet() {
082: Set set = new HashSet();
083: if (sessionExists()) {
084: Enumeration keys = session
085: .getAttributeNames(PortletSession.PORTLET_SCOPE);
086: String key;
087: while (keys.hasMoreElements()) {
088: key = (String) keys.nextElement();
089: set.add(new MapEntry(key, session.getAttribute(key),
090: true));
091: }
092: }
093: return (set);
094: }
095:
096: public boolean equals(Object o) {
097: if (sessionExists()) {
098: return (session.equals(o));
099: } else {
100: return false;
101: }
102: }
103:
104: public Object get(Object key) {
105: if (sessionExists()) {
106: return (session.getAttribute(key(key)));
107: } else {
108: return null;
109: }
110: }
111:
112: public int hashCode() {
113: if (sessionExists()) {
114: return (session.hashCode());
115: } else {
116: return 0;
117: }
118: }
119:
120: public boolean isEmpty() {
121: if (sessionExists()
122: && session.getAttributeNames().hasMoreElements()) {
123: return false;
124: } else {
125: return true;
126: }
127: }
128:
129: public Set keySet() {
130: Set set = new HashSet();
131: if (sessionExists()) {
132: Enumeration keys = session
133: .getAttributeNames(PortletSession.PORTLET_SCOPE);
134: while (keys.hasMoreElements()) {
135: set.add(keys.nextElement());
136: }
137: }
138: return (set);
139: }
140:
141: public Object put(Object key, Object value) {
142: if (value == null) {
143: return (remove(key));
144: }
145:
146: // Ensure the Session is created, if it
147: // doesn't exist
148: if (session == null) {
149: session = request.getPortletSession();
150: request = null;
151: }
152:
153: String skey = key(key);
154: Object previous = session.getAttribute(skey);
155: session.setAttribute(skey, value);
156: return (previous);
157: }
158:
159: public void putAll(Map map) {
160: Iterator keys = map.keySet().iterator();
161: while (keys.hasNext()) {
162: Object key = keys.next();
163: put(key, map.get(key));
164: }
165: }
166:
167: public Object remove(Object key) {
168: if (sessionExists()) {
169: String skey = key(key);
170: Object previous = session.getAttribute(skey);
171: session.removeAttribute(skey);
172: return (previous);
173: } else {
174: return (null);
175: }
176: }
177:
178: public int size() {
179: int n = 0;
180: if (sessionExists()) {
181: Enumeration keys = session
182: .getAttributeNames(PortletSession.PORTLET_SCOPE);
183: while (keys.hasMoreElements()) {
184: keys.nextElement();
185: n++;
186: }
187: }
188: return (n);
189: }
190:
191: public Collection values() {
192: List list = new ArrayList();
193: if (sessionExists()) {
194: Enumeration keys = session
195: .getAttributeNames(PortletSession.PORTLET_SCOPE);
196: while (keys.hasMoreElements()) {
197: list.add(session.getAttribute((String) keys
198: .nextElement()));
199: }
200: }
201: return (list);
202: }
203:
204: private String key(Object key) {
205: if (key == null) {
206: throw new IllegalArgumentException();
207: } else if (key instanceof String) {
208: return ((String) key);
209: } else {
210: return (key.toString());
211: }
212: }
213:
214: private boolean sessionExists() {
215: if (session == null) {
216: session = request.getPortletSession(false);
217: if (session != null) {
218: request = null;
219: }
220: }
221: if (session != null) {
222: return true;
223: } else {
224: return false;
225: }
226: }
227:
228: }
|