1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.chain.web.servlet;
17
18
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Enumeration;
22 import java.util.HashSet;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27 import javax.servlet.ServletContext;
28 import org.apache.commons.chain.web.MapEntry;
29
30
31 /***
32 * <p>Private implementation of <code>Map</code> for servlet context
33 * attributes.</p>
34 *
35 * @author Craig R. McClanahan
36 * @version $Revision: 1.5 $ $Date: 2004/11/30 05:52:23 $
37 */
38
39 final class ServletApplicationScopeMap implements Map {
40
41
42 public ServletApplicationScopeMap(ServletContext context) {
43 this.context = context;
44 }
45
46
47 private ServletContext context = null;
48
49
50 public void clear() {
51 Iterator keys = keySet().iterator();
52 while (keys.hasNext()) {
53 context.removeAttribute((String) keys.next());
54 }
55 }
56
57
58 public boolean containsKey(Object key) {
59 return (context.getAttribute(key(key)) != null);
60 }
61
62
63 public boolean containsValue(Object value) {
64 if (value == null) {
65 return (false);
66 }
67 Enumeration keys = context.getAttributeNames();
68 while (keys.hasMoreElements()) {
69 Object next = context.getAttribute((String) keys.nextElement());
70 if (next == value) {
71 return (true);
72 }
73 }
74 return (false);
75 }
76
77
78 public Set entrySet() {
79 Set set = new HashSet();
80 Enumeration keys = context.getAttributeNames();
81 String key;
82 while (keys.hasMoreElements()) {
83 key = (String)keys.nextElement();
84 set.add(new MapEntry(key, context.getAttribute(key), true));
85 }
86 return (set);
87 }
88
89
90 public boolean equals(Object o) {
91 return (context.equals(o));
92 }
93
94
95 public Object get(Object key) {
96 return (context.getAttribute(key(key)));
97 }
98
99
100 public int hashCode() {
101 return (context.hashCode());
102 }
103
104
105 public boolean isEmpty() {
106 return (size() < 1);
107 }
108
109
110 public Set keySet() {
111 Set set = new HashSet();
112 Enumeration keys = context.getAttributeNames();
113 while (keys.hasMoreElements()) {
114 set.add(keys.nextElement());
115 }
116 return (set);
117 }
118
119
120 public Object put(Object key, Object value) {
121 if (value == null) {
122 return (remove(key));
123 }
124 String skey = key(key);
125 Object previous = context.getAttribute(skey);
126 context.setAttribute(skey, value);
127 return (previous);
128 }
129
130
131 public void putAll(Map map) {
132 Iterator keys = map.keySet().iterator();
133 while (keys.hasNext()) {
134 String key = (String) keys.next();
135 context.setAttribute(key, map.get(key));
136 }
137 }
138
139
140 public Object remove(Object key) {
141 String skey = key(key);
142 Object previous = context.getAttribute(skey);
143 context.removeAttribute(skey);
144 return (previous);
145 }
146
147
148 public int size() {
149 int n = 0;
150 Enumeration keys = context.getAttributeNames();
151 while (keys.hasMoreElements()) {
152 keys.nextElement();
153 n++;
154 }
155 return (n);
156 }
157
158
159 public Collection values() {
160 List list = new ArrayList();
161 Enumeration keys = context.getAttributeNames();
162 while (keys.hasMoreElements()) {
163 list.add(context.getAttribute((String) keys.nextElement()));
164 }
165 return (list);
166 }
167
168
169 private String key(Object key) {
170 if (key == null) {
171 throw new IllegalArgumentException();
172 } else if (key instanceof String) {
173 return ((String) key);
174 } else {
175 return (key.toString());
176 }
177 }
178
179
180 }