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.util;
22
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.Map;
26 import java.util.Set;
27
28 import javax.servlet.jsp.PageContext;
29
30 import org.apache.struts2.ServletActionContext;
31
32
33 /***
34 * A Map that holds 4 levels of scope.
35 * <p/>
36 * The scopes are the ones known in the web world.:
37 * <ul>
38 * <li>Page scope</li>
39 * <li>Request scope</li>
40 * <li>Session scope</li>
41 * <li>Application scope</li>
42 * </ul>
43 * A object is searched in the order above, starting from page and ending at application scope.
44 *
45 */
46 public class AttributeMap implements Map {
47
48 protected static final String UNSUPPORTED = "method makes no sense for a simplified map";
49
50
51 Map context;
52
53
54 public AttributeMap(Map context) {
55 this.context = context;
56 }
57
58
59 public boolean isEmpty() {
60 throw new UnsupportedOperationException(UNSUPPORTED);
61 }
62
63 public void clear() {
64 throw new UnsupportedOperationException(UNSUPPORTED);
65 }
66
67 public boolean containsKey(Object key) {
68 return (get(key) != null);
69 }
70
71 public boolean containsValue(Object value) {
72 throw new UnsupportedOperationException(UNSUPPORTED);
73 }
74
75 public Set entrySet() {
76 return Collections.EMPTY_SET;
77 }
78
79 public Object get(Object key) {
80 PageContext pc = getPageContext();
81
82 if (pc == null) {
83 Map request = (Map) context.get("request");
84 Map session = (Map) context.get("session");
85 Map application = (Map) context.get("application");
86
87 if ((request != null) && (request.get(key) != null)) {
88 return request.get(key);
89 } else if ((session != null) && (session.get(key) != null)) {
90 return session.get(key);
91 } else if ((application != null) && (application.get(key) != null)) {
92 return application.get(key);
93 }
94 } else {
95 try{
96 return pc.findAttribute(key.toString());
97 }catch (NullPointerException npe){
98 return null;
99 }
100 }
101
102 return null;
103 }
104
105 public Set keySet() {
106 return Collections.EMPTY_SET;
107 }
108
109 public Object put(Object key, Object value) {
110 PageContext pc = getPageContext();
111 if (pc != null) {
112 pc.setAttribute(key.toString(), value);
113 }
114
115 return null;
116 }
117
118 public void putAll(Map t) {
119 throw new UnsupportedOperationException(UNSUPPORTED);
120 }
121
122 public Object remove(Object key) {
123 throw new UnsupportedOperationException(UNSUPPORTED);
124 }
125
126 public int size() {
127 throw new UnsupportedOperationException(UNSUPPORTED);
128 }
129
130 public Collection values() {
131 return Collections.EMPTY_SET;
132 }
133
134 private PageContext getPageContext() {
135 return (PageContext) context.get(ServletActionContext.PAGE_CONTEXT);
136 }
137 }