1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.dispatcher;
23
24 import java.io.Serializable;
25 import java.util.AbstractMap;
26 import java.util.Enumeration;
27 import java.util.HashSet;
28 import java.util.Map;
29 import java.util.Set;
30
31 import javax.servlet.ServletContext;
32
33
34 /***
35 * A simple implementation of the {@link java.util.Map} interface to handle a collection of attributes and
36 * init parameters in a {@link javax.servlet.ServletContext} object. The {@link #entrySet()} method
37 * enumerates over all servlet context attributes and init parameters and returns a collection of both.
38 * Note, this will occur lazily - only when the entry set is asked for.
39 *
40 */
41 public class ApplicationMap extends AbstractMap implements Serializable {
42
43 private static final long serialVersionUID = 9136809763083228202L;
44
45 private ServletContext context;
46 private Set<Object> entries;
47
48
49 /***
50 * Creates a new map object given the servlet context.
51 *
52 * @param ctx the servlet context
53 */
54 public ApplicationMap(ServletContext ctx) {
55 this.context = ctx;
56 }
57
58
59 /***
60 * Removes all entries from the Map and removes all attributes from the servlet context.
61 */
62 public void clear() {
63 entries = null;
64
65 Enumeration e = context.getAttributeNames();
66
67 while (e.hasMoreElements()) {
68 context.removeAttribute(e.nextElement().toString());
69 }
70 }
71
72 /***
73 * Creates a Set of all servlet context attributes as well as context init parameters.
74 *
75 * @return a Set of all servlet context attributes as well as context init parameters.
76 */
77 public Set entrySet() {
78 if (entries == null) {
79 entries = new HashSet<Object>();
80
81
82 Enumeration enumeration = context.getAttributeNames();
83
84 while (enumeration.hasMoreElements()) {
85 final String key = enumeration.nextElement().toString();
86 final Object value = context.getAttribute(key);
87 entries.add(new Map.Entry() {
88 public boolean equals(Object obj) {
89 if (!(obj instanceof Map.Entry)) {
90 return false;
91 }
92 Map.Entry entry = (Map.Entry) obj;
93
94 return ((key == null) ? (entry.getKey() == null) : key.equals(entry.getKey())) && ((value == null) ? (entry.getValue() == null) : value.equals(entry.getValue()));
95 }
96
97 public int hashCode() {
98 return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode());
99 }
100
101 public Object getKey() {
102 return key;
103 }
104
105 public Object getValue() {
106 return value;
107 }
108
109 public Object setValue(Object obj) {
110 context.setAttribute(key, obj);
111
112 return value;
113 }
114 });
115 }
116
117
118 enumeration = context.getInitParameterNames();
119
120 while (enumeration.hasMoreElements()) {
121 final String key = enumeration.nextElement().toString();
122 final Object value = context.getInitParameter(key);
123 entries.add(new Map.Entry() {
124 public boolean equals(Object obj) {
125 if (!(obj instanceof Map.Entry)) {
126 return false;
127 }
128 Map.Entry entry = (Map.Entry) obj;
129
130 return ((key == null) ? (entry.getKey() == null) : key.equals(entry.getKey())) && ((value == null) ? (entry.getValue() == null) : value.equals(entry.getValue()));
131 }
132
133 public int hashCode() {
134 return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode());
135 }
136
137 public Object getKey() {
138 return key;
139 }
140
141 public Object getValue() {
142 return value;
143 }
144
145 public Object setValue(Object obj) {
146 context.setAttribute(key, obj);
147
148 return value;
149 }
150 });
151 }
152 }
153
154 return entries;
155 }
156
157 /***
158 * Returns the servlet context attribute or init parameter based on the given key. If the
159 * entry is not found, <tt>null</tt> is returned.
160 *
161 * @param key the entry key.
162 * @return the servlet context attribute or init parameter or <tt>null</tt> if the entry is not found.
163 */
164 public Object get(Object key) {
165
166
167 String keyString = key.toString();
168 Object value = context.getAttribute(keyString);
169
170 return (value == null) ? context.getInitParameter(keyString) : value;
171 }
172
173 /***
174 * Sets a servlet context attribute given a attribute name and value.
175 *
176 * @param key the name of the attribute.
177 * @param value the value to set.
178 * @return the attribute that was just set.
179 */
180 public Object put(Object key, Object value) {
181 entries = null;
182 context.setAttribute(key.toString(), value);
183
184 return get(key);
185 }
186
187 /***
188 * Removes the specified servlet context attribute.
189 *
190 * @param key the attribute to remove.
191 * @return the entry that was just removed.
192 */
193 public Object remove(Object key) {
194 entries = null;
195
196 Object value = get(key);
197 context.removeAttribute(key.toString());
198
199 return value;
200 }
201 }