1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.seasar.cubby.internal.controller.impl;
18
19 import java.util.Collection;
20 import java.util.Map;
21 import java.util.Set;
22 import java.util.concurrent.ConcurrentHashMap;
23
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpSession;
26
27 import org.seasar.cubby.action.FlashMap;
28 import org.seasar.cubby.internal.controller.ThreadContext;
29
30
31
32
33
34
35 class FlashMapImpl implements FlashMap {
36
37
38 private static final String ATTRIBUTE_NAME = FlashMapImpl.class.getName()
39 + ".MAP";
40
41
42 private final HttpServletRequest request;
43
44
45 private final Map<String, Object> map;
46
47
48
49
50
51
52
53 public FlashMapImpl() {
54 this(ThreadContext.getCurrentContext().getRequest());
55 }
56
57
58
59
60
61
62
63 public FlashMapImpl(final HttpServletRequest request) {
64 this.request = request;
65 this.map = buildMap(request);
66 }
67
68 private Map<String, Object> buildMap(final HttpServletRequest request) {
69 final HttpSession session = request.getSession(false);
70 if (session != null) {
71 final Map<String, Object> map = getAttribute(session,
72 ATTRIBUTE_NAME);
73 if (map != null) {
74 return map;
75 }
76 }
77 return createMap();
78 }
79
80 protected Map<String, Object> createMap() {
81 return new ConcurrentHashMap<String, Object>();
82 }
83
84 private void export(final HttpSession session) {
85 session.setAttribute(ATTRIBUTE_NAME, this.map);
86 }
87
88
89
90
91 public int size() {
92 return map.size();
93 }
94
95
96
97
98 public boolean isEmpty() {
99 return map.isEmpty();
100 }
101
102
103
104
105 public boolean containsKey(final Object key) {
106 return map.containsKey(key);
107 }
108
109
110
111
112 public boolean containsValue(final Object value) {
113 return map.containsValue(value);
114 }
115
116
117
118
119 public Object get(final Object key) {
120 return map.get(key);
121 }
122
123
124
125
126 public Object put(final String key, final Object value) {
127 final Object previousValue = map.put(key, value);
128 export(request.getSession());
129 return previousValue;
130 }
131
132
133
134
135 public Object remove(final Object key) {
136 final Object removedValue = map.remove(key);
137 final HttpSession session = request.getSession(false);
138 if (session != null) {
139 export(session);
140 }
141 return removedValue;
142 }
143
144
145
146
147 public void putAll(final Map<? extends String, ? extends Object> t) {
148 map.putAll(t);
149 export(request.getSession());
150 }
151
152
153
154
155 public void clear() {
156 map.clear();
157 final HttpSession session = request.getSession(false);
158 if (session != null) {
159 export(session);
160 }
161 }
162
163
164
165
166 public Set<String> keySet() {
167 return map.keySet();
168 }
169
170
171
172
173 public Collection<Object> values() {
174 return map.values();
175 }
176
177
178
179
180 public Set<Entry<String, Object>> entrySet() {
181 return map.entrySet();
182 }
183
184 @SuppressWarnings("unchecked")
185 private static <T> T getAttribute(final HttpSession session,
186 final String name) {
187 return (T) session.getAttribute(name);
188 }
189
190 }