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