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