KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > chain > web > servlet > ServletApplicationScopeMap


1 /*
2  * Copyright 1999-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 package org.apache.commons.chain.web.servlet;
17
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27 import javax.servlet.ServletContext JavaDoc;
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 JavaDoc {
40
41
42     public ServletApplicationScopeMap(ServletContext JavaDoc context) {
43         this.context = context;
44     }
45
46
47     private ServletContext JavaDoc context = null;
48
49
50     public void clear() {
51         Iterator JavaDoc keys = keySet().iterator();
52         while (keys.hasNext()) {
53             context.removeAttribute((String JavaDoc) keys.next());
54         }
55     }
56
57
58     public boolean containsKey(Object JavaDoc key) {
59         return (context.getAttribute(key(key)) != null);
60     }
61
62
63     public boolean containsValue(Object JavaDoc value) {
64         if (value == null) {
65             return (false);
66         }
67         Enumeration JavaDoc keys = context.getAttributeNames();
68         while (keys.hasMoreElements()) {
69             Object JavaDoc next = context.getAttribute((String JavaDoc) keys.nextElement());
70             if (next == value) {
71                 return (true);
72             }
73         }
74         return (false);
75     }
76
77
78     public Set JavaDoc entrySet() {
79         Set JavaDoc set = new HashSet JavaDoc();
80         Enumeration JavaDoc keys = context.getAttributeNames();
81         String JavaDoc key;
82         while (keys.hasMoreElements()) {
83             key = (String JavaDoc)keys.nextElement();
84             set.add(new MapEntry(key, context.getAttribute(key), true));
85         }
86         return (set);
87     }
88
89
90     public boolean equals(Object JavaDoc o) {
91         return (context.equals(o));
92     }
93
94
95     public Object JavaDoc get(Object JavaDoc 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 JavaDoc keySet() {
111         Set JavaDoc set = new HashSet JavaDoc();
112         Enumeration JavaDoc keys = context.getAttributeNames();
113         while (keys.hasMoreElements()) {
114             set.add(keys.nextElement());
115         }
116         return (set);
117     }
118
119
120     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
121         if (value == null) {
122             return (remove(key));
123         }
124         String JavaDoc skey = key(key);
125         Object JavaDoc previous = context.getAttribute(skey);
126         context.setAttribute(skey, value);
127         return (previous);
128     }
129
130
131     public void putAll(Map JavaDoc map) {
132         Iterator JavaDoc keys = map.keySet().iterator();
133         while (keys.hasNext()) {
134             String JavaDoc key = (String JavaDoc) keys.next();
135             context.setAttribute(key, map.get(key));
136         }
137     }
138
139
140     public Object JavaDoc remove(Object JavaDoc key) {
141         String JavaDoc skey = key(key);
142         Object JavaDoc previous = context.getAttribute(skey);
143         context.removeAttribute(skey);
144         return (previous);
145     }
146
147
148     public int size() {
149         int n = 0;
150         Enumeration JavaDoc keys = context.getAttributeNames();
151         while (keys.hasMoreElements()) {
152             keys.nextElement();
153             n++;
154         }
155         return (n);
156     }
157
158
159     public Collection JavaDoc values() {
160         List JavaDoc list = new ArrayList JavaDoc();
161         Enumeration JavaDoc keys = context.getAttributeNames();
162         while (keys.hasMoreElements()) {
163             list.add(context.getAttribute((String JavaDoc) keys.nextElement()));
164         }
165         return (list);
166     }
167
168
169     private String JavaDoc key(Object JavaDoc key) {
170         if (key == null) {
171             throw new IllegalArgumentException JavaDoc();
172         } else if (key instanceof String JavaDoc) {
173             return ((String JavaDoc) key);
174         } else {
175             return (key.toString());
176         }
177     }
178
179
180 }
181
Popular Tags