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