This project has retired. For details please refer to its Attic page.
ServletRequestScopeMap xref
View Javadoc

1   /*
2    * $Id: ServletRequestScopeMap.java 581987 2007-10-04 19:03:29Z 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  package org.apache.tiles.servlet.context;
22  
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.Enumeration;
26  import java.util.HashSet;
27  import java.util.Iterator;
28  import java.util.List;
29  import java.util.Map;
30  import java.util.Set;
31  
32  import javax.servlet.ServletRequest;
33  
34  import org.apache.tiles.context.MapEntry;
35  
36  /***
37   * <p>Private implementation of <code>Map</code> for servlet request
38   * attributes.</p>
39   *
40   * @version $Rev: 581987 $ $Date: 2007-10-04 21:03:29 +0200 (Thu, 04 Oct 2007) $
41   */
42  
43  final class ServletRequestScopeMap implements Map<String, Object> {
44  
45  
46      /***
47       * Constructor.
48       *
49       * @param request The request object to use.
50       */
51      public ServletRequestScopeMap(ServletRequest request) {
52          this.request = request;
53      }
54  
55  
56      /***
57       * The request object to use.
58       */
59      private ServletRequest request = null;
60  
61  
62      /*** {@inheritDoc} */
63      public void clear() {
64          Iterator<String> keys = keySet().iterator();
65          while (keys.hasNext()) {
66              request.removeAttribute(keys.next());
67          }
68      }
69  
70  
71      /*** {@inheritDoc} */
72      public boolean containsKey(Object key) {
73          return (request.getAttribute(key(key)) != null);
74      }
75  
76  
77      /*** {@inheritDoc} */
78      @SuppressWarnings("unchecked")
79      public boolean containsValue(Object value) {
80          if (value == null) {
81              return (false);
82          }
83          Enumeration<String> keys = request.getAttributeNames();
84          while (keys.hasMoreElements()) {
85              Object next = request.getAttribute(keys.nextElement());
86              if (next == value) {
87                  return (true);
88              }
89          }
90          return (false);
91      }
92  
93  
94      /*** {@inheritDoc} */
95      @SuppressWarnings("unchecked")
96      public Set<Map.Entry<String, Object>> entrySet() {
97          Set<Map.Entry<String, Object>> set = new HashSet<Map.Entry<String, Object>>();
98          Enumeration<String> keys = request.getAttributeNames();
99          String key;
100         while (keys.hasMoreElements()) {
101             key = keys.nextElement();
102             set.add(new MapEntry<String, Object>(key,
103                     request.getAttribute(key), true));
104         }
105         return (set);
106     }
107 
108 
109     /*** {@inheritDoc} */
110     @SuppressWarnings("unchecked")
111     public boolean equals(Object o) {
112         ServletRequest otherRequest = ((ServletRequestScopeMap) o).request;
113         boolean retValue = true;
114         synchronized (request) {
115             for (Enumeration<String> attribs = request.getAttributeNames(); attribs
116                     .hasMoreElements()
117                     && retValue;) {
118                 String attributeName = attribs.nextElement();
119                 retValue = request.getAttribute(attributeName).equals(
120                         otherRequest.getAttribute(attributeName));
121             }
122         }
123 
124         return retValue;
125     }
126 
127 
128     /*** {@inheritDoc} */
129     public Object get(Object key) {
130         return (request.getAttribute(key(key)));
131     }
132 
133 
134     /*** {@inheritDoc} */
135     public int hashCode() {
136         return (request.hashCode());
137     }
138 
139 
140     /*** {@inheritDoc} */
141     public boolean isEmpty() {
142         return (size() < 1);
143     }
144 
145 
146     /*** {@inheritDoc} */
147     @SuppressWarnings("unchecked")
148     public Set<String> keySet() {
149         Set<String> set = new HashSet<String>();
150         Enumeration<String> keys = request.getAttributeNames();
151         while (keys.hasMoreElements()) {
152             set.add(keys.nextElement());
153         }
154         return (set);
155     }
156 
157 
158     /*** {@inheritDoc} */
159     public Object put(String key, Object value) {
160         if (value == null) {
161             return (remove(key));
162         }
163         String skey = key(key);
164         Object previous = request.getAttribute(skey);
165         request.setAttribute(skey, value);
166         return (previous);
167     }
168 
169 
170     /*** {@inheritDoc} */
171     public void putAll(Map<? extends String, ? extends Object> map) {
172         Iterator<? extends String> keys = map.keySet().iterator();
173         while (keys.hasNext()) {
174             String key = keys.next();
175             request.setAttribute(key, map.get(key));
176         }
177     }
178 
179 
180     /*** {@inheritDoc} */
181     public Object remove(Object key) {
182         String skey = key(key);
183         Object previous = request.getAttribute(skey);
184         request.removeAttribute(skey);
185         return (previous);
186     }
187 
188 
189     /*** {@inheritDoc} */
190     @SuppressWarnings("unchecked")
191     public int size() {
192         int n = 0;
193         Enumeration<String> keys = request.getAttributeNames();
194         while (keys.hasMoreElements()) {
195             keys.nextElement();
196             n++;
197         }
198         return (n);
199     }
200 
201 
202     /*** {@inheritDoc} */
203     @SuppressWarnings("unchecked")
204     public Collection<Object> values() {
205         List<Object> list = new ArrayList<Object>();
206         Enumeration<String> keys = request.getAttributeNames();
207         while (keys.hasMoreElements()) {
208             list.add(request.getAttribute(keys.nextElement()));
209         }
210         return (list);
211     }
212 
213 
214     /***
215      * Returns the string representation of the key.
216      *
217      * @param key The key.
218      * @return The string representation of the key.
219      * @throws IllegalArgumentException If the key is <code>null</code>.
220      */
221     private String key(Object key) {
222         if (key == null) {
223             throw new IllegalArgumentException();
224         } else if (key instanceof String) {
225             return ((String) key);
226         } else {
227             return (key.toString());
228         }
229     }
230 
231 
232 }