Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ServletContextScope |
|
| 1.9047619047619047;1.905 |
1 | /* | |
2 | * Copyright 1999-2001,2004 The Apache Software Foundation. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | package org.apache.commons.workflow.web; | |
18 | ||
19 | ||
20 | import java.util.ArrayList; | |
21 | import java.util.Collection; | |
22 | import java.util.Enumeration; | |
23 | import java.util.HashSet; | |
24 | import java.util.Iterator; | |
25 | import java.util.Map; | |
26 | import java.util.Set; | |
27 | import javax.servlet.ServletContext; | |
28 | import org.apache.commons.workflow.base.BaseScope; | |
29 | import org.apache.commons.workflow.util.MapEntry; | |
30 | ||
31 | ||
32 | /** | |
33 | * <strong>ServletContextScope</strong> is a specialized <code>Scope</code> | |
34 | * implementation corresponding the the attributes of a specified | |
35 | * <code>ServletContext</code>. | |
36 | * | |
37 | * @version $Revision: 155475 $ $Date: 2005-02-26 13:31:11 +0000 (Sat, 26 Feb 2005) $ | |
38 | * @author Craig R. McClanahan | |
39 | */ | |
40 | ||
41 | public class ServletContextScope extends BaseScope { | |
42 | ||
43 | ||
44 | // ----------------------------------------------------------- Constructors | |
45 | ||
46 | ||
47 | /** | |
48 | * Construct a new ServletContextScope with no attached ServletContext. | |
49 | */ | |
50 | public ServletContextScope() { | |
51 | ||
52 | 0 | super(); |
53 | ||
54 | 0 | } |
55 | ||
56 | ||
57 | /** | |
58 | * Construct a ServletContextScope associated with the specified | |
59 | * ServletContext. | |
60 | * | |
61 | * @param servletContext The associated ServletContext | |
62 | */ | |
63 | public ServletContextScope(ServletContext servletContext) { | |
64 | ||
65 | 0 | super(); |
66 | 0 | setServletContext(servletContext); |
67 | ||
68 | 0 | } |
69 | ||
70 | ||
71 | // ------------------------------------------------------------- Properties | |
72 | ||
73 | ||
74 | /** | |
75 | * The servlet context with which we are associated. | |
76 | */ | |
77 | 0 | protected ServletContext servletContext = null; |
78 | ||
79 | public ServletContext getServletContext() { | |
80 | 0 | return (this.servletContext); |
81 | } | |
82 | ||
83 | public void setServletContext(ServletContext servletContext) { | |
84 | 0 | this.servletContext = servletContext; |
85 | 0 | } |
86 | ||
87 | ||
88 | // ------------------------------------------------------------ Map Methods | |
89 | ||
90 | ||
91 | /** | |
92 | * Remove all beans from this Map and call <code>scopeCleared() on | |
93 | * all registered <code>ScopeListeners</code>. | |
94 | */ | |
95 | public void clear() { | |
96 | ||
97 | // Accumulate a list of the elements to be cleared | |
98 | 0 | Enumeration names = servletContext.getAttributeNames(); |
99 | 0 | ArrayList list = new ArrayList(); |
100 | 0 | while (names.hasMoreElements()) { |
101 | 0 | list.add((String) names.nextElement()); |
102 | } | |
103 | ||
104 | // Erase the accumulated elements | |
105 | 0 | int n = list.size(); |
106 | 0 | for (int i = 0; i < n; i++) { |
107 | 0 | servletContext.removeAttribute((String) list.get(i)); |
108 | } | |
109 | 0 | support.fireScopeCleared(); |
110 | ||
111 | 0 | } |
112 | ||
113 | ||
114 | /** | |
115 | * Return <code>true</code> if this map contains the specified key. | |
116 | * | |
117 | * @param key Key to be looked up | |
118 | */ | |
119 | public boolean containsKey(Object key) { | |
120 | ||
121 | 0 | return (servletContext.getAttribute((String) key) != null); |
122 | ||
123 | } | |
124 | ||
125 | ||
126 | /** | |
127 | * Return <code>true</code> if this map contains the specified value. | |
128 | * | |
129 | * @param value Value to be looked up | |
130 | */ | |
131 | public boolean containsValue(Object value) { | |
132 | ||
133 | // Check all existing attributes for a match | |
134 | 0 | Enumeration names = servletContext.getAttributeNames(); |
135 | 0 | while (names.hasMoreElements()) { |
136 | 0 | String name = (String) names.nextElement(); |
137 | 0 | if (value.equals(servletContext.getAttribute(name))) |
138 | 0 | return (true); |
139 | 0 | } |
140 | 0 | return (false); |
141 | ||
142 | } | |
143 | ||
144 | ||
145 | /** | |
146 | * Return a set view of the mappings contained in this map. | |
147 | */ | |
148 | public Set entrySet() { | |
149 | ||
150 | 0 | HashSet results = new HashSet(); |
151 | 0 | Enumeration names = servletContext.getAttributeNames(); |
152 | 0 | while (names.hasMoreElements()) { |
153 | 0 | String name = (String) names.nextElement(); |
154 | 0 | results.add(new MapEntry(name, servletContext.getAttribute(name))); |
155 | 0 | } |
156 | 0 | return (results); |
157 | ||
158 | } | |
159 | ||
160 | ||
161 | /** | |
162 | * Compare the specified object with this map for equality. | |
163 | * | |
164 | * @param object Object to be compared | |
165 | */ | |
166 | public boolean equals(Object object) { | |
167 | ||
168 | 0 | return (servletContext.equals(object)); |
169 | ||
170 | } | |
171 | ||
172 | ||
173 | /** | |
174 | * Return the value to which this map maps the specified key. | |
175 | * | |
176 | * @param key Key to be looked up | |
177 | */ | |
178 | public Object get(Object key) { | |
179 | ||
180 | 0 | return (get((String) key)); |
181 | ||
182 | } | |
183 | ||
184 | ||
185 | /** | |
186 | * Return the value to which this map maps the specified key. | |
187 | * | |
188 | * @param key Key to be looked up | |
189 | */ | |
190 | public Object get(String key) { | |
191 | ||
192 | 0 | return (servletContext.getAttribute(key)); |
193 | ||
194 | } | |
195 | ||
196 | ||
197 | /** | |
198 | * Return the hash code value for this map. | |
199 | */ | |
200 | public int hashCode() { | |
201 | ||
202 | 0 | return (servletContext.hashCode()); |
203 | ||
204 | } | |
205 | ||
206 | ||
207 | /** | |
208 | * Return <code>true</code> if this map is empty. | |
209 | */ | |
210 | public boolean isEmpty() { | |
211 | ||
212 | // Check all existing attributes | |
213 | 0 | Enumeration names = servletContext.getAttributeNames(); |
214 | 0 | while (names.hasMoreElements()) |
215 | 0 | return (true); |
216 | 0 | return (false); |
217 | ||
218 | } | |
219 | ||
220 | ||
221 | /** | |
222 | * Return a set view of the keys contained in this map. | |
223 | */ | |
224 | public Set keySet() { | |
225 | ||
226 | 0 | HashSet results = new HashSet(); |
227 | 0 | Enumeration names = servletContext.getAttributeNames(); |
228 | 0 | while (names.hasMoreElements()) |
229 | 0 | results.add(names.nextElement()); |
230 | 0 | return (results); |
231 | ||
232 | } | |
233 | ||
234 | ||
235 | /** | |
236 | * Add or replace the bean associated with the specified key. | |
237 | * | |
238 | * @param key Key with which the new value should be associated | |
239 | * (cannot be null) | |
240 | * @param bean Bean to be associated with this key (cannot be null) | |
241 | */ | |
242 | public Object put(Object key, Object bean) { | |
243 | ||
244 | 0 | return (put((String) key, bean)); |
245 | ||
246 | } | |
247 | ||
248 | ||
249 | /** | |
250 | * Add the specified bean, associated with the specified key, to this | |
251 | * scope and replace any previous bean associated with this key. If | |
252 | * the bean was added, call <code>beanAdded()</code> on all registered | |
253 | * listeners after the add is done. If an old bean was replaced, | |
254 | * call <code>beanReplaced()</code> (passing the old value in the event) | |
255 | * on all registered <code>ScopeListeners</code> after the removal | |
256 | * is done. If a bean was replaced, the old value is also returned; | |
257 | * otherwise <code>null</code> is returned. | |
258 | * | |
259 | * @param key Key with which the new value should be associated | |
260 | * (cannot be null) | |
261 | * @param bean Bean to be associated with this key (cannot be null) | |
262 | * | |
263 | * @exception IllegalArgumentException if <code>key</code> or | |
264 | * <code>bean</code> is null | |
265 | */ | |
266 | public Object put(String key, Object bean) { | |
267 | ||
268 | 0 | if (key == null) |
269 | 0 | throw new IllegalArgumentException("Key cannot be null"); |
270 | 0 | if (bean == null) |
271 | 0 | throw new IllegalArgumentException("Value cannot be null"); |
272 | ||
273 | 0 | Object old = servletContext.getAttribute(key); |
274 | 0 | if (old != null) { |
275 | 0 | servletContext.setAttribute(key, bean); |
276 | 0 | support.fireBeanReplaced(key, old); |
277 | } else { | |
278 | 0 | servletContext.setAttribute(key, bean); |
279 | 0 | support.fireBeanAdded(key, bean); |
280 | } | |
281 | 0 | return (old); |
282 | ||
283 | } | |
284 | ||
285 | ||
286 | /** | |
287 | * Copy all of the mappings from the specified map into this map, | |
288 | * firing appropriate <code>beanAdded()</code> and | |
289 | * <code>beanReplaced()</code> events along the way. | |
290 | * | |
291 | * @param in Map whose contents are to be added | |
292 | */ | |
293 | public void putAll(Map in) { | |
294 | ||
295 | 0 | Iterator keys = in.keySet().iterator(); |
296 | 0 | while (keys.hasNext()) { |
297 | 0 | Object key = keys.next(); |
298 | 0 | put(key, in.get(key)); |
299 | 0 | } |
300 | ||
301 | 0 | } |
302 | ||
303 | ||
304 | /** | |
305 | * Remove the bean associated with the specified key (if any), and return | |
306 | * the old value if removed. | |
307 | * | |
308 | * @param key Key of the bean to remove (cannot be null) | |
309 | */ | |
310 | public Object remove(Object key) { | |
311 | ||
312 | 0 | return (remove((String) key)); |
313 | ||
314 | } | |
315 | ||
316 | ||
317 | ||
318 | /** | |
319 | * Remove the bean associated with the specified key (if any). If such | |
320 | * a bean is found and removed, call <code>beanRemoved()</code> on all | |
321 | * registered <code>ScopeListeners</code> after the removal is done. | |
322 | * Return the old value (if any); otherwise return <code>null</code>. | |
323 | * | |
324 | * @param key Key of the bean to remove (cannot be null) | |
325 | * | |
326 | * @exception IllegalArgumentException if <code>key</code> is null | |
327 | */ | |
328 | public Object remove(String key) { | |
329 | ||
330 | 0 | Object old = servletContext.getAttribute(key); |
331 | 0 | if (old != null) { |
332 | 0 | support.fireBeanRemoved(key, old); |
333 | 0 | return (old); |
334 | } | |
335 | 0 | return (null); |
336 | ||
337 | } | |
338 | ||
339 | ||
340 | /** | |
341 | * Return the number of key-value mappings in this map. | |
342 | */ | |
343 | public int size() { | |
344 | ||
345 | 0 | Enumeration names = servletContext.getAttributeNames(); |
346 | 0 | int n = 0; |
347 | 0 | while (names.hasMoreElements()) { |
348 | 0 | n++; |
349 | } | |
350 | 0 | return (n); |
351 | ||
352 | } | |
353 | ||
354 | ||
355 | /** | |
356 | * Return a Collection view of the values contained in this map. | |
357 | */ | |
358 | public Collection values() { | |
359 | ||
360 | 0 | ArrayList results = new ArrayList(); |
361 | 0 | Enumeration names = servletContext.getAttributeNames(); |
362 | 0 | while (names.hasMoreElements()) { |
363 | 0 | String name = (String) names.nextElement(); |
364 | 0 | results.add(servletContext.getAttribute(name)); |
365 | 0 | } |
366 | 0 | return (results); |
367 | ||
368 | } | |
369 | ||
370 | ||
371 | } |