1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sourceforge.sannotations.scopes;
22
23 import java.util.HashMap;
24 import org.springframework.beans.factory.ObjectFactory;
25 import org.springframework.beans.factory.config.Scope;
26 import org.springframework.web.context.request.RequestAttributes;
27 import org.springframework.web.context.request.RequestContextHolder;
28
29 /***
30 * Bean that implements the "flash" Scope, beans under this scope live from the end of an HTTP request to the begining of the next one.
31 *
32 * @author Urubatan
33 */
34 public class FlashScope implements Scope {
35 private final String SCOPE_KEY = FlashScope.class.getName() + ".SCOPEDMAP";
36
37 @SuppressWarnings("unchecked")
38 private HashMap<String, Object> getScopedValues() {
39 HashMap<String, Object> scopedObj = (HashMap<String, Object>) RequestContextHolder.currentRequestAttributes().getAttribute(this.SCOPE_KEY, RequestAttributes.SCOPE_SESSION);
40 if (scopedObj == null) {
41 scopedObj = new HashMap<String, Object>();
42 RequestContextHolder.currentRequestAttributes().setAttribute(this.SCOPE_KEY, scopedObj, RequestAttributes.SCOPE_SESSION);
43 }
44 return scopedObj;
45 }
46
47 public Object get(final String name, final ObjectFactory objectFactory) {
48 Object scopedObject = this.getScopedValues().get(name);
49 if ((scopedObject == null) && (objectFactory != null)) {
50 scopedObject = objectFactory.getObject();
51 this.getScopedValues().put(name, scopedObject);
52 }
53 return scopedObject;
54 }
55
56 public Object remove(final String name) {
57 final Object scopedObject = this.getScopedValues().get(name);
58 if (scopedObject != null) {
59 this.getScopedValues().remove(name);
60 return scopedObject;
61 }
62 return null;
63 }
64
65 public void clearScope() {
66 this.getScopedValues().clear();
67 }
68
69 public void set(String name, Object value) {
70 this.getScopedValues().put(name, value);
71 }
72
73 public String getConversationId() {
74 return "flash";
75 }
76
77 public void registerDestructionCallback(String arg0, Runnable arg1) {
78
79 }
80 }