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.portlet;
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.portlet.PortletContext;
32
33 /***
34 * Portlet specific {@link java.util.Map} implementation representing the
35 * {@link javax.portlet.PortletContext} of a Portlet.
36 *
37 */
38 public class PortletApplicationMap extends AbstractMap implements Serializable {
39
40 private static final long serialVersionUID = 2296107511063504414L;
41
42 private PortletContext context;
43
44 private Set<Object> entries;
45
46 /***
47 * Creates a new map object given the {@link PortletContext}.
48 *
49 * @param ctx The portlet context.
50 */
51 public PortletApplicationMap(PortletContext ctx) {
52 this.context = ctx;
53 }
54
55 /***
56 * Removes all entries from the Map and removes all attributes from the
57 * portlet context.
58 */
59 public void clear() {
60 entries = null;
61
62 Enumeration e = context.getAttributeNames();
63
64 while (e.hasMoreElements()) {
65 context.removeAttribute(e.nextElement().toString());
66 }
67 }
68
69 /***
70 * Creates a Set of all portlet context attributes as well as context init
71 * parameters.
72 *
73 * @return a Set of all portlet context attributes as well as context init
74 * parameters.
75 */
76 public Set entrySet() {
77 if (entries == null) {
78 entries = new HashSet<Object>();
79
80
81 Enumeration enumeration = context.getAttributeNames();
82
83 while (enumeration.hasMoreElements()) {
84 final String key = enumeration.nextElement().toString();
85 final Object value = context.getAttribute(key);
86 entries.add(new Map.Entry() {
87 public boolean equals(Object obj) {
88 Map.Entry entry = (Map.Entry) obj;
89
90 return ((key == null) ? (entry.getKey() == null) : key
91 .equals(entry.getKey()))
92 && ((value == null) ? (entry.getValue() == null)
93 : value.equals(entry.getValue()));
94 }
95
96 public int hashCode() {
97 return ((key == null) ? 0 : key.hashCode())
98 ^ ((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.toString(), 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 Map.Entry entry = (Map.Entry) obj;
126
127 return ((key == null) ? (entry.getKey() == null) : key
128 .equals(entry.getKey()))
129 && ((value == null) ? (entry.getValue() == null)
130 : value.equals(entry.getValue()));
131 }
132
133 public int hashCode() {
134 return ((key == null) ? 0 : key.hashCode())
135 ^ ((value == null) ? 0 : value.hashCode());
136 }
137
138 public Object getKey() {
139 return key;
140 }
141
142 public Object getValue() {
143 return value;
144 }
145
146 public Object setValue(Object obj) {
147 context.setAttribute(key.toString(), obj);
148
149 return value;
150 }
151 });
152 }
153 }
154
155 return entries;
156 }
157
158 /***
159 * Returns the portlet context attribute or init parameter based on the
160 * given key. If the entry is not found, <tt>null</tt> is returned.
161 *
162 * @param key
163 * the entry key.
164 * @return the portlet context attribute or init parameter or <tt>null</tt>
165 * if the entry is not found.
166 */
167 public Object get(Object key) {
168
169
170 String keyString = key.toString();
171 Object value = context.getAttribute(keyString);
172
173 return (value == null) ? context.getInitParameter(keyString) : value;
174 }
175
176 /***
177 * Sets a portlet context attribute given a attribute name and value.
178 *
179 * @param key
180 * the name of the attribute.
181 * @param value
182 * the value to set.
183 * @return the attribute that was just set.
184 */
185 public Object put(Object key, Object value) {
186 entries = null;
187 context.setAttribute(key.toString(), value);
188
189 return get(key);
190 }
191
192 /***
193 * Removes the specified portlet context attribute.
194 *
195 * @param key
196 * the attribute to remove.
197 * @return the entry that was just removed.
198 */
199 public Object remove(Object key) {
200 entries = null;
201
202 Object value = get(key);
203 context.removeAttribute(key.toString());
204
205 return value;
206 }
207 }