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