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 "conversation" scope, beans under this scope live until a method annotated with
31 *
32 * @ConvEnd is executed
33 * @author Urubatan
34 */
35 public class ConversationScope implements Scope {
36 private final String SCOPE_KEY = ConversationScope.class.getName() + ".SCOPEDMAP";
37
38 @SuppressWarnings("unchecked")
39 private HashMap<String, Object> getScopedValues() {
40 HashMap<String, Object> scopedObj = (HashMap<String, Object>) RequestContextHolder.currentRequestAttributes().getAttribute(this.SCOPE_KEY, RequestAttributes.SCOPE_SESSION);
41 if (scopedObj == null) {
42 scopedObj = new HashMap<String, Object>();
43 RequestContextHolder.currentRequestAttributes().setAttribute(this.SCOPE_KEY, scopedObj, RequestAttributes.SCOPE_SESSION);
44 }
45 return scopedObj;
46 }
47
48 public Object get(final String name, final ObjectFactory objectFactory) {
49 Object scopedObject = this.getScopedValues().get(name);
50 if ((scopedObject == null) && (objectFactory != null)) {
51 scopedObject = objectFactory.getObject();
52 this.getScopedValues().put(name, scopedObject);
53 }
54 return scopedObject;
55 }
56
57 public Object remove(final String name) {
58 final Object scopedObject = this.getScopedValues().get(name);
59 if (scopedObject != null) {
60 this.getScopedValues().remove(name);
61 return scopedObject;
62 }
63 return null;
64 }
65
66 public void clearScope() {
67 this.getScopedValues().clear();
68 }
69
70 public void set(String name, Object value) {
71 this.getScopedValues().put(name, value);
72 }
73
74 public String getConversationId() {
75 return "conversation";
76 }
77
78 public void registerDestructionCallback(String arg0, Runnable arg1) {
79
80 }
81 }