1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.custom.conversation;
20
21 import org.springframework.beans.factory.config.Scope;
22 import org.springframework.beans.factory.ObjectFactory;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 import javax.faces.context.FacesContext;
27
28
29
30
31 public class SpringConversationScope implements Scope
32 {
33 private final static Log log = LogFactory.getLog(SpringConversationScope.class);
34
35 public String getConversationId()
36 {
37 FacesContext facesContext = FacesContext.getCurrentInstance();
38 ConversationManager manager = ConversationManager.getInstance(facesContext);
39 if (manager.hasConversationContext())
40 {
41 return Long.toString(manager.getConversationContextId().longValue(), 10);
42 }
43
44 return null;
45 }
46
47
48
49
50
51
52 public Object get(String name, ObjectFactory objectFactory)
53 {
54 name = buildBeanName(name);
55
56 FacesContext facesContext = FacesContext.getCurrentInstance();
57
58 ConversationManager manager = ConversationManager.getInstance(facesContext);
59
60 Object value = null;
61 boolean created = false;
62
63
64 if (!manager.hasConversation(name))
65 {
66
67 value = objectFactory.getObject();
68 created = true;
69
70
71 boolean isPersistent = (value instanceof PersistentConversation);
72
73
74 manager.startConversation(name, isPersistent);
75 }
76
77
78 Conversation conversation = manager.getConversation(name);
79 if (!conversation.hasBean(name))
80 {
81
82 if (!created)
83 {
84 value = objectFactory.getObject();
85 }
86
87 conversation.putBean(facesContext, name, value);
88 }
89
90
91 return conversation.getBean(name);
92 }
93
94
95
96
97
98 protected String buildBeanName(String name)
99 {
100 if (name == null)
101 {
102 return null;
103 }
104
105 int pos = name.lastIndexOf('.');
106 if (pos < 0)
107 {
108 return name;
109 }
110
111 return name.substring(pos+1);
112 }
113
114
115
116
117
118 public Object remove(String name)
119 {
120 FacesContext facesContext = FacesContext.getCurrentInstance();
121
122 ConversationManager manager = ConversationManager.getInstance(facesContext);
123 if (!manager.hasConversation(name))
124 {
125 return null;
126 }
127
128 return manager.getConversation(name).removeBean(name);
129 }
130
131 public void registerDestructionCallback(String name, Runnable runnable)
132 {
133 if (log.isWarnEnabled())
134 {
135 log.warn("SpringConversationScope: registerDestructionCallback not yet supported. Name=" + name);
136 }
137 }
138 }