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

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