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.PortletRequest;
027: import org.apache.commons.chain.web.MapEntry;
028:
029: /**
030: * <p>Private implementation of <code>Map</code> for portlet request
031: * attributes.</p>
032: *
033: * @author Craig R. McClanahan
034: * @version $Revision: 412719 $ $Date: 2006-06-08 11:49:18 +0100 (Thu, 08 Jun 2006) $
035: */
036:
037: final class PortletRequestScopeMap implements Map {
038:
039: public PortletRequestScopeMap(PortletRequest request) {
040: this .request = request;
041: }
042:
043: private PortletRequest request = null;
044:
045: public void clear() {
046: Iterator keys = keySet().iterator();
047: while (keys.hasNext()) {
048: request.removeAttribute((String) keys.next());
049: }
050: }
051:
052: public boolean containsKey(Object key) {
053: return (request.getAttribute(key(key)) != null);
054: }
055:
056: public boolean containsValue(Object value) {
057: if (value == null) {
058: return (false);
059: }
060: Enumeration keys = request.getAttributeNames();
061: while (keys.hasMoreElements()) {
062: Object next = request.getAttribute((String) keys
063: .nextElement());
064: if (next == value) {
065: return (true);
066: }
067: }
068: return (false);
069: }
070:
071: public Set entrySet() {
072: Set set = new HashSet();
073: Enumeration keys = request.getAttributeNames();
074: String key;
075: while (keys.hasMoreElements()) {
076: key = (String) keys.nextElement();
077: set.add(new MapEntry(key, request.getAttribute(key), true));
078: }
079: return (set);
080: }
081:
082: public boolean equals(Object o) {
083: return (request.equals(o));
084: }
085:
086: public Object get(Object key) {
087: return (request.getAttribute(key(key)));
088: }
089:
090: public int hashCode() {
091: return (request.hashCode());
092: }
093:
094: public boolean isEmpty() {
095: return (size() < 1);
096: }
097:
098: public Set keySet() {
099: Set set = new HashSet();
100: Enumeration keys = request.getAttributeNames();
101: while (keys.hasMoreElements()) {
102: set.add(keys.nextElement());
103: }
104: return (set);
105: }
106:
107: public Object put(Object key, Object value) {
108: if (value == null) {
109: return (remove(key));
110: }
111: String skey = key(key);
112: Object previous = request.getAttribute(skey);
113: request.setAttribute(skey, value);
114: return (previous);
115: }
116:
117: public void putAll(Map map) {
118: Iterator keys = map.keySet().iterator();
119: while (keys.hasNext()) {
120: String key = (String) keys.next();
121: request.setAttribute(key, map.get(key));
122: }
123: }
124:
125: public Object remove(Object key) {
126: String skey = key(key);
127: Object previous = request.getAttribute(skey);
128: request.removeAttribute(skey);
129: return (previous);
130: }
131:
132: public int size() {
133: int n = 0;
134: Enumeration keys = request.getAttributeNames();
135: while (keys.hasMoreElements()) {
136: keys.nextElement();
137: n++;
138: }
139: return (n);
140: }
141:
142: public Collection values() {
143: List list = new ArrayList();
144: Enumeration keys = request.getAttributeNames();
145: while (keys.hasMoreElements()) {
146: list.add(request.getAttribute((String) keys.nextElement()));
147: }
148: return (list);
149: }
150:
151: private String key(Object key) {
152: if (key == null) {
153: throw new IllegalArgumentException();
154: } else if (key instanceof String) {
155: return ((String) key);
156: } else {
157: return (key.toString());
158: }
159: }
160:
161: }
|