This project has retired. For details please refer to its
Attic page.
ServletApplicationScopeMap xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.tiles.servlet.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.servlet.ServletContext;
34
35 import org.apache.tiles.context.MapEntry;
36
37 /***
38 * <p>Private implementation of <code>Map</code> for servlet context
39 * attributes.</p>
40 *
41 * @version $Rev: 581987 $ $Date: 2007-10-04 21:03:29 +0200 (Thu, 04 Oct 2007) $
42 */
43
44 final class ServletApplicationScopeMap implements Map<String, Object> {
45
46
47 /***
48 * Constructor.
49 *
50 * @param context The servlet context to use.
51 */
52 public ServletApplicationScopeMap(ServletContext context) {
53 this.context = context;
54 }
55
56
57 /***
58 * The servlet context to use.
59 */
60 private ServletContext context = null;
61
62
63 /*** {@inheritDoc} */
64 public void clear() {
65 Iterator<String> keys = keySet().iterator();
66 while (keys.hasNext()) {
67 context.removeAttribute(keys.next());
68 }
69 }
70
71
72 /*** {@inheritDoc} */
73 public boolean containsKey(Object key) {
74 return (context.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 = context.getAttributeNames();
85 while (keys.hasMoreElements()) {
86 Object next = context.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 = context.getAttributeNames();
100 String key;
101 while (keys.hasMoreElements()) {
102 key = keys.nextElement();
103 set.add(new MapEntry<String, Object>(key,
104 context.getAttribute(key), true));
105 }
106 return (set);
107 }
108
109
110 /*** {@inheritDoc} */
111 @SuppressWarnings("unchecked")
112 public boolean equals(Object o) {
113 ServletContext otherContext = ((ServletApplicationScopeMap) o).context;
114 boolean retValue = true;
115 synchronized (context) {
116 for (Enumeration<String> attribs = context.getAttributeNames(); attribs
117 .hasMoreElements()
118 && retValue;) {
119 String parameterName = attribs.nextElement();
120 retValue = context.getAttribute(parameterName).equals(
121 otherContext.getAttribute(parameterName));
122 }
123 }
124
125 return retValue;
126 }
127
128
129 /*** {@inheritDoc} */
130 public Object get(Object key) {
131 return (context.getAttribute(key(key)));
132 }
133
134
135 /*** {@inheritDoc} */
136 public int hashCode() {
137 return (context.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 = context.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 = context.getAttribute(skey);
166 context.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 context.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 = context.getAttribute(skey);
185 context.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 = context.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 = context.getAttributeNames();
208 while (keys.hasMoreElements()) {
209 list.add(context.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 }